On occasions you may need to work with strictly non-negative integers, or with integers in a shorter or longer interval than the default for ints. The following types of range modifying declarations are possible:
(I) unsigned
usage: unsigned int stadium_seats;
This declaration "liberates" the sign bit, and makes the entire word (including the freed sign bit) available for the storage of non-negative integers. [Note: The sign bit_the leftmost bit of a memory word _ determines the sign of the contents of the word when it's set to 1, the value stored in the remaining bits is negative. Most architectures use two's complement arith- metic, in which the sign bit is "weighted", i.e. it has an associated place value which is nega- tive. Thus on a 16-bit machine its value is -215, or -32,768. So a 16-bit signed number such as 10000000 00111111 would have the value 20 + 21 + 22+ 23 +24 + 25 -215 = -32,705. As an unsigned integer this string of bits would have the value 32831.] On PCs the unsigned decla- ration allows for int variables the range [0, 65535] and is useful when one deals with quan- tities which are known beforehand to be both large and non-negative, e.g. memory addresses, a stadium's seating capacity, etc.
Just as %d in-the printf ( ) prints decimal int values, %u is used to output unsigned ints, as the program below illustrates. Execute the program and determine its output also examine the effect changing the % u format conversion specifiers to %d in the printf Os. Can you explain your results?
#include <stdio.h> main() { unsigned int stadium_seats, tickets_sold, standing_viewers; stadium_seats = 40000; tickets_sold = 50000; standing_viewers = tickets_sold - stadium_seats; printf ("Tickets sold: %u\n", tickets_sold); printf ("Scats available: %u\n", stidium_scats); printf ("There could be a stajnpede because\,n"); printf ("there may be nearly %u standees at the match.\n", standing_viewers); }
(II) short
usage: short int friends;
The short int declaration may be useful in instances where an integer variable is known beforehand to be small. The declaration above ensures that the range of friends will not ex- ceed that of ints, but on some computers the range may be shorter (e.g. -128 through 127) friends may be accommodated in a byte, thus saving memory. There.was a time in the olden days of computing,,when main memory was an expensive resource, that programmers tried by such declarations and other stratagems to optimise core usage to the extent possible. (The VAX computer uses two bytes to store short ints, half the amount it uses for ints but for present-day PCs, with memory cheap and plentiful, most compiler writers make no distnc- tion between ints and short ints.)
The %d specification in the printf ( ) is also used to output short ints.
(III) unsigned short
usage: unsigned short int books
The range of books will not exceed that of unsigned ints it may be shorter.
(IV) long
usage: long int stars_in_galaxy;
This declaration is required when you need to use integers larger than the default for ints. On most computers long ints are 4-byte integers ranging over the interval [-2147483648, 2147483647]. When a long integer constant is assigned a value the letter L (or I) must be written immediately after the rightmost digit: long int big_num = 123456789OL;
% ld in the printf 0 outputs long decimal ints, as you may verify by executing
#include <stdio.h> main ( ) { long int population-2000 = 123456789OL; printf ("The population of this country in 2000 AD\,n"); printf ("will exceed %Id if we do\n", populaton_2000); printf ("not take remedial steps now.\n"); }
(V) unsigned long
usage: unsigned long int population_2000
The unsigned long declaration transforms the range of long ints to the set of 4-byte non- negative integers. Here population_2000 is allowed to range over [0, 4294967295] (Let's hope that larger-sized words will not be required to store this value!) unsigned longs are output by the %lu format conversion specifier in the printf 0.
unsigned letters; /* instead of unsigned int letters*/
long rope; /* insted of long int rope*/
unsigned short note; /* instead of unsigned short int note*/ etc.
# include
ReplyDeletei n t main()
{
unsigned cheque = 54321;
long time = 1234567890L; /*seconds */
printf("I\’ve waited a long\
time (%ld seconds)\n", time);/*Continued*/
printf("for my cheque (for Rs.%u/),
\
and now\n", cheque);/*Continued*/
printf("I find it\’s unsigned!\n");
return (0);
}
Modify this program appropriately to convert the time in seconds to a value of
days, hours, minutes and seconds so that it gives the following additional output
on execution:
That was a wait of:
14288 days,
23 hours,
31 minutes
and 30 seconds.
(Hint: If a and b are int variables, then a/b is the quotient and a % b the
remainder after division of a by b if b is less than a.)
Now when i will modify this program do i have to write :
Long time = a = 1234567890L, b = 1234567890L, c; ??
Please explain how to modify this in order to get the out put 14288 days,
23 hours,
31 minutes
and 30 seconds.