C Tutorial

C Project: Phone Book

Store names and numbers in an array of structs, then search by name.

What you will build

A phone book is an array of contacts. Each contact is a struct with a name and a number. Search walks the array and compares names with strcmp from <string.h>. A match prints the number. No match prints a clear miss.

Names and numbers are hardcoded so the program runs without scanf. Compile withTry it in C at /c/try. That editor is gcc for C.

A contact struct

Two char arrays are enough: one for the name, one for the number. Keep both large enough for the demo strings plus the null terminator. Print the book as a two-column table.

Example

#include <stdio.h>

struct Contact {
  char name[24];
  char number[16];
};

int main(void) {
  struct Contact book[] = {
      {"Ada", "555-0100"},
      {"Nia", "555-0142"},
      {"Omar", "555-0199"},
  };
  int n = (int)(sizeof book / sizeof book[0]);

  for (int i = 0; i < n; i++) {
    printf("%-8s %s\n", book[i].name, book[i].number);
  }
  return 0;
}

The initializer copies each string into the field arrays. Do not assign a string with = after the struct already exists; use strcpy if you add a contact later in the program.

strcmp is the name test

strcmp(a, b) returns 0 when the two C strings are equal, including case. A non-zero result means they differ. Never write book[i].name == query. That compares pointers, not letters.

Example

#include <stdio.h>
#include <string.h>

int main(void) {
  char stored[] = "Ada";
  printf("Ada vs Ada: %d\n", strcmp(stored, "Ada"));
  printf("Ada vs ada: %d\n", strcmp(stored, "ada"));
  printf("Ada vs Nia: %d\n", strcmp(stored, "Nia"));
  return 0;
}

The first line should print 0. The others should not. Run this at /c/try.

Search the array

A helper takes the book, the length, and the name to find. It returns the index of the first match, or -1.main runs two queries: one that exists and one that does not. Both results should appear in the output.

Example

#include <stdio.h>
#include <string.h>

struct Contact {
  char name[24];
  char number[16];
};

int find_name(const struct Contact book[], int n, const char *query) {
  for (int i = 0; i < n; i++) {
    if (strcmp(book[i].name, query) == 0) {
      return i;
    }
  }
  return -1;
}

int main(void) {
  struct Contact book[] = {
      {"Ada", "555-0100"},
      {"Nia", "555-0142"},
      {"Omar", "555-0199"},
      {"Pia", "555-0115"},
  };
  int n = (int)(sizeof book / sizeof book[0]);
  const char *queries[] = {"Nia", "StudyGrid"};
  int qn = 2;

  for (int q = 0; q < qn; q++) {
    int i = find_name(book, n, queries[q]);
    if (i >= 0) {
      printf("%s: %s\n", book[i].name, book[i].number);
    } else {
      printf("%s: not found\n", queries[q]);
    }
  }
  return 0;
}

Nia prints 555-0142. StudyGrid prints not found. Search is case-sensitive: nia would miss Nia. That is how strcmp works.

List, then look up

Print the full book first so you can see every record, then run the lookups. This is the complete project shape: data, display, search.

Example

#include <stdio.h>
#include <string.h>

struct Contact {
  char name[24];
  char number[16];
};

void print_book(const struct Contact book[], int n) {
  printf("%-8s %s\n", "name", "number");
  for (int i = 0; i < n; i++) {
    printf("%-8s %s\n", book[i].name, book[i].number);
  }
}

int find_name(const struct Contact book[], int n, const char *query) {
  for (int i = 0; i < n; i++) {
    if (strcmp(book[i].name, query) == 0) {
      return i;
    }
  }
  return -1;
}

void lookup(const struct Contact book[], int n, const char *query) {
  int i = find_name(book, n, query);
  if (i >= 0) {
    printf("lookup %s -> %s\n", query, book[i].number);
  } else {
    printf("lookup %s -> not found\n", query);
  }
}

int main(void) {
  struct Contact book[] = {
      {"Ada", "555-0100"},
      {"Nia", "555-0142"},
      {"Omar", "555-0199"},
      {"Pia", "555-0115"},
  };
  int n = (int)(sizeof book / sizeof book[0]);

  print_book(book, n);
  lookup(book, n, "Omar");
  lookup(book, n, "Ada");
  lookup(book, n, "Rae");
  return 0;
}

Common mistakes

  • Using == on two char arrays. Compare with strcmp and test for 0.
  • Forgetting #include <string.h>. Then strcmp may compile as an implicit declaration and behave badly.
  • Returning 0 for “not found”. Index 0 is a valid contact. Use -1 for a miss.
  • Writing a number into a char array with %d and no room for digits. Keep numbers as strings in this project, as in 555-0100.

Practice

  1. Search by number instead of name: find who owns 555-0199.
  2. Print every contact whose name starts with the same letter as the query, using the first character only.
  3. After the table prints, add a fifth hardcoded lookup for Pia and confirm the number matches the row.

FAQ: C Project: Phone Book

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c phonebook project examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c phonebook project in this C C lesson (C Project: Phone Book).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.