Foundational Concepts of the C Programming Language
sizeof is an operator that returns the memory size (in bytes) occupied by a data type or variable.
The scanf function returns the number of successfully read items; if it fails to read any valid input, it returns EOF (a macro defined as -1, short for End of File). For continuous integer input, use a loop like while (scanf("%d", &num) == 1) — this loop runs as long as a valid integer is read, termianting when scanf returns EOF.
scanf skips all leading whitespace characters before starting to read, and stops reading at the first subsequent whitespace character, leaving remaining characters in the input buffer. The buffer content persists until it is either consumed by another read operation or explicitly flushed (e.g., fflush(stdin)). For continuous character input, use getchar() in a loop: while ((c = getchar()) != EOF). getchar() returns the ASCII value of the read character, or EOF on failure. To handle leftover newline characters in the buffer after scanf, call getchar() once to consume the newline.
Format Specifiers
%d: Prints integers in decimal format%c: Outputs a single character%s: Prints null-terminated strings%f: Displays float values, defaulting to 6 decimal places. Use%.nfto specifyndecimal places, or%mfto set a minimum output width ofmcharacters with right alignment if needed.%lf: Prints double-precision floating-point values%ld: Prints long integers%lld: Prints long long integers%p: Shows memory addresses in hexadecimal format%zd: Outputs values of typesize_t%u: Prints unsigned integers%x: Displays unsigned integers in hexadecimal format%.0f: Trims a float to its integer part with standard rounding
Data Types
C provides several built-in data types, including char, short, int, long, long long, float, and double. You can also define custom types like structs. A signed int tpyically ranges from -2147483648 to 2147483647.
1 byte equals 8 bits. A single Chinese character usually occupies 2 bytes, and 1 KB is 1024 bytes. A literal like 55.5 is treated as a double by default; append 'f' (e.g., 55.5f) to declare it as a float.
Variables and Constants
When a local variable and a global variable share the same name, the local variable takes precedence in its scope.
Scope and Lifetime
- Scope: Local variables are only accessible within their declaring block or function. Global variables are accessible across the entire project.
- Lifetime: Local variables are created when entering their scope and destroyed when exiting. Global variables exist for the entire duration of the program's execution.
- Uninitialized global variables are automatically set to 0, while uninitialized local variables contain undefined (random) values. The
externkeyword is used to declare variables or functions defined in another source file.
Strings
Strings in C are terminated by a null character ('\0'). When using printf with %s, it prints characters starting from the given address until it encounters '\0'. The printf function returns the number of characters printed, including the implicit null terminator for strings. Use %02d to print an integer with a fixed width of 2 characters (padding with leading zeros if needed), and %.2d to ensure the integer displays at least two digits.
The strlen library function calculates string length by counting characters from the starting address until it hits '\0' (the null terminator is not counted). If a character array does not end with '\0', strlen will return an undefined value as it reads past the array boundaries.
Key Differences Between sizeof and strlen
sizeofis an operator, whilestrlenis a standard library function.sizeofmeasures the total memory size (in bytes) of its operand, regardless of stored data.strlenspecifically counts string characters before'\0'and only works with strings.
Escape Characters
| Escape Character | Description |
|---|---|
\? |
Avoids trigraph interpretation when writing multiple consecutive question marks |
\' |
Represents a single quote character constant |
| `\ |