- sizeof is a unary operator in C.
1234int main(){printf("%d", sizeof(3,3.0999999));}
Output:
18
The terms in sizeof() are stored in stack. Firstly the 3 is inserted then 3.0999999. Thus as we know stack is a LIFO (last in first out) data structure. Therefore sizeof operator will give a size of last item i.e, 3.0999999. As 3.0999999 is of double data type, it returns size of double i.e, 8. - Here in the first printf, address array elements are always an integer. So that it return value of integer data type i.e, 4 bytes. In the second printf statement, it returns the number of elements in arr and ‘\0’
1234567#include <stdio.h>int main(){char arr[] = "johnjohny";printf(" %d \n ", sizeof( &arr[0] ) ); //8printf(" %d\n ", sizeof( arr ) ); //10} -
In first statement, null consider as a string. In the second statement, nothing means zero. So there are two elements zero and \0. Therefore it return number of elements.
12345int main(){printf(" %d\n ", sizeof( " null " )); //5printf(" %d\n ", sizeof(" ")); //2}