c - Is it necessary to supply the null character when declaring an character's array? -
a string constant in c stored character array, while creating such array element element, necessary supply null character.
i need store string constant, say, s[number]= "hello\n"
. string constant stored character array in c, further, such string terminated null character '\0'
. while storing phrase in array, need account null character , allocate additional space or need mention number of characters need store?
if going use c-string functions, strlen, - answer yes. string should null-terminated. if introduce custom functions deal string - can store like.
it's important mention, if create array using string constant, reserves space null-character automatically. e.g. output following code:
char s[] = "hello"; printf("%d", sizeof(s) / sizeof(char));
is
6
which 5 'h, 'e', 'l', 'l', 'o' , 1 '\0'.
Comments
Post a Comment