C++ Syntax With Example By Motionsmag
![]() |
C++ Syntax With Example |
C++ is a programming language that has a syntax similar to C. Here is a simple example of a C++ program that prints "Hello, World!" to the console:
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
This program starts by including the 'iostream' library, which provides input and output functionality. The 'main' function is the entry point of the program and is where the program's execution begins.
The 'cout' object is defined in the iostream library, and is used to print text to the console. The '<<' operator is used to "insert" text into the cout object. The endl is a special manipulator that adds a newline character to the end of the text. Finally, the 'return 0;' statement indicates that the program has completed successfully.
You can also use C++ to create variables, perform calculations, and control the flow of the program with statements such as 'if', 'for', 'while', etc.
Omitting Namespace
You might see the some C++ programs that runs without the standard namespace library.
using namespace std
line can be omitted and replaced with the std
keyword, followed by the ::
operator for some objects:Example:
#include <iostream>
int main()
{
std::cout << "Hello World!";
return 0;
}
Post a Comment