I wrote this program :
#include <stdio.h>
int main(){
char s[]="hi\0h";
for(int i=0 ;i<4;i )
printf("s[%d] = %c\n", i ,s[i]); // gives 4 chars 'h'-'i'-''-'h'
printf("%s", s); // output : hi
return 0;
}
Why the program does not show us the character string stored after \0 although it may contain an important message? And why the function strlen(s) gives us 2(the length of s) while there are more then 2 characters ?
CodePudding user response:
In C, an array is a contiguous sequence of elements of one type (C 2018 6.2.5 20) and a string is a contiguous sequence of characters terminated by and including the first null character (C 2018 7.1.1 1).
Fundamentally, an array is memory the program uses for whatever purposes it wishes, and a string is particular data inside an array that is used in conjunction with the standard library routines and other routines written to use strings.
Strings end at a null character because it is useful to have routines that work with such strings. It gives programmers simple ways to print strings, concatenate strings, search strings for characters, and otherwise work with strings. It is not always the best way, but it is easy to use for many elementary purposes. For example, sometimes it is better to have a separate record of the length of a string so that we do not need to go to the effort of figuring out string length by examining all its characters.
Strings must be inside arrays, because a string is, by definition, a contiguous sequence of characters, and contiguous sequences of things appear in arrays. An array still has all its elements even if the string currently inside it ends before the end of the array, and you can always access those elements by their array indices, regardless of the length of the string. However, library routines that work with strings will not care about the data beyond the end of the string.
If you want to work with data in an array beyond the end of the string, you can write source code to do that.
CodePudding user response:
Why the program does not show us the character string stored after \0 although it may contain an important message?
Code did not attempt to print the character string stored after \0
.
Code printed 4 of the 5 characters of the string literal "hi\0h"
with the below code:
for(int i=0 ;i<4;i )
printf("s[%d] = %c\n", i ,s[i]);
Code then printed the first string of the string literal "hi\0h"
:
printf("%s", s);
"hi\0h"
is a 5 character string literal made up of 5 char
: 'h'
, 'i'
, '\0'
, 'h'
, '\0'
. It consists of a string followed by another a string.
"%s"
directs printf()
to take the address of the string and print characters until a null character is read, so only 'h'
and 'i'
are printed.
"%s"
does not direct printf()
to take the address of the string literal and print all its characters.
To print all the characters of a string literal, use a loop.
for(size_t i=0 ;i<sizeof("hi\0h"); i )
printf("[%zu] = '%c'\n", i ,"hi\0h"[i]);
In C library:
A string is a contiguous sequence of characters terminated by and including the first null character.