What the heck is gets(stdin) on site coderbyte?C-Programming: return of gets(stdin) why is it integer?What is the difference between #include <filename> and #include “filename”?What are the differences between a pointer variable and a reference variable in C++?What does the explicit keyword mean?What is the “-->” operator in C++?What are move semantics?What is the copy-and-swap idiom?What is The Rule of Three?What are the basic rules and idioms for operator overloading?What is a lambda expression in C++11?Why is reading lines from stdin much slower in C++ than Python?

How to test the sharpness of a knife?

Does Doodling or Improvising on the Piano Have Any Benefits?

Why didn’t Eve recognize the little cockroach as a living organism?

Why is the principal energy of an electron lower for excited electrons in a higher energy state?

Why is the sun approximated as a black body at ~ 5800 K?

Air travel with refrigerated insulin

ContourPlot — How do I color by contour curvature?

What's the name of the logical fallacy where a debater extends a statement far beyond the original statement to make it true?

Pre-Employment Background Check With Consent For Future Checks

How were servants to the Kaiser of Imperial Germany treated and where may I find more information on them

Confusion over Hunter with Crossbow Expert and Giant Killer

Why does a 97 / 92 key piano exist by Bösendorfer?

Overlapping circles covering polygon

What is the smallest number n> 5 so that 5 ^ n ends with "3125"?

Can I cause damage to electrical appliances by unplugging them when they are turned on?

Storage of electrolytic capacitors - how long?

Mimic lecturing on blackboard, facing audience

How to get directions in deep space?

How to make a list of partial sums using forEach

Giving feedback to someone without sounding prejudiced

Animation: customize bounce interpolation

Ways of geometrical multiplication

What does the word 'upstream' mean in the context?

If the only attacker is removed from combat, is a creature still counted as having attacked this turn?



What the heck is gets(stdin) on site coderbyte?


C-Programming: return of gets(stdin) why is it integer?What is the difference between #include <filename> and #include “filename”?What are the differences between a pointer variable and a reference variable in C++?What does the explicit keyword mean?What is the “-->” operator in C++?What are move semantics?What is the copy-and-swap idiom?What is The Rule of Three?What are the basic rules and idioms for operator overloading?What is a lambda expression in C++11?Why is reading lines from stdin much slower in C++ than Python?













27















