What is going on with 'gets(stdin)' on the site coderbyte?
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:
template <class> struct Name;
int main() {
Name<decltype(gets)> n;
// keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;
}
And that looks ... normal:
/tmp/613814454/Main.cpp:16:19: warning: 'gets' is deprecated [-Wdeprecated-declarations]
Name<decltype(gets)> n;
^
/usr/include/stdio.h:638:37: note: 'gets' has been explicitly marked deprecated here
extern char *gets (char *__s) __wur __attribute_deprecated__;
^
/usr/include/x86_64-linux-gnu/sys/cdefs.h:254:51: note: expanded from macro '__attribute_deprecated__'
# define __attribute_deprecated__ __attribute__ ((__deprecated__))
^
/tmp/613814454/Main.cpp:16:26: error: implicit instantiation of undefined template 'Name<char *(char *)>'
Name<decltype(gets)> n;
^
/tmp/613814454/Main.cpp:12:25: note: template is declared here
template <class> struct Name;
^
1 warning and 1 error generated.
gets
is marked as deprecated and has the signature char *(char *)
. But then how is FirstFactorial(gets(stdin));
compiling?
Let's try something else:
int main() {
Name<decltype(gets(stdin))> n;
// keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;
}
Which gives us:
/tmp/286775780/Main.cpp:15:21: error: implicit instantiation of undefined template 'Name<int>'
Name<decltype(8)> n;
^
Finally we are getting something: decltype(8)
. So the entire gets(stdin)
was textually replaced with the input (8
).
And the things get weirder. The compiler error continues:
/tmp/596773533/Main.cpp:18:26: error: no matching function for call to 'gets'
cout << FirstFactorial(gets(stdin));
^~~~
/usr/include/stdio.h:638:14: note: candidate function not viable: no known conversion from 'struct _IO_FILE *' to 'char *' for 1st argument
extern char *gets (char *__s) __wur __attribute_deprecated__;
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
std::integral_constant<int, gets(stdin)> n;
It compiles.
But
std::integral_constant<int, gets(stdin)> n; // OK
std::integral_constant<int, gets(stdin)> n2; // ERROR wtf??
Doesn't with the expected error at the n2
line.
And again, almost any modification to main
makes the line cout << 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 to main
will spit out an error. Outside of this site use normal input reading methods.
Is this syntax correct?
1) Are there different versions of C?
Yes, and the specific reason that the validity of this code differs across C standards is that you are using the function gets
, which has been deprecated and later fully removed by modern C standards. There is practically no way to use gets
in production code without risking a buffer overflow, so it's recommended to use functions that check the length of the buffer. fgets
is the most common with:
fgets(buffer, BUFFER_SIZE, stdin);
2) Is this code still correct, or would it be better to use char * for the function parameter
There is no difference between the function parameters: char *foo
and char foo[]
, since when an array is passed as an argument to a function, it decays to a pointer to its first element. Both syntaxes are acceptable.
How to check for palindrome using Python logic
A pythonic way to determine if a given value is a palindrome:
str(n) == str(n)[::-1]
Explanation:
- We're checking if the string representation of
n
equals the inverted string representation ofn
- The
[::-1]
slice takes care of inverting the string - After that, we compare for equality using
==
Program works when executed in C++ Tutor, but not anywhere else
the actual answer (missing return at recursion call) was already given.
i'd like to add a simpler version for you to compare and maybe learn something :)
int ggt(int a, int b)
{
if (a < b)
{
std::swap(a, b);
}
if (a%b == 0)
{
return b;
}
return ggt(b, a%b);
}
short explanation:
- the "swap" when a < b ensures b contains the smaller value.
- the "rest" (a%b) is calculated directly when calling the recursion (this avoids storing intermediate values)
- as you see the control flow is simpler, so it is easier to reason about each step of the execution.
Related Topics
Why Doesn't Reference-To-Member Exist in C++
Efficient Bitwise Operations for Counting Bits or Find the Right|Left Most Ones
Sfinae Works Differently in Cases of Type and Non-Type Template Parameters
For Nested Templates, When Did '>>' Become Standard C++ (Instead of '> >')
Sorting a Std::Vector<Std::Pair<Std::String,Bool>> by the String
How to Use SQLite in a Multi-Threaded Application
Is the Size of a Struct Required to Be an Exact Multiple of the Alignment of That Struct
Get Pointer to Object from Pointer to Some Member
"Inline" Keyword VS "Inlining" Concept
The Behaviour of Floating Point Division by Zero
Keyboard Input & the Win32 Message Loop
How to Create a Window in Different Qt Threads
Rand() Generating the Same Number - Even with Srand(Time(Null)) in My Main!
Is It a Good Practice to Define C++ Functions Inside Header Files