Introduction to Syntax and Semantics of C++

The following is an example C++ program.  This program, together with the comments, accessible through links or scrolling, provide an introduction to the most basic features of the syntax and semantics of C++ for experienced programmers.  An excellent, but advanced reference for the C++ programming language is

The following is source code in a single file, perhaps called age.cc.  Ignore the color and underlining; they only indicate the links to explanations.

/* File: age.cc */
// Author: Richard Smaby
// Date created: 1997-08-23
// Date revised:
/*
   Requirements: A sample of C syntax and semantics
*/
// Diagnostics and known bugs: No checking of input

#include <iostream>

using namespace std;

const int overTheHill=30

int delta(int ,int ) // function prototype

int main()
{
  char name[32];
  int  age, cnt;
  cout << "What is your name? "; //Change 1
  cin >> name;
  cout << "How old are you, " << name << "? ";
  cin >> age;
  if ( delta(age, overTheHill) >= 0 ) // Changes 2 & 3
    {
      cout << "You are so";
      for (cnt=overTheHill;cnt<age;cnt++)
         cout << "o";
      cout << " old!" << endl;
    }
  else
    { //Change 4
      cout << "You are so";
      for (cnt=overTheHill;cnt>age;cnt--)
         cout << "o";
      cout << " young!" << endl;
    } //Change 4
   return 0;
}
 

int delta(int age,int base)
{
  int d;
  d = age-base;
  return d;
}

Comments

Comments in C++ are enclosed in matching   /* ....  */.  They may be on separate lines and start and end anywhere in a line. Alternatively, a double slash // means the rest of the line is a comment.

Include directive

The include directive in C++ is a directive to the compiler to insert the contents of the file named at the location of the include directive.  The insertion takes place as part of the preprocessing of the source before the actual compilation takes place.  Thus, it is almost the same as if you had typed the included file by hand in the location of the include statement.  In fact, if you have any question about the effect of an include statement, you can imagine you had typed the contents by hand.  You can even ask the compiler to show you the results of its preprocessing (see the man page or help for your C++ compiler).  The particular file being included in this program is iostream.h, which is the standard input and output header file.  The .h portion may be left off. It contains declarations for the standard input and output functions and data, e.g., for the scanf and printf functions used in the sample code.  You can find the file in the directory, /usr/include/.  The angle brackets signify to the compiler to look in the default directory.  If you enclose the name in double quotes, it will look in the same directory as the current file first.  So, any files you make up to include should be included in the form:

Normally, one only includes header files, i.e., files with declarations, not files with the definitions of data and functions.
 

const

const indicates that the contents of a variable cannot be changed. It makes the define compiler directive in C and C++ less necessary. The define directive tells the compiler to regard the first string following the #define keyword as an abbreviation for the second string.   Then you as a programmer can use the abbreviation everywhere in the subsequent code and the compiler will replace it with the actual string before it compiles the code.  Define directives can be fancier and contain arguments.  You might have noticed by now that compiler directives begin with #.  IMPORTANT: C++ is case sensitive.  So, overTheHill is not the same as overthehill
 

Return type of a function

The return type of a function in C++ occurs before the function's declaration.  All routines in C++ are functions.  If you don't give a return value for a functions, the compiler will assume its return type is int.  If you really don't want to return a value from a function, then make its return type void.
 

Return statement

A function definition must have a return statement to be correct, and every way out of the function should be through a return statement.  There can be many return statements in a function definition and can occur in any place a statement can occur.
 

Assignment statement

An assignment statement in C++ is represented with the = sign.  The test for equality is represented by a double equal sign: ==.
 

Parameter list of a function

The parameters of a function are enclosed in parentheses and separated by commas.  The type occurs before the parameter name.  Every function must have a parameter list.  If there are no parameters, then there still must be a pair of empty parentheses.
 

Block

Blocks of code in C++ are enclosed in curly braces: { and }.  Function definitions are blocks and follow the function declaration.  They are similar to begin and end in Pascal.
 

Statement

A statement in C++ ends in a semicolon.  Blocks are also considered statements, but end in a curly brace instead of a semicolon.
 

Local variables

Local variables in C++ are defined at the beginning of a block of code.  Their scope is limited to the block of code they are defined in.
 

Input and output

Standard input and output in C++ involves cout for output to the console and cin for input from the console. To write to the console use the << operator:

cout << item_to_write

To get input from the console use the >> operator:

cin >> variable_to_read_into.

Control

C++ has the standard controls of execution sequence.  Sequential order is simply listing one statement after another.  Selection takes the form of if, if-else, and select.  The test for the if follows the if and must be enclosed in parentheses.

Multiple statements must be put in a block to be included in the true or false branches of an if or if-else.  Repetition takes the form of for, while, and do-while.  while is used in the form

 

Scope

A function must be declared before it can be used.  That is, its name and parameter list followed by a semicolon must occur before it can be used.  However, its definition may occur later.  Thus, the scope of a function is the code subsequent to its declaration.

A name for a local variable only refers to that variable in the block in which the variable is defined.  That is, the scope of a local variable is the block of where it is defined.   Hence, the same name can be used for many different variables, as long as they are defined in different blocks.  Variables declared outside of any function have the code subsequent to their declaration as their scope.