C++ Basic Syntax

The purpose of this article was to provide and explain the basic syntax of a C++ program. This article also covers all of the other topics associated with C++ basic syntax.

to comprehend the fundamental syntax of any C++ program. Let's take a look at this basic and general form of a C++ program.

#include<header_file_name>
using namespace std;
int main()
{
   /* The C++ code
    * will be written
    * here
    */

   return 0;
}

For example:

#include<iostream>
using namespace std;
int main()
{
   cout<<"Hello, my name is William.";

   return 0;
}

This C++ program will produce the following output. I compiled and executed the above C++ program in the "Code::Blocks" IDE.

c++ basic syntax example program

Now let me explain all the codes written in the above example, one by one:

The first line, which is:

#include<iostream>

indicates that the "iostream" header file has been included in the program. As a result, we have access to all of the objects that are available or defined in it. The term "iostream" refers to a "Input/Output Stream." This header file was included so that I could use its object "cout" to output text to the output console.

Please note: Header files are those that appear at the beginning of a program. Because it is not possible or advisable to write the definitions of all of the most commonly used classes in almost every C++ program, the C++ creator created header files that contain the definitions of those classes so that we can include only the header file and access all of its objects to use them in the program. This saves us time while also making the program appear shorter, easier to read, and more understandable.

Now let's come to the second line of code, which is:

using namespace std;

which is used when all standard namespaces must be used without the "std::" prefix. The majority of C++ programmers used this line of code before the "main()" method to avoid writing "std::" before "cout" and "cin," as well as all other methods in the "std namespace." That is, if we remove this line of code, we must rewrite the preceding C++ program as follows:

#include<iostream>
int main()
{
   std::cout<<"Hello, my name is William.";

   return 0;
}

As a result, if your C++ program contains multiple "cout" and "cin" functions, as well as other functions from the "std namespace," it is best to use a single line of code to avoid writing "std::" before each "std namespace" function. However, I do not believe that professional C++ programmers should use "using namespace std;" to avoid conflicting names. To avoid this conflict, put related or required namespaces right before the name.

Now let me explain the third line of code, which is:

int main()

This line of code will be found in each and every C++ program. This can be called the "main()" function. The "main()" function is that from which the execution of the program starts.

The "int" is the data type, and the data type before any function definition means that the function will return the value of that type. Therefore, I have written "return 0;" as the last statement of the "main()" function.

Now I have an opening curly bracket, which indicates that the definition of the function starts. Then I wrote the following code:

cout<<"Hello, my name is William.";

which prints the text "Hello, my name is William." on the output console. Because "cout" is an object of the class "iostream," it is used to output the data to the standard output device such as a monitor.

Then I used the following last line of C++ code:

return 0;

This means that the function "main()" has finished successfully. The closing curly bracket then indicates that the "main()" function definition is complete.

Please note: Anything written between /* and */  will be ignored by the compiler. The text between these two will be referred to as "comments." You can learn more about it in a separate article.

Constants in C++

To create a constant variable in C++, use the "const" keyword before defining a normal variable. As an example:

#include<iostream>
using namespace std;
int main()
{
   const float pi = 3.1416;
   float rad = 10.3;

   cout<<"Area = "<<pi*rad*rad;
   cout<<endl;

   return 0;
}

When the above C++ program is compiled and executed, it will produce the following output:

c++ basic syntax constant example

I used the following C++ statement in the above example

cout<<endl;

only to create a single line space between the output text and the "Process returned 0 (0x0) execution time: 0.047 s" for your convenience to make the output text more visible.

Escape Sequences in C++

Characters that are part of an escape sequence do not actually represent themselves; rather, they generate other outputs. For instance, inserting the character "\n" into the escape sequence causes a new line to be displayed on the output console. The following is a list of characters that can be used as escape sequences in C++.

For example:

#include<iostream>
using namespace std;
int main()
{
   cout<<"Hello,";
   cout<<"\nmy name is William,\n\n\n";
   cout<<"\tand \t\tI\'ve been a ";
   cout<<"\n\"C++ programmer\"";
   cout<<" for \'eight years.\'";

   cout<<endl;

   return 0;
}

When the above program is compiled and executed, it will produce the following output:

c++ basic syntax escape sequence

More Examples

Here are some C++ programs that you might enjoy:

C++ Quiz


« Previous Tutorial Next Tutorial »