Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Signed vs Unsigned Integers | Binary Representation and Two’s Complement
Bitwise Operations and Binary Logic in C

bookSigned vs Unsigned Integers

In C, it is important to understand the difference between signed and unsigned integers. A signed integer can store both positive and negative values, while an unsigned integer can store only non-negative values, including zero. Because of this, unsigned types can represent a larger maximum value using the same amount of memory.

For example, on a typical system where an int is 32 bits, a signed int usually ranges from −2,147,483,648 to 2,147,483,647. An unsigned int, using the same 32 bits, ranges from 0 to 4,294,967,295. Choosing between signed and unsigned depends on the problem you are solving: use signed integers when negative values are possible, and unsigned integers when working with values like counters, sizes, or indexes that are never negative.

signed_unsigned_comparison.c

signed_unsigned_comparison.c

copy
1234567891011
#include <stdio.h> int main() { int signed_val = -1; unsigned int unsigned_val = -1; printf("Signed int assigned -1: %d\n", signed_val); printf("Unsigned int assigned -1: %u\n", unsigned_val); return 0; }

A key difference between signed and unsigned integers is how they handle overflow and underflow. For signed integers, exceeding the valid range results in undefined behavior, meaning the outcome is not guaranteed. Unsigned integers follow modular arithmetic, so values wrap around predictably, for example, subtracting 1 from 0u produces the maximum value of that type.

Their binary representation also differs. Signed integers typically use two’s complement, where the most significant bit represents the sign. Unsigned integers use all bits to store the value.

Note
Note

Because unsigned wraparound is well-defined, it can be useful for low-level or bitwise operations, but it may also cause subtle bugs if negative values are expected.

question mark

What is the key difference between signed and unsigned integers in C

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 2

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

bookSigned vs Unsigned Integers

Deslize para mostrar o menu

In C, it is important to understand the difference between signed and unsigned integers. A signed integer can store both positive and negative values, while an unsigned integer can store only non-negative values, including zero. Because of this, unsigned types can represent a larger maximum value using the same amount of memory.

For example, on a typical system where an int is 32 bits, a signed int usually ranges from −2,147,483,648 to 2,147,483,647. An unsigned int, using the same 32 bits, ranges from 0 to 4,294,967,295. Choosing between signed and unsigned depends on the problem you are solving: use signed integers when negative values are possible, and unsigned integers when working with values like counters, sizes, or indexes that are never negative.

signed_unsigned_comparison.c

signed_unsigned_comparison.c

copy
1234567891011
#include <stdio.h> int main() { int signed_val = -1; unsigned int unsigned_val = -1; printf("Signed int assigned -1: %d\n", signed_val); printf("Unsigned int assigned -1: %u\n", unsigned_val); return 0; }

A key difference between signed and unsigned integers is how they handle overflow and underflow. For signed integers, exceeding the valid range results in undefined behavior, meaning the outcome is not guaranteed. Unsigned integers follow modular arithmetic, so values wrap around predictably, for example, subtracting 1 from 0u produces the maximum value of that type.

Their binary representation also differs. Signed integers typically use two’s complement, where the most significant bit represents the sign. Unsigned integers use all bits to store the value.

Note
Note

Because unsigned wraparound is well-defined, it can be useful for low-level or bitwise operations, but it may also cause subtle bugs if negative values are expected.

question mark

What is the key difference between signed and unsigned integers in C

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 2
some-alt