The C Preprocessor
Lines that start with # in a C file are preprocessor directives — handled before compilation begins.
#define
Replaces a symbolic name with a value throughout the file. By convention, constants are written in UPPERCASE.
#define LIMIT 100
#define PI 3.14159
Changing the #define in one place takes effect everywhere — makes programs easier to update.
#include
Inserts the contents of another file (a header file) at that point during compilation. Header files end in .h.
#include <stdio.h> // standard input/output: printf, scanf
#include <conio.h> // console I/O: getch, clrscr
Standard Output: printf()
printf() sends formatted text to the screen. The "f" stands for "formatted."
Syntax:
printf("format string", variable1, variable2, ...);
Conversion specifications:
| Specifier | Meaning |
|---|---|
%c | Character |
%d | Decimal integer |
%f | Floating point (float) |
%lf | Double |
%e | Scientific notation |
%s | String |
Field width — specified between % and the conversion character:
printf("%c %3c %5c\n", 'A', 'B', 'C');
// A printed normally, B right-justified in 3 chars, C in 5
Standard Input: scanf()
scanf() reads formatted input from the keyboard. The & operator (address-of) is required before each variable.
Syntax:
scanf("%conversion_spec", &variable);
Examples:
scanf("%d", &num); // read an integer
scanf("%f", &x); // read a float
scanf("%lf", &y); // read a double
scanf("%c", &ch); // read a character
scanf("%s", name); // read a string (no & for arrays)
scanf() returns the number of successful conversions. When reading characters, whitespace is not skipped automatically (unlike numbers).
Escape Sequences and Console I/O
Escape Sequences
| Sequence | Meaning |
|---|---|
\n | Newline |
\t | Tab |
\\ | Backslash |
\" | Double quote |
\' | Single quote |
\a | Bell (sound) |
Console I/O (conio.h)
| Function | Behavior |
|---|---|
gets(str) | Reads a string until Enter; no newline stored |
getchar() | Reads one character; waits for Enter |
getch() | Reads one character without echo; no Enter needed |
getche() | Reads one character with echo; no Enter needed |
puts(str) | Writes string to screen + newline |
putchar(ch) | Writes one character |
clrscr() | Clears the screen |
General C Program Structure
global declarations;
function1() {
local variables;
statements;
}
main() {
local variables;
statements;
}
Annotated sample program:
#include <stdio.h>
#include <conio.h>
main() {
char c1, c2, c3;
int i;
float x;
double y;
printf("\nInput three characters, an int, a float, and a double:");
scanf("%c %c %c %d %f %lf", &c1, &c2, &c3, &i, &x, &y);
printf("Here is the data you entered:\n");
printf("%3c %3c %3c %5d %12f %12lf", c1, c2, c3, i, x, y);
return 0;
}
Key parts:
#include <stdio.h>— includes standard I/O declarationsmain()— entry point; every C program starts here/* ... */— comment; ignored by compiler{and}— mark start and end of a function body;— terminates every statementreturn 0;— exitsmain()and signals success
Practice Exercises — Lesson 3
Try It: Input/Output
Modify num1 and num2 to simulate different inputs and observe the formatted output.