Unsigned int in C Language

Unsigned int in C Language
Techiio-author
Written by Shuvhojit DebFebruary 2, 2022
10 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this article, we'll know what is the unsigned int in C.

Unsigned int in C

There are various data types in the C programming language that are used to declare variables before they are used as data storage for a particular variable to do certain functions, such as int data types for integers, float data types for floating real numbers, and so on. Unsigned is a data type in C that is a variable type of int that can carry both zero and positive values. A signed int data type is a variable type of int data type that can store negative, zero, and positive integers. A negative value cannot be represented by this unsigned int data type.

Syntax

An unsigned data type is one of the type modifiers in the C programming language that is used to change the data storage of a data type. In C, we commonly use the integer (int) data type, which is signed by default and can store both negative and positive numbers. Let's have a look at how to declare it in C programs.

unsigned int variable_name;

Example:

unsigned int a;

Explanation: In the above example, the variable “a” can hold the values only zero and positive values. We know that the data type “int” has the size of 4 bytes where it can hold values from -231 to 231 – 1, but in this, we have declared “x” as unsigned int so it can hold values from 0 to 232 – 1. The unsigned int can contain storage size either 2 or 4 bytes where values ranging from [0 to 65,535] or [0 to 4,294,967,295]. The format specifier used for an unsigned int data type in C is “ %u ”.

Examples to Implement Unsigned Int in C

Let us see some examples of it:

Example #1

Let us see a small C program that uses unsigned int:

Code:

#include <stdio.h>
#include <limits.h>
int main(int argc, char** argv)
{
printf("Unsigned int values range: %u\n", (unsigned int) UINT_MAX);
return 0;
}

Output:

blogpost

Explanation: So in general, in C we have signed and unsigned integer data types to declare in the program. Let us see if the variable is declared signed int and we want it to convert it into unsigned int which is a bit confusing in C programming. In C, the compiler performs implicit casting but sometimes it gives a warning so most of the time they are manually cast explicitly which is done using the data type you want to convert in the parenthesis to another data type.

Example #2

Let us see the C program that converts the signed variable to an unsigned variable:

Code:

#include <stdio.h>
int main(void)
{
int  a = 57;
unsigned int b = (unsigned int)a;
printf("The value of signed variable is: %u\n",a);
printf("The value of unsigned variable is: %u\n",b);
return 0;
}

Output:

blogpost

Explanation: In the above program, we have declared variable “a” as integer data type which is by default is signed int data type, and then we are converting variable “a” to unsigned int data type using casting for converting the variable from signed to unsigned by using “(unsigned)” before the variable “a” for converting. According to the C99 standard the integer value when converted to another type then the value will not be changed, so in the above program also the value of the variable “a” is 57 so when it is changed to unsigned int the value for the new variable “b” which stores the value of variable “a” which is converted to unsigned has the same value which was declared in the starting as “57”.

In C, the int data type is by default is signed data type which can store even negative values also other than positive values. So converting negative values to unsigned int also is possible in the C programming language. If the variable is having a negative value and if we are converting it into unsigned then the value of that variable is repeatedly converted by adding or subtracting one or more than a maximum value until the value is in the range of the new type.

Example #3

Let us see the example for converting negative signed int variable to unsigned int variable:

Code:

#include <stdio.h>
int main(void)
{
int a = -57;
unsigned int b = (unsigned int)a;
printf("The unsigned value of negative signed value 0x%x\n",a);
return 0;
}

Output:

blogpost

Explanation: In the above program, the hexadecimal representation of value -57 is 0xffffffc7 where this value is in the range of unsigned int so after the casting of this value there is no specific change in the bits of the value.

In the C programming language, the overflow of unsigned int is well defined than signed int. Unsigned int is much better than signed int as the range for unsigned int is larger than signed int and the modulus operation is defined for unsigned int and not for signed int. The unsigned int can reduce some conditional statements and also it is mostly used in embedded systems, registers, etc so only unsigned int is preferable to signed int. Unsigned int can also be declared in the function argument.

Unsigned int is usually used when we are dealing with bit values which means when we are performing bitwise operations like bit masking orbit shifting. As bit shifting in negative integers is undefined or implementation-defined outputs.

Conclusion

We examined unsigned int in the C programming language in this article. Unsigned int is a data type that can hold data values ranging from 0 to 1, but signed int can additionally hold negative values. Unsigned int is frequently preferred over signed int because unsigned int is larger. The format specifier for unsigned int is " percent u ". When working with bit values, such as bit masking or bit shifting, this data type is employed.

C Language
Unsigned int
C syntax
Programing
Software
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance