C Programming Day 7
Introduction
Welcome to Day 7 of our C Programming Tutorial series.
In the previous lessons, we learned about variables, data types, operators, conditional statements, loops, and arrays. Today, we will learn an important topic in C programming: Strings.
Character arrays in C, C strings, string functions in C, string declaration in C, string initialization in C.
Strings are used in almost every real-world program. Whenever you need to store a name, address, sentence, message, password, or any other text, you will work with strings.
Unlike some modern programming languages, C does not have a built-in string data type. Instead, strings in C are stored as arrays of characters.
In this tutorial, you will learn:
- What is a string in C?
- What is a character array?
- How to declare a string
- How to initialize a string
- How the null character
\0works - How to print and read strings
- Common string functions
- Practical C string programs
- Common mistakes beginners should avoid
Let's get started.
What Is a String in C?
A string in C is a sequence of characters terminated by a special character called the null character, represented by \0.
For example:
"Hello"is stored internally as:
H e l l o \0The \0 tells the C program where the string ends.
Therefore, a string is essentially a character array ending with a null character.
Example
char name[] = "Farhana";The characters are stored as:
F a r h a n a \0Although the word contains 7 visible characters, the array requires space for 8 characters because of the terminating \0.
Character Arrays in C
Syntax
A character array is an array whose elements have the char data type.
char array_name[size];Example
char name[20];This creates a character array capable of storing up to 19 characters plus the null character.
You can also initialize it directly:
char name[] = "Farhana";The compiler automatically calculates the required size.
Difference Between Character and String
It is important to understand the difference between a character and a string.
A character is written inside single quotes:
char letter = 'A';A string is written inside double quotes:
char word[] = "Apple";Character
'A'String
"Apple"Remember:
Single quotes are used for characters, while double quotes are used for strings.
Declaring a String in C
Method 1: Specify the Array Size
There are several ways to declare a string.
char name[20];This creates a character array with 20 elements.
Method 2: Initialize During Declaration
char name[] = "Farhana";The compiler automatically determines the size.
Method 3: Character-by-Character Initialization
char name[] = {'F', 'a', 'r', 'h', 'a', 'n', 'a', '\0'};The last element is the null character.
Why Is \0 Important?
The null character \0 is one of the most important concepts when working with strings in C.
Consider:
char name[] = {'H', 'e', 'l', 'l', 'o', '\0'};The \0 indicates that the string has ended.
Without it, functions that work with strings may continue reading memory beyond the intended characters.
For example:
char name[] = {'H', 'e', 'l', 'l', 'o'};This is a character array, but it is not a properly null-terminated C string.
How to Print a String in C
The %s format specifier is used with printf() to display a string.
Example:
#include <stdio.h>
int main() {
char name[] = "Farhana";
printf("Name: %s", name);
return 0;
}Output
Name: FarhanaHere, %s tells printf() that we are printing a string.
Taking String Input Using scanf()
You can use scanf() with %s to read a string.
Example:
#include <stdio.h>
int main() {
char name[30];
printf("Enter your name: ");
scanf("%29s", name);
printf("Hello, %s!", name);
return 0;
}Example Output
Enter your name: Farhana
Hello, Farhana!Notice that we used:
scanf("%29s", name);The width limit helps prevent writing beyond the array.
Important Limitation
scanf() with %s stops reading when it encounters whitespace.
For example, if the user enters:
Farhana Afrozonly:
Farhanawill be read.
For complete lines containing spaces, fgets() is generally a better choice.
Reading a String Using fgets()
The fgets() function can read a complete line, including spaces.
Example:
#include <stdio.h>
int main() {
char sentence[100];
printf("Enter a sentence: ");
fgets(sentence, sizeof(sentence), stdin);
printf("You entered: %s", sentence);
return 0;
}If the user enters:
C programming is easythe complete sentence can be stored in the character array.
Common String Functions in C
C provides several useful string functions through the string.h header file.
Include the header using:
#include <string.h>Some commonly used functions are:
| Function | Purpose |
|---|---|
strlen() | Finds the length of a string |
strcpy() | Copies one string to another |
strcat() | Joins two strings |
strcmp() | Compares two strings |
strchr() | Finds a character in a string |
strstr() | Finds a substring |
Let's understand the most commonly used functions.
1. strlen() — Find String Length
The strlen() function returns the number of characters in a string, excluding the null character.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char name[] = "Farhana";
printf("Length = %zu", strlen(name));
return 0;
}Output
Length = 7The null character \0 is not included in the returned length.
2. strcpy() — Copy a String
The strcpy() function copies one string into another character array.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "C Programming";
char destination[30];
strcpy(destination, source);
printf("%s", destination);
return 0;
}Output
C ProgrammingThe destination array must have enough space to store the copied string and its terminating null character.
3. strcat() — Join Two Strings
The strcat() function appends one string to another.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char first[30] = "C ";
char second[] = "Programming";
strcat(first, second);
printf("%s", first);
return 0;
}Output
C ProgrammingMake sure the destination array has sufficient capacity for the combined result.
4. strcmp() — Compare Two Strings
The strcmp() function compares two strings.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Apple";
char str2[] = "Apple";
if (strcmp(str1, str2) == 0) {
printf("Strings are equal.");
} else {
printf("Strings are different.");
}
return 0;
}Output
Strings are equal.Do not compare C strings using:
str1 == str2For string content comparison, use strcmp().
String Example Program in C
Let's create a simple program that asks the user for a name and displays its length.
#include <stdio.h>
#include <string.h>
int main() {
char name[50];
printf("Enter your name: ");
fgets(name, sizeof(name), stdin);
printf("Your name is: %s", name);
printf("Number of characters: %zu", strlen(name));
return 0;
}Because fgets() may store the newline character when there is enough space, the value returned by strlen() can include that newline.
String vs Character Array
The terms string and character array are closely related but technically not identical.
A character array can contain arbitrary characters:
char data[3] = {'A', 'B', 'C'};But this is not a C string because there is no \0 terminator.
A proper C string must be null-terminated:
char data[4] = {'A', 'B', 'C', '\0'};So:
Every C string is stored in a character array, but not every character array is a C string.
Important Rules for Strings in C
Keep these rules in mind when working with strings:
Rule 1: Use double quotes for strings
Correct:
char city[] = "Dhaka";Incorrect:
char city[] = 'Dhaka';Rule 2: Leave room for \0
For example:
char word[6] = "Hello";This works because 5 characters plus \0 require 6 elements.
Rule 3: Use %s to print a string
printf("%s", word);Rule 4: Be careful with array size
Always ensure that the destination array has enough space before copying or joining strings.
Rule 5: Include <string.h> for standard string functions
#include <string.h>Common Mistakes Beginners Make
Mistake 1: Forgetting the Null Character
Manually creating a string as a character array requires room for \0.
Mistake 2: Using == to Compare Strings
This does not compare the contents of two C strings.
Use:
strcmp(str1, str2)instead.
Mistake 3: Using an Unsafe Input Pattern
Avoid an unrestricted:
scanf("%s", name);when the input array has a fixed size.
A width limit or fgets() is safer.
Mistake 4: Insufficient Destination Space
This is dangerous:
char destination[5];
strcpy(destination, "Programming");The destination array is too small.
Mistake 5: Confusing Characters and Strings
Remember:
'A'is a character, while:
"A"is a string containing A followed by \0.
Practice Questions
Try solving these programs yourself:
Question 1
Write a C program to store your name in a character array and print it.
Question 2
Write a program to find the length of a string without using strlen().
Question 3
Write a program to copy one string into another.
Question 4
Write a program to compare two strings using strcmp().
Question 5
Write a program to concatenate two strings.
Question 6
Write a program to count the number of vowels in a string.
Question 7
Write a program to reverse a string.
Quick Revision
Let's quickly review what we learned today.
- A string in C is a sequence of characters ending with
\0. - Strings are stored using character arrays.
%sis used to print a string withprintf().scanf()with%sstops at whitespace.fgets()can read a line containing spaces.strlen()finds string length.strcpy()copies a string.strcat()joins strings.strcmp()compares strings.- Always make sure character arrays have enough space.
Conclusion
Strings are an essential part of C programming because they allow programs to work with text and user input.
The most important concept to remember is that C does not have a built-in string data type. Instead, strings are represented by character arrays terminated by the null character \0.
Once you understand character arrays, string input/output, and functions such as strlen(), strcpy(), strcat(), and strcmp(), you will be ready to build more practical C programs.
In the next lesson, C Programming Day 8, we can continue with another important C programming concept and build on what you have learned so far.
Keep practicing — the best way to learn C programming is to write and run code yourself.

Post a Comment
Thank You For Visit My Website.
I Will Contact As Soon As Possible.