In a computer language, “syntax” refers to a set of pre-defined protocols or rules that must be followed. Each programming language has its very own syntax. C++ Syntax, on the other hand, is important.
We would not take any difficult programs into consideration at this time. We’d start with the “Hello world!” program, which is the most popular program for novices. At the conclusion of this training, you will be able to:
- The basic structure of a C++ application should be recognizable to you.
- C++ is a case-insensitive programming language.
- As a result, uppercase and lowercase characters receive distinct treatment.
- It’s important to remember this when programming in C++.
- In C++, the majority of the keywords are written in lowercase.
- A semicolon must always be used to end a statement “;” .
- Between keywords, adequate whitespace should be supplied.
The following is a list of basic C++ syntax:- Let’s consider the “Hello world!” program in order to gain a clear and succinct knowledge of the C++ syntax. This is the most basic C++ program you can write.
Here’s a C++ application that prints “Hello world!” on the screen:-
#include<iostream> using namespace std; /* This is the first C++ programme I've ever written */ int main() { cout<<"Hello world!"<<endl; return 0; }
OUTPUT:-
Hello world! |
Contents
Components of the C++ Program:-
From the above program, it is pretty clear that there are various components in a C++ program which include header files, the main function, and the body of the program.
Now, let us discuss each and every component of the C++ program in detail.
#include <——— Header files using namespace std; <———Namespace statement /* This is first C++ program */ <——— Multi line comment //Main function <——— single line comment int main() <——— return type of main function { <——— opening bracket of main function cout<<“Hello world!”<0; <——— return null value } <——— closing brackets |
2) Header Files:- In the above program, we have used one header file.
#include<iostream>
In this header file, ‘iostream’ stands for the input-output stream.
In general, a header file instructs the C++ compiler to include all the functions associated with that header file.
Here, the header file allows you to use the input and output operations. We have performed an output operation of displaying the message “Hello world!” on the screen with the help of the cout function.
3)Namespace: A namespace is a declarative zone that contains all of the identifiers (a unique name assigned to variables and functions based on a set of rules).
- We have written the following sentence in the “Hello world!” program:
- using the std namespace;
Every C++ program has the above statement. It’s a new idea introduced by the most recent C standards.
- The C++ compiler is told to utilize the standard namespace by this statement.
- The scope resolution operator “::” is another means of using namespace.
This is how we will use it:
std::cout<<“Hello world!”;
It instructs the compiler that every time you write cout
you are referring to std::cout
4.)Main Function
The main function is required in every C++ program.
In the main function, every C++ statement is executed line by line.
The return type of the main function must be specified as int, according to the current C++ Standards.
5) { & }
The curly brackets ” { and } ” respectively signify the beginning and end of a statement.
The opening and closing brackets in the “Hello world!” program indicate the start and end of the main function.
6.)Print Statement
In order to print or display a message on the screen, use the ‘cout’ function. The ‘cout’ function is followed by the output operator ‘<<’ followed by the text to be printed enclosed within double-quotes.
It is similar to the printf() function in C except for the fact we do not require format specifiers.
cout<<“Hello world!”<<endl;
Here, the endl statement stands for the end of the line. It instructs the compiler to move to the new line. It is similar to the ‘\n’ escape sequence.
7.)return 0;
The return 0 statement signifies that the main function would return a null value.
Comments in C++
Comments in C++ are statements that are not executable when the program is run. They are simply used for the user’s convenience.
There are 2 types of comments in C++:
Single line comments: Anything written b=after ‘//’ operator is considered as a single-line comment
For instance,
// Welcome to Developers Dome tutorials! |
Multi-line comments: Anything written within ‘/’ and ‘/’ is considered as a multi-line comment
For instance,
/* Welcome to Developers Dome tutorials! We hope you enjoy our tutorials! */ |
In the “Hello World” program, We have used a multi-line comment
/* This is first C++ program */ |
9 Whitespaces in C++
White spaces are nothing but blank spaces. Whitespaces separate elements of a program from one another.
For instance,
In the statement
int value;
If you don’t separate int and value with whitespace then the statement would make no sense and you would get a compilation error.
10 Semicolons in C++:-
We use semicolons in C++ to indicate the termination of a statement. If they are not used as and when required, you would get a compilation error.
We hope that this article will assist you in understanding all about the C++ Basic Syntax. We have concentrated on making a basic, meaningful, and easy-to-learn guide to the concepts. Still, if you have any problems regarding this, please post them in the comment section, we will be glad to assist you.
Example:
#include<iostream> using namespace std; int main() { cout<<"Hello World"; cout<<"Developer's Dome"; return 0; }
Output:
Hello World Developer’s Dome |
Indentation: The cout and return statements have been indented or relocated to the right side, as you can see. This is done to improve the readability of the code. It is irrelevant in a program like Hello World, but as programs become more complicated, it makes the code more understandable and less error-prone. As a result, indentations and comments must always be used to make the code more understandable.
#include: In C++, all lines that begin with the pound (#) sign are referred to as directives, and they are processed by the preprocessor, which is a program that is run by the compiler. The #include directive instructs the compiler to include a file while the #include<iostream> directive instructs the compiler to include an iostream. It instructs the compiler to include the standard iostream file, which contains declarations for all of the library’s standard input/output functions.
using namespace std: This is used to import the entire std namespace into the program’s current namespace. The use of the namespace std in a statement is often frowned upon. Importing a namespace effectively pulls all type definitions into the current scope. The standard namespace is enormous. An option to this statement is to use the scope operator(::) to specify the namespace to which the identifier belongs each time we create a type.
int main(): This line declares the “main” function, which returns data of the integer type. A function is a collection of statements that accomplish a specified activity. Regardless of where the main() function is located in the program, the execution of every C++ program starts with it. As a result, a main() method is required in every C++ application.
{ and }: The opening braces ” represent the start of the main function, while the closing braces ” represent the end of the main function. The main function’s body is made up of everything between these two points.
std::cout<<“Hello World”; The compiler is instructed to display the message “Hello World” on the screen by this line. In C++, this line is known as a statement. Every remark is intended to accomplish a specific goal. To finish a statement, use the semi-colon ‘;’. The semicolon character at the conclusion of the sentence denotes that the statement is coming to a close. The standard character output device, which is normally the desktop screen, is identified via the std::cout command. Everything that comes after the character “” is sent to the output device.
c basic syntax c basic syntax c basic syntax c basic syntax c basic syntax c basic syntax c basic syntax c basic syntax c basic syntax c basic syntax c basic syntax
You may like:
Features of C++ | C++ Tutorial
Pingback: While and do...while loop with Example | C++ Programming