Back to C Programming
2026-07-126 min read

Example: Integer Input in C

Learn Example: Integer Input in C step by step with clear examples and exercises.

Title: Mastering Integer Input in C: A full guide for Practical Depth

Why This Matters

In this tutorial, we delve into understanding how to take integer input from the user in C programming. This skill is crucial for creating interactive programs and solving real-world problems. It's essential for interview preparation, as well as fixing common bugs in your code when working on projects or participating in coding competitions.

Prerequisites

Before diving into integer input, you should have a solid grasp of the following topics:

  1. C Basics: Variables, data types, operators, and control structures
  2. Input/Output (I/O) Streams: Standard I/O streams stdin, stdout, and stderr
  3. Basic File I/O: Understanding how to read and write files in C
  4. Scanning and Printing Functions: scanf(), printf(), and their format specifiers
  5. Understanding of pointers and the address-of operator (&)
  6. Basic understanding of conditional statements and loops

Core Concept

In C, we use the scanf() function to take input from the user. This function reads formatted data from the standard input stream (stdin) and stores it in variables. For integer input, we use the %d format specifier.

Here's a simple example of how to take an integer input using scanf():

#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 include the stdio.h header to access the standard I/O functions. We then declare an integer variable num. The printf() function is used to display a prompt for the user to enter an integer. The scanf() function reads an integer from the user and stores it in the memory location pointed to by the address-of operator (&). Finally, we use another printf() statement to display the entered integer.

Understanding Pointers and the Address-Of Operator (&)

When using scanf(), it's important to understand pointers and the address-of operator (&). The scanf() function expects pointers to memory locations where it should store the input data. By using the address-of operator, we provide these pointers. For example, in our simple example above:

int num;
// ...
scanf("%d", &num);

The & operator gives us the memory address of the variable num, which scanf() uses to store the input integer.

Worked Example

Let's walk through a more complex example that demonstrates how to take multiple integer inputs and perform calculations using them:

#include <stdio.h>

int main() {
int num1, num2, sum;

printf("Enter first integer: ");
scanf("%d", &num1);

printf("Enter second integer: ");
scanf("%d", &num2);

sum = num1 + num2;

printf("The sum of the two numbers is: %d\n", sum);
return 0;
}

In this example, we take two integer inputs and store them in variables num1 and num2. We then perform an addition operation using these variables and store the result in a new variable called sum. Finally, we display the result using another printf() statement.

Common Mistakes

  1. Forgetting to include the header file (stdio.h) in your C program: If you forget to include stdio.h, your program will not be able to access the standard I/O functions like printf() and scanf(). This will result in compilation errors.
  2. Not using the address-of operator (&) with the variables when using scanf(): If you forget to use the address-of operator, scanf() won't know where to store the input data, resulting in undefined behavior or incorrect results.
  3. Incorrect format specifier: Make sure to use the correct format specifier for your data type. For example, use %d for integer input and %s for string input.
  4. Not checking for errors: If the user enters invalid data, such as a non-integer value or too many inputs, you may encounter issues in your program. To handle these cases, use the fscanf() function with a check on the number of items read.

Handling Invalid Input

To handle invalid input, you can modify your code to check if the input is valid before processing it. Here's an example:

#include <stdio.h>

int main() {
int num;
int valid_input = 0;

while (!valid_input) {
printf("Enter an integer: ");
if (scanf("%d", &num) == 1) {
valid_input = 1;
} else {
printf("Invalid input. Please try again.\n");
}
}

// Process the valid input here...

return 0;
}

In this example, we use a while loop to keep asking for user input until a valid integer is entered. The scanf() function returns the number of items it successfully read, so we check if this value is 1 (indicating one integer was read) before considering the input as valid.

Practice Questions

  1. Write a program that takes three integers from the user and calculates their average.
  2. Modify the example above to handle invalid input (e.g., non-integer values or too many inputs).
  3. Write a program that takes an integer n and prints the sum of all numbers from 1 to n.
  4. Write a program that finds the largest of three integers entered by the user.
  5. Write a program that asks the user for two integers, a and b, and determines whether a is greater than, equal to, or less than b. Display the result using appropriate conditional statements.
  6. Write a program that takes an integer n and prints all prime numbers up to and including n.
  7. Write a program that asks the user for two integers, a and b, and finds their greatest common divisor (GCD). Use Euclid's algorithm to calculate the GCD.
  8. Write a program that takes an integer n and prints all Fibonacci numbers up to and including n.
  9. Write a program that asks the user for two integers, a and b, and determines whether a and b are coprime (i.e., their greatest common divisor is 1).
  10. Write a program that takes an integer n and prints all perfect squares up to and including n.

FAQ

  1. Why do I need to use the address-of operator (&) with variables when using scanf()?

The scanf() function expects pointers to memory locations where it should store the input data. By using the address-of operator, we provide these pointers. For example, in our simple example above:

int num;
// ...
scanf("%d", &num);

The & operator gives us the memory address of the variable num, which scanf() uses to store the input integer.

  1. What should I do if the user enters invalid data when using scanf()?

To handle invalid input, you can modify your code to check if the input is valid before processing it. Here's an example:

#include <stdio.h>

int main() {
int num;
int valid_input = 0;

while (!valid_input) {
printf("Enter an integer: ");
if (scanf("%d", &num) == 1) {
valid_input = 1;
} else {
printf("Invalid input. Please try again.\n");
}
}

// Process the valid input here...

return 0;
}

In this example, we use a while loop to keep asking for user input until a valid integer is entered. The scanf() function returns the number of items it successfully read, so we check if this value is 1 (indicating one integer was read) before considering the input as valid.

  1. What are some common mistakes when using scanf() in C?

Some common mistakes include:

  • Forgetting to include the header file (stdio.h) in your C program.
  • Not using the address-of operator (&) with the variables when using scanf().
  • Incorrect format specifier: Make sure to use the correct format specifier for your data type. For example, use %d for integer input and %s for string input.
  • Not checking for errors: If the user enters invalid data, such as a non-integer value or too many inputs, you may encounter issues in your program. To handle these cases, use the fscanf() function with a check on the number of items read.