Our First Program,c++ First program

Let’s write our first program in C and understand the basics of C program by explaining it line by line.

# include

main()

{
cout << "hello!"; }


The first line is # include

This is preprocessor directive. The features of preprocessor will be discussed later. For the time being take this line on faith. You have to write this line. The sign # is known as HASH and also called SHARP.

The next line contains main().

There is a main() in every C program. It occurs once in a program. When we write a program and it compiles successfully, it converts into an executable program (file). Then we execute it by typing the command or by double clicking in graphical interface. The system then loads the program into memory. Now the question arises from where the execution should start. In C programs the execution always starts from the main(). In large programs there may be many modules. But the starting point of the program will always be the main function.

Notice that there are parentheses (“( )”, normal brackets) with main. Here the parentheses contain nothing. There may be something written inside the parentheses. It will be discussed in next lectures.

Next, there is a curly bracket also called braces("{ }"). Here is a thing to remember that brackets (parentheses ( ) and braces { }) always occur in pairs. The body of main is enclosed in braces. Braces are very important in C; they enclose the blocks of the program.

The next line in the program,

cout << “hello!”;

is a statement in C language. There are many things in this line to be discussed. Let’s see them one by one.

The word ‘cout’ is known as stream in C and C++. Stream is a complicated thing, you will learn about it later. Think a stream as a door. The data is transferred through stream, cout takes data from computer and sends it to the output that is the screen of the monitor. So we use cout for output.

The sign << indicates the direction of data. Here it is towards cout and the function of cout is to show data on the screen. The thing between the double quoutes (“ ”) is known as character string. In C programming character strings are written in double quotes. Whatever will be written in quotation marks the sign << will direct it towards cout which will show it on the screen. There is a semicolon (;) at the end of the statement. This is very important. All C statements end with semicolon (;). Missing of a semicolon (;) at the end of statement is a syntax error and compiler will report an error during compilation. The only semicolon (;) on a line is a null statement. It does nothing. The extra semicolons may be put at the end but are useless and aimless. Do not put semicolon (;) at a wrong place, it may cause a problem during the execution of the program or may cause a logical error. In this program we give a fixed character string to cout and the program prints it to the screen as: hello!

For previous lesson click here: IDE (Integrated Development Environment)
For next lesson click here: Variables


the easiest way to learn programming
introduction to programming
Our First Program,c++ First program

0 comments:

Post a Comment

 

introduction to programming Copyright © 2011-2012 | Powered by Blogger