Home > Back-end >  Please comment
Please comment

Time:09-19

# include
# include
Int main ()
{char [c]="China";
Char [5] s={' C ', 'h', 'I', 'n', 'a'};
Printf (" % d \ n ", strlen (s));
Printf (" % d \ n ", sizeof (s));
return 0;
}
How much is the program of strlen output, why?

CodePudding user response:

You have a S definition of the problem of the definition of an array of strings to end with \ 0 you S five bytes input again but five values program to read \ 0 reads to the location of S [5] [5] S happened to is C [] array, so your strlen (S) instead of 5 to 10

CodePudding user response:

Strings and character arrays are still a little difference.
 # include 
# include
Int main ()
{
Char c []="China";
Char [5] s={' C ', 'h', 'I', 'n', 'a'};
Printf (" % d \ n ", strlen (s));
Printf (" % d \ n ", sizeof (s));
for(int i=0; i <10; I++) {
Printf (" % % c, p \ n ", s [I], & amp; S [I]);
}
return 0;
}

CodePudding user response:

Strlen output uncertainty, because strlen ends with the '\ 0' logo, character array s assignment is full and no \ 0, an array, with the result of the uncertainty,
  • Related