Reading and Writing Integers

In this tutorial, we will learn how to read and write different types of integers in the C programming language.


Reading and Writing an Unsigned Integer

To read and write an unsigned integer in C, we need to use different format specifiers depending on whether the number is in decimal, octal, or hexadecimal format.

For reading or writing an unsigned decimal, octal, or hexadecimal number, we use the format specifiers “%u”, “%o”, or “%x” respectively. These specifiers are used instead of “%d”.

Let’s consider an example program where we accept decimal, octal, and hexadecimal numbers from the user and print them as they are.

#include <stdio.h>

int main()
{
    unsigned int x, y, z;
    printf("Enter the number in decimal format: ");
    scanf("%u", &x);

    printf("Enter the number in octal format: ");
    scanf("%o", &y);

    printf("Enter the number in hexadecimal format: ");
    scanf("%x", &z);

    printf("The numbers entered by you are: ");
    printf("%u %o %x", x, y, z);
    return 0;
}

Output:

The program asks the user to enter a decimal number, an octal number, and a hexadecimal number. It then reads these numbers using the appropriate format specifiers and stores them in variables x, y, and z respectively. Finally, it prints the entered numbers using the format specifiers “%u”, “%o”, and “%x”.

If we run this program and enter some numbers, we will see the correct output. The program uses the correct format specifiers for each type of number.

Now, let’s make a change to the program and observe how it affects the output. We will change the format specifier “%x” in the third scanf function to “%u”. Can you guess what the output will be this time with the same inputs?

#include <stdio.h>

int main()
{
    unsigned int x, y, z;
    printf("Enter the number in decimal format: ");
    scanf("%u", &x);

    printf("Enter the number in octal format: ");
    scanf("%o", &y);

    printf("Enter the number in hexadecimal format: ");
    scanf("%u", &z);

    printf("The numbers entered by you are: ");
    printf("%u %o %x", x, y, z);
    return 0;
}

Output:

If we make this change and run the program again, the output will be different. Instead of printing the hexadecimal number correctly, it will only print the value 1. This happens because the character ‘f’ is not a valid digit for a decimal integer, so only the digit ‘1’ is read as input by the third scanf function. As a result, the variable z is assigned the value 1, and that’s what gets printed on the screen.

This example demonstrates the importance of using the correct format specifiers when accepting and printing values in C.


Reading and Writing Short Integers

To read and write short integers in C, we use the letter h in front of the format specifiers for decimal, octal, unsigned decimal, or hexadecimal numbers.

For example, if we have a variable ‘x’ of type short, we can read its value using scanf with the format specifier “%hd” and print its value using printf with the format specifier “%hd” as follows:

short x;
scanf(“%hd”, &x);
printf(“%hd”, x);

Similarly, we can read or write octal and hexadecimal short integers by using the format specifiers “%ho” or “%hx”.


Reading and Writing Long and Long Long Integers

To read and write a long integer in C, we use the letter l in front of the format specifiers for decimal, octal, unsigned decimal, or hexadecimal numbers.

For example, if we have a variable ‘x’ of type long, we can read its value using scanf with the format specifier “%ld” and print its value using printf with the format specifier “%ld”.

long x;
scanf(“%ld”, &x);
printf(“%ld”, x);

If we need to work with long long integers, we use the letter ll in front of the format specifiers. 

For example, if we have a variable ‘y’ of type long long, we can read its value using scanf with the format specifier “%lld” and print its value using printf with the format specifier “%lld”.

long long y;
scanf(“%lld”, &y);
printf(“%lld”, y);

Summary

In this tutorial, we learned how to handle different types of numbers in the C programming language. It explains how to read and write unsigned integers, short integers, long integers, and long long integers.

When working with unsigned integers, we need to use different format specifiers depending on the number’s format (decimal, octal, or hexadecimal). We have seen examples of the correct specifiers to use for each format.

For short integers, we use the letter h before the format specifiers for decimal, octal, unsigned decimal, or hexadecimal numbers.

To handle long integers, we use the letter l before the format specifiers. If we need to work with long long integers, we use the letter ll instead.

The tutorial demonstrates the importance of using the correct format specifiers. It shows an example where using the wrong specifier results in incorrect output.

Overall, we have understood how to read and write different types of numbers in C, ensuring they use the right format specifiers for each type.



Leave a comment

Leave a comment

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

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.