C Program to Print an Integer (Entered by the User)
Program to Print an Integer
#include <stdio.h>
int main()
{
int number;
// printf() dislpays the formatted output
printf("Enter an integer: ");
// scanf() reads the formatted input and stores them
scanf("%d", &number);
// printf() displays the formatted output
printf("You entered: %d", number);
return 0;
}
Output
Enter a integer: 25 You entered: 25
In this program, an integer variable number is declared.
The printf()
function displays Enter an integer: on the screen. Then, the scanf()
function reads an integer data from the user and stores in variable number.
Finally, the value stored in the variable number is displayed on the screen using printf()
function.
example C program that prompts the user to enter an integer and then prints that integer to the console
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
printf("You entered: %d", num);
return 0;
}
In this program, we start by declaring a variable called num
of type int
to store the integer value entered by the user.
We then use the printf()
function to display a message asking the user to enter an integer. We use the %d
format specifier to indicate that we expect an integer input.
Next, we use the scanf()
function to read an integer value from the user and store it in the num
variable. Note that we use the &
operator to pass the address of the num
variable to scanf()
so that it can update the value stored in that memory location.
Finally, we use another printf()
function call to display the integer value entered by the user. We use the %d
format specifier again to print the value of the num
variable.