Previous: 5.4 Operators and Expression Evaluation
Up: 5 Numeric Data Types and Expression Evaluation
Next: 5.6 Summary
Previous Page: 5.4.3 Some New Operators
Next Page: 5.6 Summary
/* File: default.c Program illustrates problems with default declarations for functions. */ #include <stdio.h> main() { float x;The function trunc_square() returns integer type and main() uses the default declaration for trunc_square(). The float argument, x in the function call in main() is converted to double. But trunc_square() declares a float formal parameter, z. An attempt will be made to access a double object as a float. The function may not access the correct value passed as an argument. Thus, it is always best to use function prototypes to avoid confusion.x = 3.0; printf("Truncated Square of %f = %d\n", x, trunc_square(x)); }
int trunc_square(float z) { return (int) (z * z); }
while (x = scanf("%d", &n) != EOF) ...Wrong! The scanf() value is compared first with EOF and the result of the comparison is assigned to x. Using parentheses:
while ((x = scanf("%d", &n)) != EOF) ...x is assigned the value returned by scanf(), and the value of x is then compared with EOF. Examples where associativity must be considered include:
a = 10; b = 5; c = 20; d = 4;a - b - c is -15 a / b / c / d is 0 a % d % b % c is 2