in Programming
1,160 views
10 votes
10 votes
#include <stdio.h>
int foo(int a[100])
{
    return sizeof(a);
}
int main()
{
    int a[10];
    printf("%d", foo(a));
}

What will be the output of the above code ignoring any compiler warnings and assuming sizeof(int) as 4 when run on a 64 bit machine?

in Programming
by
1.2k views

1 Answer

18 votes
18 votes
Best answer

In C programming language, array parameters are treated as pointers .So here we  are passing base address of array a through function foo().

 So the expression sizeof(a) will become sizeof (int *) which results in 4 or 8 depending on machine 32 bit or 64 bit.

Here it is mentioned that on 64 bit machine so it will return 8.

selected by

4 Comments

@Rajesh Pradhan

In third example given by you

int a[10];
     a[2]=2;

    printf("%d ",a[60]); // prints 0 before calling foo    ,why this would print 0,it must give garbage value
2
2
edited by

@

I am also haiving same doubt

int a[10] mean we have allocated 10  Bytes from memory may or may not be consecutive 

but a[60] how it give 0 ??

0
0
yes a[60] intial keep garbage value
0
0
Answer:

Related questions