Chapter 420 min read
C Basic Syntax, I/O & Format Specifiers
Mastering language tokens, identifiers, reserved keywords, input/output operations, and format specifiers.
Released1972
Core MottoStatically-Typed Formatted Stream Control
CreatorsDennis Ritchie / Bell Labs
1. Syntax, Tokens & Keywords
C source code consists of individual tokens processed during the compilation tokenizer phase.
Identifiers name variables/functions, while reserved keywords provide fixed language structure.
Tokens & Identifiers
Tokens:The smallest language elements (keywords, identifiers, literals, symbols).
Identifiers:Names starting with letters (A-Z, a-z) or underscore (_), followed by letters, digits, or underscores.
Case Sensitivity:C is strictly case-sensitive (`age` and `Age` are distinct).
Restrictions:Special symbols (@, $, %) and reserved keywords cannot be used as identifiers.
Reserved Keywords
Fixed Meaning:C contains reserved lowercase words that cannot be repurposed.
Core Examples:`int`, `float`, `char`, `if`, `else`, `while`, `for`, `return`, `struct`, `typedef`, `static`, `void`.
Semicolons & Whitespace
Semicolon (;):Acts as a statement terminator, allowing multiple statements on one line or splitting a single statement across multiple lines.
Compound Blocks:Statements are grouped into logical units using curly braces `{}`.
Classroom Discussion Starter
Why does C require explicit semicolons and static variable declarations before execution?
Takeaway Goal: Static type declarations and statement terminators allow the compiler to calculate precise memory offsets and parse code unambiguously without dynamic runtime interpretation.
2. User Input Handling (scanf, getchar, gets)
Standard input (`stdin`) functions convert raw character streams into typed C variables.
Formatted Input: scanf()
Address Operator (&):Variable parameters require the `&` operator to pass memory location addresses.
Space Trick for %c:Placing a leading space before `%c` (e.g., `" %c"`) forces `scanf` to skip leftover whitespace/newlines in the stream.
c
#include <stdio.h>
int main() {
int price, qty;
printf("Enter price and quantity: ");
scanf("%d %d", &price, &qty); // Takes multiple integer inputs
printf("Total: %d
", price * qty);
return 0;
}Unformatted Character & String Input
getchar():Reads a single character directly from standard input.
gets():Accepts full string lines including space delimiters (until Enter key is pressed).
c
#include <stdio.h>
int main() {
char name[20];
printf("Enter full name: ");
gets(name); // Reads text including spaces
printf("You entered: %s
", name);
return 0;
}3. Format Specifiers & Output Formatting
Format specifiers translate raw data streams into typed memory values during console and file I/O operations.
Core Format Specifiers
Integers:`%d` / `%i` (signed int), `%u` (unsigned int), `%ld` (long), `%lld` (long long)
Octal & Hex:`%o` (octal), `%x` / `%X` (hexadecimal lowercase/uppercase)
Floats & Doubles:`%f` (float), `%lf` (double), `%e` / `%E` (scientific notation)
Text & Addresses:`%c` (single char), `%s` (null-terminated string), `%p` (pointer memory address), `%%` (literal % sign)
Format Modifiers & ASCII Mapping
Character ASCII Trick:Printing a `char` with `%c` shows its character value, while `%d` reveals its ASCII numerical value.
Formatting Structure:`%[flags][width][.precision][length]specifier` (e.g., `%4.2f`, `%05d`).
c
#include <stdio.h>
int main() {
char ch = 'D';
float num = 5.347;
printf("As character: %c
", ch); // Output: D
printf("As ASCII value: %d
", ch); // Output: 68
printf("Scientific: %e
", num); // Output: 5.347000e+000
printf("Width & Precision: %4.2f
", num); // Output: 5.35
return 0;
}Format Specifiers in File I/O (fprintf & fscanf)
File Streams:The same format specifier rules apply to file streams using `fprintf()` and `fscanf()` with file pointers.
c
#include <stdio.h>
int main() {
int x = 10, y = 20, z = 30;
// Writing formatted data to file
FILE *fp = fopen("test.txt", "w");
fprintf(fp, "%d, %d, %d", x, y, z);
fclose(fp);
// Reading formatted data back from file
fp = fopen("test.txt", "r");
fscanf(fp, "%d, %d, %d", &x, &y, &z);
printf("Read from file: %d, %d, %d
", x, y, z);
fclose(fp);
return 0;
}