Input and Output in C

Subject: Data Structures and Algorithms · Language: C · Level: beginner · 4 min read

Use printf, scanf, format specifiers, and safe input patterns for beginner C programs.

A program becomes useful when it can communicate with the user. In C, output means showing something on the screen, and input means taking a value from the keyboard.

For beginner C programs, the two most important functions are `printf` and `scanf`. Both come from the standard input-output header file `stdio.h`.

What Is Output?

Output is the information displayed by a program. In C, we commonly use `printf` to print text, numbers, characters, and results.

For example, `printf("Hello");` prints Hello on the screen.

What Is Input?

Input is the data entered by the user while the program is running. In C, we commonly use `scanf` to read formatted input from the keyboard and store it inside variables.

For example, `scanf("%d", &age);` reads an integer and stores it inside the variable `age`.

Why stdio.h Is Needed

`printf` and `scanf` are not keywords. They are library functions. To use them, we include the header file at the top of the program.

```c

#include <stdio.h>

```

printf Function

`printf` is used to display output. It can print plain text and variable values.

```c

printf("Welcome to C Programming");

printf("Age = %d", age);

```

In the second line, `%d` is a placeholder. C replaces it with the value of `age`.

Formatting Output

Formatted output makes results clean and readable.

```c

printf("Marks = %.2f", marks);

```

`%.2f` prints a decimal number with two digits after the decimal point. This is useful for percentages, averages, prices, and salaries.

scanf Function

`scanf` is used to read input from the user.

```c

scanf("%d", &age);

```

Here `%d` tells C to read an integer, and `&age` tells C where to store that integer in memory.

Why We Use & in scanf

The `&` symbol gives the address of a variable. `scanf` needs the address because it must place the user input directly inside that variable.

For normal variables like `int`, `float`, `double`, and `char`, use `&` with `scanf`. For strings stored in character arrays, you usually do not use `&` with the array name.

Format Specifiers

Format specifiers tell C what type of data is being printed or scanned.

Multiple Inputs

C can read more than one value using one `scanf` statement.

```c

scanf("%d %f", &rollNo, &marks);

```

The input values should match the order of the format specifiers.

Return Value of scanf

`scanf` returns the number of values it successfully reads. This can be used to check whether input was correct.

```c

if (scanf("%d", &age) == 1) {

printf("Valid input");

}

```

This idea becomes useful when writing safer programs.

```c

#include <stdio.h>

int main(void) {

int a, b;

printf("Enter two numbers: ");

scanf("%d %d", &a, &b);

printf("Sum = %d\n", a + b);

return 0;

}

```

Newline Character

`\n` is used to move the cursor to the next line. Without `\n`, the next output may continue on the same line.

```c

printf("Hello\n");

printf("PrepCampus");

```

Reading a Character

When reading a character after numbers, beginners sometimes face problems because newline characters can remain in the input buffer. A common simple fix is to put a space before `%c`.

```c

char grade;

scanf(" %c", &grade);

```

The space before `%c` skips leftover whitespace.

Good Practice

Use clear messages before taking input, choose the correct format specifier, and keep your output easy to read. A program is not only for the computer; it should also be understandable for the person using it.

Input Validation Idea

Beginner programs often assume the user enters correct input. Later, you should learn to check whether input is valid before using it. For example, marks should not be negative, and age should usually be positive.

Prompt and Result Style

A good beginner program should tell the user what to enter and should print the result with a clear label.

Instead of only printing `30`, print `Sum = 30`. This habit makes programs easier to test and understand.

Quick Practice

Write a program that takes a student's roll number, marks, and grade as input, then prints them in a clean report format.

More C Lessons

Browse all PrepCampus study materials