Skip to main content
C Programming Basics
CHAPTER 11 Beginner

Strings in C

Updated: May 17, 2026
5 min read

# CHAPTER 11

Strings in C

1. Introduction

Unlike modern languages (Java, Python, C#), C does not have a built-in String data type. Instead, a string in C is simply an array of characters ending with a special null character ('\0'). Understanding this is crucial for managing memory and avoiding buffer overflows.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Declare and initialize strings as character arrays.
  • Understand the role of the Null Terminator (\0).
  • Read and print strings using scanf, gets, and printf.
  • Use the <string.h> library functions (strlen, strcpy, strcmp, strcat).

3. Declaring and Initializing Strings

A string must always be one character longer than the text you want to store, to make room for the null terminator.
c
12345
// Method 1: Using a string literal (Null terminator added automatically)
char greeting[6] = "Hello"; 

// Method 2: Character by character (Must manually add '\0')
char name[] = {&#039;A&#039;, &#039;l&#039;, &#039;i&#039;, &#039;c&#039;, &#039;e&#039;, &#039;\0&#039;};

4. The Null Terminator (\0)

The \0 tells the C compiler where the string ends. Without it, functions like printf will keep reading memory past the end of your string, resulting in garbage characters being printed on the screen.
12345
Memory layout of "Hello":
+---+---+---+---+---+----+
| H | e | l | l | o | \0 |
+---+---+---+---+---+----+
  0   1   2   3   4   5

5. Input and Output of Strings

Output: Use %s in printf. Input: Use %s in scanf. Note: Do NOT use the & operator when reading a string with scanf. The array name itself points to the memory address!
c
1234567891011
#include <stdio.h>

int main() {
    char name[50];
    
    printf("Enter your first name: ");
    scanf("%s", name); // No '&' needed for strings!
    
    printf("Welcome, %s!\n", name);
    return 0;
}

*Warning:* scanf("%s") stops reading when it hits a space. To read a full line (like "John Doe"), use fgets():

c
1
fgets(name, sizeof(name), stdin);

6. The <string.h> Library

C provides a library of functions to manipulate strings. You must #include <string.h>.

#### 1. strlen() - String Length Returns the number of characters (excluding \0).

c
12
char str[] = "Apple";
int len = strlen(str); // len is 5

#### 2. strcpy() - String Copy You CANNOT use str1 = str2 in C. You must copy the contents.

c
123
char source[] = "Hello";
char destination[20];
strcpy(destination, source); // Copies "Hello" into destination

#### 3. strcat() - String Concatenation Joins two strings together.

c
123
char str1[20] = "Hello ";
char str2[] = "World";
strcat(str1, str2); // str1 is now "Hello World"

#### 4. strcmp() - String Compare Compares two strings. Returns 0 if they are identical.

c
12345
char pwd1[] = "password123";
char pwd2[] = "password123";
if (strcmp(pwd1, pwd2) == 0) {
    printf("Passwords match!\n");
}

7. Mini Project: Password Validator

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

int main() {
    char correct_pwd[] = "admin123";
    char input_pwd[50];

    printf("=== LOGIN SYSTEM ===\n");
    printf("Enter password: ");
    scanf("%s", input_pwd);

    if (strcmp(correct_pwd, input_pwd) == 0) {
        printf("Access Granted.\n");
    } else {
        printf("Access Denied.\n");
    }

    return 0;
}

8. Common Mistakes

  • Forgetting \0 room: Declaring char word[5] = "Hello"; causes an overflow because there's no room for the 6th character (\0).
  • Using == to compare strings: if (str1 == str2) compares their *memory addresses*, not their text. Always use strcmp().
  • Using & in scanf for strings: scanf("%s", &name) is wrong. Use scanf("%s", name).

9. Exercises

  1. 1. Write a program that asks for the user's first and last name separately, concatenates them using strcat, and prints the full name.
  1. 2. Write a program to manually calculate the length of a string using a loop (without using strlen).
  1. 3. Write a program to reverse a string manually.

10. MCQ Quiz with Answers

Question 1

A string in C is actually:

Question 2

Which library is required for strlen()?

Question 3

What does strcmp("apple", "apple") return?

Question 4

Why do you NOT use & with scanf("%s", str)?

Question 5

What is the length of char str[] = "Cat"; in memory?

Question 6

What function copies string A to string B?

Question 7

Which format specifier is used for strings?

Question 8

What happens if you try str1 = str2; for two character arrays?

Question 9

Which function safely reads a string containing spaces?

Question 10

What does the \0 character represent?

11. Interview Questions

  • Q: Explain why C doesn't have a String data type, and how it handles text instead.
  • Q: How does strlen() work internally?
  • Q: What is a buffer overflow, and how can strings cause it?

12. Summary

Strings in C are simply arrays of characters terminated by a null character \0. To manipulate strings, you must use the <string.h> library functions like strcpy, strcat, and strcmp. Never compare strings using ==.

13. Next Chapter Recommendation

Strings and arrays are closely tied to memory addresses. In Chapter 12: Pointers in C, we will unlock the true power of C by directly manipulating memory.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·