Tuesday, July 14, 2009

Create Your First C++ Program


Create Your First C++ Program

Don't worry if you don't understand everything right now. Everything will be cleared as you proceed with further lessons. Just keep practicing every program until you are able do it with your own. If you get any error, check your source code with this one carefully. Fix your error then compile and run it again.

A Small C++ Program Example


#include <iostream>

int main ()
{

    std::cout << "Hi! Hello! Wassup!";

    return 0;
}


What are header files (Preprocessor Directive) ?

The statement that begins with #include are known as header files. It is also called as preprocessor directive. When you start the compiler the preprocessor runs first. The preprocessor will be discussed in detail in further chapters. The command #include is a preprocessor instruction that says, "What follows is a file-name. Find that file, read it, and replace it right here." The angle brackets around the file-name tell the preprocessor to look in all the usual places for this file. For example, the file iostream (input-output-stream) is used by cout which displays the output.

What is function ?

Every function starts from the opening brace ({ ) and ends with closing brace ( }). Opening and closing brace together are also called as a block. A block of code that performs one or more action is called function.

What is main function ?

In actual program starts from main function. Main function is also known as primary function or building block of the program. It is compulsory function.
Every C++ program has main function. Every function is called by other function except for main function. Main is a special function which is called directly when your program starts. The return type of main is int, which means it should return an interger value. The main function returns value to an operating system. In this case it return 0 to the operating system. It is used to determine success or failure of the program. It will be discussed in detail in later chapter of functions. Its okay if you don't understand it now but just understand that when you write "int main()" then it becomes compulsory to write "return 0;" before the closing brace of main.

What is cout ? What are syntax of cout ? How to use cout ?

The "cout" is used to display the output. But before cout there is std (standard liabrary) and two colons which tells the compiler to search for cout in standard liabrary. "cout" is a part of standard liabrary. So if we want to use cout we should write std and two colons before it. After cout there is redirection operator (<<) followed by string (whatever we write in double-inverted comma is called string. It is a series of characters.) which ends with the semi-colon. Every statement of C++ ends with semi-colon.

Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
Want to learn more? View List Of All Chapters

1 comment: