A Simple C++ Program

1 // simple program
2 //Text-printing program. 
3 #include // allows program to output data to the screen 
4 using namespace std;
5 // function main begins program execution 
6 int main()
7 {
cout << "Welcome to C++!\n"; // display message 

10  return 0; // indicate that program ended successfully 
11 } // end function main


Used to describe program
/* and */ is multiline comment
// is single line comment
Text starting with // or surrounded by /* and */ is ignored by computer

Preprocessor directive
Tells computer to load contents of a certain file
or allows standard input output operations

int main()
C++ programs contain one or more functions, exactly one of which must be main
Parenthesis used to indicate a function
int means that main "returns" an integer value
Braces ({ and }) indicate a block
The bodies of all functions must be contained in braces

cout << “Hello World\n”;

Instructs computer to perform an action
Specifically, prints the string of characters within quotes (“ ”)
Entire line called a statement
All statements must end with a semicolon (;)
Escape character (\)
\n is the newline character
<< insertion operator or put to operator

return 0;
A way to exit a function
return 0, in this case, means that the program terminated normally

Right brace }
Indicates end of main has been reached
When a function is called, linker locates it in the library
Inserts it into object program
If function name is misspelled, the linker will produce an error because it will not be able to find function in the library

Data Types

  • int
  • float
  • double
  • char
  • bool
  • void

Memory Concept 


Variable names correspond to locations in the computer's memory
Every variable has a name, a type, a size and a value
Whenever a new value is placed into a variable (=), it replaces (and destroys) the previous value
Reading variables from memory does not change them


  • variable can hold a number or a data of other types, it always holds something. A variable has a name
  • the data held in variable is called value
  • variables are implemented as memory locations and assigned certain memory address. The exact address depends on computer and compiler.
  • we think as though the memory locations are actually labeled with variable names


identifier – name of a variable or any other named construct
identifier must start with a letter or underscore symbol (_), the rest of the characters should be letters, digits or underscores
the following are legal identifiers:
x    x1 x_1   _abc sum RateAveragE

the following are not legal identifiers. Why?
13  3X  %change  data-1  my.identifier  a(3) 

C++ is case sensitive:
MyVar and myvar  are different identifiers