Strings in C (Simple Explanation for Beginners)
After learning arrays in C, the next important concept is strings.
Strings are used everywhere in names, messages, and text data.
Let’s understand them in a simple way.
What is a String?
A string is a collection of characters.
👉 In C, a string is actually an array of characters.
Example:
char name[] = "Ali";How Strings Work in C
Every string ends with a special character:
👉 \0 (null character)
So internally, "Ali" is stored like this:
A l i \0Declaring a String
char str[10];This means the string can store up to 9 characters + 1 null character.
Taking Input from User
#include <stdio.h>
int main() {
char name[20];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello %s", name);
return 0;
}Displaying a String
printf("%s", name);Important Points
- Strings are arrays of characters
- Always end with
\0 - Use
%sto print strings - Be careful with size to avoid errors
Why Strings Are Important
Strings help you:
- Work with text data
- Take user input
- Build real-world programs
Final Thoughts
Strings are a key part of programming.
Once you understand strings, your programs will become more useful and interactive.
Thanks for reading!
Comments
Post a Comment