char is one byte but equals 4294967264
How is it possible that:
char test = 224 printed as an unsigned integer with printf is: 4294967264 Code:
char test = 224; |
Posting your results instead of expecting people to compile and run the program would have been nice. My results were:
Code:
sizeof test: 1 |
I actually think i just figured out the answer printing -32 as an integer prints 10000000 0000000 00000000 00000000 or something close to that. It's printing a -32 as an unsigned integer. Sorry thanks guys.
|
You get the expected results if you change "char" to "unsigned char". Data types are signed by default.
224 is too large a number to store in a signed char, because one bit is being used for the sign and you only actually have 7 bits for the number. So what you're seeing is integer overflow. |
Quote:
Always been told to CHECK WHAT THE COMPILER DOES. Checking this in mixed source/assembly listing or in the debugger will likely explain what is happening. |
Note something being overlooked...
You are passing a byte as a value - thus it will get extended to 32 bits (an int) to be stored on the stack. |
When you are dealing with "C," it is imperative to remember that "C trusts you!" :eek:
If you tell the printf() function, by means of whatever format-string you give to it, that "the corresponding memory address(!) is the address of an 'integer,'" "C" will believe you." "C" is a (very intentionally ...) "very low-level language" that implicitly relies upon y-o-u to always say and do the right thing. |
Quote:
In this case though, no pointers were involved. |
Note: this thing is called 'promotion': a type 'char' is promoted to 'int' when used as function-parameter. Also let's note that type '[unqualified] char' might be either 'signed' or 'unsigned' depending on the platform :(
|
All times are GMT -5. The time now is 05:46 AM. |