Okay, now getting started with C++ programming. First few words about how you should learn programming. According to me, the best way to learn any programming language in this world is by writing a program. If you write and practice then surely you will become perfect and acquire good skills in short time. But if you keep reading the programming books without an practical work then it wont help much. So always practice, make errors, fix them by finding solutions. This way you will become a great programmer.
So we will now write our first program and then we will understand it.
1.) // This is my first c++ program
2.) #include <iostream.h>
3.) void main()
4.) {
5.) cout << " Hello World " ;
6.) }
So the above code is a very simple code which will help us
to print Hello World text as the output. We will have a look at every
part of this code and understand it. The first line is a ‘single line
comment’. Any line beginning with two forward slashes (//) is a single
line comment in C++ language.
Comments are
ignored by the compiler, which means that they are only for the humans.
We write comments to tell other human what our program is all about.
And in this case i am telling you people that this is my first c++
program. We should add comments to our program to help other programmers
understand our program in a better way.
1.) #include <iostream.h>
On the second line we have #include <iostream.h>. Now in this line iostream is a header file. We understand that it is a header file by looking at .h
file format. And #include is a pre-processor directive. Preprocessor
directives give instructions to the preprocessor to do certain task or
take a certain action. And in this case we are just telling the
preprocessor to include an external file (iostream.h) in our program.
You must be wondering why are we including this file in our program,
right? So the answer is that this file actually contains some elements
or functions which we are going to use in our program. We will see more
about this on 5th line of the program.
1.) void main()
Third line says void main(). Remember that if a keyword is followed by pair of parenthesis then that is a function.
So on this line main is a function in C++ and void is simply the
return-type of main function. We will understand functions in the
functions tutorial later on. However for now what you have to understand
is that main is just the starting point of your program. All program
execution begins at main function. No matter how many functions are
there in your program or how many functions are above the main
function, You have to understand and remember that always the main function will be executed first.
Then comes the opening curly brace. The opening curly brace and closing curly brace forms a block. Remember this to understand my next tutorials. And since this block occurs after main function, it forms the body of the main function.
Again don’t worry if you are finding it difficult to understand about
functions, since we will understand functions completely in functions
tutorial. So inside the main functions body, we have :
- On the right hand side there is a semicolon ( ; ). Semicolon is used to end every statement in C++. Some lines may not have a semicolon at the end just because they are not technically statements.
- Then there is Hello World in Quotations (” “). Anything between two double quotation marks is considered as string. String is simply group of characters in C++. In this case Hello World is a string.
- Then after we have two less than signs ( << ). These are called Insertion Operator. They simply insert things on the right hand side to the things on the left hand side.
- And at last comes cout (Console Out). cout is the standard output stream in C++. In simple terms whatever you pass to it will be displayed on the output screen.
Now the question remains, Why did we include iostream header file in our program?
The answer is that we have used cout keyword in our program which is defined inside iostream header file. So if we remove the 2nd line completely then a error will occur. Because the compiler does not understand the meaning of cout.
In our program the compiler approaches cout and says i dont know what is cout ! And then it searches for the meaning of cout inside our program. Then it searches inside the ‘included’ header files. If it finds the meaning then compilation is successful or else error is reported to us.
So our basic code or syntax which we will use in all our beginner programs is as follows :
1.) #include <iostream.h>
2.) void main()
3.) {
4.) // Statements can be added here
5.) }
To fix output issues in Turbo C++
There are 3 more lines which we should add to fix output issues in Turbo C++. These output issues can be understood in a better way by watching the video. Click here to view the video. The issues which come in Turbo C++ are:- Screen is not cleared before showing new output. Hence previous output is shown along with new output.
- Output screen disappears in less than one second.
So finally our fundamental program code becomes :
1.) #include <iostream.h>
2.) #include <conio.h>
3.) void main()
4.) {
5.) clrscr();
6.) // Statements can be added here
7.) getch();
8.) }
No comments:
Post a Comment