
Originally Posted by
Vespasian_2
So on your system they both print the same?
I'm using GCC, codeblocks IDE.
Yes. Of course they do. And you haven't even mentioned your operating system. I assume it's windows since you seem happy not to put newlines at the end of your input, which on linux causes the prompt to appear on the same line right after the output. But windows outputs it's own newline so you may not be seeing that annoying effect.
printf never, ever sees a float. It is impossible to pass printf a float. If you try to pass it a float, the float will be promoted to a double before it is passed. That's the reason that %f is a synonym for %lf for printf (but, crucially, not for scanf!).
Additionally, if you try to pass a char or a short, they will both be promoted to ints. This is not specific to printf (i.e., it's not a magical function), but is instead specific to variable parameter lists (the ... syntax). We can make our own function with this behavior:
Code:
#include <stdio.h>
#include <stdarg.h>
void func(int n, ...) {
va_list va;
va_start(va, n);
// It is invalid to say va_arg(va, float)! You must use double.
while (n-- > 0) printf("%f ", va_arg(va, double));
putchar('\n');
va_end(va);
}
int main() {
float f = 123.45f;
double d = 123.45;
func(1, f);
func(1, d);
func(2, f, d);
return 0;
}
Try copying and pasting the following code into a new "project" (or whatever it's called) and running it. What are your results?
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("%lf\n", strtod("123.023", NULL)); // if you're not using the end pointer, you can just pass NULL
return 0;
}
EDIT: Now that I think about it (and remember!) the old (C89/90) standard didn't have the %lf format spec. So if you are compiling to that standard (which requires adding -std=c89 or -std=c90 to the gcc flags) then maybe that could cause a problem, although it's much more likely to spit out a warning and still work properly. Remember that to get full warnings you need three flags: -Wall -W -pedantic. (-W is a synonym for -Wextra)