Contact

How to Print an Integer in C Programming

In the world of programming, printing integers, whether directly assigned or entered by the user, is one of the fundamental tasks every aspiring C programmer learns. This article or guide will walk you through the basics of displaying integers in C, covering both straightforward printing of predefined values and interactive input from the user.

By exploring various methods of printing integers, you'll gain a solid understanding of essential concepts such as variable declaration, input/output functions, and format specifiers.

Printing Integers Directly in C

To print an integer directly in C programming, you can use the printf() function along with the %d format specifier. Here is an example:

#include <stdio.h>

int main() {
    int number = 123;
    
    printf("The integer is: %d\n", number);

    return 0;
}

Let's break down how this C code works in step by step manner:

#include <stdio.h>

This line (above/previous code) includes the standard input/output library (stdio.h), which provides functions like printf() for displaying output on the screen.

int main() {
    // Code goes here
    return 0;
}

This defines the main() function, which is the entry point of the C program. Execution of the program starts from here.

int number = 123;
printf("The integer is: %d\n", number);

Finally, return 0; indicates successful termination of the main() function. In C, returning 0 from main() typically indicates that the program executed without errors.

The dry run of the program should be like:

Upon running the program, you will see the following output printed on the screen:

The integer is: 123

Printing Calculated Integer in C

C Code
#include <stdio.h>

int main() {
    int a = 10, b = 20;
    int sum = a + b;
    
    printf("The sum of %d and %d is: %d\n", a, b, sum);

    return 0;
}
Output
The sum of 10 and 20 is: 30

Printing an Integer Entered by User in C

To print an integer entered by the user, you can use the scanf() function to read the input and then display the entered integer using printf(). Here are a few approaches:

Let's write a simple C program that accomplishes this task:

C Code
#include <stdio.h>

int main() {
    int number; // Variable to hold the entered integer

    // Prompt the user to enter an integer
    printf("Enter an integer: ");

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

    // Display the entered integer
    printf("You entered: %d\n", number);

    return 0;
}
Output
Enter an integer: 46
You entered: 46

scanf("%d", &number); reads an integer value entered by the user from the standard input (keyboard) and stores it in the variable number. The %d format specifier is used to indicate that we are expecting an integer input. The & operator is used to pass the address of number to scanf() for storing the input value.

Printing Multiple Integers Entered by User in C

C Code
#include <stdio.h>

int main() {
    int num1, num2;
    
    printf("Enter two integers separated by space: ");
    scanf("%d %d", &num1, &num2);

    printf("You entered: %d and %d\n", num1, num2);

    return 0;
}
Output
Enter two integers separated by space: 10 20
You entered: 10 and 20

That's all for now! I hope you found this article helpful. If you enjoyed it, please consider sharing it with your friends and family. Thank you for reading!




Sharing is stylish! Gift your friends this awesome read!