0

I have been trying to use Visual Studio to run my C++ codes, but I couldn't find a way around it. I came across this extension called C/C++ Compile Run, which was simple to use. I also use the standard C/C++ Microsoft Extension.

I then tried it out by running this code:

#include<iostream>
using namespace std;
int main(){
    cout<<"hello";
    char x[10];
    cout<<"\nEnter a string please: ";
    gets(x);
    puts(x);
}

I forgot to include stdio.h, but the code compiled successfully with the C/C++ Compile Run extension. It gave this output:

hello
Enter a string please: string
string

But, I didn't include stdio.h. Is there any way to fix this? Or should I try another way of compiling my C++ code?

Henry Woody
  • 14,024
  • 7
  • 39
  • 56
  • Standard library headers may themselves include other headers. You are just not guaranteed what they include with any given impletation. – Jesper Juhl Apr 01 '19 at 04:18
  • Headers can include other headers. You can't count in it, but `iostream` may well be including the other headers you require. – user4581301 Apr 01 '19 at 04:19
  • `stdio.h` gets included indirectly. Don't count on such behavior. Include the appropriate standard header file for the functions you are using. That will keep your code clean. – R Sahu Apr 01 '19 at 04:19
  • Haven't found a general duplicate, but [Why does omission of “#include ” only sometimes cause compilation failures?](https://stackoverflow.com/questions/9539650/why-does-omission-of-include-string-only-sometimes-cause-compilation-failur) is pretty close – user4581301 Apr 01 '19 at 04:39
  • If you are working on Linux, just go to this path **/usr/include/x86_64-linux-gnu/c++/** and visit **iostream** header file. It includes **c++config.h** file which include other c header file and goes on. As mentioned in above comments, it's always good to include your required header files explicitly. – Vencat Apr 01 '19 at 04:48
  • Is there any way I can prevent it from being included directly? I mean, it'd be useful if I forget to include any in the future. – CatGlasses77 Apr 01 '19 at 10:27
  • Not really. The including header probably is written in such a way that it will not function without the included header and headers should be complete entities, including everything they need to prevent other (often worse) families of errors. – user4581301 Apr 01 '19 at 15:03

0 Answers0