By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,953 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,953 IT Pros & Developers. It's quick & easy.

basic C code question, what does this in code mean %f,%f\n

P: 12
what does this in code mean %f,%f\n
Mar 9 '16 #1

✓ answered by donbock

Expand|Select|Wrap|Line Numbers
  1. f = f + j/i;
By the order of operations, the first thing that happens is that j is divided by i, then that result is added to f.
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.

Share this Question
Share on Google+
6 Replies


Expert 100+
P: 2,396
This is not code. It is probably a format string passed as an argument to some function. The meaning of the format string depends on precisely which function it is being passed to. Likely functions are variants of fprintf or fscanf.
Mar 10 '16 #2

P: 12
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?
Mar 10 '16 #3

Expert 100+
P: 2,396
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.
Mar 10 '16 #4

Expert 100+
P: 2,396
Expand|Select|Wrap|Line Numbers
  1. f = f + j/i;
By the order of operations, the first thing that happens is that j is divided by i, then that result is added to f.
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.
Mar 10 '16 #5

P: 12
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!
Mar 10 '16 #6

P: 3
"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 rounded
Apr 19 '17 #7

Post your reply

Sign in to post your reply or Sign up for a free account.