I used the following code:
#include <stdio.h>
#include <string.h>
void main()
{
char c[] = {'J', 'O', 'H', 'N'};
printf("size (bytes)=%d\n", sizeof(c));
int len = strlen(c);
printf("Length = %d\n", strlen(c));
printf("%s", c);
The output size of the string is 4, but the length is 5. is the string not supposed to have /0
at the end? So the output size of the string should be 5, but the length is 4.
If I write code as char c[] = {'J', 'O', 'H', 'N', \0};
or c[] = "JOHN"
then I got output size of the string is 5, the length is 4
CodePudding user response:
In C, a string is a sequence of character values including a 0-valued terminator - the string "JOHN"
would be represented as the sequence {'J', 'O', 'H', 'N', 0 }
.
Strings are stored in arrays of character type - because the you need to store the terminator in addition to the characters of the string, the size of the array has to be at least one more than the length of the string. IOW, to store the 4-character string "JOHN"
you need an array that's at least 5 elements wide.
Had you initialized the array as
char c[] = "JOHN";
then the terminator would have properly been accounted for.
If the terminator isn't present, then what you have isn't a string and the various str*
library functions won't handle it properly. In your case above, strlen
looked through the elements {'J', 'O', 'H', 'N'}
and didn't find the 0 terminator, so it kept looking past the end of the array until it saw a zero-valued byte.
CodePudding user response:
The definition of a string in C is that it is an array of characters, terminated by a null character, or '\0'
.
You wrote:
char c[] = {'J', 'O', 'H', 'N'};
So let's see.
- array of char: ✅
- terminated by a null character: ❌
So c
is not a string. So it's not valid to call strlen
on it, and it's not valid to print it out using printf
's %s
format.
You could make it a proper string by changing the initialization to:
char c[] = {'J', 'O', 'H', 'N', 0};
or equivalently
char c[] = {'J', 'O', 'H', 'N', '\0'};
Or it would be easier and clearer to just write
char c[] = "JOHN";
When you use a string literal (that is, a string in double quotes) as an initializer for a character array like that, the compiler automatically does exactly the same thing for you as if you had written {'J', 'O', 'H', 'N', '\0'};
, but it's obviously a lot less typing.