According to a reference i am using, 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 wrote a simple code to verify the above
Expand|Select|Wrap|Line Numbers
- 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
Expand|Select|Wrap|Line Numbers
- 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 ├
char m[]="elvira"; is used to declare and initiliase the string, printf outputs
elvira ONLY!
if instead i keep char m[6]="elvira" and then set m[5]='\0', the printf shows only "elvir" as expected. however, if i set m[i]='\0' for any i<6, only the characters up to (i-1) are displayed. The remaining are not displayed and "m" has a shorter length!!!
i am surprised with such executions. i was expecting the array of char to be handled via its subscript like with any other type of array but it seems it is not the case
why ??? they would have better define a string type. any explanations for the above please
Cheers.