while studying a quine program in C, I found that, main was passed with just a
, there is no datatype. The below runs fine and outputs the program correctly.
main(a){printf(a="main(a){printf(a=%c%s%c,34,a,34);}",34,a,34);}
I would like to know, how does this work (not the actual quine program), but what is data-type of a
? What value is it getting?
First, let me tell you, considering a hosted environment, the above code is non-standard and very bad way of coding, too. You should not write code like that.
This code, make use of the "default-to-int" property of legacy C. This property has been removed from the standard since C99
. Compilers might be supporting and accepting this for legacy reasons.
Most probably, it used to get the value similar to that of argc
.
There is a difference between functions in general and main(), which is a special case.
For normal functions, the syntax you use would work in obsolete versions of C, where the types would be treated as "implicit int" and your function would become int func (int);
. This nonsense feature was removed from the language 16 years ago and such programs will no longer compile.
As for the form of main(), there are some special cases, all described in detail with references in this answer.
TL;DR of that answer:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With