1

This code works fine after commenting statement one. Statement two gives some garbage value but at least it runs. I'm not getting how does the printf in argument gives a value if it's not called as a function?

#include<stdio.h>

int main()
{
    int printf = 123;    // statement one
    printf ("%d", printf); // statement two  
    return 0;
}
1
  • 1
    The compiler warning should give a clue: "warning: format ‘%d’ expects type int, but argument 2 has type int (*)(const char * __restrict__)". i.e. printf is the pointer that points to printf (). Commented Oct 6, 2012 at 13:46

2 Answers 2

4

A function name without parentheses doesn't return a value - it evaluates to the address of the function (i. e. it is a function pointer of type size_t (*)(const char *, ...)). Essentially, this is the address where the CPU will jump among the instructions whenever it encounters a call to printf().

By the way, if I recall correctly, assigning to a function's name is undefined behavior, so is printing a pointer using the %d format specifier - you should use

printf("%p", (void *)printf);

Also, you can declare function pointers, assign them the address of another function and call them just fine:

size_t (*my_printf)(const char *, ...) = printf;
my_printf("This actually called printf!\n");

printf("my_printf: %p\n", (void *)my_printf);
printf("original printf: %p\n", (void *)printf);

The last two statements print the same address.

Edit: Strictly speaking, even the above "solution" is nonstandard as the Standard doesn't guarantee that function pointers can be cast to data pointer types. That means basically that there's no way for print the address of a function, however, on most systems, casting to void * apparently works.

Sign up to request clarification or add additional context in comments.

12 Comments

Hehe, somebody upvoted me, then realized it was me, then revoked the upvote :D
"assigning to a function's name is undefined behavior" - Where is the function name being assigned to?
@ArjunShankar int printf = 123, maybe? I'm not sure if there can be a function and a variable with the same name, but I suspect not really.
If you don't comment that statement, the program won't even compile. Because int printf masks the stdio.h declaration of printf. The error I get on 'statement two' is: "error: called object ‘printf’ is not a function".
All I was saying was: In the OP's question, a function isn't being assigned to. But an integer is being 'called' (which of course is a weird thing to do).
|
3

In statement 2, when the identifier printf is passed to the function, it is a function pointer - a pointer to the function printf - and the garbage value is the result of trying to print this address using printf and the format: %d: you should see a compiler warning when printing a pointer using %d.

4 Comments

This answer is still causing UB. Since printf("%p") takes a void * argument, passing in a size_t (*)(const char *, ...) invokes undefined bahvior if you don't do a cast to void pointer.
@H2CO3 are you sure? remember that function pointers are different than data pointers. At any rate, I removed the language about %p altogether.
@H2CO3 You shoudn't be so sure. you might find this useful. A search will bring up even more.
so it's even worse than that of I knew. In this case, there's no way to print the address of a function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.