%u in The C Programming Language
Introduction
The %u sign is known as the unsigned integer format specifier. In the C programming language, it is mandatory to use a format specifier to print a value or scan a value. %u is such a format specifier.
This article will discuss the unsigned integer format specifier %u. We will understand the basics of format specifiers, the syntax of %u, and various use cases of the %u.
Format Specifier in C
As stated, %u is a format specifier. So, let us first understand what format specifier is in the C programming language.
The format specifiers in the C programming language are the tokens used to tell the compiler about the data type of the value inputted or outputted. It always starts with a % character. These are used in the functions like printf(), scanf(), sprintf(), etc.
The C programming language provides various format specifiers for different data types. For example, %c, %d, %e, %f, %g, %ld, %u,etc. These symbols are used to place a particular type of data type in the functions like printf(), scanf(), sprintf(), etc, for input and output.
Here is the list of the format specifiers available in the C programming language.
Format Specifier | Description |
%d | Represents a signed decimal integer. |
%u | Represents an unsigned decimal integer. |
%f | Represents a floating-point number in decimal notation. |
%c | Represents a single character. |
%s | Represents a string of characters. |
%p | Represents a pointer. |
%x or %X | Represents an unsigned hexadecimal integer (lowercase or uppercase letters respectively). |
%o | Represents an unsigned octal integer. |
%% | Represents a literal percent character. |
%u Format Specifier in C
%u format specifier is known as the unsigned integer format specifier. This is used with the printf() function to format an unsigned decimal integer. An unsigned integer is an integer that can hold only positive values. It stores values without any sign; this directly means that the number will be positive.
%u is commonly used to deal with the numbers specified as unsigned integers.
Syntax and Code
The syntax for implementing the %u format specifier in the C programming language is as follows.
Syntax:
printf(“%u”, variable_name);
or
printf(“%u”, value);
Let us implement the code based on the above syntax to demonstrate using unsigned integer format specifiers in the C programming language.
Code:
#include <stdio.h>
// Driver code
int main()
{
unsignedint number = 42;
printf("The value of number is: %u", number);
return 0;
}
Output:
![%u in The C Programming Language](https://static.tutorialandexample.com/c/u-in-the-c-programming-language1.png)
We can see that the number variable which belongs to the data type unsigned integer is printed successfully by placing the unsigned decimal integer format specifier in the printf() function.
Various Use Cases
We have discussed the use case of unsigned integer format specifier %u with the help of syntax and its code implementation. Here, we will check the use %u in different cases and see if they will work.
Print char value using %u
Here we will write a code to print a char value with the help of the %u format specifier.
Code:
#include<stdio.h>
intmain() {
char ch = 'A';
unsignedchar uch = (unsignedchar)ch;
printf("Character value: %c\n", ch);
printf("Unsigned integer value: %u\n", uch);
return0;
}
Output:
![%u in The C Programming Language](https://static.tutorialandexample.com/c/u-in-the-c-programming-language2.png)
In this code, the ch variable stores the value `A`, and when it is converted to the unsigned integer value, it takes up an integer value 65, ASCII encoding for the `A` letter. Interpreting the char value in an unsigned integer can take different values based on the encoding followed. ASCII encoding is followed here, so the unsigned integer value for the letter `A` is 65.
Print positive integer value using %u
Code:
// C program to implement
// the format specifier
#include <stdio.h>
// Driver code
int main()
{
// Print value 20 using %u
printf("%u\n", 20);
return 0;
}
Output:
![%u in The C Programming Language](https://static.tutorialandexample.com/c/u-in-the-c-programming-language3.png)
Print negative integer value using %u
Code:
// C program to demonstrate
// the above concept
#include <stdio.h>
// Driver code
int main()
{
// The -20 value is converted
// into its positive equivalent
// by %u
printf("%u", -20);
}
Output:
It will result in an error! As %u is the format specifier for unsigned integers, but -20 is negative, which is not an unsigned integer.
Print float value using %u
// C program to demonstrate
// the concept
#include <stdio.h>
// Driver code
int main()
{
float f = 2.50;
// Printing the variable f value
printf("%u", f);
return 0;
}
Output:
It will result in an error, as the value is floating.