Bing Sun Bing Sun -4 years ago 82
C Question

Why don't we use a short integer to represent a return value?

So many books say we must never waste a bit for arithmetic and function definitions. I saw so many small numbers from 0 to 255 that were defined as char type variables, with even larger positive numbers defined as an unsigned short int type.

Why don't we use an unsigned short int or a char type instead of int to represent a return value?

I tried:

char main(){return 0;}


It is ok.

Answer Source

There is very seldom any savings involved in returning a short or even a char in preference to an int. In the vast majority of C implementations, a machine register will be used to hold the return value, and the only difference in returning a short instead of an int is that part of the register will remain unused. (As an aside, this is also the reason why a main returning a char "works" for you, even though the standard doesn't guarantee that it will.)

Virtually the only reason, then, for returning a short is when you have a particular algorithm that has distinct reasons for working with shorts instead of ints, and that's just not very common.

For the record, the same thing can be said of function arguments as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download