Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

String Handling: Text in C

Lesson 18 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

Strings: The Hidden Reality

Unlike modern languages like Java or Python, C does not have a native "String" data type. Instead, a string is simply an array of characters ending with a special marker: the Null Character (\0).

Why the Null Character?

Imagine the computer's memory is a long street. A string is a row of houses. The \0 is the "End of Street" sign. When you ask C to print a string, it starts at the first character and keeps reading until it sees that zero. If you forget the null character, C will keep reading memory past your array, printing random junk until it eventually crashes.

Initialization

char name[] = "Rahul"; // C adds `\0` automatically at the end.
char city[6] = {'D', 'e', 'l', 'h', 'i', '\0'}; // Manual initialization.

The <string.h> Library

This is your toolkit for handling text. You must include it at the top of your file.

  • strlen(str): Returns the number of characters (not counting the null).
  • strcpy(dest, src): Copies the content of one string into another.
  • strcat(s1, s2): Appends (joins) s2 to the end of s1.
  • strcmp(s1, s2): Compares two strings. Returns 0 if they are identical.

Comparing Strings (The Common Trap)

if (str1 == str2) // WRONG! This compares the memory addresses, not the text.
if (strcmp(str1, str2) == 0) // CORRECT! This compares the actual characters.

Reading Strings with Spaces

scanf("%s", ...) stops at the very first space. To read a full line like "Siksha Sarovar", you should use fgets().

char buffer[100];
fgets(buffer, 100, stdin);

Memory Calculation

The word "C is Fun" has 8 characters. To store it as a string, you need an array of at least 9 bytes (8 for text + 1 for \0).

Always ensure your array is big enough! Writing a 20-character name into a 10-character array will cause a "Buffer Overflow," which is the source of many famous security vulnerabilities.