hi,
I went through following program in a C puzzle book.
#include<stdio.h>
#define PR(x) printf("x=%.8g\t",(double)x);
#define NL putchar('\n');
#define PRINT4(x1,x2,x3,x4) PR(x1) PR(x2) PR(x3) PR(x4) NL
main()
{
double d;
float f;
long l;
int i;
i=l=f=d=100/3; PRINT4(i,l,f,d)
d=f=l=i=100/3; PRINT4(i,l,f,d)
i=l=f=d=100/3.; PRINT4(i,l,f,d)
d=f=l=i=(double)100/3; PRINT4(i,l,f,d)
i=l=f=d=(double)100000/3; PRINT4(i,l,f,d)
d=f=l=i=100000/3; PRINT4(i,l,f,d)
}
answer is also given there but i am not getting it.
Can some body please explain output of above program..
Some basics:
1. If both operands of the / operator are integer types, then the
result of the division is also an integer type; e.g., 5/2 == 2, 5/3 ==
1, 5/4 == 1, etc.
2. If the operands of the / operator are different types (for
example, float and int), then the value of the operand that is of
lower rank (shorter or less precise) is converted to the type of the
other operand before doing the division, and the result is the same
type as the operand of higher rank; e.g., int / long == long, int /
float == float, float / double == double, etc.
3. If the left hand side and the right hand side of the assignment
operator (=) are of different types, then the value of the right hand
side is converted to the type of the left hand side (modulo some
restrictions, such as that the target type must be wide enough able to
hold the converted value). If you're assigning an int value to a
double variable, the value will first be converted to double before
the assignment. Similarly, if you're trying to assign a double value
to an int variable, the value will be converted to an integer (by
truncating the fractional portion) before assignment.
4. The assignment operator is right-associative; that is, the
expression a=b=c is evaluated as a=(b=c); in other words, a is
assigned the result of b=c.
So let's look at the statement
i=l=f=d=100/3;
The expression 100/3 evaluates to an integer value, 33. This result
is first assigned to d. Since d is of type double, the integer value
33 is first converted to double (33.0). The result of d=100/3 (33.0)
is then assigned to f, converting the double value 33.0 to the float
value 33.0. This result (f=d=100/3) is then assigned to l, meaning
the float value 33.0 must first be converted to the long integer value
33. Finally, i is assigned the result of (l=f=d=100/3), after
converting the long value 33 to an int.
The second statement does pretty much the same thing, just changing
the order of assignment.
The third statement,
i=l=f=d=100/3.;
gives a different result because 3. is shorthand for 3.0, meaning that
3. is a floating-point value. Thus the result of 100/3. is
33.33333.... This is what gets assigned to d and f (taking the
conversions into account). l and i still get assigned 33, because
that's the result after converting the floating point value to an
integer.
Please also explain % .8g format specifier....
This is what your handy C reference manual is for. Basically, it
works the same as the f and e conversion specifiers, except that it
strips any trailing zeros, and if as a result there are no digits
after the decimal point, then the decimal point is stripped as well.