459,491 Members | 1,204 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,491 IT Pros & Developers. It's quick & easy.

# 0 converted to 0.0 for float variable?

 P: n/a Newbie question: 0 converted to 0.0 for float variable? If I have the following code, float myTotal; myTotal = 0; will C automatically add a decimal point and at least one zero to the right of the decimal place? I am expecting that 0 will become at least 0.0 or maybe something with more zeros. Nov 8 '07 #1
6 Replies

 P: n/a vlsidesign wrote: Newbie question: 0 converted to 0.0 for float variable? If I have the following code, float myTotal; myTotal = 0; will C automatically add a decimal point and at least one zero to the right of the decimal place? I am expecting that 0 will become at least 0.0 or maybe something with more zeros. #include inline void pf(int n, float x) { printf("%%9.%df %9.*f; ", n, n, x); printf("%%9.%dg %9.*g; ", n, n, x); printf("%%9.%de %9.*e\n", n, n, x); } int main(void) { float myTotal = 0; int i; for (i = 0; i < 4; i++) pf(i, myTotal); putchar('\n'); myTotal = 1; for (i = 0; i < 4; i++) pf(i, myTotal); return 0; } %9.0f 0; %9.0g 0; %9.0e 0e+00 %9.1f 0.0; %9.1g 0; %9.1e 0.0e+00 %9.2f 0.00; %9.2g 0; %9.2e 0.00e+00 %9.3f 0.000; %9.3g 0; %9.3e 0.000e+00 %9.0f 1; %9.0g 1; %9.0e 1e+00 %9.1f 1.0; %9.1g 1; %9.1e 1.0e+00 %9.2f 1.00; %9.2g 1; %9.2e 1.00e+00 %9.3f 1.000; %9.3g 1; %9.3e 1.000e+00 Nov 8 '07 #2

 P: n/a vlsidesign wrote: Newbie question: 0 converted to 0.0 for float variable? If I have the following code, float myTotal; myTotal = 0; will C automatically add a decimal point and at least one zero to the right of the decimal place? I am expecting that 0 will become at least 0.0 or maybe something with more zeros. Almost good. 0 gets converted to (float)0, which indeed can be written as 0.0f, but that is not the same as adding extra zeros in the conversion. C works with values, that's all you need to know. Nov 8 '07 #3

 P: n/a vlsidesign wrote On 11/08/07 15:54,: Newbie question: 0 converted to 0.0 for float variable? If I have the following code, float myTotal; myTotal = 0; will C automatically add a decimal point and at least one zero to the right of the decimal place? I am expecting that 0 will become at least 0.0 or maybe something with more zeros. Strictly speaking, the statement involves a float variable and an int value. When you assign a value of one type to a variable of a different type, the value is converted to the type of the variable (if possible) as part of the process. So the statement says - obtain an int with the value zero - convert it to the equivalent float value - store that float value in myTotal In practice, most compilers will recognize that 0 is not just an int but an int whose value does not change, and therefore they will know that the conversion from int to float will always produce the same result. So they'll take a shortcut and generate code to do - obtain a float with the value zero - store that float value in myTotal Note that I'm being very careful to talk about types (int, float) and values (zero), not about how the values are represented in C source. The description above does not talk about adding decimal points or adding zero digits to something in your source code; it talks about what the computer does with the values and variables that your source code defines. You need to know what the various code constructs mean -- 0 and 0.0 and .0f and 0L and 0u are all zeroes, but all different -- but you should think of the program as working with the values, not with the source code that produced them. You'll be happier that way, I promise. -- Er*********@sun.com Nov 8 '07 #4

 P: n/a vlsidesign

 P: n/a On Thu, 08 Nov 2007 16:14:29 -0500, Martin Ambuhl vlsidesign wrote: >Newbie question: 0 converted to 0.0 for float variable?If I have the following code, float myTotal; myTotal = 0;will C automatically add a decimal point and at least one zero to theright of the decimal place? I am expecting that 0 will become at least0.0 or maybe something with more zeros. #include inline void pf(int n, float x) There should be an informal rule that states that code that uses the inline function specifier should also include at least a sprinkle of "//" comments :^) And anyone who uses the inline function specifier should insure that their compiler conforms to the current standard (in at least one specific area) by subjecting it to the following code (in a hosted environment): inline int main(void) { return 0; } Regards -- jay Nov 9 '07 #6

 P: n/a jaysome wrote: On Thu, 08 Nov 2007 16:14:29 -0500, Martin Ambuhl vlsidesign wrote: >>Newbie question: 0 converted to 0.0 for float variable?If I have the following code, float myTotal; myTotal = 0;will C automatically add a decimal point and at least one zero to theright of the decimal place? I am expecting that 0 will become at least0.0 or maybe something with more zeros. #include inline void pf(int n, float x) There should be an informal rule that states that code that uses the inline function specifier should also include at least a sprinkle of "//" comments :^) And anyone who uses the inline function specifier should insure that their compiler conforms to the current standard (in at least one specific area) by subjecting it to the following code (in a hosted environment): inline int main(void) { return 0; } Regards OOOOOPS!!! I just discovered I forgot that test. Thanks :-) -- jacob navia jacob at jacob point remcomp point fr logiciels/informatique http://www.cs.virginia.edu/~lcc-win32 Nov 9 '07 #7

### This discussion thread is closed

Replies have been disabled for this discussion.