Structure of a program – C Tutorials

4 programming

Structure of a program

Line 2: #include <iostream> Lines beginning with a hash sign ( # ) are directives read and interpreted by what is known as the preprocessor. They are special lines interpreted before the compilation of the program itself begins. In this case, the directive #include <iostream> , instructs the preprocessor to include a section of standard C++ code, known as header iostream, that allows to perform standard input and output operations, such as writing the output of this program ( Hello World ) to the screen.

Line 3: A blank line. Blank lines have no effect on a program. They simply improve readability of the code.

Line 4: int main () This line initiates the declaration of a function. Essentially, a function is a group of code statements which are given a name: in this case, this gives the name “main” to the group of code statements that follow. Functions will be discussed in detail in a later chapter, but essentially, their definition is introduced with a succession of a type ( int ), a name ( main ) and a pair of parentheses ( () ), optionally including parameters.

Lines 5 and 7: < and > The open brace ( < ) at line 5 indicates the beginning of main 's function definition, and the closing brace ( >) at line 7, indicates its end. Everything between these braces is the function’s body that defines what happens when main is called. All functions use braces to indicate the beginning and end of their definitions.

Line 6: std::cout << “Hello World!”; This line is a C++ statement. A statement is an expression that can actually produce some effect. It is the meat of a program, specifying its actual behavior. Statements are executed in the same order that they appear within a function’s body.

You may have noticed that not all the lines of this program perform actions when the code is executed. There is a line containing a comment (beginning with // ). There is a line with a directive for the preprocessor (beginning with # ). There is a line that defines a function (in this case, the main function). And, finally, a line with a statements ending with a semicolon (the insertion into cout ), which was within the block delimited by the braces ( < >) of the main function.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.