C
Console Input and Output
I/O Statements

Talking to the Program

Programs need data to work with, and a way to show results. C provides standard library functions to communicate with the console.

What happens if you write scanf("%d", age); and forget the & symbol?
io_basics.cstdio.h
#include <stdio.h> int main(void) { int age; // Output to console printf("Enter your age: "); // Input from console scanf("%d", &age); printf("You are %d years old.", age); return 0; }
Formatted Output

printf Format Specifiers

The printf function replaces format specifiers (like %d or %f) with the values of the variables provided after the comma.

🖨️

Multiple Variables

Specifiers are matched with variables in exact sequential order.

int h = 10, m = 30; printf("%d hours, %d mins", h, m); // Output: 10 hours, 30 mins
📐

Width and Precision

Control alignment and decimal places inside the % symbol.

float pi = 3.14159; printf("%.2f", pi); // 3.14 printf("%03d", 5); // 005
Taking Input

Why scanf needs the & (Address) Operator

When a delivery person brings a package, they need your home address, not just your name. scanf works the exact same way.

The Memory Mailbox

Try delivering the value 42 to the variable score.

42
?
Address: 0x7FFE

The Breakdown

int score = 10; // Memory creates a box at 0x7FFE containing 10.

Missing the &

scanf("%d", score);
You are passing the value `10` to scanf. Scanf tries to deliver the input to memory address `0x0000000A` (10), which belongs to the operating system. Crash!

Using the &

scanf("%d", &score);
You are passing the address `0x7FFE`. Scanf goes to that exact address and deposits the user's input safely.

Common Pitfalls

The Invisible Newline (\n) Trap

When you type a number and press Enter, both the number and a newline character \n enter the keyboard buffer. scanf("%d") reads the number but leaves the \n behind!

Keyboard Input Buffer

Scenario: The user typed 42, pressed Enter, then typed Y and pressed Enter.

Step 1: Read the Integer

%d automatically ignores leading spaces, reads the digits, and stops exactly at the first non-digit (the \n).

scanf("%d", &age);
Value stored in `age`:
?

Step 2: Read the Character

Unlike %d, the %c specifier reads the very next character in the buffer, even if it is a newline!

Reads the leftover \n
Space skips the \n first
Value stored in `grade`:
?
Alternative Functions

getchar and putchar

While `printf` and `scanf` are versatile, C provides specialized, faster functions when you only need to read or write a single character.

⌨️

getchar()

Reads a single character directly from the standard input (keyboard buffer).

char ch; printf("Press any key: "); ch = getchar();
🖥️

putchar()

Writes a single character directly to the standard output (screen).

char initial = 'A'; putchar(initial); putchar('\n'); // Prints a newline
Knowledge Check

Test Your Understanding

Question 1 of 5
Final recap

Key Takeaways

%

Match Specifiers

Ensure the number and type of % format specifiers exactly match the variables you provide.

&

Use Addresses for Input

Always use the & operator with scanf for basic data types so it knows where to store the input.

\n

Mind the Buffer

Numbers leave \n behind. Use a space before %c in scanf(" %c") to safely skip leftover whitespace.