Signed Vs Unsigned

By default, if we're just doing

int a = 1;

This will automatically be signed integer.

We can declare unsigned by using:

unsigned int a = 1;

unsigned can store double the value of positive signed integer. For example:

  • int values are from -2,147,483,648 to 2,147,483,647
  • unsigned int are from 0 to 4,294,967,295
int main() {
    int a = 2147483647;
    unsigned int unsignedA = 2147483647;

    printf("a is %d, unsigned_a is %u\n", a + 1, unsignedA + 1);
}
a is -2147483648, unsigned_a is 2147483648

Printing signed vs unsigned

Use Printf format type, for

  • signed, we use %d for int, %l for long
  • unsigned we use %u for int, %lu for long

[!note]
If you print unsigned using %d, it's not going to present it true value.