20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Understand<strong>in</strong>g Data Types and Constants<br />

29<br />

On some mach<strong>in</strong>es, a short <strong>in</strong>t takes up half the amount of storage as a regular <strong>in</strong>t<br />

variable does. In any case, you are guaranteed that the amount of space allocated for a<br />

short <strong>in</strong>t will not be less than 16 bits.<br />

There is no way to explicitly write a constant of type short <strong>in</strong>t <strong>in</strong> C.To display a<br />

short <strong>in</strong>t variable, place the letter h <strong>in</strong> front of any of the normal <strong>in</strong>teger conversion<br />

characters: %hi, %ho, or %hx. Alternatively, you can also use any of the <strong>in</strong>teger conversion<br />

characters to display short <strong>in</strong>ts, due to the way they can be converted <strong>in</strong>to <strong>in</strong>tegers<br />

when they are passed as arguments to the pr<strong>in</strong>tf rout<strong>in</strong>e.<br />

The f<strong>in</strong>al specifier that can be placed <strong>in</strong> front of an <strong>in</strong>t variable is used when an <strong>in</strong>teger<br />

variable will be used to store only positive numbers.The declaration<br />

unsigned <strong>in</strong>t counter;<br />

declares to the compiler that the variable counter is used to conta<strong>in</strong> only positive values.<br />

By restrict<strong>in</strong>g the use of an <strong>in</strong>teger variable to the exclusive storage of positive <strong>in</strong>tegers,<br />

the range of the <strong>in</strong>teger variable is extended.<br />

An unsigned <strong>in</strong>t constant is formed by plac<strong>in</strong>g the letter u (or U) after the constant,<br />

as follows:<br />

0x00ffU<br />

You can comb<strong>in</strong>e the letters u (or U) and l (or L) when writ<strong>in</strong>g an <strong>in</strong>teger constant, so<br />

20000UL<br />

tells the compiler to treat the constant 20000 as an unsigned long.<br />

An <strong>in</strong>teger constant that’s not followed by any of the letters u, U, l, or L and that is<br />

too large to fit <strong>in</strong>to a normal-sized <strong>in</strong>t is treated as an unsigned <strong>in</strong>t by the compiler. If<br />

it’s too small to fit <strong>in</strong>to an unsigned <strong>in</strong>t, the compiler treats it as a long <strong>in</strong>t. If it still<br />

can’t fit <strong>in</strong>side a long <strong>in</strong>t, the compiler makes it an unsigned long <strong>in</strong>t. If it doesn’t fit<br />

there, the compiler treats it as a long long <strong>in</strong>t if it fits, and as an unsigned long long<br />

<strong>in</strong>t otherwise.<br />

When declar<strong>in</strong>g variables to be of type long long <strong>in</strong>t, long <strong>in</strong>t, short <strong>in</strong>t,or<br />

unsigned <strong>in</strong>t,you can omit the keyword <strong>in</strong>t.Therefore, the unsigned variable<br />

counter could have been equivalently declared as follows:<br />

unsigned counter;<br />

You can also declare char variables to be unsigned.<br />

The signed qualifier can be used to explicitly tell the compiler that a particular variable<br />

is a signed quantity. Its use is primarily <strong>in</strong> front of the char declaration, and further<br />

discussion is deferred until Chapter 14,“More on Data Types.”<br />

Don’t worry if the discussions of these specifiers seem a bit esoteric to you at this<br />

po<strong>in</strong>t. In later sections of this book, many of these different types are illustrated with<br />

actual program examples. Chapter 14 goes <strong>in</strong>to more detail about data types and conversions.<br />

Table 4.1 summarizes the basic data types and qualifiers.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!