Check out the First Program in C++

0
1902
Intoduction to C++

Intoduction to C++Now, we are ready to take-off, so please tighten your seat belt, and close your mobile.
Bon voyage!!!

Our First Program
As usual, our first program will be a simple one that prints a short hello message. We will go together through the procedure to write, compile, and execute the program. So, let’s start and take it step by step:

1. At the beginning, we need to start our IDE (the Turbo C++). As we have learned from the previous article, double-click the Turbo C++ shortcut on the desktop.
2. Click the File menu, and then click New.
3. Now, type the code as follows:

1

4. Save your program:
a) Click the File menu, then Save.

2

b) A “Save File As” dialog is open, asking for the directory and name to save your file as.

3

c) Type First.CPP as the file name, and click OK. The file name appears in the top of the editor window.

4

5. After writing and saving the program, it should be compiled. The compiler does two main operations:
a) Checks the entire code for any syntax errors, or typing errors.
b) Converts (translates) the C++ code into object code. The resulting object code is set of instructions that the machine can understand (machine language).

To compile the program, either click the Compile menu, then Click Compile, or press Alt+F9.

5

6. The following dialog is open, showing warnings and errors (if any). If the compilation is done without errors, you will notice the word Success at the bottom of the compiling dialog.

6

7. As I told you in step 5, the result of (successful) compilation is translating the CPP (C++ code) file into object file. To prove this, I opened my Windows Explorer, and browsed to the Turbo C++ installation directory (defaults to C:\TurboC4). The produced object files are located under the directory C:\TurboC4\TC\SOURCE. You can easily notice what we are searching for.

7

8. To be able to execute your program, the produced object file(s) should be linked together to produce an executable file. For this mission, a special program called Linker is called:

8

9. Another dialog is opened to show the results of the linking operation.

9

10. As you have already expected, the produced executable should be somewhere under the Turbo C++ installation directory:

10

11. Now, it is time to reap the fruit. To run the program, either click the Run menu, then Run, or press Ctrl+F9

11

– So, what?! Where is the program output? Nothing shown!!!

Just wait, step 12 will answer your question.

12. To see the output, either click the Window menu, then User screen, or press Alt+F5.

12

13. The output will be shown as below:

13

We have made it!!! Hurray!!!!

Learn the Basics of C Programming Language
Explanation

– Does this simple program need explanation?!

Sure, and you will see within minutes how this short program will tell us many important things about C++ program structure. Be my guest!!

14

Preprocessor Directives

In the first line of our first program, you will notice a strange statement:

This statement is called a preprocessor directive. As its name indicates, it is a directive (instruction) to the compiler to “include” (insert) the file stdio.h into our source code. When seen by the preprocessor (a part of the compiler that takes care of these directives before the actual compilation process starts), the #include directive is replaced by the specified file.

Preprocessor directives must:

• Be written on a separate line of its own.
• Be preceded by the # character.

– OK, move on to the second line.

Wait, we are not finished with the first one yet!!!

The file stdio.h is called a header file. This file (stdio.h) contains normal C code that defines the necessary functions needed for performing Standard Input and Output operations.
When included by the #include directive, the preprocessor imports the code in this file, and use it as part of our program. (Does this remind you of something? Bravo, the import statement in Python. You are really excellent!!)

15

As you can see, stdio.h is just one of a long list of header files that comes ready-to-use with the C/C++ compiler (Turbo C++ in our case).

Now, we can switch to the next line.

void main()
Line 2 introduces the main() function. The main() function is the core of any C/C++ program. If your program doesn’t contain a main() function, the compiler will not complain, but the linker will shout in your face!!!

16

Although your program’s source file could start by listing first the definitions of classes and functions of your own definition followed by the main() function, when it comes to the execution, program execution will always start with the main() function. A rule with no exception!!

The keyword void that preceded the definition of the main() function means the function returns nothing. So, nothing expected to be returned by the main() function.

Lines 3 and 5: curly braces are used in C/C++ to delimit a function body.

Line 4: the body of the main() function. It uses the built-in function printf() to write the hello message to the standard output (screen). Notice that the statement was ended with a semi-colon “;”. As a general rule, a semi-colon is required to mark the end of a C/C++ statement.

That is it!!

* * * * * *

Conclusion

• In this article, we have written our first program: a simple one that prints a hello message. Although it was 5-lines short, we have drawn several conclusions out of it about the structure of the C/C++ programs.
• Preprocessor Directives are instructions to the compiler to insert one or more header files into the source code.
• Program execution always starts with the main () function.
• C/C++ statements are terminated with semi-colon.
• Curly braces are used in C/C++ to delimit code blocks.

See you in Part 3.

LEAVE A REPLY

Please enter your comment!
Please enter your name here