Question:
a. Why does the program deliver a 8-bit hex number (0xFFFFFFFF) when the input is 0xFF and when the sizeof() delivers char as 4-bit ?
This code iswrong when you want to display the size of a char (in bits):
printf("Size of char: %d-bits\n", sizeof(char)*4);
Sure it will report that achar variable consists out of 4 bits, but there's something wrong with your code, for example: how did you come up with this magic 4? For what does it stand? Second: the sizeof operator will always (under all circumstances, on all platforms return 1 if you invoke: sizeof(char) ), so this multiplied by 4 willalways give 4 as the result.
The correct way to get the correct size in bits per byte (a char is always 1 byte) is by using the CHAR_BIT macro as defined in the limits.h header (you have to include this file if you want to use the CHAR_BIT macro, you can do this by adding this directive to the top of your program: #include <limits.h> ).
The correct way of getting the size (in bits) of a variable of typechar and or integer in C:
char:
printf("Size of a variable of type char (in bits): %lu", CHAR_BIT);
integer:
printf("Size of a variable of type integer (in bits): %lu", CHAR_BIT*sizeof(int));
What exactly does CHAR_BIT represent?
The CHAR_BIT macro represents the number of bits in a byte (a variable of type char does always have the size of one byte, as defined per standard, but this isn't the case for variables of other types, they depend on your C-implementation).
So multiplying the value of CHAR_BIT by the sizeof of a certain variable type (int, double, float...) will give you the number of bytes that type consists of.
Note that sizeof(type) returns the size (in bytes) of a variable type.
Also notice that I used the %lu format specifier in the format string passed to the printf() function, this is because the sizeof operator returns a value of an unsigned integral datatype (as defined per standard), to produce portable code I added the %lu format specifier (which will ensure a cast to an unsigned long, the biggest unsigned integral type) to make sure that the size will always be displayed correctly on the screen.
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
But, what about my other question. Why do I get 0xFFFFFFF and -1 as the decimal?
Oh sorry, I forgot to answer that one :P
Just as a convenience for anyone who's reading the thread:b. The program returns 0xFFFFFFFF as -1 in decimal. However when I use a Hex-Decimal converter I get 0xFF as 255 and 0xFFFFFFFF as 4294967295? Well, this is because in your code you usesigned ints (that is what you get when you declare a variable by just using <strong>int</strong> <em>a_variable_name</em> ).
Asint exists in both signed and unsigned flavors, your hex-converter was using an unsigned int to do the conversion, while in your code you have a signed int, as the name tells you it's signed, which means that it can have a sign, this isn't the case with unsigned integers which cannot have a sign.
A very non-technical explanation (but maybe less correct) is: with signed integers the high-order bit signifies whether the number is negative (high-order bit is one), or positive (high-order bit is zero).
With unsigned integers, the high-order bit is used to store another range of possible numbers.
Important to mention is that both signed and unsigned types do consume the same amount of bytes, this is also why an unsigned integer variable can store twice as huge positive values as a signed one, while a signed one can also store negative numbers.
Maybe something interesting for you to google up is: two's complement, this will give you a better insight in how the whole thing (with sign bits, etc.) works.
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
b. The program returns 0xFFFFFFFF as -1 in decimal. However when I use a Hex-Decimal converter I get 0xFF as 255 and 0xFFFFFFFF as 4294967295?
In my previous post I forgot to point out that 0xFFFFFFFF has a binary representation wherein every bit is one, if such a binary value goes into a signed integer variable, then the variable contains a value equivalent to-1 in decimal.
If you read about the two's complement (A must read! Google it up), you'll be able to understand why this is the case :)
Also thanks to Tom Gunn for pointing out a mistake in my post and providing an excellent correction :)
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
However, the strange thing, it doesn't matter whether I have the int variable signed or unsigned - the program always returns the same number:
int y=0xFFFF returns 65,535
unsigned y=0xFFFF returns 65,535 (should return -1)
Any advice?
I'm rather curious why you think an unsigned value should be negative, but do remember to try to use the correct format specifiers with printf for the data type being used.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
#include <stdio.h>
#include <limits.h>
int main()
{
int x;
unsigned y;
printf("INT_MAX = %d\n", INT_MAX);
if ( 0xFFFF < INT_MAX )
{
puts("Don't be surprised if 0xFFFF doesn't wrap to -1");
}
x = 0xFFFF;
y = 0xFFFF;
printf("0x%X in decimal: %d\n", (unsigned)x, x);
printf("0x%X in decimal: %d\n", y, y);
x = UINT_MAX;
y = UINT_MAX;
printf("0x%X in decimal: %d\n", (unsigned)x, x);
printf("0x%X in decimal: %u\n", y, y);
return 0;
}
/* my output
INT_MAX = 2147483647
Don't be surprised if 0xFFFF doesn't wrap to -1
0xFFFF in decimal: 65535
0xFFFF in decimal: 65535
0xFFFFFFFF in decimal: -1
0xFFFFFFFF in decimal: 4294967295
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Questions:
1. Why does UINT_MAX return -1 (as it is max unsigned - I would expect a 32-bit unsigned max of 4.294.967.295)
It doesn't. The specifier %d is not correct for use with an unsigned value outside the rage of an int . Using %u displays the correct value.2. Why do x and y not return the same number (after x and y have been set to UINT_MAX) as x is cast as "unsigned" and y is already defined as unsigned (x returns -1 and y returns 4.294.967.295) See #1.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314