LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   char is one byte but equals 4294967264 (https://www.linuxquestions.org/questions/programming-9/char-is-one-byte-but-equals-4294967264-a-4175591758/)

errigour 10-19-2016 01:14 PM

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;
  printf("sizeof test: %lu\r\n", sizeof(test));
  printf("char test = 224 %%u: %u\r\n", test);
  printf("char test = 224 %%d: %d\r\n", test);


dugan 10-19-2016 01:16 PM

Posting your results instead of expecting people to compile and run the program would have been nice. My results were:

Code:

sizeof test: 1
char test = 224 %u: 4294967264
char test = 224 %d: -32


errigour 10-19-2016 01:20 PM

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.

dugan 10-19-2016 01:26 PM

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.

rtmistler 10-19-2016 01:55 PM

Quote:

Originally Posted by dugan (Post 5620171)
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.

Dugan's point is probably the most applicable here.

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.

jpollard 10-19-2016 03:18 PM

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.

sundialsvcs 10-19-2016 07:38 PM

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.

jpollard 10-19-2016 09:39 PM

Quote:

Originally Posted by sundialsvcs (Post 5620341)
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.

quite true.

In this case though, no pointers were involved.

NevemTeve 10-19-2016 10:18 PM

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.