Wednesday, February 16, 2011

Reason why sizeof charstr adds another 1 byte in C Programming

Returning back to C programming again, now I was stumbling why does
char o[] = "1"

and


sizeof o 

returns 2, while
char o = '1'; 

returns 1. Or If I do
char o[] = "2345"; 

and

sizeof o


is 5. It seems that when char is an array/string, it adds 1.

Then I just found the answer that when it becomes a string, it includes the '\0' or null character or 0 byte mark which indicates the end of the string. Some call it as zero termination. So in the example, char o[] = "1", it goes like {'1', '\0'}.

No comments: