🚀Beginner

Print an Integer Entered By User

Read and display an integer

Printing an Integer in C is one of the most basic tasks. Input and output of elements are essential for taking input from the user and giving output back. Here we learn to take an integer as input using scanf() and print it using printf().

📚 What You'll Learn

  • Declaring integer variables using int
  • Taking integer input with scanf()
  • Using the %d format specifier
  • Understanding the address-of operator &

1. Printing Integer Values in C

First, let's understand how to print an integer value that is already stored in a variable.

Approach:

  1. Store the integer value in a variable of type int
  2. Print this value using the printf() function

📝 Syntax:

printf("%d", variableOfIntType);
print_integer.c
C
1// C Program to Print Integer value
2#include <stdio.h>
3
4// Driver code
5int main()
6{
7 // Declaring integer
8 int x = 5;
9
10 // Printing values
11 printf("Printing Integer value %d", x);
12 return 0;
13}
Output

Printing Integer value 5

2. Reading Integer Values in C

Now let's learn how to take an integer value as input from the user.

Approach:

  1. Declare a variable to store the integer
  2. Prompt the user to enter a value
  3. Use scanf() to read the input
  4. Display the entered value using printf()

📝 Syntax:

scanf("%d", &variableOfIntType);
read_integer.c
C
1// C program to take an integer
2// as input and print it
3#include <stdio.h>
4
5// Driver code
6int main()
7{
8 // Declare the variables
9 int num;
10
11 // Input the integer
12 printf("Enter the integer: ");
13 scanf("%d", &num);
14
15 // Display the integer
16 printf("Entered integer is: %d", num);
17
18 return 0;
19}
$ ./read_integer

Enter the integer: 10\nEntered integer is: 10

📖 Code Explanation

CodeExplanation
#include <stdio.h>Include standard I/O library for printf and scanf
int num;Declare an integer variable named num
printf("Enter...");Display a prompt message to the user
scanf("%d", &num);Read an integer from keyboard and store in num
%dFormat specifier for integer (decimal) values

⚠️ Why is & Required in scanf()?

The & (address-of) operator is required because scanf() needs to know where in memory to store the value. It passes the memory address of the variable, not the variable itself.

❌ scanf("%d", num); → Wrong! Passes value (garbage)
✓ scanf("%d", &num); → Correct! Passes address

🔄 Step-by-Step Execution

1
Program starts

Execution begins at main()

2
Variable declared

int num; allocates 4 bytes of memory

3
Prompt displayed

printf() shows "Enter the integer: "

4
User enters value

scanf() waits for input, user types 10 and presses Enter

5
Value stored

The value 10 is stored at the address of num

6
Output displayed

printf() shows "Entered integer is: 10"

Common Mistakes to Avoid

  • ❌ Forgetting the & before variable in scanf()
  • ❌ Using wrong format specifier (e.g., %f for int)
  • ❌ Not including <stdio.h> header
  • ❌ Using uninitialized variables before scanf()

📋 Common Format Specifiers

SpecifierData TypeExample
%dint (signed decimal)-42, 0, 100
%uunsigned int0, 100, 65535
%ldlong int-2147483648
%ffloat3.14, -0.5
%cchar'A', 'z', '5'

🎯 Key Takeaways

Use %d for integer I/O
& is required in scanf()
printf() displays output
scanf() reads input

Want to Learn More?

Explore our comprehensive tutorials for in-depth explanations of C programming concepts.

Browse Tutorials
Back to All Examples