How to Print an Integer, long, double in C Program?

In this C Programming example, we will implement the program to get the integer, long, or double as user input and print it on screen. We will also discuss different code sections and the meaning of each line of the code.

1. Print Integer in C

Helpful topics to understand this program better are

Let’s implement the program to get an integer as user input and also print it on screen. We will also implement some mathematical operations on the user input integer.

#include <stdio.h>

void user_input_integer()
{
    int number;

    printf("Enter an integer: ");

    // reads and stores input
    scanf("%d", &number);

    // displays output
    printf("You entered: %d", number);
    printf("\nSquare of entered integer: %d", number * number);
}

int main()
{
    printf("Testing for integer...");
    user_input_integer();
    return 0;
}
Output
Testing for integer...
Enter an integer: 100
You entered: 100
Square of entered integer: 10000

2. Understanding the program’s building blocks

Let’s discuss various sections of the program in the previous section

  • int number – declaration of a variable of type integer to hold the value of user input.
  • scanf("%d", &number); – Reads and stores the value of the input value as an integer. %d is the format specifier used here which compiles only if the variable is of type integer.
  • Printing the output to the console using %d which again notifies that the output is an integer.

3. Reading long and double as user input

Similar to reading integer value we can read other numbers like long and double.

To achieve this we just have to take care of the following points.

  • Use correct data type for the variable like long or double
  • Use proper format specifier i.e.
    • %ld for long
    • %lf for double

3.1 Print a long variable input by User

Let’s update the method implemented above and now accept the long as the user input and print it. For a long variable, we will use %ld as the format specifier.

void user_input_long()
{
    long number;

    printf("Enter a long: ");
    scanf("%ld", &number);

    printf("You entered: %ld", number);
    printf("\nSquare of entered long: %ld", number * number);
}
Output
Testing for long...
Enter a long: 1219201902
You entered: 1219201902
Square of entered long: 1486453277840417604

3.2 Print double in C input by User

Let’s update the method implemented above and now accept the double as the user input and print double in C. For a double variable, we will use %lf as the format specifier.

void user_input_double()
{
    double number;

    printf("Enter a double: ");
    scanf("%lf", &number);

    printf("\nYou entered: %lf", number);
    printf("\nSquare of entered double: %lf", number * number);
}
Output
Testing for double...
Enter a double: 100.123
You entered: 100.123000
Square of entered double: 10024.615129% 

4. Conclusion

In this C Programming example, we have discussed how to get an integer, long, or a double as the user input, print it to the console, and perform a very basic operation on that number. We have also discussed the meaning of all the different important statements and the format specifiers used for a different type of input.


Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index