In C, strings are arrays of characters ending with a special null character '\0'.
char str[] = "hello"; // Actually stored as: {'h', 'e', 'l', 'l', 'o', '\\0'}
#include <stdio.h>
int main() {
char str1[] = "Hello";
printf("%s\\n", str1);
return 0;
}
#include <stdio.h>
int main() {
const char* str2 = "World"; // immutable!
printf("%s\\n", str2);
return 0;
}
#include <stdio.h>
int main() {
char name[100];
printf("Enter your name: ");
scanf("%s", name); // Note: reads until space
printf("Hello, %s!\\n", name);
return 0;
}
⚠️ Limitation: scanf stops at space. To read full lines, use fgets.
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "Hello";
char str2[] = "World";
// strlen
printf("Length of str1: %lu\\n", strlen(str1));
// strcat
strcat(str1, str2);
printf("After strcat: %s\\n", str1); // HelloWorld
// strcpy
char copy[100];
strcpy(copy, str1);
printf("Copied: %s\\n", copy);
// strcmp
int result = strcmp(str1, copy);
printf("Compare result: %d\\n", result); // 0 if equal
return 0;
}