You are here

Print an integer in C

Print an integer in C language: a user will input an integer, and it will be printed. Input is done using scanf function and number is printed on the screen using printf.

C program to print an integer

#include <stdio.h>
 
int main()
{
  int a;
 
  printf("Enter an integer\n");
  scanf("%d", &a);
 
  printf("The integer is %d\n", a);
 
  return 0;
}

Output of the program:
Print an integer C program output

In C language we have data types for different types of data, for integers, it is int, for characters it is char, for floating point data it's float and so on. For large integers, you can use long or long long data type. To store integers which are larger than (2^18-1) which is the range of long long data type you may use strings. In the below program we store an integer in a string and then display it.

C program to store an integer in a string

#include <stdio.h>
 
int main () 
{
   char n[1000];
 
   printf("Input an integer\n");
   scanf("%s", n);
 
   printf("%s", n);
 
   return 0;
}

Output of program:

Input an integer
12345678909876543210123456789
12345678909876543210123456789

An advantage of using a string is that we can store very big integers but arithmetic operations can't be performed directly, for this you can create functions. C programming language does not have a built-in data type to handle such numbers.