By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

Type conversion question

P: n/a
Hi,
I am totally confused now about C type conversion.

I know that C does some implicit type conversion like integer
promotion and float to double. I imagine that such conversion must
keep the value through, meaning that the converted value equals to
their originals.

But the following code blew me away:

int main(){
float ff = 42.7;
double dd = ff;
double dd1 = ff + 1.0;
printf("ff=%f\ndd=%d\ndd1=%d\n", ff, dd, dd1);

return 0;
}

I had imagined the output is
ff=42.7
dd=42.7
dd1=43.7

But what I see is
ff=42.700001
dd=-1610612736
dd1=1078286745

Then I coded this test:

int main(){
int ii = 7;
char cc = (char) ii;
printf("cc=%c\n", cc);

cc = 7;
printf("cc=%i\n", cc);

union {
char c;
int i;
long l;
} u;

u.i = 7;
printf("u.c=%c\n", u.c);
printf("u.l=%li\n", u.l);

u.c = (char) u.i;
printf("u.c=%c\n", u.c);
printf("u.l=%li\n", u.l);
return 0;
}
I see this result:

cc=
cc=7
u.c=
u.l=7
u.c=
u.l=7

This is as I expected, but since I was so off by the first test,
could you confirm there is no compiler/OS dependent thing in the 2nd
test -- it should always output that result?

And how about the 1st test?

Aug 15 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
123098...@gmail.com wrote:
Hi,
I am totally confused now about C type conversion.

I know that C does some implicit type conversion like integer
promotion and float to double. I imagine that such conversion must
keep the value through, meaning that the converted value equals to
their originals.

But the following code blew me away:

int main(){
float ff = 42.7;
double dd = ff;
double dd1 = ff + 1.0;
printf("ff=%f\ndd=%d\ndd1=%d\n", ff, dd, dd1);
What is the format specifier for double values?

Aug 16 '07 #2

P: n/a
On Wed, 15 Aug 2007 16:35:22 -0700, 1230987za wrote:
<Snip>
But the following code blew me away:

int main(){
float ff = 42.7;
double dd = ff;
double dd1 = ff + 1.0;
printf("ff=%f\ndd=%d\ndd1=%d\n", ff, dd, dd1);

return 0;
}

I had imagined the output is
ff=42.7
dd=42.7
dd1=43.7

But what I see is
ff=42.700001
42.7 is not exactly representable as a fp number.
dd=-1610612736
dd1=1078286745
Check your printf format specifiers (%d is not what you think)!
Then I coded this test:
<Snip>
union {
char c;
int i;
long l;
} u;

u.i = 7;
printf("u.c=%c\n", u.c);
printf("u.l=%li\n", u.l);

u.c = (char) u.i;
printf("u.c=%c\n", u.c);
printf("u.l=%li\n", u.l);
return 0;
}
I see this result:

cc=
cc=7
u.c=
u.l=7
u.c=
u.l=7

This is as I expected, but since I was so off by the first test,
could you confirm there is no compiler/OS dependent thing in the 2nd
test -- it should always output that result?
Again check the definitions of your printf format specifiers, I don't think
%c does quite what you expect.

Also I think the type punning in the union is either implementation or
undefined behaviour, but I don't have a copy of the standard to hand to
check.

HTH.

Regards, Dan.
--
Nil coitus alto - seen above the door to a 11KV switchroom.
Aug 16 '07 #3

P: n/a
12*******@gmail.com writes:
Hi,
I am totally confused now about C type conversion.
No, you are confused about printf formats (and some other stuff).

<snip>
int main(){
float ff = 42.7;
double dd = ff;
double dd1 = ff + 1.0;
printf("ff=%f\ndd=%d\ndd1=%d\n", ff, dd, dd1);

return 0;
}

I had imagined the output is
ff=42.7
dd=42.7
dd1=43.7

But what I see is
ff=42.700001
dd=-1610612736
dd1=1078286745
%d expects an int. You *must* include a prototype for printf or your
program exhibits the dreaded "undefined behaviour". Executive
translation: #include <stdio.hat the top or your program is wrong.
Then I coded this test:
(BTW, This is not a good way to learn. There are books that just tell
you what various things in C do.)
int main(){
int ii = 7;
char cc = (char) ii;
printf("cc=%c\n", cc);

cc = 7;
printf("cc=%i\n", cc);

union {
char c;
int i;
long l;
} u;

u.i = 7;
printf("u.c=%c\n", u.c);
Undefined behaviour again. You've not been lurking have you? Only a
few moments ago there was a thread explaining that you can only
retrieve the value from that last member you assign to (u.i here).

There are portable ways to find out how things are represented in
memory. In fact, another recent thread discussed just that.
printf("u.l=%li\n", u.l);

u.c = (char) u.i;
printf("u.c=%c\n", u.c);
printf("u.l=%li\n", u.l);
return 0;
}
I see this result:

cc=
cc=7
u.c=
u.l=7
u.c=
u.l=7

This is as I expected, but since I was so off by the first test,
could you confirm there is no compiler/OS dependent thing in the 2nd
test -- it should always output that result?
What character is denoted by the integer value 7 is system dependant.
The other stuff is not permitted by the standard C so, technically,
anything can happen.

--
Ben.
Aug 16 '07 #4

P: n/a
On Aug 15, 6:00 pm, "christian.bau" <christian....@cbau.wanadoo.co.uk>
wrote:
123098...@gmail.com wrote:
Hi,
I am totally confused now about C type conversion.
I know that C does some implicit type conversion like integer
promotion and float to double. I imagine that such conversion must
keep the value through, meaning that the converted value equals to
their originals.
But the following code blew me away:
int main(){
float ff = 42.7;
double dd = ff;
double dd1 = ff + 1.0;
printf("ff=%f\ndd=%d\ndd1=%d\n", ff, dd, dd1);

What is the format specifier for double values?
Sorry, my fault.

Please discard this post. I do have another question, will post
seperately.

Aug 16 '07 #5

P: n/a
12*******@gmail.com wrote:
Hi,
I am totally confused now about C type conversion.

I know that C does some implicit type conversion like integer
promotion and float to double. I imagine that such conversion must
keep the value through, meaning that the converted value equals to
their originals.

But the following code blew me away:

int main(){
float ff = 42.7;
double dd = ff;
double dd1 = ff + 1.0;
printf("ff=%f\ndd=%d\ndd1=%d\n", ff, dd, dd1);
^^^^^ dd is not an int
^^^ dd1 is not an int
If you lie to printf, then you deserve the garbage it
will produce based on that lie.
>
return 0;
}

I had imagined the output is
ff=42.7
dd=42.7
dd1=43.7
Why in heave's name would you think that printf given the specifier %d,
which is for integers would
a) do anything "reasonable" with a floating point value?
b) print something that is impossible if is printing an integer?
Aug 16 '07 #6

P: n/a
Martin Ambuhl <ma*****@earthlink.netwrites:
12*******@gmail.com wrote:
>Hi,
I am totally confused now about C type conversion.

I know that C does some implicit type conversion like integer
promotion and float to double. I imagine that such conversion must
keep the value through, meaning that the converted value equals to
their originals.

But the following code blew me away:

int main(){
float ff = 42.7;
double dd = ff;
double dd1 = ff + 1.0;
printf("ff=%f\ndd=%d\ndd1=%d\n", ff, dd, dd1);
^^^^^ dd is not an int
^^^ dd1 is not an int
If you lie to printf, then you deserve the garbage it
will produce based on that lie.
>>
return 0;
}

I had imagined the output is
ff=42.7
dd=42.7
dd1=43.7

Why in heave's name would you think that printf given the specifier
%d, which is for integers would
a) do anything "reasonable" with a floating point value?
Quite obvious really. A lot of people MIGHT make the reasonable
assumption that the float would cast to an int and only the integer part
used.

e.g 3.4 -3

I don't think that is as ridiculous an assumption for a nOOb as you make
it sound.
b) print something that is impossible if is printing an integer?
That makes no sense.
Aug 16 '07 #7

P: n/a
Richard <rg****@gmail.comwrote:
Martin Ambuhl <ma*****@earthlink.netwrites:
12*******@gmail.com wrote:
float ff = 42.7;
double dd = ff;
double dd1 = ff + 1.0;
printf("ff=%f\ndd=%d\ndd1=%d\n", ff, dd, dd1);
I had imagined the output is
ff=42.7
dd=42.7
dd1=43.7
Why in heave's name would you think that printf given the specifier
%d, which is for integers would
a) do anything "reasonable" with a floating point value?

Quite obvious really. A lot of people MIGHT make the reasonable
assumption that the float would cast to an int and only the integer part
used.
For a cast, which is an explicit conversion, that would indeed be the
reasonable assumption - and it would be correct. But nowhere in the
above is there any cast, and there isn't even a conversion, neither
explicit nor implicit.
The only bit in the above which is even slightly surprising to a newbie
is that variadic functions do not automagically (try to) convert their
arguments to the desired type, as properly declared normal functions do;
but a moment's thought should make it clear that, in the absence of a
full declaration that can tell the implementation what kind of arguments
a variadic function expects, there is no magic that it _can_ do in such
cases, no more than for normal functions which are not fully declared.

Richard
Aug 16 '07 #8

P: n/a
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Richard <rg****@gmail.comwrote:
>Martin Ambuhl <ma*****@earthlink.netwrites:
12*******@gmail.com wrote:
> float ff = 42.7;
double dd = ff;
double dd1 = ff + 1.0;
printf("ff=%f\ndd=%d\ndd1=%d\n", ff, dd, dd1);
> I had imagined the output is
ff=42.7
dd=42.7
dd1=43.7

Why in heave's name would you think that printf given the specifier
%d, which is for integers would
a) do anything "reasonable" with a floating point value?

Quite obvious really. A lot of people MIGHT make the reasonable
assumption that the float would cast to an int and only the integer part
used.

For a cast, which is an explicit conversion, that would indeed be the
reasonable assumption - and it would be correct. But nowhere in the
above is there any cast, and there isn't even a conversion, neither
explicit nor implicit.
"would cast" - implicit.
The only bit in the above which is even slightly surprising to a newbie
is that variadic functions do not automagically (try to) convert their
Most newbies don't know what a variadic function is.
arguments to the desired type, as properly declared normal functions do;
but a moment's thought should make it clear that, in the absence of a
full declaration that can tell the implementation what kind of arguments
a variadic function expects, there is no magic that it _can_ do in such
cases, no more than for normal functions which are not fully declared.
I probably don't understand you, but I dont think I agree. It seems
relatively "obvious" (if incorrect) that some people would assume the
integer part is used.
>
Richard
--
Aug 16 '07 #9

P: n/a
Richard <rg****@gmail.comwrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Richard <rg****@gmail.comwrote:
Martin Ambuhl <ma*****@earthlink.netwrites:

12*******@gmail.com wrote:
float ff = 42.7;
double dd = ff;
double dd1 = ff + 1.0;
printf("ff=%f\ndd=%d\ndd1=%d\n", ff, dd, dd1);
I had imagined the output is
ff=42.7
dd=42.7
dd1=43.7

Why in heave's name would you think that printf given the specifier
%d, which is for integers would
a) do anything "reasonable" with a floating point value?

Quite obvious really. A lot of people MIGHT make the reasonable
assumption that the float would cast to an int and only the integer part
used.
For a cast, which is an explicit conversion, that would indeed be the
reasonable assumption - and it would be correct. But nowhere in the
above is there any cast, and there isn't even a conversion, neither
explicit nor implicit.

"would cast" - implicit.
Casts are explicit. A cast is something like this: (type). Conversions
can be implicit or explicit, but a cast is a piece of code.
The only bit in the above which is even slightly surprising to a newbie
is that variadic functions do not automagically (try to) convert their

Most newbies don't know what a variadic function is.
If someone isn't struck by the fact that some functions can be called
with a variable number of arguments, while most functions cannot, that
someone should not be a programmer. He doesn't have the natural
curiosity that is needed to solve problems.

Richard
Aug 16 '07 #10

P: n/a
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Richard <rg****@gmail.comwrote:
>rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Richard <rg****@gmail.comwrote:

Martin Ambuhl <ma*****@earthlink.netwrites:

12*******@gmail.com wrote:

float ff = 42.7;
double dd = ff;
double dd1 = ff + 1.0;
printf("ff=%f\ndd=%d\ndd1=%d\n", ff, dd, dd1);

I had imagined the output is
ff=42.7
dd=42.7
dd1=43.7

Why in heave's name would you think that printf given the specifier
%d, which is for integers would
a) do anything "reasonable" with a floating point value?

Quite obvious really. A lot of people MIGHT make the reasonable
assumption that the float would cast to an int and only the integer part
used.

For a cast, which is an explicit conversion, that would indeed be the
reasonable assumption - and it would be correct. But nowhere in the
above is there any cast, and there isn't even a conversion, neither
explicit nor implicit.

"would cast" - implicit.

Casts are explicit. A cast is something like this: (type). Conversions
can be implicit or explicit, but a cast is a piece of code.
Yes. I know. What part of "newbies assuming" seems to allude you?
>
The only bit in the above which is even slightly surprising to a newbie
is that variadic functions do not automagically (try to) convert their

Most newbies don't know what a variadic function is.

If someone isn't struck by the fact that some functions can be called
with a variable number of arguments, while most functions cannot, that
someone should not be a programmer. He doesn't have the natural
curiosity that is needed to solve problems.
Err, right.
>
Richard
--
Aug 16 '07 #11

P: n/a
Richard <rg****@gmail.comwrites:
Martin Ambuhl <ma*****@earthlink.netwrites:
[...]
>Why in heave's name would you think that printf given the specifier
%d, which is for integers would
a) do anything "reasonable" with a floating point value?

Quite obvious really. A lot of people MIGHT make the reasonable
assumption that the float would cast to an int and only the integer part
used.
[...]

My guess is that the OP thought "%d" is for double. Note that he used
"%i" for int (and "%f" for float). It's a silly misconception, but
easily corrected.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 16 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.