0

why second scanf doesnt take any input ? does %s cancatanae the string?

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char string1[50];
char string2[50];


scanf("%[^\n]s",string1);
scanf("%[^\n]s",string2);

printf("\nfirst string :%s \n",string1);
printf("\nsecnd string :%s \n",string2);

return 0;
}

input : this is bbc [enter]

output: first string :this is bbc second string:{some characters symbol}

deepak pandey
  • 101
  • 3
  • 13

3 Answers3

1

The problem is that your scanf format string requires the string to end in s: %[^\n] is interpreted as the format specifier, and then you have an s which must be matched by the input. Since the second string does not start in s, the second scanf reads nothing.

Replacing s with a space will fix the problem:

scanf("%[^\n] ", string1);
//           ^
// Space is important

Even better, put space in front of %[^\n] in the second scanf:

scanf("%49[^\n]", string1);
scanf(" %49[^\n]", string2);

Since your string buffers have limited capacity, putting a limit of 49 is necessary to avoid buffer overruns.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

"%[^\n]s" tells to scanf to "eat" all the character that aren't a newline and put them in the argument; this means that, after the first scanf returns, it is leaving the newline that made it stop in the read buffer. The next scanf finds it and immediately stops reading - after all, you told it to read up to the first newline!

A solution here can be to use "%[^\n]s\n", which "eats" even the newline that follows the string, or even "%[^\n]s ", where the space is "magic", in that it tells scanf to eat all the other whitespace it can find, which includes newlines (notice however that this is a bad idea if you want to be able to read an empty string in the next line).

Even simpler, you can simply use fgets(string1, sizeof(string1), stdin), which, unlike scanf with %s, is safe against buffer overflows (yes, you can make even %s safe, but it's more work). Notice that fgets puts even the trailing newline into your string.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • got it .thanks :) one more quest if this is the situation scanf("%s",array1) and next line scanf("%s",array1) the same array .if the size of array is 10.and the input exceed 10 what will be the output – deepak pandey Jul 02 '16 at 12:15
  • @deepakpandey: anything can happen, from everything appearing to work fine to one string partially corrupting the other to the program crashing. Don't do it, size your buffers appropriately and always use string input functions that allow to specify the size of the buffer. – Matteo Italia Jul 02 '16 at 12:19
  • thanks for the valuable info :) – deepak pandey Jul 02 '16 at 12:21
-1

Your code will only read one input. As %[^\n]s fails to read an 's'

Replace two scanf() statement with following one. Just executed your code on my system it works.

scanf("%[^\n]s %[^\n]s",string1,string2);

Or just remove s from your statements and add space.

scanf("%[^\n] ",string1);
scanf("%[^\n] ",string2);

checked with your input for both the string and it displays the proper output.

K J Gor
  • 750
  • 5
  • 17