Sanjay Rajpal
Sanjay Rajpal

Reputation: 498

Programming in C - Problem in Functions

int fn()
{
    return 10;
}

int main()
{
    printf("%d\n\n",fn);
    system("pause");
}

This program gives a random number, but when a function call is made, then it returns the value 10.

Can I conclude that when we use function name in a printf statement, it gives a garbage value or there is some other concept in it ?

Thanks

Upvotes: 2

Views: 185

Answers (7)

SAQIB SOHAIL BHATTI
SAQIB SOHAIL BHATTI

Reputation: 79

You should change your printf() statement to this

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

Now, this will work fine.

Upvotes: -1

phoxis
phoxis

Reputation: 61910

  • When you say to fn() it will make a function call
  • When you say to fn it refers to the address of the function where the call should be made

So printf("%d\n\n",fn); will print the address of the address of the function actually not a random number, and printf("%d\n\n", fn()); will call the function and print what was returned.

Note the difference:

int fn (void)
{
  return 10;
}
int main (void)
{
  int x, y;
  x = fn();
  y = fn;
}

Here is the compiler output:

fn:
        push    ebp
        mov     ebp, esp
        mov     eax, 10
        pop     ebp
        ret
        .size   fn, .-fn
.globl main
        .type   main, @function
main:
        lea     ecx, [esp+4]
        and     esp, -16
        push    DWORD PTR [ecx-4]
        push    ebp
        mov     ebp, esp
        push    ecx
        sub     esp, 20

        ; below code does x=fn();
        call    fn                                 ; calls fn, return value in eax
        mov     DWORD PTR [ebp-12], eax            ; stores eax in ebp-12, the location for x on the local stack allocated by compiler


        ; below code does x=fn;
        mov     DWORD PTR [ebp-8], OFFSET FLAT:fn  ; stores the label address in ebp-8, the location for y on local stack allocated by compiler

        add     esp, 20
        pop     ecx
        pop     ebp
        lea     esp, [ecx-4]
        ret

Upvotes: 2

7VoltCrayon
7VoltCrayon

Reputation: 662

You aren't calling the function, when you type fn() THAT is when your'e calling the function and that should give you correct results.(SEE BELOW)

int fn()
{
    return 10;
}

int main()
{
    printf("%d\n\n" , fn() );
    system("pause");
}

Upvotes: 1

deadalnix
deadalnix

Reputation: 2325

You are printing the actual adress of fn code in memory, not calling it. Call it and your life will enlight !

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

And please, put a space after comma, like always.

Upvotes: 3

Armen Tsirunyan
Armen Tsirunyan

Reputation: 132994

Forgot to call the function :)

change fn to fn()

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

Now this is what you get with ellipsis - no type checks...

Upvotes: 2

clyfish
clyfish

Reputation: 10450

fn() not fn

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

Upvotes: 1

insumity
insumity

Reputation: 5459

It should be:

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

fn corresponds to a ponter address of the function. That's why you get the garbage number. In order to call a function you must use parentheses like this:

foo();
foo(parameter1, ..., parameterN);

Upvotes: 6

Related Questions