Hello World
Hello World

Reputation: 577

What does signed and unsigned values mean?

What does signed mean in C? I have this table to show:

enter image description here

This says signed char 128 to +127. 128 is also a positive integer, so how can this be something like +128 to +127? Or do 128 and +127 have different meanings? I am referring to the book Apress Beginning C.

Upvotes: 22

Views: 94785

Answers (10)

Fredrik
Fredrik

Reputation: 1

It means that there will likely be a sign ( a symbol) in front of your value (+12345 || -12345 )

Upvotes: 0

KumarAnkit
KumarAnkit

Reputation: 743

Nobody mentioned this, but range of int in table is wrong: it is

-2^(31) to 2^(31)-1

i.e.,

-2,147,483,648 to 2,147,483,647

Upvotes: 1

Pubby
Pubby

Reputation: 53097

A signed integer can represent negative numbers; unsigned cannot.

Signed integers have undefined behavior if they overflow, while unsigned integers wrap around using modulo.

Note that that table is incorrect. First off, it's missing the - signs (such as -128 to +127). Second, the standard does not guarantee that those types must fall within those ranges.

Upvotes: 41

rekordboy
rekordboy

Reputation: 156

Signed usually means the number has a + or - symbol in front of it. This means that unsigned int, unsigned shorts, etc cannot be negative.

Upvotes: 1

Fendrix
Fendrix

Reputation: 566

First, your table is wrong... negative numbers are missing. Refering to the type char.... you can represent at all 256 possibilities as char has one byte means 2^8. So now you have two alternatives to set ur range. either from -128 to +128 or 0 to 255. The first one is a signed char the second a unsigned char. If you using integers be aware what kind of operation system u are using. 16 bit ,32 bit or 64 bit. Int (16 bit,32 bit,64 bit). char has always just 8 bit value.

Upvotes: 0

1-----1
1-----1

Reputation: 1433

A signed integer can have both negative and positive values. While a unsigned integer can only have positive values.

For signed integers using two's complement , which is most commonly used, the range is (depending on the bit width of the integer):

char s -> range -128-127

Where a unsigned char have the range:

unsigned char s -> range 0-255

Upvotes: 0

Julio Batista Silva
Julio Batista Silva

Reputation: 2099

It was a typo in the book; signed char goes from -128 to 127.

Signed integers are stored using the two's complement representation, in which the first bit is used to indicate the sign.

In C, chars are just 8 bit integers. This means that they can go from -(2^7) to 2^7 - 1. That's because we use the 7 last bits for the number and the first bit for the sign. 0 means positive and 1 means negative (in two's complement representation).

  • The biggest positive 7 bit number is (01111111)b = 2^7 - 1 = 127.
  • The smallest negative 7 bit number is (11111111)b = -128
    (because 11111111 is the two's complement of 10000000 = 2^7 = 128).

Unsigned chars don't have signs so they can use all the 8 bits. Going from (00000000)b = 0 to (11111111)b = 255.

Upvotes: 4

jwaliszko
jwaliszko

Reputation: 17074

By default, numerical values in C are signed, which means they can be both negative and positive. Unsigned values on the other hand, don't allow negative numbers.

Because it's all just about memory, in the end all the numerical values are stored in binary. A 32 bit unsigned integer can contain values from all binary 0s to all binary 1s. When it comes to 32 bit signed integer, it means one of its bits (most significant) is a flag, which marks the value to be positive or negative. So, it's the interpretation issue, which tells that value is signed.

Positive signed values are stored the same way as unsigned values, but negative numbers are stored using two's complement method.

If you want to write negative value in binary, first write positive number, next invert all the bits and last add 1. When a negative value in two's complement is added to a positive number of the same magnitude, the result will be 0.

In the example below lets deal with 8-bit numbers, because it'll be simple to inspect:

positive 95: 01011111
negative 95: 10100000 + 1 = 10100001 [positive 161]
          0: 01011111 + 10100001 = 100000000
                                    ^
                                    |_______ as we're dealing with 8bit numbers,
                                             the 8 bits which means results in 0

Upvotes: 9

Faizan
Faizan

Reputation: 1898

Signed numbers are those that have either + or - appended with them. E.g +2 and -6 are signed numbers. Signed Numbers can store both positive and negative numbers thats why they have bigger range. i.e -32768 to 32767

Unsigned numbers are simply numbers with no sign with them. they are always positive. and their range is from 0 to 65535.

Hope it helps

Upvotes: 2

Lie Ryan
Lie Ryan

Reputation: 64923

The table is missing the minuses. The range of signed char is -128 to +127; likewise for the other types on the table.

Upvotes: 4

Related Questions