Introduction

In this tutorial, we will create a Contact Book application in C programming language. This application will allow users to store, view, and manage their contact information efficiently. You will learn how to work with structures, functions, and basic file handling to store contacts. This is a beginner-friendly project to help you understand basic C concepts.

Objective

The objective of this program is to create a simple and functional contact management system. The program will:

  • Store contact details such as name, phone number, and email.
  • Display the contact list.
  • Allow adding and deleting contacts.
  • Provide an option to search for a contact by name.

Code

#include 
#include 
#include 

// Define the structure for a contact
struct Contact {
    char name[50];
    char phone[15];
    char email[50];
};

// Function to add a contact
void addContact() {
    struct Contact newContact;
    FILE *file = fopen("contacts.dat", "ab");
    
    if (!file) {
        printf("Could not open file.\n");
        return;
    }
    
    printf("Enter name: ");
    getchar();  // To consume any leftover newline character from previous input
    fgets(newContact.name, sizeof(newContact.name), stdin);
    newContact.name[strcspn(newContact.name, "\n")] = '\0';  // Remove trailing newline

    printf("Enter phone number: ");
    fgets(newContact.phone, sizeof(newContact.phone), stdin);
    newContact.phone[strcspn(newContact.phone, "\n")] = '\0';

    printf("Enter email: ");
    fgets(newContact.email, sizeof(newContact.email), stdin);
    newContact.email[strcspn(newContact.email, "\n")] = '\0';

    fwrite(&newContact, sizeof(struct Contact), 1, file);
    fclose(file);
    printf("Contact added successfully.\n");
}

// Function to display all contacts
void displayContacts() {
    struct Contact contact;
    FILE *file = fopen("contacts.dat", "rb");

    if (!file) {
        printf("No contacts found.\n");
        return;
    }

    printf("\nContacts List:\n");
    while (fread(&contact, sizeof(struct Contact), 1, file)) {
        printf("Name: %s\nPhone: %s\nEmail: %s\n\n", contact.name, contact.phone, contact.email);
    }

    fclose(file);
}

// Function to search for a contact
void searchContact() {
    char searchName[50];
    struct Contact contact;
    int found = 0;
    
    printf("Enter name to search: ");
    getchar();  // To consume any leftover newline character
    fgets(searchName, sizeof(searchName), stdin);
    searchName[strcspn(searchName, "\n")] = '\0';

    FILE *file = fopen("contacts.dat", "rb");
    
    if (!file) {
        printf("Could not open file.\n");
        return;
    }
    
    while (fread(&contact, sizeof(struct Contact), 1, file)) {
        if (strcmp(contact.name, searchName) == 0) {
            printf("Contact found!\n");
            printf("Name: %s\nPhone: %s\nEmail: %s\n", contact.name, contact.phone, contact.email);
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("Contact not found.\n");
    }

    fclose(file);
}

// Main function to run the application
int main() {
    int choice;

    while (1) {
        printf("\nContact Book Application\n");
        printf("1. Add Contact\n");
        printf("2. Display All Contacts\n");
        printf("3. Search Contact by Name\n");
        printf("4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addContact();
                break;
            case 2:
                displayContacts();
                break;
            case 3:
                searchContact();
                break;
            case 4:
                printf("Exiting application...\n");
                exit(0);
            default:
                printf("Invalid choice! Please try again.\n");
        }
    }

    return 0;
}

Explanation of the Program

The Contact Book program is divided into several key sections:

  • Structures: The `struct Contact` is defined to hold the details of each contact, such as name, phone number, and email.
  • Functions: The program defines three key functions:
    • addContact(): Adds a new contact to the `contacts.dat` file.
    • displayContacts(): Displays all the stored contacts from the file.
    • searchContact(): Searches for a contact by name in the stored contacts.
  • Main function: The `main()` function is the entry point of the program, presenting the user with a menu to perform different actions.

How to Run the Program

  1. Save the code in a file named contact_book.c.
  2. Open a terminal or command prompt and navigate to the directory containing the file.
  3. Compile the program using a C compiler (e.g., gcc contact_book.c -o contact_book).
  4. Run the program using ./contact_book (on Linux/macOS) or contact_book.exe (on Windows).

The program will run in a loop, allowing you to add, display, or search contacts until you choose to exit the program.

© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

One thought on “Contact Book Application in C Programming”
  1. Dear Sir/Madam,

    we specialize in connecting our investors with high-quality, revenue-generating companies seeking
    funding across various stages and sectors. Further details regarding the capital funding and requirements
    can be shared upon your confirmation of interest. Please let us know if you would like to proceed
    with a discussion. If this opportunity is not of interest to you, no further action is required.

    Best regards,
    RB
    Vert & M | Outside Sales Representative

Leave a Reply to Ramiro Basualdo Cancel reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)