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
%dformat 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:
- Store the integer value in a variable of type
int - Print this value using the
printf()function
📝 Syntax:
printf("%d", variableOfIntType);1// C Program to Print Integer value2#include <stdio.h>34// Driver code5int main()6{7 // Declaring integer8 int x = 5;910 // Printing values11 printf("Printing Integer value %d", x);12 return 0;13}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:
- Declare a variable to store the integer
- Prompt the user to enter a value
- Use
scanf()to read the input - Display the entered value using
printf()
📝 Syntax:
scanf("%d", &variableOfIntType);1// C program to take an integer2// as input and print it3#include <stdio.h>45// Driver code6int main()7{8 // Declare the variables9 int num;1011 // Input the integer12 printf("Enter the integer: ");13 scanf("%d", &num);1415 // Display the integer16 printf("Entered integer is: %d", num);1718 return 0;19}Enter the integer: 10\nEntered integer is: 10
📖 Code Explanation
| Code | Explanation |
|---|---|
| #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 |
| %d | Format 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
Execution begins at main()
int num; allocates 4 bytes of memory
printf() shows "Enter the integer: "
scanf() waits for input, user types 10 and presses Enter
The value 10 is stored at the address of num
printf() shows "Entered integer is: 10"
Common Mistakes to Avoid
- ❌ Forgetting the
&before variable in scanf() - ❌ Using wrong format specifier (e.g.,
%ffor int) - ❌ Not including
<stdio.h>header - ❌ Using uninitialized variables before scanf()
📋 Common Format Specifiers
| Specifier | Data Type | Example |
|---|---|---|
| %d | int (signed decimal) | -42, 0, 100 |
| %u | unsigned int | 0, 100, 65535 |
| %ld | long int | -2147483648 |
| %f | float | 3.14, -0.5 |
| %c | char | 'A', 'z', '5' |
🎯 Key Takeaways
%d for integer I/O& is required in scanf()Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials