I am resuming studying the ANSI C book. looked at the section of string. according to the author, ANSI C does not have a built in string type. instead an array of characters need to be used. According to the book, the compiler ends then the array of char with the character zero '\o'. hence if you want to use a string of 4 chars, you need to declare an array of 5 chars.
i can't see on which circumstance this extra character can be useful? why such decision has been made?
i wrote a simple code to verify the above
char m[6]="elvira";
length=strlen(m);
for( i=0;i<length;i++)
printf(" the chara at order %d os %c\n",i,m[i]);
the output was to my suprise
the chara at order 0 os e the chara at order 1 os l the chara at order 2 os v the chara at order 3 os i the chara at order 4 os r the chara at order 5 os a the chara at order 6 os ) the chara at order 7 os N the chara at order 8 os ├ the chara at order 9 os wi was expecting a compile error as the array should have an extra byte to hold the terminating zero or at least the size of the array to be 6. the result of the execution of the program was unexpected to me
any help please?
Cheers,


Sign In
Create Account


Back to top










