Header Ads

Basic Syntax in C++

C++ Basic Syntax

When you consider a C ++ language program, you can define it as a collection of objects that communicate while invoking one another's methods. Let us now tell you what the meaning of class, object, methods and immediate variables is.

  • Object means - objects and states are in objects. For example: a dog has states - its color, name, breed, and behavior alongside it - wagging, barking, and food. An object is an example of a class.

  • Class - In it you can define a class as a template or a blueprint. Which describes the behavior and its states aimed at supporting its kind.

  • Methods - In this you need to know that, a method is basically a behavior. There can be a variety of methods in this category. It occurs in the methods where the argument is written, the data is used in it and all its functions are executed.

  • Instant Variable - It contains a unique set of event variables in each object. In this, the position of any object is created by the values ​​allocated to these frequency variables.

Basic Syntax in C++

C ++ Structure
Let's make a simple code of C ++ programming, which will print the word "I Love you".

#include <iostream>
using namespace std;

// main() is where program execution begins.
int main() {
   cout << "I love you"; // prints I loveyou
   return 0;

Understand this example -

The C ++ language is one of the best programming languages. This language defines many types of titles, which contain many types of information that is essential and useful for your program in C ++. For this program of C ++, the header <iostream> is required. That's why we used it at the beginning of the program.

Namespace is very important in C ++. Line using namespace std; And in that STD tells the compiler to use that namespace (the information reaches the information). Namespace keeps the relatively recent addition of the C ++ program.

This program of C ++ is the next line // main (). Where the program execution starts. A single-line is a comment that is available in the C ++ program. Single-line comments are introduced with // and it is terminated at the end of the line.

Line int main () This is the main task where C ++ program execution is started.

The next line of this program is cout << "I love you". Which causes the message "I love you" to appear on the screen. cout << This word appears on the screen. In C programming, printf () also shows the words on the screens.

The last line return is 0. It ends the main () function. And it returns the value 0 into the calling process. This is necessary at the end of the program, if you do not use it, then this program will continue to run.

Compile and execute the C ++ program
Let's know how to save the C ++ program file, how to compile and run the program. For this, please follow the steps given below -

For this, you open a text editor and add the above code. You can take any text editor in it, just like you do in the HTML code and save it. You can use the software of Windows to provide a C ++ program.

Save the file as: love.cpp
Remember that at the end of the file name is the .cpp.

Now open the command prompt on your computer and go to the directory where you saved the file.

In it you type 'G ++ hello.cpp' and press Enter to compile your C ++ code. If there is no error in your code when compiling it. The command prompt will take you to the next line and generate the executable file. But if there is an error in the code, its execution will stop.

Now, after this you have to type 'a.out' to run your program.

After this you will see the 'I Love you' printed on the window.

$ g++ hello.cpp
$ ./a.out
I Love you

Semi-colon and block in C ++
In the C ++ program, semicolon is a statement terminator. Each individual statement should end with a semicolon in it. This indicates the end of a logical unit in C ++.

for example,
x = y;
y = y + 1;
add(x, y);

A block in C ++ is a complete set of logically connected statements that surround the opening and closing of the braces. for example -

   cout << "I love you"; // prints I love you
   return 0;

C ++ does not recognize the end of the line in a form of terminator. For this reason, it does not matter in C ++ where you put in a row and how to put it. for example -
x = y;
y = y + 1;
add(x, y);
same as
x = y; y = y + 1; add(x, y);

C ++ identifier
A C ++ language identifier is a variable, the variable which is used to identify items defined by the function, class, module, or any other user. Always be an identifier letter, that starts with zero zeros or a z or underscores (_) with zero plus letters, underscores, and digits (0 to 9).

C ++ does not provide this permission within punctuation in languages ​​such as @, $, and% identifiers. C ++ is considered a case-sensitive programming language. In this way, there are two separate identifiers in manpower and manpower C ++.

Some examples of this are:
mohd       zara    abc   move_name  a_123

myname50   _temp   j     a23b9      retVal

A line in C ++ which only contains whitespace, with a comment in C ++, it is known as an empty line, and the C ++ compiler ignores it completely.

WhiteSpace is used to describe empty space, tabs, newline characters and comments in C ++. Whitespace separates one part of a statement from C ++ into another. And enables the compiler to recognize that a statement contains an element, such as int, end, and next element begins.

  • Statement

int age;

In the above statement, to be able to differentiate the C ++ compiler. It should have at least one white space (usually one place) between int and age.

  • Statement 

fruit = apples + mango;   // Get the total fruit

In this statement, no white space between fruit and =, or = and apples is not necessary for the character, if you want it for readability purpose, then you are free to include something in it accordingly.

C++ keywords
This list displays the reserved word of C ++. It is not possible to use these reserved words on a continuous basis or as a variable or another identifier name.

asm else new this
auto enum operator throw
bool explicit private true
break export protected try
case extern public typedef
catch false register typeid
char float reinterpret_cast typename
class for return union
const friend short unsigned
const_cast goto signed using
continue if sizeof virtual
default inline static void
delete int static_cast volatile
do long struct wchar_t
double mutable switch while
dynamic_cast namespace template


  1. Nice and very usefull contact.
    Thank you a.s.chundawat for staring.

  2. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! Webdesign bureau