1

So I got this small block of code .The main function printing the main function. What does it print ? Is it some sort of address ?

int main() {
   printf( "%d", main ) ;
 }
billpcs
  • 633
  • 10
  • 17
  • 1
    `warning: format '%d' expects type 'int', but argument 2 has type 'const char*'` – Hanky Panky Jun 03 '14 at 14:08
  • main() returns `EXIT_SUCCESS` or `EXIT_FAILURE`. Defined `stdlib.h`. **0** is standard. But here no call is made. – eMi Jun 03 '14 at 14:08
  • To be pedantic, for this to be a valid C program it should `return 0` or something. – Maxim Egorushkin Jun 03 '14 at 14:09
  • 4
    Since 1999 `main()` returns `0` in the absence of a `return` statement. – pmg Jun 03 '14 at 14:09
  • 2
    format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int (*)()’ – billpcs Jun 03 '14 at 14:09
  • @SakthiKumar, unfortunately there is no portable way to print the address of a function. `%p` is for `void*` pointers and only *object* pointers can be converted to these. – Jens Gustedt Jun 03 '14 at 15:07

5 Answers5

5

Yes, the name of a function evaluates to its address. Without the function-call operator (), no call is made.

But this is not valid code, %d is not a valid format specifier for a function pointer (and the return is missing). Unfortunately, printing a function pointer is not very simple to get right.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
0

Assuming a function fn()

printf("%d",fn());

Will print its return value as a decimal int. The compiler will probably complain if it returns something that is not an int.

printf("%d",fn);

Will print its address as an int. The compiler may complain about the function pointer-to-int conversion.

The correct way to print an address is:

printf("%p",variable);

EDIT: However, as Pascal and others mentioned, %p is only valid for pointers to objects, so there's no correct way of printing the adress of functions.

humodz
  • 600
  • 3
  • 8
  • `%p` is for printing data pointers (pointers to objects in C standard vocabulary). Such pointers can be converted to `void*`. A function pointer is not a pointer to an object, it cannot be converted to `void*`, and it shouldn't be passed as an argument corresponding to `%p` in printf. – Pascal Cuoq Jun 03 '14 at 15:52
-1

it will print the address of main. It is as simple as you are trying to print the address of any normal function.

-1

The compiler will display an error.

user3328692
  • 99
  • 1
  • 2
  • 8
  • it may print the address but it may throw an error. – user3328692 Jun 03 '14 at 14:24
  • No, it won't throw an error. I actually tested it. The compiler will throw two warnings, but because of an implicit cast it will not error. – phyrrus9 Jun 03 '14 at 14:40
  • @phyrrus9 are warnings bad? – user3328692 Jun 03 '14 at 14:44
  • 1
    No, this is perfectly invalid code. Function pointers can't be printed as easily. – Jens Gustedt Jun 03 '14 at 15:09
  • @user3328692, warnings are as bad as errors. Actually the C standard only has the term "diagnostic" and even if the compiler continues after such a diagnostic, all bets are off. – Jens Gustedt Jun 03 '14 at 15:10
  • @phyrrus9 See http://stackoverflow.com/questions/18418202/is-there-a-recommended-integer-type-to-store-function-pointers-in-standard-c . It is related but not equivalent since there could be a format for printing function addresses without an integer type to convert them to, but it turns out that there isn't either. – Pascal Cuoq Jun 03 '14 at 15:48
  • @JensGustedt the code is 100% C. It is valid, it is not what the user wanted it to do, but as far as the compiler cares, it is valid – phyrrus9 Jun 03 '14 at 17:30
  • @phyrrus9, you probably have a different concept of C than I have. For me it is described by the standard with the goal to be portable and not to depend on one particular compiler. And the C standard clearly states that a mismatch between argument and `printf` specifier is "undefined behavior". Or said otherwise, this is invalid. – Jens Gustedt Jun 03 '14 at 21:42
-1

This will print the address of the function main() but in decimal format.You should use %x instead of %d. When compiler compiles a program it creates many memory segment but all the code goes into the .text segment which is a read only segment. So this address is from .text segment. try writing to this address, you should get an error.

Vikram Singh
  • 948
  • 6
  • 10
  • I did not downvote, but `%p` should be the right thing than `%x` – Mohit Jain Jun 03 '14 at 14:47
  • 2
    @MohitJain They are both wrong. `%p` is for printing data pointers (pointers to objects in C standard vocabulary). Such pointers can be converted to `void*`. A function pointer is not a pointer to an object, it cannot be converted to `void*`, and it shouldn't be passed as an argument corresponding to `%p` in printf any more than as an argument corresponding to `%x`. – Pascal Cuoq Jun 03 '14 at 15:52
  • @PascalCuoq Thank you for pointing. With Wall gcc gives gives `ISO C forbids conversion of function pointer to object pointer type`. So in short there is no way (and no practical need) to print function pointers. Trying to print a function pointers looks UB. – Mohit Jain Jun 03 '14 at 18:08