Nearby lessons

62 of 124

C - Strings (Character Arrays)

Strings (Character Arrays) is one of the foundational topics in C programming. This lesson explains What is a String in C?, Declaring and Initializing Strings and Reading Strings — scanf, gets, fgets with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

What is a String in C?

In C, a string is simply an array of characters ending with a special character '\0' (called the null character). This \0 tells C where the string ends.

So a string "Rahul" needs an array of size at least 6 (5 characters + the \0). This is a common beginner mistake — forgetting space for the null character.

In simple words: a string is just a row of characters with a full-stop marker — \0 — at the end. The \0 is invisible; it only tells C "the text ends here".
Trainer's Note: Memory trick: `\0` is not the digit 0 and not the letter O — it is one character that means "end of text". Count it whenever you size a char array: text length + 1.
Example01
CCode Cell
1char name[10] = "Rahul";
2 
3R a h u l \0
4^-----------^-----^
55 real chars null marks the end

Declaring and Initializing Strings

Trainer's Note: "Rahul" (double quotes) is a string — it ends with \0. 'A' (single quotes) is a single character — it has no \0. Do not mix them.
Example02
CCode Cell
1char name[20]; // empty string variable (capacity 20)
2char name[20] = "Rahul"; // initialize with a string
3char name[] = "Rahul"; // size is automatic (6)
4char name[] = {'R','a','h','u','l','\0'}; // as an array of chars

Reading Strings — scanf, gets, fgets

FunctionReadsLimitation
scanf("%s", name)One word (no spaces)Stops at the first space
gets(name)A whole line including spacesUnsafe (no size check) — avoid
fgets(name, size, stdin)A whole line, safelyThe modern safe choice
Trainer's Note: scanf with %s reads only one word — "Rahul Kumar" would store only "Rahul". To read a full line with spaces, use fgets() (modern and safe). The old textbook's gets() is dangerous because it does not check the size — it can overflow memory. Prefer fgets.
Example03
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 char name[30];
6 
7 printf("Enter your name: ");
8 fgets(name, 30, stdin); // safe - reads spaces too
9 
10 printf("Hello %s", name);
11}
Output
Enter your name: Rahul Kumar Hello Rahul Kumar

String Basics Without Functions

Understand how it works underneath by doing it with loops (the classic exam approach):

Example04
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 char s[30];
6 int len = 0, i;
7 
8 printf("Enter a string: ");
9 gets(s); // or fgets
10 
11 for (i = 0; s[i] != '\0'; i++) {
12 len++; // count until the null character
13 }
14 printf("Length = %d\n", len);
15 
16 // print it in reverse
17 for (i = len - 1; i >= 0; i--) {
18 printf("%c", s[i]);
19 }
20 printf("\n");
21}
📝 Key Takeaways
  • A string = char array + a null character '\0' at the end.
  • Remember to leave room for '\0' when sizing arrays.
  • scanf %s reads one word; fgets reads a full line safely.
  • Use string.h functions: strlen, strcpy, strcat, strcmp.
  • Never use = or == for strings — use strcpy and strcmp.
  • Loops can count, reverse and check strings from scratch.

🧠 Test Your Knowledge

5 Questions
Progress: 0 / 5