Open In App

Difference Between Unsigned Int and Signed Int in C

Last Updated : 30 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Integers are typically stored as 32-bit values, but in some environments, they may contain 16-bit values (or even a different number, usually the product of two powers). For example, let’s examine 4-bit integers. They are small but can help illustrate a point. signed int can represent both positive and negative values, and unsigned int can only represent non-negative integer values.

For every value which is greater than INT_MAX and less than INT_MIN we can encounter discontinuity i.e., we can get unexpected results if we use a signed integer. But for unsigned integer types, the discontinuity will only be a very large value than INT_MAX and a very less value than INT_MIN. This is because signed integers use a sign bit to represent positive and negative numbers and unsigned integers use all bits to represent the magnitude of the number, without a sign bit.

We can see the discontinuity of signed integer values i.e., signed integer values get int overflow error as shown in the below program.

C




// C program to show integer
// overflow error signed
// integer
#include <stdio.h>
 
// Driver code
int main()
{
    int x = 4294967295;
    printf("%d", x);
    return 0;
}


Output

-1

Explanation: The above program will print -1 as its output because it will be out of range.

But the same above program will run for unsigned int data type:

C




// C program for unsigned integer
#include <stdio.h>
 
// Driver code
int main()
{
    unsigned int x;
    x = 4294967295;
 
    printf("%u", x);
    return 0;
}


Output

4294967295

Difference between Signed Int and Unsigned Int 

Signed Int Unsigned Int
A signed int can store both positive and negative values. Unsigned integer values can only store non-negative values.
A signed integer can hold values from -232/2 – 1 (-2147483648) to 232/2 – 1 ( 2147483647 ) A 32-bit unsigned integer can store only positive values from 0 to 232 -1 ( 4294967295 )
A signed integer can get an overflow error while used in a program with larger values. Wraps around to 0 if the value exceeds the maximum limit.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads