Print an Integer Value in C
Last Updated :
26 May, 2023
Printing an Integer in C is one of the most basic tasks. Input and output of the elements are essential for taking input from the user and then giving the output to the user. Here we are going to take an integer as input from the user with the help of the scanf() function and print that integer with the help of the printf() function in C language.
How to Read and Print an Integer Value in C
1. Printing Integer values in C
Approach:
- Store the integer value in the variableOfIntType x.
- Print this value using the printf() method. The printf() method, in C, prints the value passed as the parameter to it, on the console screen.
Syntax:
printf("%d", variableOfIntType);
Below is the C program to print the integer value:
C
#include <stdio.h>
int main()
{
int x = 5;
printf ( "Printing Integer value %d" , x);
return 0;
}
|
Output
Printing Integer value 5
2. Reading Integer values in C
Approach:
- The user enters an integer value when asked.
- This value is taken from the user with the help of the scanf() method. The scanf() method, in C, reads the value from the console as per the type specified.
- For an integer value, the X is replaced with the type int. The syntax of the scanf() method becomes as follows then:
Syntax:
scanf("%d", &variableOfIntType);
Below is the C program to read integer values in C:
C
#include <stdio.h>
int main()
{
int num;
printf ( "Enter the integer: " );
scanf ( "%d" , &num);
printf ( "Entered integer is: %d" , num);
return 0;
}
|
Output:
Enter the integer: 10
Entered integer is: 10
Share your thoughts in the comments
Please Login to comment...