Essential Statements of a C++ program

Oct 10, 2015 at 7:54am
Actually i want to know about the essential statements of a C++ program without which, the code will not become a C++ program....

I have a Multiple Choice Question related to this which I have mentioned below.

Every C++ program must have a
a)cout statement
b)function main
c)#include statement
d)All of above

Tell the write option with logical explaination and reasons so that my concept would be cleared.....
Oct 10, 2015 at 8:21am
Following is a minimal valid C++ program: int main() {}
Oct 10, 2015 at 10:13am
What @MiiNiPaa said is correct, that is all you need to write a program, but the the program in his comment doesn't do anything, but is it still the minimal valid c++ program.

The first program most, if not all programmers write is the classic "Hello World". It looks like this.

1
2
3
4
5
6
7
8
#include <iostream>

using namespace std;

int main(){
      
      cout << "Hello World" << endl;
}


This one includes all of the ones you mentioned, includning the iostream and namespace std. You do not need the namespace std, instead you can just do this

1
2
3
4
5
6
#include <iostream>

int main(){

     std::cout << "Hello World" << std::endl;
}


Edit: Fixed the second snippet, thanks @MiiNiPaa
Edit2: Damn it still got the snippet wrong, fixed it now thx @LB...
Last edited on Oct 10, 2015 at 1:18pm
Oct 10, 2015 at 10:25am
Your second snippet is incorrect, you need to include iostream to gain access to cout.
Oct 10, 2015 at 12:59pm
The second snippet is still wrong, you forgot to prefix endl as std::endl.
Topic archived. No new replies allowed.