Back to C Programming
2026-07-125 min read

Integer Input (C Programming)

Learn Integer Input (C Programming) step by step with clear examples and exercises.

Why This Matters

Learning how to take integer input in C is essential for writing interactive programs. This skill is crucial for exams, interviews, and real-world programming tasks where user input plays a significant role. In this lesson, we'll look closely at understanding the concept of taking integer input using the scanf() function in C.

Importance of User Input

User input allows programs to interact with users, making them more dynamic and responsive. By learning how to take user input, you can create applications that cater to specific needs or preferences, enhancing their usability and functionality.

Prerequisites

To fully understand this lesson, you should be familiar with:

  1. Basic C syntax and structure
  2. Variables and data types
  3. Operators and expressions
  4. Control structures (if-else statements)
  5. Functions (including the main() function)
  6. Understanding of arrays (optional but beneficial for more complex input scenarios)
  7. Knowledge of pointers (essential for understanding how scanf() works)

Core Concept

In C, we use the scanf() function to take input from the user. The scanf() function reads formatted data from the standard input (keyboard). To read an integer, we use the format specifier %d. Here's a simple example:

#include <stdio.h>

int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num); // Read an integer and store it in the variable 'num'
printf("You entered %d\n", num);
return 0;
}

In this example, we first include the standard input/output library stdio.h. Then, we define a variable num of type int. We use printf() to display a prompt asking the user for an integer input. The scanf("%d", &num) line reads an integer from the keyboard and stores it in the memory location pointed to by &num, effectively assigning the value to the variable num. Finally, we print the entered integer using another printf() statement.

Reading Multiple Inputs

To read multiple inputs, you can use multiple scanf() statements or a single scanf() with multiple format specifiers (separated by spaces). For example:

#include <stdio.h>

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

In this example, we read two integers and find their sum.

Worked Example

Let's work on a simple example where we read two integers and find their product:

#include <stdio.h>

int main() {
int num1, num2;
printf("Enter the first integer: ");
scanf("%d", &num1);
printf("Enter the second integer: ");
scanf("%d", &num2);
printf("The product of %d and %d is %d\n", num1, num2, num1 * num2);
return 0;
}

Save this code in a file named integer_input.c. Compile it using the command gcc integer_input.c -o integer_input and run it with ./integer_input. You should see a prompt asking for two integers, and after entering them, you'll get the product of those numbers.

Common Mistakes

  1. Forgetting the ampersand (&): The ampersand is used to get the address of a variable when reading input with scanf(). If you forget it, your program will not work as expected.
  2. Not checking for errors: It's essential to check if the user entered the correct data type or if there was an error while reading the input. We can do this using the return value of scanf().
  3. Buffering issues: Sometimes, when you enter a newline character (pressing Enter), it might get stuck in the buffer and cause unexpected behavior. To avoid this, you can use functions like fflush(stdin) or simply read an extra newline character using getchar().
  4. Incorrect format specifier: If you use the wrong format specifier (like %i instead of %d) for your input variable, the program might crash or behave unexpectedly.
  5. Not checking the return value of scanf(): The scanf() function returns the number of successful conversions. If there is an error in reading input, it will return 0. You can use this information to handle errors gracefully.
  6. Using incorrect variable types: Using the wrong data type for a variable can lead to unexpected results or program crashes. Make sure you're using the correct data type (int, float, etc.) for each variable.
  7. Not clearing the buffer: As mentioned earlier, buffering issues can cause problems when reading input. To avoid this, you can read an extra newline character after taking user input or use functions like fflush(stdin).

Practice Questions

  1. Write a program that asks for the user's age and checks if they are eligible to vote (18+ in most countries).
  2. Write a program that takes two integers as input, finds their greatest common divisor (GCD), and prints it.
  3. Write a program that takes three integers as input and finds their average.
  4. Write a program that reads an array of integers and calculates the sum of all elements in the array.
  5. Write a program that sorts an array of integers using bubble sort algorithm.

FAQ

  1. Why do we need the ampersand (&) when using scanf()?

The ampersand is used to get the address of a variable so that scanf() can store the user's input in the correct memory location.

  1. What happens if I forget to include stdio.h?

If you don't include stdio.h, the compiler won't be able to find the declarations for functions like printf() and scanf(). This will cause your program to fail to compile.

  1. What is buffering, and how does it affect input?

Buffering refers to temporarily storing data in a buffer (memory) before processing it. In the context of C input, if you press Enter after entering an integer, the newline character might get stuck in the buffer and cause unexpected behavior when reading the next input.

  1. What is the return value of scanf()?

The scanf() function returns the number of successful conversions. If there's an error in reading input, it will return 0. You can use this information to handle errors gracefully.