printf() and scanf()
These two functions are used in many beginner C programs. printf() is mainly used to display output, while scanf() is commonly used to read formatted input.
printf() — display output
| int age = 20; printf(“Age = %d”, age); |
Important format specifiers
| Specifier | Meaning | Example |
| %d | integer | printf(“%d”, age); |
| %f | floating-point value | printf(“%.2f”, price); |
| %c | single character | printf(“%c”, grade); |
| %s | string | printf(“%s”, name); |
| %lf | double with scanf() | scanf(“%lf”, &value); |
scanf() — take input
| int age; printf(“Enter age: “); scanf(“%d”, &age); |
The & before a normal variable gives its address. scanf() uses that address to store the input value.
Complete program
| #include <stdio.h> int main() { printf(“Enter age: “); printf(“Enter marks: “); printf(“Enter grade: “); printf(“\nAge = %d”, age); return 0; |
| OUTPUT Enter age: 21 Enter marks: 87.5 Enter grade: A Age = 21 |
| ⭐ EASY TIP: Notice the space before %c in scanf(” %c”, &grade). It helps skip whitespace left in the input stream. |