First read https://www.geeksforgeeks.org/sizeof-operator-c/
sizeof() operator returns size_t type, https://www.geeksforgeeks.org/size_t-data-type-c-language/
#include <stdio.h>
int main()
{
int a = 1;
char d[] = "ab";
printf("%ld", sizeof(d));
return 0;
}
in general, size of array = no.of elements* size of each element.
Note that, there is a null character present at end of the string by default.
Output :- d is character array ==> no.of elements = 3 including null character, and each element takes 1 Bytes ==> 3 Bytes as output.
#include <stdio.h>
int main()
{
int a = 1;
char d[] = "ab";
printf("%ld", sizeof(a+d));
return 0;
}
in this case integer is added to the character array ==> pointer + int ==> result type is pointer
Output :- size of pointer.
Now you may have doubt that, why sizeof(d) doesn't returns size of pointer ?
it is due to " type assigned to d during declaration. ", but in case of sizeof(d+a), type once again calculated, So it returns size of pointer ( Thanks to @arjun sir, for clarification. )
IMPORTANT POINT :- sizeof never evaluates the expression value, it just evaluates the type of expression only.
https://www.geeksforgeeks.org/anything-written-sizeof-never-executed-c/
https://www.geeksforgeeks.org/why-does-sizeofx-not-increment-x-in-c/