Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does sizeof (function(argument)) return?

Tags:

c

sizeof

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;
}
like image 546
Bharat Kul Ratan Avatar asked Sep 30 '12 07:09

Bharat Kul Ratan


People also ask

What does the sizeof function return?

It returns the size of a variable. It can be applied to any data type, float type, pointer type variables. When sizeof() is used with the data types, it simply returns the amount of memory allocated to that data type.

What type does sizeof return?

The sizeof operator yields the size in bytes of the operand, which can be an expression or the parenthesized name of a type. The result for either kind of operand is not an lvalue, but a constant integer value. The type of the result is the unsigned integral type size_t defined in the header file stddef. h .

Why does sizeof return 4?

Operands of type char are promoted to type int and the actual addition is performed within the domain of int (or unsigned int , depending on the properties of char on that platform). So your a + b is actually interpreted as (int) a + (int) b . The result has type int and sizeof(int) is apparently 4 on your platform.

What does sizeof buffer return?

Description. The sizeof operator returns the number of bytes in a variable type, or the number of bytes occupied by an array.


1 Answers

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.

like image 73
md5 Avatar answered Sep 29 '22 12:09

md5