Saturday, 17 January 2015

SYNTAX OF A C++ PROGRAM (ANALYZING A C++ PROGRAM)

WHAT IS C++ PROGRAM ??

A C++ program is a collection of commands, which tell the computer to do "something". This collection of commands is usually called C++ source code, source code or just code. Commands are either "functions" or "keywords". Keywords are a basic building block of the language, while functions are, in fact, usually written in terms of simpler functions.


BASIC SYNTAX :

#include <iostream>
 using namespace std;
 int main()
{
    return 0;
 }

UNDERSTANDING THE CODE :


1) #include <iostream>

           The hash sign (#) signifies the start of a preprocessor command. The include command is a specific preprocessor command that effectively copies and pastes the entire text of the file specified between the angle brackets into the source code. In this case the file is "iostream" which is a standard file that should come with the C++ compiler. This file name is short for "input-output streams"; in short, it contains code for displaying and getting text from the user.

             The include statement allows a program to "include" this functionality in the program without having to literally cut and paste it into the source code every time. The iostream file is part of the C++ standard library, which provides a set of useful and commonly used functionality provided with the compiler. The "include" mechanism, however, can be used both for standard code provided by the compiler and for reusable files created by the programmer.



2) using namespace std;

            C++ supports the concept of namespaces. A namespace is essentially a prefix that is applied to all the names in a certain set. One way to think about namespaces is that they are like toolboxes with different useful tools. The "using" command tells the compiler to allow all the names in the "std" namespace to be usable without their prefix.The iostream file defines names used in the program such as cout, cin, endl etc.(will be used in upcoming programmes) - which are all defined in the std namespace. 
           "std" is short for "standard" since these names are defined in the standard C++ library that comes with the compiler.Without using the std namespace, the names would have to include the prefix and be written as "std::cout", "std::cin", and "std::endl".

3) int main()

           The entry point of all C++ programs is the main function. This function is called by the operating system when your program is loaded."int" is the return type of main function.It means when the function ends an integer value is returned back that marks the end of a function.


4) The braces " {} "

           A block of code is defined with the { } tokens. { signifies the start of a block of code and } signifies the end.


5) return 0;

          The return keyword stops the function where it is and returns a value to the calling function. Using the return keyword is mainly effective to stop the program.


6) semicolon " ; " 

          Each statement in C++ is ended by a semicolon. This is very roughly the equivalent of a sentence in a language like English. This tells the compiler where one operation ends and the next begins.

I will recommend this book to learn C++ language :
Starting out with C++ by Tonny Gaddis ( 7th Edition ).
FROM NOW ON I WILL BE POSTING C++ PROGRAMS ( ALGORITHMS ).

No comments:

Post a Comment