[cfe-dev] Default argument type for function without prototype?
Kirill Balunov via cfe-dev
cfe-dev at lists.llvm.org
Sat Jan 20 02:21:23 PST 2018
Hi,
I am a beginner to C and I previously asked this question on SO (and it was
marked as duplicate :-) ) I still do not understand the problem to the end. I
understand the need of function prototypes and the default argument
promotion machinery. But I am still confused with the obtained warning and
final result for this example:
#include <stdio.h>
void impl();
int main(void){
impl(3.0);
return 0;
}
void impl(val){
printf("%.2f", val);
}
I get the following warning: *format specifies type 'double' but the
argument has type 'int' [-Wformat].*I can not understand why it is supposed
by a compiler that *val* should be treated as *int* and not to skip the
number and types of arguments and then perform default argument promotion
at run time. Also I read that if you don't provide argument types in ANSI C:
> An ANSI C compiler will assume that you have decided to forego function
> prototyping, and it will not check arguments.
The result which is printed to the console is *0.00*, but as I understood
the caller should place double on the stack and *%f* also means double in
*printf* so it reads double off the stack, and the result should not be
affected.
So I'm still confused what is happening here. Also I can't find where it is
stated that the argument without a type should be treated by a compiler as
type *int*. I understand that the question is stupid in nature, for this I
apologize in advance, but it somehow sat down in my head and I nowhere can
I find why it happens. Thank you!
p.s.: Compiled with *clang 6.0* and *--std=c89* flag under Windows 10.
Apologies if this question is off-topic for this list, but i can't find any
other.
With kind regards,
-gdg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180120/d920d445/attachment.html>
More information about the cfe-dev
mailing list