C Programming to Print an Integer Entered by the User | C Programming Guide

C Programming to Print an Integer Entered by the User | C Programming Guide

C programming code that allows the user to input an integer and prints it back. This example is fundamental for beginners in C programming, helping them understand basic input and output operations.

C Program Code

C
#include <stdio.h>
int main() {
int number;
// Ask the user to enter an integer as input
printf("Enter an integer: ");

// get input from the user
scanf("%d", &number);

//Print the user-entered number
printf("You entered: %d\n", number);

return 0;
}

Article Explanation:

This C programming code shows how to interact with users through input and output commands, using core functions like scanf() and printf().

Including Necessary Header Files in C Programming

In C programming, we need to include the stdio.h header file:

C
#include <stdio.h>

This header file is essential, it provides the C programming functions for input and output, like printf() and scanf().

Declaring Variables in C Programming

Next, we declare an integer variable named number:

C
int number;

the integer entered by the user will stored in the variable when the program is executed.

Prompting for Input in C Programming

then the program will ask the user to enter an integer using the printf() function:

C
printf("Enter an integer: ");

The printf() function is used for output operations in C programming, thet will allow the program to communicate with the user by displaying messages on the screen or monitor.

Reading User Input Using scanf() in C Programming

Once the user enters a value, we capture that input using scanf():

C
scanf("%d", &number);

In C programming, scanf() is used for reading input. The %d format specifier tells the program that we are expecting an integer input, and &number stores the entered value in the number variable.

Displaying the Entered Integer in C Programming

Finally, we print the entered integer using the printf() function:

C
printf("You entered: %d\n", number);

In C programming, format specifiers like %d are essential to display the correct type of data, in this case, an integer.

Conclusion of the C Programming Example

The program ends with the return 0;

C
return 0;

In C programming, returning 0 signals that the program has executed successfully.

when i enter 5 as input then the integer will be stored in the variable number and it will be printed as output.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top