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 the number is printed on screen using printf.

C program to print an integer

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int a;
  6.  
  7.   printf("Enter an integer\n");
  8.   scanf("%d", &a);
  9.  
  10.   printf("The integer is %d\n", a);
  11.  
  12.   return 0;
  13. }

Output of the program:
Print an integer C program output

C program to print first hundred integers [1, 100] using a for loop:

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int c;
  6.  
  7.   for (c = 1; c <= 100; c++)
  8.     printf("%d ", c);
  9.  
  10.   return 0;
  11. }

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

  1. #include <stdio.h>
  2.  
  3. int main ()
  4. {
  5.    char n[1000];
  6.    
  7.    printf("Input an integer\n");
  8.    scanf("%s", n);
  9.    
  10.    printf("%s", n);
  11.  
  12.    return 0;
  13. }

Output of program:

  1. Input an integer
  2. 12345678909876543210123456789
  3. 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.