-1
int main(){

     char str1[20], str2[20];
    printf("Enter string 1 : ");
    scanf("%[^\n]s",str1);

    printf("Enter string 2 : ");
    scanf("%[^\n]s",str2);

    printf("String 1 is %s\n",str1);
    printf("String 2 is %s\n",str2);
    removeFromSecond(str1,str2);
}

The output is :

Enter string 1 : in

Enter string 2 : String 1 is in

String 2 is ■   a

It's just not asking to enter the second string. I can't remember, but I've read somewhere that there is a line we need to add to eat up all unused '\n's. Please, if anyone knows that, I need exactly that line.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
WangJexi
  • 1
  • 5
  • The _unused_ `\n` from the first input, is used to terminate the second input. – Clifford Jul 30 '21 at 19:35
  • 2
    Why do you have `s` at the end of each format string? – Barmar Jul 30 '21 at 19:36
  • 2
    If you want to read a line of input, use `fgets()`, not `scanf()`. – Barmar Jul 30 '21 at 19:36
  • The `&` befor `str1` and `str2` is also incorrect. – Clifford Jul 30 '21 at 19:37
  • 1
    My opinion, but: `scanf` is a primitive tool. It's only good for simple input. `%[]` is not a simple format string. If you find yourself wanting to use `%[]`, it probably means you're doing something fancy enough that you should be [moving beyond `scanf`](https://stackoverflow.com/questions/58403537) and using something else, perhaps `fgets`. Or if you'd rather keep using `scanf` for now, limit yourself to `%d`, `%f`, and `%s`. All the rest are just too much trouble. – Steve Summit Jul 30 '21 at 19:47
  • 1
    `scanf(" %[^\n]", str2);` note the added space before `%` (to consume the previous newline), and the removed `s` (a beginner's mistake). – Weather Vane Jul 30 '21 at 19:48
  • @WeatherVane `gets()` is a better alternative than `scanf(" %[^\n]", ...`. Yet both are not used in quality code. – chux - Reinstate Monica Jul 30 '21 at 19:55
  • You may want to read this: [A beginners' guide away from scanf()](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html) – Andreas Wenzel Jul 30 '21 at 20:05
  • @chux true, I overlooked a length restriction. – Weather Vane Jul 30 '21 at 20:20

2 Answers2

1

2nd scanf("%[^\n]s",str2); fails to save anything as the previous line's '\n' was not consumed by the prior scanf("%[^\n]s",str1); nor this scanf("%[^\n]s",str2);.

To read a line of user input use fgets().

// scanf("%[^\n]s",str1);
fgets(str1, sizeof str1, stdin);

As fgets() also reads and saves a potential '\n', use below to lop it off if it is there.

str1[strcspn(str1, "\n")] = 0;

scanf() is rarely as good choice. Do not use until you understand why it is bad.


If you _must use scanf(), drop the useless s in the format, add a width:

// Ugly POS code
char str1[20] = "";  // Set to empty string in case nothing saved
// Read up to 19 non-\n characters.  If more available, read them but don't save.
scanf("%19[^\n]%*[^\n]",str1);  
// consume up to 1 trailing \n, do not save.
scanf("%*1[\n]");

// TBD check return values.
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Thanks, I was using scanf because I have an online interview next week and I don't know what compiler they will b using. I heard gets doesn't work in some compilers, and I've never used fgets and I thought it might fail just like gets. – WangJexi Jul 30 '21 at 20:31
  • @HimanshuRai `gets()` is [bad](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) because it lacks a width limit and is not standard since C11. `fgets()` accepts a _size_ to prevent overfilling. – chux - Reinstate Monica Jul 30 '21 at 21:17
-1

Well, use fgets() as everyone is telling me to because it is the right choice. But still if anyone wants to know the answer for what I actually asked, it is as weather-vane said in comments It's a rookie's mistake, just add a space before the % in the second scanf.

Like scanf("%[^\n]", str2); -> to -> scanf(" %[^\n]", str2);

WangJexi
  • 1
  • 5
  • 1
    And as @chux pointed out you need a length restriction. `scanf(" %19[^\n]", str2);` – Weather Vane Jul 30 '21 at 20:22
  • `scanf(" %[^\n]", str2);` reads all leading white-space - 0 or more. Input like `"\n"` will block until more input. Leading spaces will not get saved, yet internal and trailing non-\n spaces will get saved. Input like `"\n\n\n123\n"` will skip 3 lines. `scanf(" %[^\n]", str2);` is not a good solution to read a _line_. – chux - Reinstate Monica Jul 30 '21 at 21:30