Format specifier for unsigned short int

Learn about the format specifier for unsigned short int, i.e., which format specifier is used with the unsigned short int variables in C language?
Submitted by Shubh Pachori, on July 07, 2022

In C language the format specifier we use for the unsigned short int is %hu. %hu is used to read and display results that are to be stored or already stored in the unsigned short int variable.

In C language there are many data types like, unsigned char, signed char or char, unsigned int, signed int or int, unsigned short int, signed short int or short int, unsigned long int, signed long int or long int, long double, double, float, etc. The size and range of data types depend a lot on the compiler. However, the code that the compiler compiles is targeted for some specific types of microcontrollers or microprocessors. The format specifiers are used in C for input and output purposes. Using this concept the compiler can understand what type of data is in a variable during taking input using the scanf() function and printing using the printf() function.

Here is a table of different data types with their range, size, and format specifiers.

Example:

Consider the below given code for better understanding.

#include <stdio.h>

int main()
{
    //declaring unsigned short int variables
    unsigned short int usi;

    //declaring int, float, double, and char variables
    int i;
    float f;
    double d;
    char c;

    printf("Enter Character: ");
    scanf("%c", &c);

    printf("Enter Unsigned Short Int: ");
    scanf("%hu", &usi);

    printf("Enter Int: ");
    scanf("%d", &i);

    printf("Enter Float: ");
    scanf("%f", &f);

    printf("Enter Double: ");
    scanf("%lf", &d);

    printf("Unsigned Short Int: %hu\nInt: %d\nFloat: %f\nDouble: %lf\nCharacter: %c", usi, i, f, d, c);

    return 0;
}

Output:

Enter Character: R
Enter Unsigned Short Int: 1234
Enter Int: 12
Enter Float: 12.34
Enter Double: 2323.243
Unsigned Short Int: 1234
Int: 12
Float: 12.340000
Double: 2323.243000
Character: R



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.