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;
}
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.
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 .
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.
Description. The sizeof operator returns the number of bytes in a variable type, or the number of bytes occupied by an array.
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
operators2 [...] 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With