C Input/Output (I/O)
Input/Output in C is performed using functions from the <stdio.h> (Standard Input/Output) library.
Common I/O Functions
Section titled “Common I/O Functions”| Function | Purpose |
|---|---|
printf() | Prints output to the console |
scanf() | Reads input from the keyboard |
fprintf() | Writes output to a file |
fscanf() | Reads input from a file |
sprintf() | Writes formatted output to a string |
sscanf() | Reads formatted input from a string |
Format Specifiers
Section titled “Format Specifiers”A format specifier begins with % and tells the compiler the data type of the value to print or read.
| Specifier | Data Type |
|---|---|
%d | int |
%u | unsigned int |
%c | char |
%s | String (char[]) |
%f | float (scanf), float/double (printf) |
%lf | double (scanf) |
%x | Hexadecimal |
%o | Octal |
%p | Pointer (memory address) |
%% | Prints % |
printf()
Section titled “printf()”printf() is used to display formatted output on the console.
Syntax
printf("format_string", arguments);Example
int age = 20;float cgpa = 8.7;char grade = 'A';
printf("Age = %d\n", age);printf("CGPA = %f\n", cgpa);printf("Grade = %c\n", grade);Important Notes
- Pass values, not addresses.
- Uses format specifiers to determine how data is printed.
floatis automatically promoted todouble, so bothfloatanddoubleuse%f.
float f = 3.5;double d = 3.5;
printf("%f\n", f);printf("%f\n", d);scanf()
Section titled “scanf()”scanf() is used to read formatted input from the keyboard.
Syntax
scanf("format_string", &variable);Example
int age;float cgpa;
scanf("%d", &age);scanf("%f", &cgpa);Important Notes
- Pass the address of variables using
&. scanf()stores the user’s input in the given memory location.floatuses%f, whiledoubleuses%lf.
float f;double d;
scanf("%f", &f);scanf("%lf", &d);Why printf() uses x but scanf() uses &x
Section titled “Why printf() uses x but scanf() uses &x”
printf()requires the value
printf() only reads the value and prints it.
int x = 10;printf("%d", x);Here, x (value 10) is passed.
scanf()requires the address
scanf() needs to store the input into the variable, so it requires the variable’s memory address.
int x;scanf("%d", &x);Here, &x (address of x) is passed.
Difference between x and &x
Section titled “Difference between x and &x”x → Value stored in the variable&x → Memory address of the variableExample:
int x = 25;
x = 25&x = 0x61FF08 // Example addressWhy Arrays (Strings) Don’t Use &
Section titled “Why Arrays (Strings) Don’t Use &”For arrays, the array name itself represents the address of its first element.
char str[20];
scanf("%s", str);printf("%s", str);Here,
str == &str[0]Therefore,
scanf("%s", str); // Correctscanf("%s", &str); // IncorrectCommonly Used Format Specifiers
Section titled “Commonly Used Format Specifiers”%d // int%u // unsigned int%c // char%s // string%f // float (scanf), float/double (printf)%lf // double (scanf)%x // hexadecimal%o // octal%p // pointer (address)%% // prints %Summary
Section titled “Summary”printf()→ Displays output → Pass values.scanf()→ Reads input → Pass addresses using&.- Exception: Arrays (e.g., strings) do not use
&because the array name is already an address. - In
printf(), bothfloatanddoubleuse%f. - In
scanf(),floatuses%fanddoubleuses%lf. - Always use the correct format specifier to match the variable’s data type.