C Strings in C Programming – Full Tutorial with Examples
In C programming, strings are used to store and work with text. However, unlike languages like Python or Java, C doesn’t have a built-in string
data type. Instead, we use arrays of characters, terminated with a null character '\0'
.
đ What Is a String in C?
A string in C is a sequence of characters ending with '\0'
:
char city[] = "Dhaka";
This will store: 'D' 'h' 'a' 'k' 'a' '\0'
đ¤ How to Print a String
#include <stdio.h>
int main() {
char city[] = "Dhaka";
printf("%s\n", city);
return 0;
}
đ¯ Accessing Individual Characters
#include <stdio.h>
int main() {
char country[] = "Bangladesh";
printf("First: %c\n", country[0]);
printf("Fifth: %c\n", country[4]);
return 0;
}
✏️ Modifying a String
#include <stdio.h>
int main() {
char food[] = "Mango";
food[0] = 'B';
printf("%s\n", food); // Bango
return 0;
}
đ Looping Through a String
Simple loop:
#include <stdio.h>
int main() {
char name[] = "Ahsan";
for (int i = 0; i < 5; i++) {
printf("%c\n", name[i]);
}
return 0;
}
Loop with sizeof
(more sustainable):
#include <stdio.h>
int main() {
char name[] = "Ahsan";
int len = sizeof(name) / sizeof(name[0]);
for (int i = 0; i < len - 1; i++) {
printf("%c\n", name[i]);
}
return 0;
}
đ§ą Manual String Creation
#include <stdio.h>
int main() {
char country[] = {'B', 'a', 'n', 'g', 'l', 'a', '\0'};
printf("%s\n", country);
return 0;
}
Note: '\0'
is required to terminate the string properly.
⚖️ Compare Two Methods
#include <stdio.h>
int main() {
char s1[] = {'H','e','l','l','o','\0'};
char s2[] = "Hello";
printf("%zu\n", sizeof(s1)); // 6
printf("%zu\n", sizeof(s2)); // 6
return 0;
}
đ Real-Life Example: Welcome Message
#include <stdio.h>
int main() {
char greeting[] = "Hello";
char name[] = "Amina";
printf("%s, %s!\n", greeting, name); // Hello, Amina!
return 0;
}
✅ Summary Table
Feature | Description |
---|---|
String type | No built-in type; use char[] |
Null terminator | Use '\0' to mark end |
Print string | Use %s in printf() |
Access characters | Use indexing like string[0] |
Modify string | Assign new char to index |
Loop | for loop + sizeof |
Manual string | Use char array + '\0' |
C Strings – Special Characters and Escape Sequences
In C programming, strings are written within double quotes. But what happens if you need to include a quote "
or backslash \
inside the string?
If you try this directly, it causes an error:
// Invalid – will cause a compile error
char txt[] = "We are the so-called "Vikings" from the north.";
✅ The Solution: Escape Characters
C uses the backslash (\
) as an escape character. It tells the compiler that the next character has a special meaning.
đ Common Escape Sequences
Escape Sequence | Result | Description |
---|---|---|
\' | ' | Inserts a single quote |
\" | " | Inserts a double quote |
\\ | \ | Inserts a backslash |
đ§Ē Example: Double Quotes
char txt[] = "We are the so-called \"Vikings\" from the north.";
printf("%s\n", txt);
Output:
We are the so-called "Vikings" from the north.
đ§Ē Example: Single Quote
char txt[] = "It\'s alright.";
printf("%s\n", txt);
Output:
It's alright.
đ§Ē Example: Backslash
char txt[] = "The character \\ is called backslash.";
printf("%s\n", txt);
Output:
The character \ is called backslash.
✨ Other Popular Escape Characters
Escape Sequence | Result | Usage |
---|---|---|
\n | New Line | Moves cursor to the next line |
\t | Tab | Inserts a horizontal tab |
\0 | Null | Marks end of string (automatically added) |
đ Summary
- Use
\"
to insert double quotes inside a string. - Use
\'
for single quotes. - Use
\\
for backslash. - Use
\n
for new lines, and\t
for tabs.
C Programming – Getting User Input
In C, we use the scanf()
function to accept input from the user. You’ve already seen how printf()
displays output — now let's explore how to take input.
đĸ Basic Input: Integer
This example takes a number from the user and prints it back:
#include <stdio.h>
int main() {
int myNum;
printf("Type a number: \n");
scanf("%d", &myNum);
printf("Your number is: %d", myNum);
return 0;
}
Explanation:
%d
is the format specifier for integers.&myNum
means "store the value at the address of myNum".
đ§Ž Multiple Inputs: Integer + Character
#include <stdio.h>
int main() {
int myNum;
char myChar;
printf("Type a number AND a character: \n");
scanf("%d %c", &myNum, &myChar);
printf("Your number is: %d\n", myNum);
printf("Your character is: %c\n", myChar);
return 0;
}
This accepts two inputs in one line: an integer and a character.
đĨ String Input: Single Word
scanf()
can read a single word (until a space or newline):
#include <stdio.h>
int main() {
char firstName[30];
printf("Enter your first name: \n");
scanf("%s", firstName);
printf("Hello %s", firstName);
return 0;
}
Note: You do not need the reference operator &
when scanning strings.
⚠️ Limitation of scanf() for Strings
scanf("%s", ...)
only reads up to the first whitespace. So if the user types “John Doe”, only “John” is captured.
char fullName[30];
scanf("%s", fullName); // Will only capture "John"
đ Full Name Input: Using fgets()
To read a full line of text (including spaces), use fgets()
:
#include <stdio.h>
int main() {
char fullName[30];
printf("Type your full name: \n");
fgets(fullName, sizeof(fullName), stdin);
printf("Hello %s", fullName);
return 0;
}
Explanation:
fgets()
reads until newline or buffer limit.sizeof(fullName)
prevents buffer overflow.- It retains the newline
\n
, which you may want to remove usingstrcspn()
or similar.
đ Summary
Function | Purpose | Limitation |
---|---|---|
scanf() |
Reads formatted input (numbers, characters, single-word strings) | Cannot read strings with spaces |
fgets() |
Reads an entire line (including spaces) | Includes newline character |
āĻোāύ āĻŽāύ্āϤāĻŦ্āϝ āύেāĻ:
āĻāĻāĻি āĻŽāύ্āϤāĻŦ্āϝ āĻĒোāϏ্āĻ āĻāϰুāύ