7

What will be the output of program

#include <stdio.h>

int fun(char *a){
    printf("%d\n",sizeof(a));
    return 1;
}

int main(){
    char a[20];
    printf("%d\n",sizeof (fun(a)));
    return 0;
}
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Bharat Kul Ratan
  • 985
  • 2
  • 12
  • 24
  • 1
    its not really `sizeof(function)`, but rather `sizeof(function())` or `sizeof(int)` – jedwards Sep 30 '12 at 07:28
  • StackOverflow has great searching capabilities. Please use them before asking a question. For example this search string: `[sizeof] function` would give you many related questions including this one: [Output of using sizeof on a function](http://stackoverflow.com/questions/6988227/output-of-using-sizeof-on-a-function) – Jarekczek Sep 30 '12 at 07:49
  • 1
    @jarekczek, that particular question isn't a dupe (though other may be) - it specifically asks about the size of the function rather than the size of the return value from a function. – paxdiablo Sep 30 '12 at 08:19
  • 2
    You know, I wonder whether people even _bother_ to check proposed dupes before closing. One is for a function designator itself, _not_ a function "call". Hence it's not the same thing. The other is for the size of an array! Voting to reopen. – paxdiablo Sep 30 '12 at 23:17
  • jarekczek: both of the links posted above question aren't similar to the question asked. – Bharat Kul Ratan Oct 01 '12 at 17:58

4 Answers4

18

Except with variable length arrays, sizeof does not evaluate its operand. So it will just yield the size of fun(a) type, i.e. sizeof(int) (without calling the function).

C11 (n1570) §6.5.3.4 The sizeof and _Alignof operators

2 [...] If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

Community
  • 1
  • 1
md5
  • 23,373
  • 3
  • 44
  • 93
4

It returns the size of the return type from that function (4 on my implementation since that's what an int takes up for me), which you would discover had you run it as is, then changed the return type to char (at which point it would give you 1).

The relevant part of the C99 standard is 6.5.3.4.The sizeof operator:

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

Keep in mind that bold bit, it means that the function itself is not called (hence the printf within it is not executed). In other words, the output is simply the size of your int type (followed by a newline, of course).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
3

The function returns int, so it's sizeof(int), which, on 32 bit systems is typically 4 bytes. Though it could be 2 or 8, depending on implementation.

alf
  • 681
  • 1
  • 8
  • 19
1

keyword sizeof followed by ellipsis returns the number of elements in a parameter pack.

The type of the result is the unsigned integral type size_t defined in the header file

Database_Query
  • 626
  • 4
  • 14