Code being accepted with scanf but not with gets

These are the two solutions i tried submitting today-
https://www.codechef.com/viewsolution/19063316- THE CORRECT ONE,
https://www.codechef.com/viewsolution/19063318- THE INCORRECT ONE
the only difference between the two is that in one of the codes i have used scanf() to read the string while in the other one i have used gets(). Codechef is not accepting the code in which i have used gets() . Can somebody please tell why?

Change the line where you are taking input for len including a newline character match.

From — scanf("%d",&len);
To — scanf("%d\n",&len);

2 Likes

Yes, that worked. Thank you so much, but i still haven’t understood the logic behind it. Kindly explain the logic also. Infact, this piece of code is giving wrong output over my ide but codeblocks has accepted it!

Little bit of theory first:

  1. When you use gets() function [ Note:- avoid using it its deprecated] it take the complete string until it reaches EOF or “\n” while in case of scanf it reads upto any whitespaces.
  2. When you mentioned “\n” in scanf while reading it removes the whitespaces characters until it reach any non-whitespace character.

So, when you used only scanf to read your len variable it reads upto the integer value in the input buffer and when you called gets function it start reading from next character onwards which is of course new line character in input stream so gets() match newline character and store it in your string value instead of your actual string what you wanted.

While in case of scanf with newline character at last eats up all the whitespaces after the integer value and then when gets() read it reads your actual string.

Sorry to comment in two parts but because of character limits i did so.

I am sorry to bother you again and again with my stupid questions but my code flushes the buffer before accepting string using gets() ,so how’s it considering the newline charctr to be the string. Also in my previous commnt i meant codechef and not codeblocks. My code after the suggested modification is correct according to codechef only otherwise codeblocks is giving wrong output with that code. Why’s this happening?

flush() should be used with output stream only as using it with input stream is undefined behaviour. While some compilers will perform as expected in your code but not all, if you were using microsoft visual studio it might be working but better is to avoid all the things having undefined behaviour in C/C++.

Lecture 30 Writing file using fwrite function in C language Hindi - YouTube . At 18:22 he has used fflush with input stream. fflush is used to clean the input buffer if i am corrrect?

Instead of sticking to this tutorial better to look at documentation. Please follow the link below it is from C++ but the function is same for C.

http://www.cplusplus.com/reference/cstdio/fflush/

It clearly mentions that behaviour is implementation dependent for input streams.

Even if you want specific answer to your question check out link below.

http://c-faq.com/stdio/stdinflush.html

ADVICE :- If its not very important to stick with C please get into C++ it will help in CP more than C because of its STL.

okay one last question. Why am i not getting the correct output on codeblocks after the modification you suggested?

To that i can’t say much i don’t use codeblocks and i tried your code on ideone and on linux it works fine but you should try debugging your code with the inbuilt codeblocks debugger and try to check every input variable. You can also use gdb for debugging or even you can simply use print statements to check what your variables actually contain.

If you use any other compiler setup for codeblock instead of gcc it might be because of that.

If you are unable to find out the problem let me know i will install and check codeblocks.

OK thank you so much.

1 Like