Strings in C
Text in C is just a special array of characters — here's how it really works.
What is a String in C?
Unlike many languages, C has no separate 'string' type — a string is simply a char array ending with a special null character '\0' that marks where the string ends. This null terminator is what lets functions know when to stop reading.
char name[20] = "ShikshaCS";
// stored as: S h i k s h a C S \0Common String Functions
The <string.h> library provides essential functions: strlen() (length, not counting \0), strcpy() (copy one string to another), strcat() (join two strings), and strcmp() (compare two strings, returns 0 if equal).
#include <string.h>
char a[20] = "Hello";
printf("%d", strlen(a)); // prints 5Reading Strings Safely
scanf("%s", name) stops reading at the first space, so it can't read full sentences. fgets() is safer and can read a full line including spaces, while also preventing buffer overflow by limiting how many characters it reads.
🌍 Real-World Use
Every text input field, username validator, or password checker in a C program relies on these string functions — comparing passwords with strcmp() or checking username length with strlen() are extremely common real tasks.
💡 Pro Tip
Forgetting to leave room for the null terminator '\0' when sizing a char array is a classic bug — a string of 5 characters needs an array of at least 6 elements (5 characters + 1 for '\0').
🧪 Quick Self-Test
Check what you just learned — no pressure, just practice.
1. What marks the end of a string in C?
2. Which function compares two strings for equality?