coderbyte is an online coding challenge site (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 the first one is using namespace std but that is irrelevant to my issue here.










share|improve this question




























    27















    coderbyte is an online coding challenge site (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 the first one is using namespace std but that is irrelevant to my issue here.










    share|improve this question


























      27












      27








      27


      4






      coderbyte is an online coding challenge site (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 the first one is using namespace std but that is irrelevant to my issue here.










      share|improve this question
















      coderbyte is an online coding challenge site (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 the first one is using namespace std but that is irrelevant to my issue here.







      c++ input gets standards-compliance






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 53 mins ago









      Cody Gray

      195k35382469




      195k35382469










      asked 4 hours ago









      bolovbolov

      32.2k675139




      32.2k675139






















          2 Answers
          2






          active

          oldest

          votes


















          27














          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.






          share|improve this answer




















          • 10





            I'm genuinely amazed. Maybe this Q/A can be a canonical post on why not to learn from coding challenge sites.

            – alter igel
            3 hours ago






          • 10





            Something really evil is happening, and I think it's at the level of text replacement in the source code outside of the compiler. Try this: std::cout << "gets(stdin)"; and the output is 8 (or whatever you type into the 'input' field. This is a disgraceful abuse of the language.

            – alter igel
            3 hours ago







          • 6





            @Stobor note the quotes around "gets(stdin)". That's a string literal that even the preprocessor wouldn't touch

            – alter igel
            3 hours ago






          • 8





            "Guaranteed to Make You a Better Coder" ... uff

            – user463035818
            3 hours ago






          • 6





            double-u -- tee -- eff !! My first impression was that someone added some header that replaced gets() function with something that returns an object that is implicitly-convertible to int/float/etc.. but what bolov found out is simply reactiongifs.com/r/oh-shi.gif

            – quetzalcoatl
            3 hours ago



















          16














          I tried the following addition to main in the coderbyte editor:



          std::cout << "gets(stdin)";


          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 string gets(stdin) to the standard output. And yet we see the following output, when compiled and run on coderbyte:



          8


          Where the value 8 is taken straight from the convenient 'input' field under the editor.



          Magic code



          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 an abomination and abuse of the language that's far worse than careless preprocessor macros.



          In the context of an online coding challenge website, I would shun this, firstly because it teaches unconventional, non-standard, meaningless, and at least unsafe practices like gets(stdin), and secondly because it teaches that such a construct is basically the hand of god reaching through your source code, defying all syntax and rules.



          I'm sure it can't be this hard to just use std::cin and just stream input to a program.






          share|improve this answer

























          • and it's not even a blind "find and replace" because sometimes it replaces it sometimes it does not.

            – bolov
            3 hours ago






          • 2





            @bolov could it be just the first occurrence of gets(stdin) that is replaced? I meant 'blind' in the sense that it appears to be unaware of the language's syntax or grammar.

            – alter igel
            3 hours ago











          • yes, you are right. It replaces the first occurence. I tried putting one before main and that's what I got indeed.

            – bolov
            3 hours ago











          • Further research suggests that that site does it for all languages, not just C++ - python/ruby it uses the function call ("raw_input()" or "STDIN.gets") which would typically return a string from stdin, but ends up doing a string substitution of that string instead. I guess finding a regex match for the getline function was too hard, so they went with gets(stdin) for C/C++.

            – Stobor
            26 mins ago











          • @Stobor dang, you're right. I can confirm this happens for Java too, the line System.out.print(FirstFactorial(s.nextLine()9)); prints 89 even when s is undefined.

            – alter igel
            23 mins ago











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55269252%2fwhat-the-heck-is-getsstdin-on-site-coderbyte%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          27














          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.






          share|improve this answer




















          • 10





            I'm genuinely amazed. Maybe this Q/A can be a canonical post on why not to learn from coding challenge sites.

            – alter igel
            3 hours ago






          • 10





            Something really evil is happening, and I think it's at the level of text replacement in the source code outside of the compiler. Try this: std::cout << "gets(stdin)"; and the output is 8 (or whatever you type into the 'input' field. This is a disgraceful abuse of the language.

            – alter igel
            3 hours ago







          • 6





            @Stobor note the quotes around "gets(stdin)". That's a string literal that even the preprocessor wouldn't touch

            – alter igel
            3 hours ago






          • 8





            "Guaranteed to Make You a Better Coder" ... uff

            – user463035818
            3 hours ago






          • 6





            double-u -- tee -- eff !! My first impression was that someone added some header that replaced gets() function with something that returns an object that is implicitly-convertible to int/float/etc.. but what bolov found out is simply reactiongifs.com/r/oh-shi.gif

            – quetzalcoatl
            3 hours ago
















          27














          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.






          share|improve this answer




















          • 10





            I'm genuinely amazed. Maybe this Q/A can be a canonical post on why not to learn from coding challenge sites.

            – alter igel
            3 hours ago






          • 10





            Something really evil is happening, and I think it's at the level of text replacement in the source code outside of the compiler. Try this: std::cout << "gets(stdin)"; and the output is 8 (or whatever you type into the 'input' field. This is a disgraceful abuse of the language.

            – alter igel
            3 hours ago







          • 6





            @Stobor note the quotes around "gets(stdin)". That's a string literal that even the preprocessor wouldn't touch

            – alter igel
            3 hours ago






          • 8





            "Guaranteed to Make You a Better Coder" ... uff

            – user463035818
            3 hours ago






          • 6





            double-u -- tee -- eff !! My first impression was that someone added some header that replaced gets() function with something that returns an object that is implicitly-convertible to int/float/etc.. but what bolov found out is simply reactiongifs.com/r/oh-shi.gif

            – quetzalcoatl
            3 hours ago














          27












          27








          27







          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 hours ago









          scohe001

          8,15212442




          8,15212442










          answered 4 hours ago









          bolovbolov

          32.2k675139




          32.2k675139







          • 10





            I'm genuinely amazed. Maybe this Q/A can be a canonical post on why not to learn from coding challenge sites.

            – alter igel
            3 hours ago






          • 10





            Something really evil is happening, and I think it's at the level of text replacement in the source code outside of the compiler. Try this: std::cout << "gets(stdin)"; and the output is 8 (or whatever you type into the 'input' field. This is a disgraceful abuse of the language.

            – alter igel
            3 hours ago







          • 6





            @Stobor note the quotes around "gets(stdin)". That's a string literal that even the preprocessor wouldn't touch

            – alter igel
            3 hours ago






          • 8





            "Guaranteed to Make You a Better Coder" ... uff

            – user463035818
            3 hours ago






          • 6





            double-u -- tee -- eff !! My first impression was that someone added some header that replaced gets() function with something that returns an object that is implicitly-convertible to int/float/etc.. but what bolov found out is simply reactiongifs.com/r/oh-shi.gif

            – quetzalcoatl
            3 hours ago













          • 10





            I'm genuinely amazed. Maybe this Q/A can be a canonical post on why not to learn from coding challenge sites.

            – alter igel
            3 hours ago






          • 10





            Something really evil is happening, and I think it's at the level of text replacement in the source code outside of the compiler. Try this: std::cout << "gets(stdin)"; and the output is 8 (or whatever you type into the 'input' field. This is a disgraceful abuse of the language.

            – alter igel
            3 hours ago







          • 6





            @Stobor note the quotes around "gets(stdin)". That's a string literal that even the preprocessor wouldn't touch

            – alter igel
            3 hours ago






          • 8





            "Guaranteed to Make You a Better Coder" ... uff

            – user463035818
            3 hours ago






          • 6





            double-u -- tee -- eff !! My first impression was that someone added some header that replaced gets() function with something that returns an object that is implicitly-convertible to int/float/etc.. but what bolov found out is simply reactiongifs.com/r/oh-shi.gif

            – quetzalcoatl
            3 hours ago








          10




          10





          I'm genuinely amazed. Maybe this Q/A can be a canonical post on why not to learn from coding challenge sites.

          – alter igel
          3 hours ago





          I'm genuinely amazed. Maybe this Q/A can be a canonical post on why not to learn from coding challenge sites.

          – alter igel
          3 hours ago




          10




          10





          Something really evil is happening, and I think it's at the level of text replacement in the source code outside of the compiler. Try this: std::cout << "gets(stdin)"; and the output is 8 (or whatever you type into the 'input' field. This is a disgraceful abuse of the language.

          – alter igel
          3 hours ago






          Something really evil is happening, and I think it's at the level of text replacement in the source code outside of the compiler. Try this: std::cout << "gets(stdin)"; and the output is 8 (or whatever you type into the 'input' field. This is a disgraceful abuse of the language.

          – alter igel
          3 hours ago





          6




          6





          @Stobor note the quotes around "gets(stdin)". That's a string literal that even the preprocessor wouldn't touch

          – alter igel
          3 hours ago





          @Stobor note the quotes around "gets(stdin)". That's a string literal that even the preprocessor wouldn't touch

          – alter igel
          3 hours ago




          8




          8





          "Guaranteed to Make You a Better Coder" ... uff

          – user463035818
          3 hours ago





          "Guaranteed to Make You a Better Coder" ... uff

          – user463035818
          3 hours ago




          6




          6





          double-u -- tee -- eff !! My first impression was that someone added some header that replaced gets() function with something that returns an object that is implicitly-convertible to int/float/etc.. but what bolov found out is simply reactiongifs.com/r/oh-shi.gif

          – quetzalcoatl
          3 hours ago






          double-u -- tee -- eff !! My first impression was that someone added some header that replaced gets() function with something that returns an object that is implicitly-convertible to int/float/etc.. but what bolov found out is simply reactiongifs.com/r/oh-shi.gif

          – quetzalcoatl
          3 hours ago














          16














          I tried the following addition to main in the coderbyte editor:



          std::cout << "gets(stdin)";


          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 string gets(stdin) to the standard output. And yet we see the following output, when compiled and run on coderbyte:



          8


          Where the value 8 is taken straight from the convenient 'input' field under the editor.



          Magic code



          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 an abomination and abuse of the language that's far worse than careless preprocessor macros.



          In the context of an online coding challenge website, I would shun this, firstly because it teaches unconventional, non-standard, meaningless, and at least unsafe practices like gets(stdin), and secondly because it teaches that such a construct is basically the hand of god reaching through your source code, defying all syntax and rules.



          I'm sure it can't be this hard to just use std::cin and just stream input to a program.






          share|improve this answer

























          • and it's not even a blind "find and replace" because sometimes it replaces it sometimes it does not.

            – bolov
            3 hours ago






          • 2





            @bolov could it be just the first occurrence of gets(stdin) that is replaced? I meant 'blind' in the sense that it appears to be unaware of the language's syntax or grammar.

            – alter igel
            3 hours ago











          • yes, you are right. It replaces the first occurence. I tried putting one before main and that's what I got indeed.

            – bolov
            3 hours ago











          • Further research suggests that that site does it for all languages, not just C++ - python/ruby it uses the function call ("raw_input()" or "STDIN.gets") which would typically return a string from stdin, but ends up doing a string substitution of that string instead. I guess finding a regex match for the getline function was too hard, so they went with gets(stdin) for C/C++.

            – Stobor
            26 mins ago











          • @Stobor dang, you're right. I can confirm this happens for Java too, the line System.out.print(FirstFactorial(s.nextLine()9)); prints 89 even when s is undefined.

            – alter igel
            23 mins ago
















          16














          I tried the following addition to main in the coderbyte editor:



          std::cout << "gets(stdin)";


          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 string gets(stdin) to the standard output. And yet we see the following output, when compiled and run on coderbyte:



          8


          Where the value 8 is taken straight from the convenient 'input' field under the editor.



          Magic code



          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 an abomination and abuse of the language that's far worse than careless preprocessor macros.



          In the context of an online coding challenge website, I would shun this, firstly because it teaches unconventional, non-standard, meaningless, and at least unsafe practices like gets(stdin), and secondly because it teaches that such a construct is basically the hand of god reaching through your source code, defying all syntax and rules.



          I'm sure it can't be this hard to just use std::cin and just stream input to a program.






          share|improve this answer

























          • and it's not even a blind "find and replace" because sometimes it replaces it sometimes it does not.

            – bolov
            3 hours ago






          • 2





            @bolov could it be just the first occurrence of gets(stdin) that is replaced? I meant 'blind' in the sense that it appears to be unaware of the language's syntax or grammar.

            – alter igel
            3 hours ago











          • yes, you are right. It replaces the first occurence. I tried putting one before main and that's what I got indeed.

            – bolov
            3 hours ago











          • Further research suggests that that site does it for all languages, not just C++ - python/ruby it uses the function call ("raw_input()" or "STDIN.gets") which would typically return a string from stdin, but ends up doing a string substitution of that string instead. I guess finding a regex match for the getline function was too hard, so they went with gets(stdin) for C/C++.

            – Stobor
            26 mins ago











          • @Stobor dang, you're right. I can confirm this happens for Java too, the line System.out.print(FirstFactorial(s.nextLine()9)); prints 89 even when s is undefined.

            – alter igel
            23 mins ago














          16












          16








          16







          I tried the following addition to main in the coderbyte editor:



          std::cout << "gets(stdin)";


          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 string gets(stdin) to the standard output. And yet we see the following output, when compiled and run on coderbyte:



          8


          Where the value 8 is taken straight from the convenient 'input' field under the editor.



          Magic code



          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 an abomination and abuse of the language that's far worse than careless preprocessor macros.



          In the context of an online coding challenge website, I would shun this, firstly because it teaches unconventional, non-standard, meaningless, and at least unsafe practices like gets(stdin), and secondly because it teaches that such a construct is basically the hand of god reaching through your source code, defying all syntax and rules.



          I'm sure it can't be this hard to just use std::cin and just stream input to a program.






          share|improve this answer















          I tried the following addition to main in the coderbyte editor:



          std::cout << "gets(stdin)";


          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 string gets(stdin) to the standard output. And yet we see the following output, when compiled and run on coderbyte:



          8


          Where the value 8 is taken straight from the convenient 'input' field under the editor.



          Magic code



          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 an abomination and abuse of the language that's far worse than careless preprocessor macros.



          In the context of an online coding challenge website, I would shun this, firstly because it teaches unconventional, non-standard, meaningless, and at least unsafe practices like gets(stdin), and secondly because it teaches that such a construct is basically the hand of god reaching through your source code, defying all syntax and rules.



          I'm sure it can't be this hard to just use std::cin and just stream input to a program.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago









          user2357112

          157k12172266




          157k12172266










          answered 3 hours ago









          alter igelalter igel

          2,92811128




          2,92811128












          • and it's not even a blind "find and replace" because sometimes it replaces it sometimes it does not.

            – bolov
            3 hours ago






          • 2





            @bolov could it be just the first occurrence of gets(stdin) that is replaced? I meant 'blind' in the sense that it appears to be unaware of the language's syntax or grammar.

            – alter igel
            3 hours ago











          • yes, you are right. It replaces the first occurence. I tried putting one before main and that's what I got indeed.

            – bolov
            3 hours ago











          • Further research suggests that that site does it for all languages, not just C++ - python/ruby it uses the function call ("raw_input()" or "STDIN.gets") which would typically return a string from stdin, but ends up doing a string substitution of that string instead. I guess finding a regex match for the getline function was too hard, so they went with gets(stdin) for C/C++.

            – Stobor
            26 mins ago











          • @Stobor dang, you're right. I can confirm this happens for Java too, the line System.out.print(FirstFactorial(s.nextLine()9)); prints 89 even when s is undefined.

            – alter igel
            23 mins ago


















          • and it's not even a blind "find and replace" because sometimes it replaces it sometimes it does not.

            – bolov
            3 hours ago






          • 2





            @bolov could it be just the first occurrence of gets(stdin) that is replaced? I meant 'blind' in the sense that it appears to be unaware of the language's syntax or grammar.

            – alter igel
            3 hours ago











          • yes, you are right. It replaces the first occurence. I tried putting one before main and that's what I got indeed.

            – bolov
            3 hours ago











          • Further research suggests that that site does it for all languages, not just C++ - python/ruby it uses the function call ("raw_input()" or "STDIN.gets") which would typically return a string from stdin, but ends up doing a string substitution of that string instead. I guess finding a regex match for the getline function was too hard, so they went with gets(stdin) for C/C++.

            – Stobor
            26 mins ago











          • @Stobor dang, you're right. I can confirm this happens for Java too, the line System.out.print(FirstFactorial(s.nextLine()9)); prints 89 even when s is undefined.

            – alter igel
            23 mins ago

















          and it's not even a blind "find and replace" because sometimes it replaces it sometimes it does not.

          – bolov
          3 hours ago





          and it's not even a blind "find and replace" because sometimes it replaces it sometimes it does not.

          – bolov
          3 hours ago




          2




          2





          @bolov could it be just the first occurrence of gets(stdin) that is replaced? I meant 'blind' in the sense that it appears to be unaware of the language's syntax or grammar.

          – alter igel
          3 hours ago





          @bolov could it be just the first occurrence of gets(stdin) that is replaced? I meant 'blind' in the sense that it appears to be unaware of the language's syntax or grammar.

          – alter igel
          3 hours ago













          yes, you are right. It replaces the first occurence. I tried putting one before main and that's what I got indeed.

          – bolov
          3 hours ago





          yes, you are right. It replaces the first occurence. I tried putting one before main and that's what I got indeed.

          – bolov
          3 hours ago













          Further research suggests that that site does it for all languages, not just C++ - python/ruby it uses the function call ("raw_input()" or "STDIN.gets") which would typically return a string from stdin, but ends up doing a string substitution of that string instead. I guess finding a regex match for the getline function was too hard, so they went with gets(stdin) for C/C++.

          – Stobor
          26 mins ago





          Further research suggests that that site does it for all languages, not just C++ - python/ruby it uses the function call ("raw_input()" or "STDIN.gets") which would typically return a string from stdin, but ends up doing a string substitution of that string instead. I guess finding a regex match for the getline function was too hard, so they went with gets(stdin) for C/C++.

          – Stobor
          26 mins ago













          @Stobor dang, you're right. I can confirm this happens for Java too, the line System.out.print(FirstFactorial(s.nextLine()9)); prints 89 even when s is undefined.

          – alter igel
          23 mins ago






          @Stobor dang, you're right. I can confirm this happens for Java too, the line System.out.print(FirstFactorial(s.nextLine()9)); prints 89 even when s is undefined.

          – alter igel
          23 mins ago


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55269252%2fwhat-the-heck-is-getsstdin-on-site-coderbyte%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Are there any comparative studies done between Ashtavakra Gita and Buddhim?How is it wrong to believe that a self exists, or that it doesn't?Can you criticise or improve Ven. Bodhi's description of MahayanaWas the doctrine of 'Anatta', accepted as doctrine by modern Buddhism, actually taught by the Buddha?Relationship between Buddhism, Hinduism and Yoga?Comparison of Nirvana, Tao and Brahman/AtmaIs there a distinction between “ego identity” and “craving/hating”?Are there many differences between Taoism and Buddhism?Loss of “faith” in buddhismSimilarity between creation in Abrahamic religions and beginning of life in Earth mentioned Agganna Sutta?Are there studies about the difference between meditating in the morning versus in the evening?Can one follow Hinduism and Buddhism at the same time?Are there any prohibitions on participating in other religion's practices?Psychology of 'flow'

          fallocate: fallocate failed: Text file busy in Ubuntu 17.04? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)defragmenting and increasing performance of old lubuntu system with swap partitionIssue with increasing the root partition from the swapthis /usr/bin/dpkg returned error || ubuntu-16.04, 64bitDefault 17.04 swap file locationHow to Resize Ubuntu 17.04 Zesty Swap file size?Ubuntu freezes from online formsMy Laptop is not starting after upgrade ubuntu 16.04 (Kernel 4.8.0-38 to 04.10.0-36)hcp: ERROR: FALLOCATE FAILED!Not sure my swap is being usedWine 3.0 asking for more virtual free swap

          Where else does the Shulchan Aruch quote an authority by name?Parashat Metzora+HagadolPesach/PassoverShulchan Aruch UTF-8Anonymous glosses in the Shulchan AruchWhy is the Shulchan Aruch definitive?Siman 32, Kitzur Shulchan Aruch: UntranslatedLitvaks/Yeshivish and Shulchan AruchBuying a Shulchan AruchEnglish version of SHULCHAN ARUCHIs there any place where Shulchan Aruch rules with the Rosh against the Rif and Rambam?Are there practices where Sepharadim do not hold by Shulchan Aruch?5th part of the shulchan aruch