-5
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?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Kiran
  • 21
  • 1

3 Answers3

5
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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
3

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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    Not using those two forms of main (for a hosted system) is not undefined, it is implementation-defined behavior. – Lundin Jul 06 '15 at 07:33
  • 1
    Changed it blindly: can't verify right now - in a meeting in which I should be paying attention. – Bathsheba Jul 06 '15 at 07:34
2

First of all,

void main(i)
{
    printf("%d",i);
}

is invalid syntax in C, you should not use it, because, C standard says

  1. Case 1: [C11, §5.1.2.2.1 ], in a hosted environment,

    • main() should return int
    • it shall take either 0 (void) or 2 (int argc, char*argv[] or equivalent) arguments.
  2. Case 2: In a freestanding environment,

    • it is invalid (as per the latest standard), because the "variable type default to 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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    It is invalid because implicit int has been removed from the language. It is not invalid for the reasons stated here. Furthermore you quote the _hosted_ implementation chapter, nothing in the question indicates that this is a hosted system. – Lundin Jul 06 '15 at 07:46
  • @Lundin Thanks for the correction. Will update accordingly. Also sir, in freestanding the `main()` itself needs not to be named `main()`, isn't it? – Sourav Ghosh Jul 06 '15 at 07:49
  • Nope, it could be named anything. – Lundin Jul 06 '15 at 07:50
  • 1
    Problem is, C99 and C11 have the clause "or in some other implementation-defined manner" which could be interpreted in two ways: either that the arguments of main() are allowed to differ (I made that interpretation [here](http://stackoverflow.com/questions/5296163/why-is-the-type-of-the-main-function-in-c-and-c-left-to-the-user-to-define/5296593#5296593)) or that the whole form of main might be different (such as for example Windows' WinMain function). – Lundin Jul 06 '15 at 08:09
  • @Lundin Absolutely right sir. Just my personal opining, till date I used to read it like _"....and with no parameters....or with two parameters...or in some other implementation-defined manner"_, so I believed that it is talking about the parameters only, but now I see your point. it appears ambiguous. – Sourav Ghosh Jul 06 '15 at 08:49
  • When i tried it with the gcc compiler it is giving 1 as output – Kunal Saini Jul 06 '15 at 12:51