CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy C++ · 7 min · free

Strings in C

7 min4 KwickClipsFull text belowFree
Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

A C string is a char array that ends with the null character \0, so it needs one box more than its letters.

On screen in this lesson

A string is a character array

C has no separate string type
A string is an array of char
It ends with the null character '\0'
So size needed = letters + 1

"Riya" in memory

BoxCharacterASCII code
name[0]'R'82
name[1]'i'105
name[2]'y'121
name[3]'a'97
name[4]'\0' (null)0

Pause and predict

char c[4] = "Riya"; is it safe?
4 letters fill all 4 boxes
No room for '\0', so it is not a proper string
printf may print extra garbage after Riya

Reading functions compared

FunctionSpaces?Safe?
scanf("%29s", s)stops at spaceyes, with width
fgets(s, 30, stdin)reads spacesyes, size limit
gets(s)reads spacesremoved from C

Functions in string.h

FunctionWhat it doesExample
strlen(s)counts letters"Riya" gives 4
strcpy(d, s)copies s into dd becomes s
strcat(d, s)adds s to end of d"Hi"+"Ya"
strcmp(a, b)compares a and b0 if same

What strcmp returns

0 when both strings are the same
Negative when the first comes earlier
Positive when the first comes later
Order follows ASCII codes: 'Z' comes before 'a'

Quick answers

Why does "Riya" need 5 boxes?

4 letters plus the null character \0.

How do you compare two strings?

With strcmp, never with ==. It returns 0 when they are equal.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. The name Riya has four letters. So why does C need five boxes to store it? Today we learn how C stores strings and how to read and print them. Then we use four famous string functions and write exam programs.

Many languages have a special string type, but C does not. In C, a string is simply an array of characters, using the char type. Every string ends with a special character called the null character, written as backslash zero. Its value is zero, and it marks where the string ends. That is why a string always needs one extra box, for the null character.

Here is the word Riya stored in an array called name. Box zero holds capital R, stored as its ASCII code, eighty two. Box one holds small i, code one hundred five. Box two holds y, code one hundred twenty one. Box three holds a, code ninety seven. Box four holds the null character, whose code is zero, and this answers our opening question.

There are two ways to create a string. With double quotes, as in array a, C adds the null character for you. With single characters in curly braces, as in array b, you must add backslash zero yourself. To print a string, use percent s in printf. The output is Riya Aman.

Pause and predict. What happens with char c, four, equals Riya? The four letters fill all four boxes. There is no room left for the null character, so c is not a proper string. Printing it with percent s may show extra garbage after Riya, because printf keeps going until it finds a zero.

To read a string, you can use scanf with percent s. Notice there is no ampersand before name, because an array name already gives its address. The twenty nine stops scanf from reading more than twenty nine characters, leaving one box for the null character. If you type Riya Shah, this prints only Hello Riya, because percent s stops at the first space.

To read a full line, spaces included, use fgets. It takes the array, its size and stdin, which means the keyboard. Fgets never reads more than the size allows, so it is safe. If you type Riya Shah, it stores the whole name, and also the Enter key as a newline at the end. The function puts prints a string and then moves to a new line.

Let us compare the ways to read a string. Scanf with percent s stops at the first space, and it is safe only when you give a width. Fgets reads the whole line with spaces, and it respects the size limit. Gets also reads spaces, but it has no size limit, so it was removed from the C language in the 2011 C standard. Never use gets.

To use the four famous string functions, include the header file string.h. Strlen returns the length, counting letters but not the null character, so Riya gives four. Strcpy copies the source string s into the destination d. Strcat joins s to the end of d, so Hi and Ya become HiYa. Strcmp compares two strings, and returns zero if they are the same.

Let us use three functions together. The array s has room for twenty characters. Strcpy puts K w i c k into s. Then strcat adds p r e p to the end. Strlen now counts nine letters. Percent z u is the format for the value strlen returns. The destination must be big enough, or these functions write past the end of the array.

Here we check a password, stored in the array pin. Strcmp compares pin with the correct password, character by character. They match, so strcmp returns zero, and it prints Welcome. Remember, never compare strings with double equals. Double equals only compares the addresses of the two arrays, not the letters inside.

Strcmp gives three kinds of answers. It returns zero when both strings are exactly the same. It returns a negative number when the first string comes earlier in order, like apple before mango. It returns a positive number when the first string comes later. The order follows ASCII codes, so every capital letter comes before every small letter.

Now two programs that exams ask often. First, find the length without strlen. We start n at zero. While s of n is not the null character, we add one to n. The loop stops at the null character, after counting seven letters of Gujarat. So it prints seven.

Second, print a string in reverse. The last letter is at index length minus one, which is four for Surat. The loop starts there and counts down to zero. Percent c prints one character at a time. So Surat comes out as t a r u capital S. If the reverse matches the original, like nitin, the word is a palindrome.

Let us revise what we learned today. A string in C is a char array that ends with the null character. So it needs one box more than the number of letters. Scanf with percent s stops at a space, while fgets reads the full line. Strlen, strcpy, strcat and strcmp all need string.h. And always compare strings with strcmp, never with double equals. Now try counting the vowels in your own name.

Courses that teach this

CourseUnit
Programming All levels CArrays and Strings

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →