473,396 Members | 1,760 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.

Type conversion question

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
11 1497
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Nitin Bhardwaj | last post by:
Hi all, It is said that C++ is a strongly typed language and thus a type-safe language (unlike C). So how does one explain the following behaviour : int main(void) { char *p = NULL; p = "A...
8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
4
by: Master of C++ | last post by:
Hi, This is a simple question. In the following example, .. class Vector .. { .. private: .. int *Numbers; .. int vLength; ..
14
by: tweak | last post by:
I'm struggling with the concept of typecasting when setting up a TCP client. Here's a code snip (modified from W. Richard Stevens Unix Programming book) to demonstrate where I am struggling: ...
19
by: Randy Yates | last post by:
Consider the following code: #include "dsptypes.h" /* definitions */ #define VECTOR_LENGTH 64 /* local variables */
7
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the...
1
by: lovecreatesbeauty | last post by:
There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming...
6
by: NM | last post by:
I am given an int and have to tell whether the bit representation of that int is a palindrome or not. Here is what I have come up with bool isIntPalindrome(int num) { unsigned int temp = num;...
4
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) :...
8
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.