C++ Program

Video Tutorial
FREE
Introduction to C++ thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate
Topics Covered

C++ is a versatile programming language popular in Competitive Programming and Game Development, boasting imperative, object-oriented, and generic features. It operates across various platforms like Windows, Linux, Unix, and Mac. Understanding its basics is crucial for proficiency.

Basic C++ Programming Examples

There are multiple components of simple C++ programs.

Let's try to understand it with the help of the following code snippet.

Output:

Let's break the above code into small components,

  1. #include<iostream> - There are certain files that have various functionalities in them. These files can be included as header files in our code. #include is the preprocessor directive which tells the compiler to add this iostream file in code before its compilation. iostream is one of the libraries in C++ which contains all the operations to work with the inputs and outputs.

  2. int main() - It is known as the main function. Execution of the C++ basic programs starts from here, and this function is called by the operating system. At the end of this function, we return 0 to the operating system which indicates the successful execution of the program. Returning 0 and not any other number is just a matter of a convention. We can also return 1 to indicate program gave some unexpected results. C++ standards require main() to return int and not a void, so we don't use void main().

  3. std::cout - It comes from the iostream library and helps to display the message on the console. It is present in the standard output stream.

  4. std::cin - It comes from the iostream library and helps to take input from the console. It is present in the standard input stream.

Compile and Execute C++ Basic Programs Examples

It is not possible to directly run the basic C++ programs.

There are certain things to do for the execution of a program.

First, we need to have a C++ compiler installed in our system which helps to convert the above source code into machine code.
There are various processes involved in this process as follows -

1. Preprocessor - It handles the statements that start with #. In our case #include<iostream> is handled by preprocessor, and this code is then replaced with a temporary processed file.
2. Compiler - Compiler is also known as the translator and as the name suggests, it converts C++ source code into machine-understandable code. It gives the object file.
3. Linker - Linker takes the object file from the compiler and links it with other library files and the runtime files to create the executable(.exe) file.
compiler

Follow the next steps to run the code.

  • Open the text editor and write the above code.
  • Save this file as firstcode.cpp, here firstcode is the name of file and .cpp is the extension for C++ code.
  • Now open the cmd or power shell in the directory where our file is present.
  • Use the g++ command to compile our file and -o to generate object file.
  • The above command will compile the firstcode.cpp file with g++ compiler and generates the executable file with the file name mycode as explained above.

  • If there is some syntax error in our code, it gets detected at the time of compilation process, and appropriate error gets displayed on the command prompt.

  • Now, if there is no syntax error, then we simply run the code by typing the executable file name i.e. mycode in cmd or by doing a double click on generated executable file in same directory.

  • Executable files contains the machine code which are direct instructions for the CPU. It directly gets loaded in memory by above instruction and starts to execute.

Below are the some C++ Programming Examples:

Semicolons and Blocks in C++

In the above program, you may have observed one thing that after the end of every statement, we used the semicolon (;;).

In the way C++ language is built, it does not recognize the end of a statement.
We need to terminate the statement explicitly with the help of a semicolon.

Above are the 2 different statements each one is terminated by the semicolon. The above code is similar to the following code

In the simple C++ programs, there are multiple code blocks or scopes are created for the logical design.

One code block is written in between curly braces({...}). Code block is the set of logically connected statements.
Example:

The above example represents one code block. The code statements are written in between the opening and closing curly braces.

One more reason to call it scope is because variable or object declared in particular block is only accessible in that block.

Example.

Identifiers in C++ programs

In simple C++ program, we give names for every variable, function, class, or any other entity in a code.

All this names need to follow a certain set of rules at the time of declaration.

The names which fulfills these rules and identify the entities uniquely are known as identifiers.

In identifiers, we can use characters from AZA-Z or aza-z. We can also use underscores(_) or digits(090-9).

The C++ programming language does not allow some symbols like @, $, and % to be used within identifiers.

C++ is case-sensitive programming language thus, ScaleR and scaler are two different identifiers.

Examples of some of the acceptable identifiers in C++ are as follows-

Trigraphs in C++ Programs

In some of the old computer systems, some ASCIIASCII characters are missing, to represent these characters trigraphs are created.

Trigraph is the sequence of 33 symbols to represent another symbol or character.

Trigraphs and their replacements are as given in the below table -

TrigraphReplacement
??=??=#
??/??/\
????'^
??(??([
??)??)]
??!??!|
??<??<{
??>??>}
????-~

Whitespace in C++

Whitespace in C++ is vital for code structure and readability. A blank line, composed of whitespace or comments, is disregarded by the compiler. Whitespace includes blanks, tabs, and newlines, aiding in distinguishing elements in statements. For instance:

In the above example, whitespace separates the variable declarations, and in the expression, it enhances readability by spacing out the operands and the multiplication operator. A simple C++ program relies on whitespace for a clear and organized representation of code, promoting better understanding and maintainability.

FAQs

Q. What is the purpose of whitespace in C++? A. Whitespace aids in code structure and readability by separating elements within statements and allowing for clear organization.

Q. Why are semicolons necessary in C++ programs? A. Semicolons are required to explicitly terminate statements in C++ programs, indicating the end of each instruction.

Q. What is the role of the main() function in C++? A. The main() function serves as the entry point for execution in C++ programs, where code execution begins and ends.

Conclusion

  • There are so many components in a C++ basic program.
  • Before executing the C++ source code, it needs to go through compilation, where it gets converted into machine understandable code.
  • Simple C++ program is written with the help of code statements, and all these statements are logically arranged inside the block of code.
  • Identifiers are the acceptable names given to entities in code that uniquely identifies that entity.
  • Trigraph is a sequence of 33 characters used to represent the missing ASCII characters in old computer systems.