Building Blocks of a Cpp Program

Hello everyone with the first C++ post. First of all, we need to talk about building blocks of a C/C++ program. As we said in previous introductory post the C++ is a low level compiled language. This means, when you write and compile a C++ code, it will yield an executable program code which consist of machine instructions. This makes C++ different from newer interpreted languages such as Python, C# or Java. Those are not executed directly on the processor but instead are sent to an interpreter program that is responsible for execution of  the intermediate instructions (byte codes). For example, Java programs are executed by the Java virtual machine (JVM), while C# programs are executed by the Common Language Runtime (CLR).

Due to the C++ programs can be compiled directly to processor instructions, it still broad use in fields where absolute performance necessity. Thus, C++ allows developers to write applications that take full advantage of underlying architecture.

When you would like to write a C/C++ program you will need three essential tools. The first is a compiler. There are several compiler tools in the wild such as 

LLVM Clang and GNU Compiler Collections (gcc) are mostly preferred compiler tools. I also used mostly LLVM Clang. However, other compiler tools which is Visual Studio and Embarcadero is used in large scale enterprise development environment.

Another essential tool is editor

Actually a C++ code consists of 3 essential blocks. 

The first is Header definitions. Header definitions block is responsible for including external library header files. These header files can include other library header definitions, variable definitions, type and class definitions, etc. These header files can include some business logic codes but it isn't recommended, instead it should be handled in source code files.

Second block is constant and variable definitions. Variables are a broad topic which will later involved.

Third and the last block is executable code block. This block can contain some function definitions and bodies. The main() function which is entrypoint of all C/C++ programs also resides here.

Now let's see all of the blocks together with an example. This example is a simple hello world application.

 

#include <iostream>
int main() 
{
    std::cout << "Hello World!" << std::endl;
    return 0;
}


Now, let's examine the "Hello World!" example above line by line to understand how the blocks we mentioned earlier look in practice. This simple piece of code perfectly demonstrates the basic syntax and structure of C++.

1. Header File: #include <iostream>

This line is a preprocessor command. The compiler reads this line before starting the actual compilation process and includes the contents of the header file <iostream> in our code. iostream, which stands for "input/output stream."

This header file contains the definitions necessary for basic input/output operations, such as printing text to the screen (std::cout) or receiving data from the user (std::cin).

This corresponds to the "Header definitions" block that we mentioned at the beginning of the text.

2. Main Function: int main()

This line is the heart of our program. This is exactly where every C/C++ program starts running, that is, the entry point of the program.

  •  main is a special name for a function. When the operating system runs your program, it looks for a function with this name.
  • int indicates that the function will return an integer value. This value is used to inform the operating system whether the program has completed successfully.
  • (), these are parentheses that indicate it is a function.
This is the beginning of the "Executable code block" that we mentioned.

3. Code Block and Commands

The curly braces { and } that follow the main function define the beginning and end of a code block. All commands belonging to the main function are located within these parentheses.

  1. std::cout << "Hello World!" << std::endl;
This is the line of our program that does the actual work. Let's break it down:
  • std::: This is a namespace. In C++, the standard library elements are located within the std (standard) namespace. Namespaces prevent functions or variables with the same name from interfering with each other in large projects. We indicate that cout and endl belong to the standard library with the std:: prefix.
  • cout: it is derived from the words "character output" and represents the standard output stream (usually the console screen).
  • <<: This operator is a stream insertion operator. It "sends" or "appends" the data on its right to the stream on its left. In other words, it sends the text "Hello World!" to the cout stream, or the console.
  • std::endl: stands for "end line." This command does two things: it moves the cursor to the next line (like pressing Enter on a keyboard) and flushes the output buffer, ensuring that text appears on the screen immediately.
  • return 0: This command terminates the main function and returns a value of 0 to the operating system. In programming, a return value of 0 typically means "everything went well, the program completed successfully." If the program had terminated with an error, a value other than 0 (usually 1) would have been returned.

As you can see, with just a few lines of code, we have understood the basic structure of a C++ program: include the necessary libraries, create the main function which is the entry point of the program, and write the commands inside this function.

We are ready to build on this solid foundation. In our next article, we will delve deeper into variables and data types, which are the most fundamental building blocks of our programs. By learning how to store data and manipulate it, we will begin to write more meaningful and dynamic programs. Stay tuned!

Yorumlar

Popüler Yayınlar