Sample
program – A first look at C++ program
Let
us start our first C++ program that prints a string “Welcome to Programming in
C++” on the screen.
The
above program produces, the following output:
This is very simple C++ program which includes the basic elements that every C++ program has. Let us have a look at these elements:
This
is a comment statement. Any statement that begins with // are considered as
comments. Compiler does not execute any comment as part of the program and it
simply ignores. If we need to write multiple lines of comments, we can use /*
……. */.
Usually
all C++ programs begin with include statements starting with a # (hash /
pound). The symbol # is a directive for the preprocessor. That means, these
statements are processed before the compilation process begins.
#include <iostream> statement
tells the compiler’s preprocessor to include the header file “iostream” in the
program.
The
header file iostream should includ in every C++ program to implement input /
output functionalities.
In
simple words, iostream header file
contains the definition of its member objects cin and cout. If you fail to include iostream in your program, an error
message will occur on cin and cout; and we will not be able to get any input or
send any output.
The
line using namespace std; tells the compiler to use standard namespace.
Namespace collects identifiers used for class, object and variables. Namespaces
provide a method of preventing name conflicts in large projects. It is a new
concept introduced by the ANSI C++ standards committee.
C++
program is a collection of functions. Every C++ program must have a main
function.
The
main( ) function is the starting point where all C++ programs begin their
execution.
Therefore,
the executable statements should be inside the main( ) function.
The
statements between the curly braces (Line number 5 to 8) are executable
statements. This is actually called as a block of code. In line 6, cout simply
sends the string constant “Welcome to Programming in C++” to the screen. As we
discussed already, every executable statement must terminate with a semicolon.
In line 7, return is a keyword which is used to return the value what you
specified to a function. In this case, it will return 0 to main( ) function.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.