Sunday, August 7, 2011

C++ Basics

Simple C++ Program
Here is a very basic structure of a C++ program

#include <iostream>
using namespace std;
int main ()
{
int i;
cout << "a simple program: ";
return 0;
}

Components of a program: 
On line 1, the file is included in the file. Each time you start your compiler, the preprocessor is run. The preprocessor reads through your source
code, looking for lines that begin with the pound symbol (#), and acts on those
lines before the compiler runs.If your compiler is set up correctly, the angle brackets will cause the preprocessor to look for the file iostream in the directory that holds all the Header files for your
compiler.The file iostream (Input-Output-Stream) is used by cout, which assists with writing to the screen.

Line 3 begins the actual program with a function named main(). Every C++ program has a main() function.Function is a block of code that performs one or more actions.
main(), like all functions, must state what kind of value it will return. The return value type for main() in the above stated program is void, which means that this function will not return any value at all.
Comments In C++
There are two ways for comments in C++
1) // It is single line comment
2) /* This is multiple line comment */
Example
#include <iostream>                   // include iostream for input and output
using namespace std;
int main ()               // main function starts
{
int i;                 // a variable of in type
cout << "a simple program: ";
return 0;
/* it ia a multiple line comment
* return 0 means this function
* returns nothing at all
*/
}
Statements In C++
Statement end with a semicolon(;), not with the end of a line

Example
Cout<<" it is a statement";

No comments:

Post a Comment