void main(i)
{
printf("%d",i);
}
what is meaning of main(i)
here and how it works? and what is value and type of i
?
void main(i)
{
printf("%d",i);
}
what is meaning of main(i)
here and how it works? and what is value and type of i
?
void main(i)
Declaring a function parameter with no type is only valid in the old, obsolete C90 standard. In C90 i
would then default to type int
.
If this code was for a freestanding implementation (embedded system or OS), it would have been valid in C90. It would be equivalent to void main (int i)
. Your compiler is required to document what this form of main() is supposed to do.
If this code was for a hosted implementation (programming running on an OS), it is not valid and will not compile. C90 2.1.2.2 Hosted environment only allows two forms of main():
int main(void)
int main(int argc, char *argv[])
In newer C standards, the code will be invalid no matter if freestanding or hosted, as the "default to int" rule has been removed from the language.
It's possibly of type int
, and represents the number of arguments passed on the command line; including the name of the program.
But you should not write the main
prototype like that as formally the program behaviour is implementation-defined and so can vary from platform to platform.
Use int main(void)
or int main(int argc, char **argv)
instead.
First of all,
void main(i)
{
printf("%d",i);
}
is invalid syntax in C
, you should not use it, because, C
standard says
Case 1: [C11
, §5.1.2.2.1 ], in a hosted environment,
main()
should return int
void
) or 2 (int argc, char*argv[]
or equivalent) arguments.Case 2: In a freestanding environment,
int
" concept has been no longer supported by C
standard.[This is just for understanding, not supported any more in the standards and hence, the behaviour is not guaranteed.]
Now, coming to the meaning part for the above code, it is a hacky and obsolete way to provide the definition of i
in main()
, mainly used in code golfing to shorten the code size. The type of i
defaults to int
here, and holds the number of augments (including the program name) supplied to the program.
So, for example, if the program is run like
./test
in the program, i
is most likely to have a value of 1
.