1
char *text;
text = malloc(256);
printf("Enter a sentence:");
scanf("%[^\n]s", text);
printf("%s",text);

Why doesn't the scanf function work in this code?

I don't enter a sentence and immediately the program finished.

user438383
  • 5,716
  • 8
  • 28
  • 43
  • 1
    This works fine for me; how are you compiling/running this program – Scott Hunter Jan 05 '22 at 13:15
  • 3
    You will have to provide a full [mre] to get an answer. That piece of code should not exhibit the described behaviour by itself alone. BTW it should be `"%[^\n]"` without the `s`... – Serge Ballesta Jan 05 '22 at 13:22
  • 1
    @SergeBallesta "it should be "%[^\n]" ..." --> why not suggest the equally good [`gets()`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used)? – chux - Reinstate Monica Jan 05 '22 at 14:45
  • 1
    @chux-ReinstateMonica: because my religion does not allow me to use (or advise) `gets` ;-) Yes, I know that `"%[^\n]"` is not better but I just wanted to tell OP not to use s after [], and felt too tired to explain why not controling size on input was bad and that only `fgets` or `%xxxs` or `%yyy[...]` should be used. May be I should have a coffee... – Serge Ballesta Jan 05 '22 at 14:52
  • Yes - I too feel the strain of the `scanf()` quagmire - time for a break. – chux - Reinstate Monica Jan 05 '22 at 14:55
  • @cat_software, Who or what text suggested `"%[^\n]s"`? – chux - Reinstate Monica Jan 05 '22 at 14:58

2 Answers2

0

My guess is that your program runs, but the console closes before you can see the result.

Try to modify to the following:

char *text;
text = malloc(256);
printf("Enter a sentence:");
scanf("%[^\n]s", text);
printf("%s",text);
system("pause");
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • Yes it works but Enter a sentence:É#© this sentences are printed. When i add the white spice scanf(" %[^\n]s", text); like that program works well. what is the problem i don't understand. Why i have to add white space – cat_software Jan 05 '22 at 13:20
  • I don't understand your comment. Can you please elaborate more? – Athanasios Kataras Jan 05 '22 at 13:22
  • For example. when i write this function --scanf(" %[^\n]s",text);-- "There is a white space betwee " and %-- program works well but when there is no space between them program directly write randomly and program finish. the thing i wanna say is why i add the white spice between them. my english language is bad sorry for that – cat_software Jan 05 '22 at 13:25
0

scanf("%[^\n]s", text); is worse than gets(). text[] remains unassigned if only "\n" entered *1, possible buffer overflow with no width limit. The s in for format serves no purpose.

Instead use fgets() with its input limit. Check return value.

#define N 256
char *text = malloc(N);
printf("Enter a sentence:");
if (fgets(test, N, stdin)) {
  test[strcspn(test, "\n")] = '\0';  // Lop off potential trailing \n
  printf("%s\n",text);
}

OP may also have trouble due to a prior scanf() calls that leaves a dangling '\n' in stdin that chokes on this scanf("%[^\n]s".... Avoid using scanf() at all until you know why it is weak.

Sigh - if you must stay with scanf(), use below which 1) uses a leading space to consume all prior white-space like left-over '\n', 2) has a width limit and 3) tests the scanf() success.

if (scanf(" %255[^\n]", text) == 1) {  // No 's' in format.
  printf("%s\n",text);
}

*1 This explains the "I don't enter a sentence and immediately the program finished." as prior input did not consume a '\n' and OP's code does not either and so stops the scan. Since OP's code did not consume the '\n' and scanf() did not return 1 (which OP's code failed to test), following unposted code certainly fell into undefined behavior.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256