Lab: questions about C integers
Here's a list of questions. For each question, show how to answer the question by writing a little C code.
A handy tool
If you want to know the exact bits contained in an int, do this:
int j = whatever;
printf("0x%08X\n", j);
It gets slightly weirder for long (note the l before the X)
long k = whatever;
printf("0x%016lX\n", k);
It gets even weirder for char. See below.
The questions
- How many bytes are in an int? (don't forget the C sizeof function) (it's not really a function, but it looks like one, so whatever)
- How many bytes are in a long?
- How many bytes are in a char?
- How many bytes are in an unsigned int?
- How many bytes are in an unsigned long?
- How many bytes are in an unsigned char?
- If you do this:
int j = -1; unsigned int k = -1;what bits are in j? what bits are in k?
- What the heck is going on here?
char c1 = 0x41; printf("c1 as char: %c\n", c1); printf("c1 as decimal int: %d\n", c1); printf("c1 as hexadecimal int: %X\n", c1); char c2 = 0xCE; printf("c2 as char: %c\n", c2); printf("c2 as decimal int: %d\n", c2); printf("c2 as hexadecimal int: %X\n", c2);
- What the heck is going on here?
int j = -1; int k = (j >> 4); printf("j (-1): 0x%08X\n", j); printf("j >> 4: 0x%08X\n", k);
- Do the same thing as the previous item, but with j and k unsigned.