what does this in code mean %f,%f\n
basic C code question, what does this in code mean %f,%f\n
Collapse
X
-
Tags: None
-
here is the full code. I need to know why F = 3 after the code is ran. This is all new to me and some help is greatly appreciated. Thanks in advance.
#include <stdio.h>
int main(void) {
int i,j;
float f,g;
i = 5; j = 2;
f = 3.0;
f = f + j / i;
g = (f + j )/i;
printf("value of f,g is %f,%f\n", f,g);
return 0;
}
output : value of f,g is 3.00000, 1.00000
Why is does F = 3?Comment
-
The format string has nothing to do with why f is 3, but this is a tricky area that could cause you trouble in the future.
You should always provide a prototype before a function is defined or referenced. This insures that only arguments of the expected type are passed to the function. A common source of bugs in the old pre-prototype days was mismatched argument types between function call and function definition. Prototypes have virtually eliminated this kind of bug ... except for printf-style functions.
There is no complete prototype for printf. This is a varargs function: the number of arguments and the types of the arguments can vary from one call to another. The format string tells printf how many arguments to expect, and what each argument's type is. The C Standard does not require compilers to verify that a printf format string matches the arguments, so argument mismatch is a possibility that must be guarded against.
Format string characters %f signify that the corresponding argument is a double; so your function string calls for two double arguments to follow the format string. However, arguments f and g are floats! Why did the program work? It worked because in the absence of a complete prototype, the compiler applies the usual argument conversions -- one of which is that floats are converted to doubles before being passed to the function.
Whew! That was close.Comment
-
Code:f = f + j/i;
j and i are both ints, so the compiler uses integer arithmetic: 2/5 is 0 using integer arithmetic. Adding integer 0 to floating-point f causes 0 to be converted to floating-point 0.0 and floating-point arithmetic to be used, but adding 0.0 to f doesn't change it, so f is still 3.0.Comment
-
Thanks so much DON! You and your explanation haves been a great help to me. This class I'm taking is a tough one and I'm sure it will bring me back to this site quite a bit this semester. I hope your around to clarify. Thanks!Comment
-
"print" treats the % as a special character you need to add, so it can know, that when you type "f", the number (result) that will be printed will be a floating point type, and the ".2" tells your "print" to print only the first 2 digits after the point.
%.2f" % total
% is a symbol for variable and is not a modulo!
. and the number fallowing modifies how many digit decimal points you want to print
%f string formatting option treats the value as a decimal, and prints it to six decimal places
the second % outside of the " " sets the variable total to the variable %.2f inside the " "
"%.4f" would print 54.6293
"%.2f" prints 54.63 roundedLast edited by Niheel; Apr 19 '17, 03:37 PM. Reason: please don't link spam, you can add links to resources with addtional specific information, but not broad marketing type links.Comment
Comment