+ 1

Strings and array of strings in C. Why using '\0'

In this code below, in the while loop, what is '\0' in the condition? #include <stdio.h> typedef struct String{ char* word; }String; int length(String s){ int count=0; while(s.word[count]!='\0'){ count++; } return count; } int main() { String string[]={{"Werg0"},{"Joan"},{"Gohan"},{"Frieza"},{"Gandalf"}}; int value=sizeof(string)/sizeof(string[0]); printf("sizeof(string): %ld, sizeof(string[0]): %ld\n",sizeof(string),sizeof(string[0])); for(int i=0;i<value;i++){ printf("%s\n",string[i].word); printf("length: %d\n",length(string[i])); } return 0; }

6th Nov 2023, 5:34 PM
Werg Serium
Werg Serium - avatar
1 Réponse
+ 5
the null character '\0' (also null terminator), is a control character with the value zero. the character has much more significance and it serves as a reserved character used to signify the end of a string,often called a "null-terminated string" .
6th Nov 2023, 6:35 PM
MO ELomari
MO ELomari - avatar