Saturday, March 28, 2020

C++ TUTORIALS : STRUCTURE OF A PROGRAMME IN C++

A Sample C++ Program
Let's start with the short sample C++ program shown here.
 the output here is shown in dev c++
I am coming back to vscode later first understand the lines...

As you can see, this program looks much different from the C subset programs

found in Part One. A line-by-line commentary will be useful. To begin, the header
<iostream> is included. This header supports C++-style I/O operations. (<iostream>
is to C++ what stdio.h is to C.) Notice one other thing: there is no .h extension to thename iostream. The reason is that <iostream> is one of the modern-style headers
defined by Standard C++. Modern C++ headers do not use the .h extension.
The next line in the program is



    using namespace std ;

    This tells the compiler to use the std namespace. Namespaces are a recent addition
    to C++. Anamespace creates a declarative region in which various program elements can
    be placed. Namespaces help in the organization of large programs. The using statement
    informs the compiler that you want to use the std namespace. This is the namespace in
    which the entire Standard C++ library is declared. By using the std namespace you
    simplify access to the standard library. The programs in Part One, which use only the C
    subset, don't need a namespace statement because the C library functions are also
    available in the default, global namespace.

    Now examine the following line.

    int main()

    Notice that the parameter list in main( ) is empty. In C++, this indicates that main( )
    has no parameters. This differs from C. In C, a function that has no parameters must
    use void in its parameter list, as shown here:
    int main(void)
    This was the way main( ) was declared in the programs in Part One. However, in
    C++, the use of void is redundant and unnecessary. As a general rule, in C++ when
    a function takes no parameters, its parameter list is simply empty; the use of void is
    not required.
    The next line contains two C++ features


    So the question arrives 

    What is the Difference Betwwen int main() , void main() ,main()

    Like any other function, main is also a function but with a special characteristic that the program execution always starts from the ‘main’. ‘int’ and ‘void’ are its return type. So, let’s discuss them 
    • void main – The ANSI standard says "no" to the ‘void main’ and thus using it can be considered wrong. One should stop using the ‘void main’ if doing so.

    • int main – ‘int main’ means that our function needs to return some integer at the end of the execution and we do so by returning 0 at the end of the program. 0 is the standard for the “successful execution of the program”.

    • main – In C89, the unspecified return type defaults to int. So, main is equivalent to int main in C89. But in C99, this is not allowed and thus one must use int main.


    cout << "This is output.\n"; // this is a single line comment


    First, the statement

    cout << "This is output.\n";

    This is output. to be displayed on the screen, followed by a carriage returnlinefeed
    combination.You can use cout and the << to output any of the built-in data types, as well as strings of characters.Note that you can still use printf( ) or any other of C's I/O functions in a C++program. However, most programmers feel that using << is more in the spirit of C++.
    Further, while using printf( ) to output a string is virtually equivalent to using << in
    this case, the C++ I/O system can be expanded to perform operations on objects that
    you define (something that you cannot do using printf( ))

    coming to the 

    // this is a single line comment

    You can define a single-line comment
    by using //; whatever follows such a comment is ignored by the compiler until the end of

    the line is reached.

    Next, the program prompts the user for a number. The number is read from the

    keyboard with this statement:

    cin >> i;

    In C++, the >> operator still retains its right shift meaning. However, when used
    as shown, it also is C++'s input operator. This statement causes i to be given a value
    read from the keyboard. The identifier cin refers to the standard input device, which
    is usually the keyboard. In general, you can use cin >> to input a variable of any of

    the basic data types plus strings.
    BAsically what scanf does to c , cin>> does to c++.

    like in the above code user give the value 5 to the computer

    cout << i << "squared is " << i*i << "\n";

    we will deal with this operator later .In short it is just squaring the the number after taking the input from the user by multiplying with itself.

    like in the above code compiler returned a value 25 which is square of 5.



    return 0;


    This causes zero to be returned to the calling process (which is usually the operating

    system). This works the same in C++ as it does in C. Returning zero indicates that the
    program terminated normally. Abnormal program termination should be signaled by
    returning a nonzero value.But without this also your code will run.





    Share on Whatsapp

    No comments:

    Post a Comment