Back to C Programming
2026-07-125 min read

Example: Multiple Integer Inputs in C

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

Title: Multiple Integer Inputs in C - A full guide with Worked Examples, Common Mistakes, and Practice Questions

Why This Matters

Taking user input is a fundamental aspect of programming. In this lesson, we will focus on how to take multiple integer inputs from the user in a C program. This skill is essential for various coding tasks, such as creating calculators, data collection programs, and more. By mastering this technique, you'll be able to create interactive applications that can respond to user input effectively.

Prerequisites

Before diving into taking multiple integer inputs, it's important that you have a good understanding of:

  • Basic C syntax and variables
  • The scanf() function for user input
  • Control structures like loops and conditional statements
  • Understanding the difference between value and address in memory (variables vs. pointers)

If you're not familiar with these concepts, we recommend reviewing our previous lessons on C basics before proceeding.

Core Concept

To take multiple integer inputs in C, we use the scanf() function repeatedly, each time reading one integer at a time. Here's a simple example:

#include <stdio.h>

int main() {
int num1, num2;

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

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

// Perform calculations or other operations with the input numbers

return 0;
}

In this example, we declare two integer variables num1 and num2. Then, we use the printf() function to prompt the user for their inputs. After each prompt, we use the scanf() function to read the user's input and store it in the respective variable using the address-of operator (&).

Understanding Variables and Memory Addresses

In C, every variable has a unique memory location where its value is stored. When we declare a variable, we create an identifier that represents its memory address. The scanf() function requires the actual memory address to store the input, so we use the & operator to provide this address.

int num; // Declare a variable called 'num' with no initial value
int *ptr = &num; // Create a pointer (ptr) that points to the memory location of 'num'

In the above example, we declare an integer variable num and create a pointer ptr that points to its memory address. Now, if we use scanf() with the pointer instead of the variable, it will store the input value at the correct memory location:

scanf("%d", ptr); // Correct usage of scanf() with a pointer

Worked Example

Let's create a program that takes three integers as input and calculates their sum:

#include <stdio.h>

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

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

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

printf("Enter third number: ");
scanf("%d", &num3);

sum = num1 + num2 + num3;

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

return 0;
}

In this example, we declare three integer variables num1, num2, num3, and a variable sum to store the result. We then take user input for each number and calculate their sum using simple arithmetic. Finally, we display the sum using the printf() function.

Common Mistakes

  • Forgetting semicolons: Semicolons are essential in C to separate statements. Forgetting a semicolon can lead to syntax errors.
printf("Enter first number: "); // Missing semicolon
scanf("%d", &num1);
  • Not using the address-of operator (&): The scanf() function requires a memory address to store the input, so it's crucial to use the & operator when declaring variables.
int num;
scanf("%d", num); // Incorrect: missing the address-of operator
  • Using the wrong format specifier: The format specifier for integers is %d. Using the incorrect format specifier can lead to unexpected behavior or errors.
int num;
scanf("%s", &num); // Incorrect: using %s instead of %d for an integer input
  • Not checking for invalid input: It's essential to validate user inputs to ensure they are within the expected range or meet other criteria. Failing to do so can lead to program crashes or unexpected behavior.

Common Mistakes - Handling Invalid Input

One common mistake is not handling invalid input properly. For example, if a user enters non-numeric characters when prompted for an integer, the program may crash or behave unexpectedly. To avoid this, we can use conditional statements to check if the input is valid before processing it:

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

if (num == -1) { // scanf() returns -1 when an error occurs
printf("Invalid input. Please enter a valid integer.\n");
exit(EXIT_FAILURE);
}

Practice Questions

  1. Write a program that takes three integers as input and calculates their average.
  2. Modify the previous example to handle negative numbers and calculate the absolute sum of the numbers.
  3. Create a program that takes an array of five integers as input and displays the largest number in the array.
  4. Write a program that takes two integers as input, compares them, and determines whether they are equal or not.
  5. Modify the initial example to prompt the user for the number of integers to be entered (up to 10). Then, take those numbers as input and calculate their sum.
  6. Write a program that takes an integer array as input and sorts it in ascending order using a bubble sort algorithm.
  7. Modify the initial example to validate user inputs and ensure they are within a specific range (e.g., between 1 and 100). If an invalid input is detected, prompt the user to enter a valid number.
  8. Write a program that takes two integers as input, calculates their greatest common divisor (GCD), and displays the result.
  9. Create a program that takes an integer array as input and finds the second-largest number in the array.
  10. Modify the initial example to handle floating-point inputs as well as integer inputs. Prompt the user for the type of input (integer or float) before taking the input.

FAQ

Q: Why do I need to use the address-of operator (&) with scanf()?

A: The scanf() function requires a memory address to store the user's input. Using the & operator provides that address, allowing scanf() to write the input value into the variable.

Q: What happens if I forget a semicolon in my C program?

A: Forgetting a semicolon can lead to syntax errors. The compiler will not be able to parse the code correctly and may produce error messages or cause your program to crash.

Q: How do I validate user inputs in C?

A: Validating user inputs is essential to ensure that your program behaves as expected. You can use conditional statements, loops, and custom functions to check if the input meets certain criteria (e.g., within a specific range).

Q: Can I take floating-point numbers as input using scanf()?

A: Yes, you can take floating-point numbers as input using scanf(). The format specifier for floating-point numbers is %f. For example:

float num;
scanf("%f", &num);