Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,543 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,545 people online right now. Registration is fast and FREE... Join Now!




variable type conversion

 
Reply to this topicStart new topic

variable type conversion

raedbenz
23 Nov, 2007 - 03:20 PM
Post #1

New D.I.C Head
*

Joined: 13 Dec, 2006
Posts: 19


My Contributions
Hi..if i have this piece of code in C language...
Note: i dont want to use casting

e.g.

int num=0xABCD;
char ch;

ch=num;

what is the value of ch ??
is the value of ch depends on the compiler or it is the same for all C compilers.?

User is offlineProfile CardPM
+Quote Post

SueJ
RE: Variable Type Conversion
23 Nov, 2007 - 03:39 PM
Post #2

New D.I.C Head
*

Joined: 23 Nov, 2007
Posts: 3


My Contributions
QUOTE(raedbenz @ 23 Nov, 2007 - 04:20 PM) *

Hi..if i have this piece of code in C language...
Note: i dont want to use casting

e.g.

int num=0xABCD;
char ch;

ch=num;

what is the value of ch ??
is the value of ch depends on the compiler or it is the same for all C compilers.?


It is the same for all compilers
User is offlineProfile CardPM
+Quote Post

raedbenz
RE: Variable Type Conversion
23 Nov, 2007 - 06:13 PM
Post #3

New D.I.C Head
*

Joined: 13 Dec, 2006
Posts: 19


My Contributions
But what is the value of ch ?
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Variable Type Conversion
23 Nov, 2007 - 06:46 PM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
This is actually an undefined case, so it's implementation dependent (though this may depend on the standard being used - I'm not completely sure).

0xABCD has a decimal value of 43981, which exceeds the maximum value possible in an 8-bit (single byte) char (usually -128 to +127, or 0 to 255). Generally, in type overflow problems such as this, the number either gets (repeatedly) wrapped around starting at the smallest possible value of a char, or the number gets assigned some implementation-specific value, such as MAX_CHAR.

However, the bigger issue here is that whether a char is signed or unsigned by default is also implementation dependent. So even if there was a standard way of handling overflow in the char datatype, you still can't know what the value is a priori, because the minimum/maximum value for a char (without explicit signing) is implementation dependent.

In short, throw your statements in a main() function with an output call, compile it, and see for yourself on the target machine. If you're distributing source instead of compiled executables, then you can't rely on consistent behavior from implementation-dependent cases.

*(edit - gcc 3.4.2 on windows wraps overflowed chars, and the default char signing is unsigned)

This post has been edited by jjhaag: 23 Nov, 2007 - 07:04 PM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Variable Type Conversion
23 Nov, 2007 - 07:14 PM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
it is an interesting question: 0xABCD = 43981 = 10101011 11001101b

So the value of 0xABDC needs at least 16 bits to be represented. So the next question is how many bits are in a char... well we were all taught that 1 byte is 8 bits... BUT, that is actually not entirely accurate. When it comes to C a char holds a single byte capable of carrying a single character in local character set. On one particular system I had a chance to work on that was 4 bits, this was often 7 bits, on many modern systems (many mobile phones for example) a byte is 32 bits.

One of the rather irritating things about C is that it does not really DEFINE the size of any of its integer data types. This can be very irritating when you expect a data type to be of a given size: a very common error even among people who should know better...
User is online!Profile CardPM
+Quote Post

raedbenz
RE: Variable Type Conversion
24 Nov, 2007 - 02:58 AM
Post #6

New D.I.C Head
*

Joined: 13 Dec, 2006
Posts: 19


My Contributions
HI...I have figured out sthg...

usually when we assign an integer to char , the char will take the lower byte(e.g. in our case is 0xDC), but as long as char's value is up to 128, and the value of 0xDC=220 is larger than 128, then the compiler (I use microsoft visual C++) will behave strangely..
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:25PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month