0

Consider the following programme

void main()
{
char str[]="\12345s\n";
printf("%d",sizeof(str));
}

Output is :6
can someone explain me why.And is sizeof contain null value while
gigving the size of any string.

Edited by Nick Evan: Fixed formatting

3
Contributors
4
Replies
5
Views
13 Years
Discussion Span
Last Post by Dave Sinkula
0
char str[] = "\12345s\n";

This is equivalent to the following.

char str[] = {0123,'4','5','s','\n','\0'};

The array has six elements, each of one byte in size -- so sizeof reports the array size as 6.

0

And if you said:
void main()
{
char *str ="\12345s\n";
printf("%d",sizeof(str));
}

The output will be 4, the size of a pointer (not the size of what it points to)

0

char str[] = {0123,'4','5','s','\n','\0'};
how it is treated like this first element like 0123.it is very much confusing pls clear my doubt.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.