Basic Structure of a C program

Basic Structure of a C++ program

Now that you have seen variable declaration, C++ expressions, and the basic math operators, let’s take a quick look at the basic structure of a C++ program. All C++ programs must have a main function. This is where the program begins. Chapter 4 goes into greater detail regarding functions. For now, suffice it to say that a function is one or more statements grouped together in a logical manner to accomplish some goal under a common name. Chapter 4 covers all the intricacies of C++ functions. The following is a basic C++ program.

int main() {    return 0; } 

Now, this program will not do much—in fact, it won’t do anything useful at all. It is, however, a valid C++ program. It has a main function that returns an integer value. The main function is where all C++ programs start. This is the starting point for your entire program. All main functions are required by the ANSI (American National Standards Institute) standards to return an integer. The integer is a 0 if the program executes fine, and a 1 if some problem is encountered. This was originally done because some operating systems require any program to return a value telling the operating system that everything is OK. You will see a lot of Windows programmers use a main function that returns a void like the following.

void main()

This will work in some cases, but it is not technically correct. This book endeavors to conform to the ANSI standards and use return 0.

The next thing to notice about our sample program is the presence of brackets. These brackets are C++’s way of establishing borders around any block of code. (Incidentally, C, Java, and JavaScript™ do the same thing. Learning it here will help you learn other languages as well.). Every time you see an opening bracket, {there must be a matching} closing bracket. You will see a lot more about this when we discuss loops, decision structures, and functions. For now, suffice it to say that brackets form borders around blocks of code.

With all that said, let’s look at a program that has a few statements.

int main() {  int j;  j = 5;  j++;  return 0; }

This simple program creates a variable named j, sets the value of that variable to 5, then increments that value by one. This is still not a particularly exciting piece of code, but it does illustrate the basic structure of a C++ program. Three statements are executed, a 0 is returned to indicate that everything is OK, and there are brackets surrounding the main function. If you carefully examine this code, and follow this template in all your programming, then you will do well!

Header Files

There is only one more item that needs to be discussed regarding the basic structure of a C++ program. That item is header files. Header files are files that contain the definitions for functions and/or classes. Basically, if you have a lot of functions or variables that you might want to use in several programs, you can put them in a header file, then include that header file anywhere you need to use it. After creating a header file you can then include a reference to a header file in your program and use the functions and/or classes defined in that header file. The real intricacies of header files are discussed in a later chapter, in which you will actually create some header files. However, you should realize that there are a lot of these files built into C++ that you can use! Well, “built in” is not exactly accurate. They are actually installed with your compiler and you can include them in your programs. One of which you will use in Chapter 2 is the iostream header file. It gives you functions to handle input and output from the screen. The way you include any header file is simple. At the beginning of your file, you reference the header file. This is demonstrated in the following example.

#include <iostream> int main() {  return 0; }

You now have access to all the classes and functions defined in the iostream header file. In the following chapters, you will see several different header files, each providing a different set of functions for you to use. For now, all that is important is that you realize that you include a header file using the # sign and the word include. If the header file is one that is part of C++, then you simply use the <headerfilename>. Make sure that this is the first thing you do in your file! Remember that you use .cpp to indicate a C++ source file and .c to indicate a C source file, while the .h indicates a header source file. A source file is simply a plain text file that has the source code for your C++ program. Source code is the basic lines of programming language that you write, and that the compiler will later use as a source to create an executable program.


C requires you to specify the .h in the file. For example, screen input and output is implemented in C using the <stdio.h> header file. The .h extension is not needed in C++. Many compilers will support it, but not all. It is best to refer to <iostream> rather than <iostream.h>. It is also important to realize that the ISO and ANSI C++ standard dictates that, unless you are using an old C-styled header file, you will not add the .h extension.

C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom © 2008-2017.
If you may any questions please contact us: