In C, a string is a sequence of characters enclosed in double quotes. Every string literal is implicitly terminated by a null character '\0', which marks the end of the string. If this terminator is missing in a manual built character array, functions like printf("%s", ...) will continue reading memory beyond the array until encountering a zero byte, producing unpredictable output.
Consider the following14 example:
#include <stdio.h>
int main() {
char label[] = "demo";
char symbols[] = {'d', 'e', 'm', 'o'};
printf("%s\n", label);
printf("%s\n", symbols);
return 0;
}
The first printf prints demo correctly because label contains a hidden \0. The second line may show demo followed by random characters—the missing terminator causes the function to scan beyond the array. To fix this, append '\0' to symbols:
char symbols[] = {'d', 'e', 'm', 'o', '\0'};
Escape sequences allow special characters within strings and character constants. Common ones include \n (newline), \t (tab), \\ (backslash), \' (single quote), \" (double quote), \b (backspace), and \a (alert). Additionally, numeric escape sequences represent characters via their octal or hexadecimal values: \ddd for a one‑ to three‑digit octal number and \xdd for two hexadecimal digits. For example, '\102' is the same as 'B', and '\x41' is 'A'.
The behavior of \b can be confusing: if it is followed by another character, nothing is erased; if it is the last character before a string’s end, the previous character appears to be "overwritten" on some systems. Here are demonstrations:
#include <stdio.h>
int main() {
printf("before\bafter\n"); // prints "befater"
printf("before\b"); // may show "befor" depending on terminal
printf("C:\\projects\\main.c\n");
printf("%c\n", '\''); // '
'
printf("%s\n", "\""); // "
printf("folder\\data\\file.txt\n");
printf("\a"); // alert sound
printf("%c\n", '\102'); // B
return 0;
}
C statements come in several forms:
- Empty statement: A lone semicolon
;does nothing.
; // empty statement
- Expression statement: Any expression followed by a semicolon, e.g.,
int count = 5;float price = 9.99f;.
int items = 20;
char initial = 'A';
float pi = 3.14159f;
- Compound statement: A block enclosed in braces
{ }that groups multiple statements into a single logical unit.
{
int values[] = {10, 20, 30, 40, 50};
int size = sizeof(values) / sizeof(values[0]);
process(values, size);
}
- Control statements govern program flow. They include conditional branches (
if,switch), loops (while,do‑while,for), and jump statements (break,continue,goto,return). These will be explored detail in later topics.
C supports three structural building blocks:
- Sequence – instructions execute one after another.
- Selection – decision making using
iforswitch. - Itertaion – repetition with loops.
ASCII characters have numeric codes. The most memorable ranges are:
- Digits
'0'to'9': codes 48‑57 - Uppercase letters
'A'to'Z': 65‑90 - Lowercase letters
'a'to'z': 97‑122 '\n'is 10- The first 32 codes (0‑31) are non‑printable control characters.
To display all printable characters, you can iterate over the visible range:
#include <stdio.h>
int main() {
int code = 32;
while (code <= 126) {
printf("%c ", code);
code++;
}
return 0;
}
This loop prints spaces, punctuation, digits, uppercase letters, lowercase letters, and other symbols.