Essential Statements of a C++ program

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.....
Following is a minimal valid C++ program: int main() {}
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
Your second snippet is incorrect, you need to include iostream to gain access to cout.
The second snippet is still wrong, you forgot to prefix endl as std::endl.
Topic archived. No new replies allowed.