In this tutorial, you will learn how to write a C program to print an integer entered by user. This is a very simple program. You just need to capture the user input using scanf
, and store it in an int variable, then you can use printf
to print the value of the variable.
C Program to print an integer
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
// reads the user input stores in 'number'
scanf("%d", &number);
// print the 'number'
printf("Integer entered by user: %d", number);
return 0;
}
Output:
Explanation of the above program:
This is how a variable is declared, since we want to store an integer number so we have declared the variable as int
. The variable name can be anything, however it is good to choose a meaningful simple name:
int number;
This line displays a message to the user to enter a number:
printf("Enter an integer: ");
Entered number is scanned and stored in variable number
:
scanf("%d", &number);
Finally the value of the variable number
is displayed:
printf("Integer entered by user: %d", number);
Leave a Reply