Tuesday 8 January 2013

FIRST C++ PROGRAM

INTRO

Now that we know the basic details  about tokens, the building blocks of a program, let us go on and build a program ourselves!
But wait, first let's take a lookat  the C++ IDE (IDE is the acronym for Integrated Development Environment).


To open a new file click on File and then New.



Now, lets go ahead creating our first program.

//PROGRAM : 1
//To print a string (remember that strings are character constants enclosed in double quotes.) 



type the above code in your IDE. Then compile it. 


Then similarly click on Run>Run to run the program. After that click Window>Output.


the output window appears, with the output of our program.


Click on the small green square on the top left corner to close it and return to the IDE.

So, what's happening here? How did our program run and what made it run?

The iostream.h is a header file (which explains the .h file extension) which is a pre-written code file conatining many functions and their definitions. So, we included iostream with the keyword 'include'  to enable input/output facilities in our program (though we don't take any input, the string we displayed is considered as an output). So, we tell the compiler that first you include the required header file(s) and then you begin the program execution.

Next, the program execution begins from the main() part. The general syntax of the main() function is:

Firstly, the type of the main function is first written. In this case it is void, which tells the compiler than the main() function doesn't return any value. Next, the program body is written inside curly braces{}.The main() is where program execution starts. You can also define your functions (we will come to that later).

Then comes the function cout. cout stands for "console output" whose definitions are written in the iostream header file (so you now see why it is important to include them. with the increasing complexity of the programs, you'll appreciate their importance), which we have already included.
The "put to" operator << is used to indicate what we want to print followed by our string "MY FIRST C++ PROGRAM". Here, the task of  >> is to print whatever string succeeds it.

So, you see the codes that you've written follows some 'logic'. So, in programming what matters is the logic. Hence, if your logic is correct, the programs you design are correct, if anywhere you become illogical, the program too becomes incorrect.

So, I hope you got how to print a string in C++. If you have any queries, please feel free to contact me at
lostsoulofthegeek@gmail.com.
:)


POINTS FOR YOU TO PONDER ON:-
1. Why do you think we included the header file called iostream.h in the beginning and not in the end?
    (HINT: Use the notion of logic I just mentioned.)

2. Why is are pre-written header files provided to us so that we may use them even though we can write codes for almost anything?

[{ ANSWERS IN NEXT POST }]







No comments:

Post a Comment