+ 1

What is the effect on my code if I declare the header files after main() in C or C++?

I think it will not affect my code.

6th Mar 2018, 6:16 PM
Arihant Jain
Arihant Jain - avatar
9 Answers
+ 7
If you put the headers after main, you'll get an error message, such as, "cout not declared"; the program displays some output after the error message (at least here in SoloLearn). Headers must go b4 main. https://code.sololearn.com/cQ0QREsS28vV/?ref=app (edit: It only appears to display output. Actually, the line where the error occurred is displayed -- which in this case happens to contain the output statement.)
7th Mar 2018, 12:09 AM
Eric Zatarack
Eric Zatarack - avatar
+ 6
This is similar to writing functions. You can either put the functions before main or you can write prototypes, which are basically just the title of the functions -- with semicolon at end. These prototypes must be above main. Everything must be declared before it can be used, with the exception of external variables. Here you put "extern" in front of int or whatever. These can be placed after the functions they are used in. I wouldn't use them though, since they are like global variables, which can get messed up (with scope, etc.).
7th Mar 2018, 12:48 AM
Eric Zatarack
Eric Zatarack - avatar
+ 6
You're welcome @Arihant Jain. Glad to help, if I can.
7th Mar 2018, 12:02 PM
Eric Zatarack
Eric Zatarack - avatar
+ 1
You cannot declare a header file inside a normal c or cpp file. You have to create it in a different file with a .h extension. After that you include your header file into the cpp file with the main() function in it.
6th Mar 2018, 6:41 PM
JRE
JRE - avatar
+ 1
You can include both the stdio and your file. Like this: #include <stdio.h> //and #include "theNameOfYourFile.h"
6th Mar 2018, 6:47 PM
JRE
JRE - avatar
+ 1
Thank you very much @Eric ZatarackšŸ˜Š
7th Mar 2018, 3:00 AM
Arihant Jain
Arihant Jain - avatar
+ 1
when u write an header file in c or c++ it tells that what inside that program is used for example #include<stdio.h> tells that an standard input and output function is used in that program after that u used main function . if u use main function before header file computer will not understand what types of function are used in that program that's why computer will show an error . and if use main before header it will like u r entering that place about which u don't know anything like what's the behavior of ppls for others etc so due to this u feel uncomfortable and which will cause u will make irritate or bore.
7th Mar 2018, 3:01 AM
Abhishek Shukla
Abhishek Shukla - avatar
0
But, I want to use the predefined library/ header file like #include"stdio.h" in my code. So, tell me what is the effect occurs in my code.
6th Mar 2018, 6:45 PM
Arihant Jain
Arihant Jain - avatar
0
Listen I want to like this in my code:- void main() { int i; printf("Hello"); scant("%d", &i); prints(i); } #include"stdio.h"
6th Mar 2018, 6:53 PM
Arihant Jain
Arihant Jain - avatar