Curra
Curra

Reputation: 11

Why would you specify an integer as signed in C?

In my textbook, it says that all integers are considered signed by default, and most of the sources I've found online say that floating point numbers have to be signed, so what's the point of using the signed keyword?

Upvotes: 1

Views: 169

Answers (3)

Roy Avidan
Roy Avidan

Reputation: 768

the signed word isnt get used so much. the unsigned does because all the integer-types(char is worth-checking) are self-defined as signed. also, unsigned float, unsigned double or unsigned long double do not exist.//signed float/double/long double are errors to.

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

At least two situations exist where the keyword unsigned is needed.

The first one is using the keyword with the type specifier char because it can behave either as signed char or as unsigned char. That is there are three distinct types: char, signed char and unsigned char.

The second one is using the keyword with bit fields because a bit field with the type specifier int can behave either as signed int or as unsigned int.

Upvotes: 2

Kerrek SB
Kerrek SB

Reputation: 477040

In C, you can omit parts of a typename, so you can actually use signed instead of int, which may look nicely symmetric:

void f(signed a, unsigned b);

Or more verbosely:

unsigned int foo;
signed int bar;

If you don't use both types in close vicinity, you would probably prefer the simpler form of the name:

int x;   // normal
auto y;  // quaint
signed auto graph;  // why not

Upvotes: 6

Related Questions