C Program To Print A User-Entered Integer

This tutorial will teach you how to write a C program to print an integer entered by the user. This is a very straightforward program. Simply use scanf to capture user input and store it in an int variable, then use printf to print the variable’s value.

An integer is printed using a C program

#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;

Explanation of the preceding program:
This is how a variable is declared; because we want to store an integer number, we have named the variable int. The variable name can be anything, but it is preferable to use a meaningful simple name:

int number;

This line requests that the user enter a number:

printf("Enter an integer: ");

The number entered is scanned and saved in variable number:

scanf("%d", &number);

Finally, the variable number’s value is displayed:

printf("Integer entered by user: %d", number);

Related Articles

Leave a Reply

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

Back to top button