Coderbyte is an online coding challenge site (I found it just 2 minutes ago).
The first C++ challenge you are greeted with has a C++ skeleton you need to modify:
#include <iostream> #include <string> using namespace std; int FirstFactorial(int num) { // Code goes here return num; } int main() { // Keep this function call here cout << FirstFactorial(gets(stdin)); return 0; }
If you are little familiar with C++ the first thing* that pops in your eyes is:
int FirstFactorial(int num);
cout << FirstFactorial(gets(stdin));
So, ok, the code calls gets
which is deprecated since C++11 and removed since C++14 which is bad in itself.
But then I realize: gets
is of type char*(char*)
. So it shouldn't accept a FILE*
parameter and the result shouldn't be usable in the place of an int
parameter, but ... not only it compiles without any warnings or errors, but it runs and actually passes the correct input value to FirstFactorial
.
Outside of this particular site, the code doesn't compile (as expected), so what is going on here?
*Actually the first one is using namespace std
but that is irrelevant to my issue here.
I tried the following addition to
main
in the Coderbyte editor:Where the mysterious and enigmatic snippet
gets(stdin)
appears inside a string literal. This shouldn't possibly be transformed by anything, not even the preprocessor, and any C++ programmer should expect this code to print the exact stringgets(stdin)
to the standard output. And yet we see the following output, when compiled and run on coderbyte:Where the value
8
is taken straight from the convenient 'input' field under the editor.From this, it's clear that this online editor is performing blind find-and-replace operations on the source code, substitution appearances of
gets(stdin)
with the user's 'input'. I would personally call this a misuse of the language that's worse than careless preprocessor macros.In the context of an online coding challenge website, I'm worried by this because it teaches unconventional, non-standard, meaningless, and at least unsafe practices like
gets(stdin)
, and in a manner that can't be repeated on other platforms.I'm sure it can't be this hard to just use
std::cin
and just stream input to a program.I am intrigued. So, time to put the investigation goggles on and since I don't have access to the compiler or compilation flags I need to get inventive. Also because nothing about this code makes sense it's not a bad idea question every assumption.
First let's check the actual type of
gets
. I have a little trick for that:And that looks ... normal:
gets
is marked as deprecated and has the signaturechar *(char *)
. But then how isFirstFactorial(gets(stdin));
compiling?Let's try something else:
Which gives us:
Finally we are getting something:
decltype(8)
. So the entiregets(stdin)
was textually replaced with the input (8
).And the things get weirder. The compiler error continues:
So now we get the expected error for
cout << FirstFactorial(gets(stdin));
I checked for a macro and since
#undef gets
seems to do nothing it looks like it isn't a macro.But
It compiles.
But
Doesn't with the expected error at the
n2
line.And again, almost any modification to
main
makes the linecout << FirstFactorial(gets(stdin));
spit out the expected error.Moreover the
stdin
actually seems to be empty.So I can only conclude and speculate they have a little program that parses the source and tries (poorly) to replace
gets(stdin)
with the test case input value before actually feeding it into the compiler. If anybody has a better theory or actually knows what they are doing please share!This is obviously a very bad practice. While researching this I found there is at least a question here (example) about this and because people have no idea that there is a site out there who does this their answer is "don't use
gets
use ... instead" which is indeed a good advice but only confuses the OP more since any attempt at a valid read from stdin will fail on this site.TLDR
gets(stdin)
is invalid C++. It's a gimmick this particular site uses (for what reasons I cannot figure out). If you want to continue to submit on the site (I am neither endorsing it neither not endorsing it) you have to use this construct that otherwise would not make sense, but be aware that it is brittle. Almost any modifications tomain
will spit out an error. Outside of this site use normal input reading methods.I'm the founder of Coderbyte and also the guy who created this
gets(stdin)
hack.The comments on this post are correct that it is a form of find-and-replace, so let me explain why I did this really quickly.
Back in the day when I first created the site (around 2012), it only supported JavaScript. There was no way to "read in input" in JavaScript running in the browser, and so there would be a function
foo(input)
and I used thereadline()
function from Node.js to call it likefoo(readline())
. Except I was a kid and didn't know better, so I literally just replacedreadline()
with the input at run-time. Sofoo(readline())
becamefoo(2)
orfoo("hello")
which worked fine for JavaScript.Around 2013/2014 I added more languages and used third-party service to evaluate code online, but it was very difficult to do stdin/stdout with the services I was using, so I stuck with the same silly find-and-replace for languages like Python, Ruby, and eventually C++, C#, etc.
Fast forward to today, I run the code in my own containers, but never updated the way stdin/stdout works because people have gotten used to the weird hack (some people have even posted in forums explaining how to get around it).
I know it is not best practice and it isn't helpful for someone learning a new language to see hacks like this, but the idea was for new programmers to not worry about reading input at all and just focus on writing the algorithm to solve the problem. One common complaint about coding challenge sites years ago was that new programmers would spend a lot of time just figuring out how to read from
stdin
or read lines from a file, so I wanted new coders to avoid this problem on Coderbyte.I'll be updating the entire editor page soon along with the default code and
stdin
reading for languages. Hopefully then C++ programmers will enjoy using Coderbyte more :)