SizeOf operator in C : Why is the output 6?

#include 
#include 
char name[] = "aadit";
int main(void)
{
    printf("%d\n",sizeof(name) );
}
Why is the output 6?

Replies

  • Kaustubh Katdare
    Kaustubh Katdare
    I guess because it includes the null at the end; the terminating \0. By the way the correct way to get the length of string would be strlen() and not sizeof(); because sizeof will return the length of the array, not the string.

    C/C++ experts may offer better help.

    PS: Been away from programming since eternity.
  • Jason Estibeiro
    Jason Estibeiro
    The sizeof() function provides the size of the data type in bytes. The character array consist of a terminating character '\0' (hence name[5]='\0') (as told by #-Link-Snipped-# ) and since each char value occupies a space of one byte, hence the result is 6 ... 6 bytes.

    And again as #-Link-Snipped-# said, if you want the length of the string use strlen() function. Then you would get the answer as 5.
  • Shailaja Tiwari
    Shailaja Tiwari
    Yes it is the null character assumed to be appended at the end of the string ....
  • Aadit Kapoor
    Aadit Kapoor
    Thank you friends!

You are reading an archived discussion.

Related Posts

Sir, I want to deblurr a blurred image using adaptive filter in matlab. Please help...
GOG formerly known as Good Old Games (owned by CD Projekt RED - The company behind super dope game series 'The Witcher'), is very well known for DRM free games...
With the evolution of new advancements, machines have been taking over the capabilities that were once supposed to be human-specific. Deep learning is a field of Artificial Intelligence that emphasizes...
Why is this program not printing the first element? #include #include #include int main(void) { char names[][5] = { "aadit", "jobs", "mark", }; printf("%s\n",names[0]); getch(); } when...