O
O
Osmanov Osman2020-12-12 22:00:43
C++ / C#
Osmanov Osman, 2020-12-12 22:00:43

Why is only the first word printed from scanf() to printf() in C?

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

int main() {
  char name[1024];
  printf("Your name: ");
  scanf("%s", &name);
  printf("Hello, %s!\n", name);
  system("pause");
  return 0;
}

And if I write Peter Petrov, then only Peter is displayed. Why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2020-12-12
@OSMANOF

Why?

Because it scans the field up to the first whitespace character. If you want a line to the end of the line use, for example,scanf("%s", name)
scanf("%[^\n]", name)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question