473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

conversion of longer int to shorter ones

Hi,

Consider following piece of code:

int i = 0x12345678;
char c;
c = i;
printf("0x%x\n",c);

What value will be printed ?
As per K&R, longer integers are converted to shorter ones by dropping
the higher order bits. So, value printed should be "0x78".

However, my question is that can this value be different on machines
with different endianness ?

thanks a lot for any help ...

Jan 12 '07 #1
2 1444
ju**********@yahoo.co.in wrote:
Hi,

Consider following piece of code:

int i = 0x12345678;
char c;
c = i;
printf("0x%x\n",c);

What value will be printed ?
As per K&R, longer integers are converted to shorter ones by dropping
the higher order bits. So, value printed should be "0x78".
In standard C, this is only true for unsigned integer types. On many
implementations this is also true for signed integer types, but if
possible, don't rely on it.
However, my question is that can this value be different on machines
with different endianness ?
It is not likely to depend on endianness. If both types are declared as
unsigned, and if the size of the types is what you expect they are
(you're assuming an int is large enough to store 0x12345678, and that
char only has 8 bits), then it would print 0x78 no matter how the value
is stored in memory. However, with signed types, "either the result is
implementation-defined or an implementation-defined signal is raised".
The output could be 0x78. But it's just as possible to have 0xFF
assigned to c, or 0, or to have the program abort.

Jan 12 '07 #2
"ju**********@yahoo.co.in" wrote:
>
Consider following piece of code:

int i = 0x12345678;
char c;
c = i;
printf("0x%x\n",c);

What value will be printed ?
As per K&R, longer integers are converted to shorter ones by dropping
the higher order bits. So, value printed should be "0x78".
Provided that CHAR_BIT is 8.
>
However, my question is that can this value be different on machines
with different endianness ?
No.

--
"I have a creative mind. You (singular) are eccentric.
He is insane. We are losing sight of reality.
You (plural) are smoking crack. They are certifiable."
Declension of verbs, per Lewin Edwards
Jan 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: weasel | last post by:
Why is the Farenheit to Celsius part not working properly? Instead of showing a similar range of what the farenheit is listing, the celsius portion is showing half the range of farenheit. print...
8
by: Perception | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
11
by: Peter Oliphant | last post by:
I've been trying all morning to convert my 2003 project (managed) to 2005 (/clr since I have both managed and unmanaged code). I'm guessing I have tens of thousands of lines of code to change. Did...
3
by: Pradeep Kurra | last post by:
I have a string stored as HEX in my database. I want to convert it to string. How can i do that. Example: equivalent of "56454849434C4520312057415320494E2054484520" would be something like...
7
by: guy | last post by:
Has anyone any experience of the conversion wizard for VB6 to VB2005? if so how good is it? also how does it handle database related conversions i.e is ADO converted to ADO.NET etc. the project...
15
by: Mik0b0 | last post by:
Hallo everybody, my problem is: there are two single-dimension arrays, longer and shorter, every array is organized in ascending order. We need to build a new array out of two. This is what I...
4
by: Stephen | last post by:
Hello all. I have some LONG hex strings (between 20 and 40 bytes) that I would like to convert to binary. I have found a few options but they only allow for short hex strings, the ones I have...
0
by: maheshmohta | last post by:
Background Often while remodeling legacy application, one of the important tasks for the architects is to have an optimum usage of storage capabilities of database. Most of the legacy applications...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.