0

The following code 1 is fine

#include <stdio.h>    // code 1
main()
{
    printf("%u",main);
}

but this code 2 gives segmentation fault.

#include <stdio.h>  // code 2
main()
{
    printf("%u",main());
}

I'm not getting what's the difference between main and main()?

Bharat Kul Ratan
  • 985
  • 2
  • 12
  • 24
  • 11
    It's amazing that you found this site, as its name is so relevant to your question... – Greg Hewgill Jul 28 '12 at 07:39
  • That's because 'main' is the technically the address of the function, while main() is the function call. Though I have to ask: What's the return type of that main of yours? void? The segfault is actually, because you're causing an infinite loop there...you call main, which calls main, which calls main...at some point, your stack is full and you get: Who guessed it: A stack overflow! (Disguised as segmentation fault) – ATaylor Jul 28 '12 at 07:39
  • @ATaylor: the return type is `int`, because that's the default in C when not otherwise specified. – Greg Hewgill Jul 28 '12 at 07:40
  • 2
    Both pieces of code are wrong. The only difference is why. – Dietrich Epp Jul 28 '12 at 07:40
  • @GregHewgill That's what I thought, but shouldn't his compiler scream out for not including a return statement then? – ATaylor Jul 28 '12 at 07:41
  • @ATaylor: yeah, for now it's void. – Bharat Kul Ratan Jul 28 '12 at 07:42
  • 1
    @ATaylor: Probably, but C compilers are notoriously lax on that point. – Greg Hewgill Jul 28 '12 at 07:46
  • @GregHewgill: `int` is the default in C89/C90. In C99, declaring a function with no return type is a constraint violation. – Keith Thompson Jul 28 '12 at 08:03

1 Answers1

5

Did you compile with all warnings enabled from your compiler? With gcc that means giving the -Wall argument to gcc (and -g is useful for debugging info).

First, your printf("%u", main) should be printf("%p\n", main). The %p prints a pointer (technically function pointers are not data pointers as needed for %p, practically they often have the same size and similar representation), and you should end your format strings with newline \n. This takes the address of the main function and passes that address to printf.

Then, your second printf("%u", main()) is calling printf with an argument obtained by a recursive call to the main function. This recursion never ends, and you blow up your call stack (i.e. have a stack overflow), so get a SIGSEGV on Unix.

Pedantically, main is a very special name for C standard, and you probably should not call it (it is called auto-magically by startup code in crt0.o). Recursing on main is very bad taste and may be illegal.

See also my other answer here.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • No, I just compiled it as gcc file.c and also are we allowed to call other functions than main()? – Bharat Kul Ratan Jul 28 '12 at 07:47
  • You should *always* enable warnings. Of course you are allowed to call functions, and you do call `printf`. – Basile Starynkevitch Jul 28 '12 at 07:48
  • That's incorrect, you can call main in C (but not in C++). Not returning from main isn't a problem either, it's equivalent to returning 0/calling exit(0). – Mat Jul 28 '12 at 07:48
  • Please show the standard phrase allowing call to `main`. I remember having read something which allows the compiler to produce a non-function for it (even if most implementations do compile it to a function). – Basile Starynkevitch Jul 28 '12 at 07:50
  • 1
    No, `printf("%p\n", main)` has undefined behavior. `%p` requires a `void*` argument; `main` (after decay) is a function pointer. It's possible for function pointers to have an entirely different representation than `void*`. – Keith Thompson Jul 28 '12 at 08:05
  • `main` is a function. §5.1.2.2.1/1: "The function called at program startup is named `main`." C++ says that: "main shall not be used within a program" (§3.6.1/3), but the C standard doesn't say anything of the sort. – Jerry Coffin Jul 28 '12 at 08:06
  • [N1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) 6.5.2.2p11: "Recursive function calls shall be permitted, both directly and indirectly through any chain of other functions." No exception to this rule is stated for `main`. (C++ forbids recursive calls to `main`.) – Keith Thompson Jul 28 '12 at 08:10