0

when I execute this code below with the text: hello world, the result is 5 characters. Why is that?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(void)
{
     char *n = malloc(sizeof(char) * 50);
     printf("Text: ");
     scanf("%s", n);
     int characters = strlen(n);
     printf("%s\n", n);
     printf("%i\n", characters);
}

1 Answer 1

1

From man scanf (conversions):

s

Matches a sequence of non-white-space characters; the next pointer must be a pointer to character array that is long enough to hold the input sequence and the terminating null byte ('\0'), which is added automatically. The input string stops at white space or at the maximum field width, whichever occurs first.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .