Print an int (integer) in C

Created with Sketch.

 

Print an int (integer) in C

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

C priogram to print an int (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

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

#include <stdio.h>

int main()
{
int c;

for (c = 1; c <= 100; c++)
printf(“%d “, c);

return 0;
}

In C language, we have data types for different types of data, for integers, it’s int, for characters it’s 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 that are greater 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 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 huge integers, but we can’t perform arithmetic operations directly; for this, you can create functions. C programming language does not have a built-in data type for such numbers.

Leave a Reply

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