0

I'm just trying to figure out what's going on in the background, when I tokenize a c string with strtok and print out the tokens by printf with %s.

So this is the example:

char str[] = "Where - is - the - end - of - tokens?";
const char s[2] = "-";
char *token;

/* get the first token */
token = strtok(str, s);

/* walk through other tokens */
while( token != NULL ) {
    printf( " %s\n", token );
    token = strtok(NULL, s);
}

return(0);

My beliefs: - strtok doesn't create a copy of the given c string, just return the memory address of the first character of the given c string. - printf with %s will print out characters from a memory address until /0.

My question is the following: How does printf know where to stop printing out characters of a token with %s?

Please help my understand this behaviour of printf with %s.

Mat
  • 202,337
  • 40
  • 393
  • 406
Sami
  • 11
  • 4

1 Answers1

1

I've got my answer what is so simple: "Each delimiter is replaced by a null character." Source: IBM documentation

Sami
  • 11
  • 4