- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Environmental Science
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a program in C++ to print Hello World
Program : To print hello world using C++
// Your First C++ Program
#include
int main() {
std::cout << "Hello World!";
return 0;
}
Output :
Hello World!
Explanation:
1. // Your First C++ Program
In C++, any line starting with // is a comment. Comments are intended for the person reading the code to better understand the functionality of the program. It is completely ignored by the C++ compiler.
2. #include
The #include is a preprocessor directive used to include files in our program. The above code is including the contents of the iostream file.
This allows us to use cout in our program to print output on the screen.
For now, just remember that we need to use #include to use cout that allows us to print output on the screen.
3. int main() {…}
A valid C++ program must have the main() function. The curly braces indicate the start and the end of the function.
The execution of code beings from this function.
4. std::cout << “Hello World!”;
std::cout prints the content inside the quotation marks. It must be followed by << followed by the format string. In our example, “Hello World!” is the format string.
Note: ” ; ” is used to indicate the end of a statement.
5. return 0;
The return 0; statement is the “Exit status” of the program. In simple terms, the program ends with this statement
Note : If you dont want to use std::cout you can used namespace.
#include
using namespace std;
int main()
{
cout<<"Hello world";
return 0;
}
Output :
Hello World!
- Related Questions & Answers
- Write C++ Program to Find Quotient and Remainder
- Write C++ Program to Print Number Entered by User
- Write C++ Program to Add Two Numbers
- Write a program in C++ to print Hello World
ads