Reading a String (Input):
- Using scanf() (single word only):
char str[20];
scanf("%s", str); // Stops reading at whitespace- Using gets() (reads whole line):
char str[50];
gets(str); // Deprecated but allows spaces- Using fgets() (safe alternative):
fgets(str, sizeof(str), stdin);Writing a String (Output):
- Using printf():
printf("%s", str);- Using puts():
puts(str); // Adds newline automaticallyNull Character (‘\0’):
The null character is used to terminate a string in C. Without it, functions like printf() would not know where the string ends.
char s[] = "Hi"; // Actually stored as {'H', 'i', '\0'}