Input and Output in C
Input and Output (I/O) in C are used to receive data from the user and display results on the screen. C performs input and output using standard library functions provided in the stdio.h header file.
Input in C
Input means taking data from the user through the keyboard. C uses functions that read data from the standard input stream (stdin).
Common Input Function: scanf()
scanf("%d", &age);- Reads input from the keyboard
%dspecifies the type of input (integer)&agegives the memory address where input is stored
Example:
int age;
scanf("%d", &age);Output in C
Output means displaying data on the screen. C sends output to the standard output stream (stdout).
Common Output Function: printf()
printf("Age is %d", age);- Displays formatted output
- Uses format specifiers to represent data types
- Does not require the address operator
Complete Input–Output Program Example
c
1#include <stdio.h>
2
3int main() {
4 int number;
5 printf("Enter a number: ");
6 scanf("%d", &number);
7 printf("You entered: %d", number);
8 return 0;
9}Format Specifiers
Format specifiers define the type of data being input or output.
| Specifier | Meaning |
%d | Integer |
%f | Float |
%c | Character |
%s | String |
%lf | Double |
Character Input and Output
C also provides functions for single-character I/O.
char ch;
ch = getchar();
putchar(ch);getchar()reads one character from keyboardputchar()prints one character to screen
Standard Streams in C
C uses three standard streams:
| Stream | Purpose |
stdin | Input (keyboard) |
stdout | Output (screen) |
stderr | Error messages |
Functions like scanf() and printf() operate on these streams.
Key Differences Between printf() and scanf()
| Feature | printf | scanf |
| Operation | Output | Input |
Uses & operator | No | Yes |
| Direction | Program → Screen | Keyboard → Program |
Important Points
- Input functions store data into memory
- Output functions read data from memory and display it
- Incorrect format specifiers cause runtime errors
- Proper use of
&is essential in input operations
One-Line Exam Answer
Input functions accept data from the user, and output functions display data to the user in a C program.
