C Input and Output Functions
Defination
In C programming, interacting with the user or the outside world often involves two major operations:
- Receiving values (Input)
- Displaying results (Output)
This is accomplished through a set of special utility functions that handle communication between the program and devices like the keyboard or screen.
Header Requirement
To make use of input and output capabilities, the following header must be attached at the beginning of your code:
#include <stdio.h>
This allows access to the standard I/O library functionalities.
OUTPUT FUNCTIONS
1. printf() — Data Presenter
This function pushes content from the program to the console window. You can print numbers, text, characters, or combinations of them.
Format:
printf("Message or format", values);Sample:
int age = 30;
Printf("My age is %d", age); - %d is a placeholder for integer values.
- You can also use %f, %c, %s for float, char, and strings respectively.
2. puts() — Text Line Writer
Primarily used to display plain text strings followed automatically by a newline.
Example:
puts("Hello, World!");3. putchar() — Character Display
Used to show a single character.
Example:
putchar('A');INPUT FUNCTIONS
1. scanf() — Value Receiver
Reads user-provided data and stores it into corresponding variables.
Format:
scanf("format specifier", &variable);Sample:
int age;
Scanf("%d", &age); - The ampersand (&) symbol gives the memory address to store the input.
2. gets() — Full Line Text Collector
Captures an entire line of input including spaces until a newline character is encountered. (Note: This function is deprecated due to safety risks.)
Example:
char name[50]; Gets(name);
3. getchar() — Single Key Capture
Captures a single keystroke directly from the standard input buffer without requiring the Enter key.
Example:
char ch; Ch = getchar();
Special Notes & Best Practices
- Always use fgets() instead of gets() for safe input handling.
- Avoid forgetting & in scanf() unless dealing with strings.
- printf() can be chained with multiple placeholders:
printf("Sum of %d and %d is %d", a, b, a + b);Summary Table
| Function | Direction | Type | Description |
|---|---|---|---|
| printf() | Output | Formatted | Shows values with format |
| puts() | Output | String | Prints string + newline |
| putchar() | Output | Character | Displays one character |
| scanf() | Input | Formatted | Reads formatted data |
| gets() | Input | String | Takes full line (unsafe) |
| getchar() | Input | Character | Accepts one character |
Real-Life Analogy
Think of:
- printf() as speaking out loud
- scanf() as listening to someone's response
- putchar() emits one character to the output stream, while getchar() retrieves a lone character from user input.
Prefer Learning by Watching?
Watch these YouTube tutorials to understand C Tutorial visually:
What You'll Learn:
- 📌 Input and Output: Printf and Scanf - C Programming Tutorial 06
- 📌 Input Output Functions in C | printf and scanf in C | Formatted and Unformatted IO Function in C