Back to C Programming
2026-07-125 min read

Example 5: Integer Input/Output (C Programming)

Learn Example 5: Integer Input/Output (C Programming) step by step with clear examples and exercises.

Title: Example 5: Integer Input/Output (C Programming)

Why This Matters

In C programming, understanding how to input and output integers is essential for creating interactive applications. This skill is crucial for various scenarios such as user input validation, data analysis, and even in competitive coding challenges. In this lesson, we'll delve into the printf() and scanf() functions that enable us to perform integer I/O in C.

Prerequisites

To fully grasp this lesson, you should have a good understanding of:

  1. Basic C syntax: variables, data types, operators, and control structures (if-else statements, for loops)
  2. Basics of functions: function definitions, function calls, and parameters
  3. Understanding the standard input/output streams: stdin and stdout

Core Concept

Introduction to printf() and scanf()

printf() is a function used in C for outputting formatted data to the console. It takes two arguments: the format string, which specifies how the data should be displayed, and the data itself.

On the other hand, scanf() is used for inputting formatted data from the user. It also requires a format string and pointers to variables that will store the input values.

Basic Usage of printf()

Here's an example of using printf() to output a simple message:

#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}

In this code, the printf() function is called with the string "Hello, World!" as its argument. The output will be:

Hello, World!

Basic Usage of scanf()

Now let's see how to use scanf() for inputting an integer from the user:

#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
return 0;
}

In this example, we first declare a variable num of type int. Then we use printf() to display a prompt asking the user for an integer. The scanf() function is called next, with "%d" as the format string and the address of num (&num) as its argument. This tells scanf() to read an integer from the user and store it in the variable num. Finally, we use another printf() call to display the entered integer.

Formatted Output with printf()

printf() can also be used for formatted output, allowing you to control how the data is displayed. For example:

#include <stdio.h>
int main() {
int num1 = 42;
float num2 = 3.14159;
printf("The integer is: %d\n", num1);
printf("The floating-point number is: %.6f\n", num2);
return 0;
}

In this code, we have two variables: num1 of type int and num2 of type float. In the first printf() call, "%d" tells printf() to display the integer value of num1. In the second call, we use ".6f" as a format specifier for floating-point numbers. This means that six digits after the decimal point will be displayed for num2. The output will be:

The integer is: 42
The floating-point number is: 3.141590

Formatted Input with scanf()

Similarly, you can use format specifiers with scanf() to specify the type of input expected:

#include <stdio.h>
int main() {
int num1;
float num2;
printf("Enter an integer: ");
scanf("%d", &num1);
printf("Enter a floating-point number: ");
scanf("%f", &num2);
printf("You entered:\nInteger: %d\nFloating-point: %.6f\n", num1, num2);
return 0;
}

In this example, we use "%d" for reading an integer and "%f" for reading a floating-point number. The output will be:

Enter an integer: 7
Enter a floating-point number: 2.71828
You entered:
Integer: 7
Floating-point: 2.718280

Worked Example

Let's write a program that asks the user for two integers, calculates their sum, and displays the result using formatted output:

#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter the first integer: ");
scanf("%d", &num1);
printf("Enter the second integer: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("The sum of %d and %d is: %d\n", num1, num2, sum);
return 0;
}

When you run this program, it will prompt the user to enter two integers. After entering the numbers, the program calculates their sum and displays the result using formatted output:

Enter the first integer: 5
Enter the second integer: 7
The sum of 5 and 7 is: 12

Common Mistakes

  1. Forgetting semicolons: Semicolons are required at the end of all declarations, statements, and function calls in C. For example:
int num; // Correct
printf "Hello, World!"; // Incorrect - missing semicolon
  1. Incorrect format specifiers: Using incorrect format specifiers can lead to unexpected results or even program crashes. For example:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: %f", &num); // Incorrect - "%f" is for floating-point numbers, not integers
return 0;
}
  1. Ignoring the address of operator: When using scanf(), you must pass the address of the variable where you want to store the input. For example:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", num); // Incorrect - missing & operator
return 0;
}
  1. Not checking for input errors: It's important to check if the input operation was successful, as scanf() may fail when the user enters invalid data. For example:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num == -1) { // scanf() returns -1 when it encounters an input error
printf("Invalid input.\n");
} else {
printf("You entered: %d\n", num);
}
return 0;
}

Practice Questions

  1. Write a program that asks the user for their name and age, then displays a personalized greeting using formatted output.
  2. Modify the worked example to calculate and display the difference between two integers entered by the user.
  3. Write a program that reads three integers from the user and finds the largest one using scanf() and appropriate format specifiers.
  4. Write a program that calculates and displays the product of two integers entered by the user, handling input errors gracefully.

FAQ

  1. Why do I need to pass the address of a variable when using scanf()?

When you use scanf(), it needs to store the input data in a variable. To do this, it requires the memory address (pointer) of the variable. By passing the address of the variable using the & operator, you provide scanf() with the necessary information.

  1. What happens if I forget the semicolon at the end of a statement in C?

If you forget the semicolon at the end of a declaration or statement in C, the compiler will generate an error. The semicolon signals the end of the statement and is required for proper parsing of your code.

  1. Why do I need to check for input errors when using scanf()?

Checking for input errors is essential because scanf() may fail if the user enters invalid data, such as non-numeric values or characters instead of numbers. By checking for errors, you can handle these situations gracefully and avoid program crashes.

  1. Can I use printf() to read input from the user?

No, printf() is used only for outputting data. To read input from the user, you should use scanf(). If you need to read a line of text (including spaces and newlines), consider using fgets() instead.