Wednesday, 27 November 2013

A Simple C++ Code to Display an Output

This time, we are going to write a simple C++ code that will output a string provided to it. Let's say that string would be a name, for the instance.
So here is the code for that program:


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"My name is ABC"<<endl;
getch();
}

Explanation


  1. Line 1 & Line 2: We have included two header files in our program, those header files include some useful functions which will be used inside our code.
  2. Line 3: The brackets "()" at the end of third line indicate that it is a function. The code "void main()" has two parts:
    void: means that this function doesn't return any value.
    main: indicates that this is the main part of the object code.
  3. Line 4: Starting curly bracket "{" indicates that this is the start of the function "void main()"
  4. Line 5: The function "clrscr()" clears the output-screen.
  5. Line 6: The keyword "cout" is used to display something as the output on the screen. Two left angle brackets "<<" concatenate or combine the two parts of a line of code. The words in double quotes "My name is ABC" are actually a string that is to be printed on screen. "endl" is a keyword in c++ that ends working on the current line and moves the cursor to next line.
  6. Line 7: "getch()" is a keyword in C++ which is used when we want the IDE to keep the program in running mode until the user gives a character (or simply, until the user presses any key on the keyboard). This helps us to keep the output steady on the user's screen until he/she presses a key.
  7. Line 8: Closing curly bracket "}" indicates that the function "void main()" has came to an end.

Output
The output of the above given program will look like this image:
Simple-C-Program-Output



Suggestions or Feedback:

You found our content helpful, or useless? Share your thoughts and suggestions with us. We appreciate your feedback.

No comments:

Post a Comment