Jan 22, 2026
## Chapter 4: Input and Output
**What is input?**
Input means the information that the user gives to the program.
For example:
* Entering age
* Entering height
* Writing a letter
In C language, we use **scanf()** to take input.
**What is output?**
Output means the result that the program displays on the screen.
For example:
* "Your age is 20"
* Printing height and letter
In C, printf() is used to display output.
```c
#include
int main() {
int ageValue;
float heightValue;
char nameChar;
printf("Enter your age: ");
scanf("%d", &ageValue);
printf("Enter your height (in meters): ");
scanf("%f", &heightValue);
printf("Enter your first initial: ");
scanf(" %c", &nameChar); // Note the space before %c
printf("nYour information:n");
printf("Age: %d yearsn", ageValue);
printf("Height: %.2f metersn", heightValue);
printf("First initial: %cn", nameChar);
return 0;
}
```
Important points about scanf():
1. Why is the & (ampersand) used?
When we take values using scanf(), we need to provide the address of the variable.
Therefore, & is used before the variable (this is covered in detail in the pointer topic).
```c
scanf("%d", &age);
```
2. Why is there a space before %c?
To avoid the keyboard's Enter (newline) character.
If a space is not given, incorrect input may result.
**Correct way:**
```c
scanf(" %c", &ch);
```
3. The format specifier must match the variable type.
* %d -> int
* %f -> float
* %c -> char
* %s -> string
If they don't match -> the output may be incorrect.
Reading strings
**What is a string?**
A group of characters = string
For example: "Rahul", "Anita Sharma"
In C, strings are stored in a character array.
**Why not use gets()?**
gets() is unsafe.
fgets() is safer.
Therefore, fgets() is recommended.
```c
#include
int main() {
char fullName[40]; // Will store the user's full name
printf("Enter your full name: ");
fgets(fullName, 40, stdin); // Safe way to read a string
printf("Hello, %s", fullName);
return 0;
}
```
---