PDFVersion
No ads? No problem! Download the PDF book of this tutorial for just $24.99. Your support will help us reach more readers.
Thank You!
C++ Basic Syntax
Let's start by creating our first C++ program called "Hello World". This program will output a single string. Take a look at the provided code, run it yourself, and then we can discuss each part of the code.
#include < filename >; using namespace std; main() { /*this is the body of main() function, also known as block. Everything in between these two curly braces will be executed; */ }
Let's Create our first C++ program, Hello World
. This program will display a single string. Review the provided code, run it, and then we can analyze each section of the code in this tutorial.
#include<iostream> using namespace std; main() { cout<<"Hello World"; //Simple statement to be executed return 0; }
Okay, so first we write the program and then we run it. When we run it, it gives us a message saying Hello World
. Now, it's time to take a closer look at the code and understand how it actually works.
First, we include the Iostream
header file in our program using the include
statement. This header file, which comes with our compiler, allows us to perform input and output operations.
Our computer doesn't know how to execute the program or display the output by default. That's where the Iostream
header file comes in. It defines how to use the "Output" statement and how to display the written code on the screen.
Next, we have the Using Namespace std;
statement. This is a common line of code that we include in almost all C++ programs. For now, just remember to include it at the top of all our programs, right below the include statements. We'll discuss this topic in more detail in the "Namespace Tutorial". Also, don't forget that this line ends with a semicolon ;
which is a syntax requirement in C++. Each statement should always be terminated with a semicolon.
Note:
Don't forget to end your statements with a semicolon (;).
class myclass { /*Not executable*/ } main() { /*This is the sole code block that will execute when we run our program.*/ /*We will include the class for execution here,*/ /*And the function for execution will also be included in this section.*/ } function() { /*Not executable*/ }
The Main
function is a special function that runs when we execute our program. The code inside this function block will only execute when the program is run. We can create other functions and classes outside the "Main" function and include them inside the "Main" function to execute them.
The section of a C++ program that starts with Main()
, followed by an opening brace {
, is called the main function. A C++ program is actually a collection of functions, and the function called "main()" is always required and the first function that gets executed.
In the simple program above, the entire program is within the "Main()" function because the closing brace }
that matches the opening brace {
of "Main()" is at the end of the program. Everything between two matching braces is called a block.
We'll learn more about braces in our Function tutorials.
The is known as the output stream in C and C++. Think of a stream as a door through which data is transferred. In this case, cout
is the stream that takes data from the computer and sends it to the output. Currently, the output is being shown on the screen, so we use "cout" for output.
The <<
sign indicates the direction of data flow. Here, it is towards "cout", and the function of "cout" is to display data on the screen. The string "Hello world" is placed within double quotes, which is how character strings are written in C and C++ programming. Anything written after "<<" and within quotation marks " "
will be directed to "cout" to display on the screen. There is a semicolon ;
at the end of this string because it is a statement. Remember that every C++ statement should be terminated by a semicolon.
There is one more line starting with //
which is known as a single-line comment. We'll discuss comments in our Comments tutorials.
The line return 0
followed by a semicolon, terminates the "main" function and the program. It returns a value of 0 as an exit code to the calling program. It is a standard practice to use the exit code 0 to indicate that a program has terminated correctly.
Paying attention to detail is crucial in programming. A good programmer always carefully analyzes the problem statement, considering all aspects and not being vague. It's important to be clear and precise in describing the program and to pay attention to the program's logic, calculations, and flow. Sometimes a grammatically correct sentence may not make sense, just like the verse from the poem "Through the Looking-Glass" by Lewis Carroll: "Twas brillig, and the slithy toves Did gyre and gamble in the wabe."
The grammar is correct but there is no meaning, Similarly, the sentence, "Mr. ABC sleeps thirty hours every day", is grammatically correct but it an illogical.
So, even if a program is grammatically correct and compiles successfully, it may still produce incorrect or absurd results if the logic is flawed or does not solve the problem. Therefore, it's essential to pay attention to the logic of the program.
Sardar Omar
I did my hardest to present you with all of the information you need on this subject in a simple and understandable manner. However, if you have any difficulties understanding this concept or have any questions, please do not hesitate to ask. I'll try my best to meet your requirements.