-4
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main(main) {
    clrscr();
    printf("%d",main +=pow(++main,++main));
    getch();
}

So, i have run the above code in turboC compiler n got the output 12. i have noticed here the value of main is always initialised with 1. so i want to know why its value is 1? why not anything else?? please anyone help me out :)

Note: i also tried to change the name of parameter "main" with something else like x,y etc.. the value still 1 and also output also same i.e 12.

  • How do you know `main` is initialized with `1`? Your code doesn't print `main`. – melpomene May 19 '19 at 18:43
  • 4
    This code is seriously broken for a lot of reasons, but the answer ignoring all of that is that it’s taking on the usual value of the first `main` parameter when you have a correct signature `int main(int argc, char* argv[])`. – Ry- May 19 '19 at 18:44
  • 1
    Unrelated: *"i have run the above code in turboC compiler"* - That toolchain is literally at least a decade older than *you are*. The only reason its still used in academia is because it runs on archaic hardware that is, likewise, just as old (and thus cheap). I sincerely hope the offerings of modern, *free* toolchains like clang and/or gcc someday find their way to your desktop. – WhozCraig May 19 '19 at 18:47
  • 4
    Doing x+=pow(++x, ++x) is undefined behavior, you don't need to investigate since the result may vary depending on too many factors (compiler...) –  May 19 '19 at 18:47
  • regarding: `void main(main) {` This is NOT a valid signature for `main()` There are two valid signatures: `int main( void )` and `int main( int argc, char *argv[] )` The posted signature for `main()` does not match either signature – user3629249 May 20 '19 at 01:17
  • @melpomene atleast try to run it . main is not a keyword bro. we can use it as a identifier although it is not a good practice. – Saurabh Agrawal May 20 '19 at 20:00
  • @Ry- dude the code is all correct . it's just that you didn't understand that. and reason why am i asking here is that this question asked by Microsoft in interview. – Saurabh Agrawal May 20 '19 at 20:02
  • @Nico238 thats why i mentioned turbo C compiler ;) – Saurabh Agrawal May 20 '19 at 20:03
  • @user3629249 i know that was not a valid signature. that question was asked in the interview thats the reason why i am asking here – Saurabh Agrawal May 20 '19 at 20:06
  • @SaurabhAgrawal I never said anything about `main` being a keyword. I asked you a simple question. Also, Ry- is correct that the code is very broken. – melpomene May 20 '19 at 20:08
  • I'm not going to download random executables from the internet and run them on my computer. Also, could you answer my question, please? – melpomene May 20 '19 at 20:16
  • @melpomene ok bro no problem. by the way which question you are talking abt? – Saurabh Agrawal May 20 '19 at 20:18
  • The very first sentence in the very first comment: "*How do you know `main` is initialized with `1`?*" – melpomene May 20 '19 at 20:19
  • @melpomene okay. here main acts as a argc parameter . since no parameter is passed in the program thats why it is by default initialised with 1 as Eric mentioned in the answer. – Saurabh Agrawal May 20 '19 at 20:21

1 Answers1

2

In strictly conforming C code, main should be declared as one of the following or an equivalent:

int main(void)

int main(int argc, char *argv[])

When main is declared with the latter, argv contains pointers to (the first characters of) strings, and argc contains the number of such pointers in argv. argv[0] represents the program name, and the following elements are parameters to the program.

Your compiler is accepting a very old syntax in which the type may be omitted in a declaration. So int main(x) declares x to be a parameter of type int. This is not strictly conforming C, but your compiler is accepting it.

When you run the program with no arguments, it is passed only the program name, in argv[0]. Since there is just the program name and no arguments, argc is set to 1.

Using main for the parameter name, as in int main(main), is bad practice, but it has no special effects except that it causes the identifier “main” inside the function to refer to the parameter and not to the function.

melpomene
  • 84,125
  • 8
  • 85
  • 148
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312