C program to print an integer value

In this program scenario, we will see how to take an integer as input from user and print an integer value to console screen.

Pre-requisites:

Input output functions, data types, variables

Approach:

An integer data can be considered as any whole number. Integer values are present with no fractional or floating point values.

Here understand that user will provide this integer data and then we have to echo/print this value on screen.

  • First take user input and store it inside variable called IntNum

  • Read the input into IntNum using scanf() function and specify %d format specifier so compiler interprets it as integer data

  • Print the value on screen using printf() function

Program:

Output:

Program explanation:

In this program, we have declared a variable called IntNum. Here we intend to store the input entered by user and this gets allocated memory based on size of integer data i.e. 4 bytes

Next we are displaying a message to the user that tells the user to provide the input. Printf() function is used here that prints formatted message and here we have placed the message inside double quotes inside printf() and printf() echoes the message to screen.

Scanf() function is used in c programming to read the input data/formatted input into variable prefixed with &. %d tells compiler that data to be stored is of integer format.

Again we have used printf() function but this time we are displaying message as well as displaying the value stored inside IntNum.

We just need to specify variable name after message in double quotes and put it separated by a comma. In the end return 0 statement is added because the main method has return type int and we must return a value to function so that return 0 is added. It indicates status as the successful execution of the program.

Leave a Reply

%d bloggers like this: