Showing posts with label c programs with output. Show all posts
Showing posts with label c programs with output. Show all posts

Sunday 6 October 2013

Creating A C++ Program

How to Create a C++ program??

In this C tutorial for beginners, we are going to learn how to create C++ programs with output. After studying previous tutorials, we now have the basic and required concepts needed to create a C++ program. A C++ program is a collection of function, one of which is the function main. A function is a set of instructions designed to accomplish specific task. We’ll deal with functions in further tutorials in detail.

The syntax of the function main is:

int main()
{
    Statement 1
    Statement 2
    .
    .
    .
    Statement n

return 0;
}

This main function body contains two type of statements:

1. Declaration statement:

Declaration statements are used to declare things as    variable.

2. Executable statements:

Executable statements perform calculations, manipulate data, create output, accept input and so on. Some executable statements that we came across with are assignment, input and output statements.

 The return 0 function is necessary and is always the last statement. In C++, return is reserved keyword.
 
This main function should be saved in .cpp extension file. 

The file containing this main function or source code is called the source code file or source file.

Things stated step by step needed for creating C++ program examples:

  • Comments if needed to introduce some information about your program
  • Use preprocessor directives to include header filese.g: #include<iostream>
    using namespace std;
  • Then we introduce any global scope variables if necessary.
  • Then we introduce main body as explained above with syntax.


Now, we’ll show you  simple c program examples to explain everything that we mentioned above.

//******************************************************
// Made by shapesinc.blogspot.com
//******************************************************

#include<iostream>    //preprocessor directive
using namespace std;

int main()
{
    int a, b, c;    //declarative statement
    double x, y    //declarative statement

    a=4;        //assignment statement
    cin >> b;    //input statement
    cout << a << “ ” << b <<endl;    //output statement
    return 0;
}

Saturday 5 October 2013

Cout in C++

Cout in C++:

In previous tutorial, we learnt how to store data. Now we will learn in this C++ tutorial that how to print that desired result on the console.
In C++, output on the standard output device is accomplished through the use of "cout" and the operator “ << ”.

Note: 

The standard output device is usually the screen.

Syntax:

cout << expression or identifier ;

This is called an output statement. In C++, "<<" is called the stream insertion operator.

Example:

 Here are some simple c programs with output to show the use of this command.
1.  char ch = A.
     cout << ch;

Output:

A                             //This A was stored in char variable ch

2.  cout << ‘A’;

Output:

A                             //This A was hard coded

For char ch variable, you didn’t needed double quotes but for hard coded ‘A’ you needed double quotes.

Statement                                                  Output
cout << 29 / 4 << endl;                                   7
cout <<“Hello World”<< endl;                    Hello World
cout <<20<< endl;                                         20
cout <<3.2<< endl;                                       3.2
cout << ”29 / 4“ << endl;                             29 / 4
cout <<20 + 100<< endl;                              120        
cout <<‘A’<< endl;                                          A
cout <<“4 + 16 =”<<4 + 20 <<endl;           4 + 16 = 20
cout <<2 + 3 * 4<< endl;                               14
cout <<“Hello \nWorld”<< endl;                Hello
                                                                                World
Look at the statement cout << “Hello \nWorld” << endl;
This statement contains \n. It is a newline character. It causes the insertion point to move to the beginning of the next line before printing there. That’s why it prints Hello one line and World on the next line.

Note:In C++, \ is called the escape character and \n is called newline escape sequence.
If we don’t assign any value to a variable and try to cout it on the screen then it will print a junk value.

Example:

int num;
cout << num <<endl;
The output will be some junk value.

Final Words:

In this c tutorial for beginners we have discussed how to print something on console with the help of cout in c ++.We have also showed some c programs with output which clarify the use of cout command in C++.