Strings in C are sequences of characters terminated by a null character (\0).
Unlike other languages, C does not have a dedicated string datatype—strings are implemented using character arrays.
1. String Declaration in C
Syntax
char string_name[size];
Examples
1.1 Declaring without initialization
char name[20];
1.2 Declaring with initialization
char name[] = "Sagar";
1.3 Using character array form
char name[] = {'S', 'a', 'g', 'a', 'r', '\0'};
1.4 Using scanf to read
char city[20];
scanf("%s", city); // Stops at space
1.5 Using gets() or fgets()
char sentence[50];
fgets(sentence, sizeof(sentence), stdin); // Safe method
2. Common String Functions in C (from <string.h>)
Below are the most frequently used string functions:
2.1 strlen() – Length of a string
strlen(str);
Example
int len = strlen("Hello"); // Output: 5
2.2 strcpy() – Copy a string
strcpy(dest, source);
Example:
char a[20], b[20] = "C Language";
strcpy(a, b); // a = "C Language"
2.3 strcat() – Concatenate two strings
strcat(str1, str2);
Example:
char name[20] = "Hello ";
strcat(name, "World!"); // Output: Hello World!
2.4 strcmp() – Compare two strings
Returns:
- 0 → strings are equal
- positive → first is greater
- negative → second is greater
Example:
strcmp("abc", "abd"); // Output: -1
2.5 strlwr() & strupr() (Compiler-specific)
Convert to lowercase or uppercase.
3. String Manipulation in C
3.1 Traversing a string
for (int i = 0; str[i] != '\0'; i++) {
printf("%c\n", str[i]);
}
3.2 Changing specific characters
char name[] = "Sagar";
name[0] = 'R';
printf("%s", name); // Output: Ragar
3.3 Reversing a string
#include <stdio.h>
#include <string.h>
int main() {
char str[20] = "Hello";
int len = strlen(str);
for(int i = len - 1; i >= 0; i--)
printf("%c", str[i]);
return 0;
}
Program Structure & Storage Classes in C
Storage classes define visibility, lifetime, and scope of variables in C.
The four major storage classes are:
- Automatic (
auto) - External (
extern) - Static (
static) - (optional) Register (
register)
1. Automatic Variables (auto)
These are local variables inside a function.
Features
- Default storage class for local variables
- Memory allocated when function is called
- Memory released when function ends
- Not accessible outside the function
Example
void demo() {
auto int a = 10; // auto is optional
int b = 20; // same as auto
}
2. External Variables (extern)
Used to declare global variables across multiple files.
Features
- Declared outside all functions
- Can be used by any function
- Use
externkeyword to access in other files
Example (Single File)
#include <stdio.h>
int x = 10; // global variable
void display() {
extern int x;
printf("%d", x);
}
int main() {
display();
return 0;
}
3. Static Variables (static)
Static variables retain their value even after the function ends.
Features
- Lifetime = entire program
- Scope = local to the block/function
- Value persists between function calls
Example
void counter() {
static int count = 0;
count++;
printf("%d\n", count);
}
int main() {
counter(); // 1
counter(); // 2
counter(); // 3
return 0;
}
Summary Table
| Storage Class | Scope | Lifetime | Default Value |
|---|---|---|---|
| auto | Local | Within function | Garbage |
| extern | Global | Entire program | Zero |
| static (local) | Local | Entire program | Zero |
| static (global) | File scope | Entire program | Zero |
📚 Citations
🔗 View other articles about C Programming:
https://savanka.com/category/learn/c-programming/
🔗 External C Documentation:
https://www.w3schools.com/c/