8 Replies - 616 Views - Last Post: 21 January 2013 - 07:58 AM Rate Topic: -----

#1 vigneshvicky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-January 13

HOW TO STORE SIGNED CHAR INTO AN INT ?

Posted 18 January 2013 - 07:35 PM

char is treated as signed char, what will be printed by this program:
main()
{
	char x = 0xFF;
	unsigned int y;
	int z;
	y = x; 	z = x;
	printf("%x %x\n", y, z)
}

This post has been edited by jimblumberg: 18 January 2013 - 08:23 PM
Reason for edit:: Added missing code tags. Please learn to use them properly.

Is This A Good Question/Topic? 0
  • +

Replies To: HOW TO STORE SIGNED CHAR INTO AN INT ?

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10773
  • View blog
  • Posts: 40,122
  • Joined: 27-December 08

Re: HOW TO STORE SIGNED CHAR INTO AN INT ?

Posted 18 January 2013 - 07:36 PM

Run it and see.
Was This Post Helpful? 0
  • +
  • -

#3 vigneshvicky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-January 13

Re: HOW TO STORE SIGNED CHAR INTO AN INT ?

Posted 18 January 2013 - 07:44 PM

View Postmacosxnerd101, on 18 January 2013 - 07:36 PM, said:

Run it and see.

WHAT IS THE LOGIC BEHIND THIS ?
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: HOW TO STORE SIGNED CHAR INTO AN INT ?

Posted 18 January 2013 - 10:50 PM

Quote

WHAT IS THE LOGIC BEHIND THIS ?


Have a read about Two's complement, it's not just an operation - it's the representation of signed numbers.

I'll give you a start
char x = 0xFF;



On most systems, char is default signed (although this is not guaranteed). 0xFF represented in binary is 1111 1111. The first bit is set to '1' in the signed character, this implies that the value is in fact a negative value. Implementing two's complement, you will find that the variable 'x' is equal to negative 1.

This takes care of the explanation for one of the cases. What do you think happens when you assign a negative signed integer to a unsigned integer?

This post has been edited by jjl: 18 January 2013 - 10:51 PM

Was This Post Helpful? 2
  • +
  • -

#5 undefined behaviour  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 36
  • Joined: 17-January 13

Re: HOW TO STORE SIGNED CHAR INTO AN INT ?

Posted 20 January 2013 - 08:15 PM

"Try it and see" is a dangerous philosophy that leads to unportable code. This code invokes implementation-defined behaviour as indicated by §6.3.1.3 of the C standard. Consider if the implementation were to decide to raise a signal indicating that the code above has performed a computational error, and the signal handler were to return. This code would, for that implementation, be in the realms of undefined behaviour.

@jjl: Have you ever heard of ones' complement or sign and magnitude?
Was This Post Helpful? 0
  • +
  • -

#6 undefined behaviour  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 36
  • Joined: 17-January 13

Re: HOW TO STORE SIGNED CHAR INTO AN INT ?

Posted 20 January 2013 - 08:35 PM

Furthermore, this code depends upon CHAR_BIT. There are existing systems where CHAR_BIT is 16, and a signed char can store values between the range of -32767 and 32767. On such systems, char x = 0xFF; would result in x storing the positive value 255. This has nothing to do with signed representation.

The output for that program is entirely implementation defined.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10773
  • View blog
  • Posts: 40,122
  • Joined: 27-December 08

Re: HOW TO STORE SIGNED CHAR INTO AN INT ?

Posted 20 January 2013 - 11:30 PM

Quote

"Try it and see" is a dangerous philosophy that leads to unportable code.

If someone is going to post some code (especially something basic) and ask what it does without having made any apparent effort, I think running it is a valid way to figuring it out. Once the result is known, explanation is a good next step.
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: HOW TO STORE SIGNED CHAR INTO AN INT ?

Posted 21 January 2013 - 02:51 AM

Quote

@jjl: Have you ever heard of ones' complement or sign and magnitude?

Yes, but without the specifics of the architecture, I think it's fair to assume he is using something that is fairly mainstream.
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,281
  • Joined: 25-December 09

Re: HOW TO STORE SIGNED CHAR INTO AN INT ?

Posted 21 January 2013 - 07:58 AM

There are several things wrong with the code provided. A couple of these problems actually lead to undefined behaviour. The use of the "%x" format specifier in the printf() is one example of undefined behaviour. The "%x" format specifier is only valid for an unsigned int value. And the C standard states that using a specifier that doesn't match the type is undefined behaviour. From Section 7.21.6.1 The fprintf function.

Quote

9
If a conversion specification is invalid, the behavior is undefined.282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.


Since the OP stated that a char is a signed char, assigning a value of 0xFF will produce undefined behaviour, if this char is also an 8 bit value, since overflow of a signed integral value is undefined.

A decent compiler should reject the code as provided, since no include files were used the arguments for the printf() call should not match and it should be able to detect the problem with the signed char overflow. So in my opinion the telling the OP to try to compile this program is a valid response to the code dump.

By the way here are the errors and warnings my compiler produced:

Quote

main.c|2|error: return type defaults to ‘int’|
main.c||In function ‘main’:|
main.c|4|warning: overflow in implicit constant conversion [-Woverflow]|
main.c|8|error: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]|
main.c|8|warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]|
||=== Build finished: 2 errors, 2 warnings ===|



Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1