Prev Next

Elementary Features of C++: Structure, Types, Variables

I expect you to know C++ already. We will go through features of C++ relatively quickly.

History

Through the 1960s, and into the early 1970s, operating systems were written in assembler language. The C programming language was originally developed as a language to replace assembler in systems programming. It was very successful, making system code portable and easier to write and read.

C began to grow in popularity not just for systems programming, but as a general purpose programming language. Today C is one of the most used programming languages. The C programmer must realize, however, that C was designed to replace assembler language, and that in several important ways, it retains a very low level view of the machine. This language summary will discuss, among other things. some of the low level characteristics of C.

The C++ programming language was designed as a higher level version of C, providing support for object-oriented programming. C++ retains almost all of C, however, and therefore is a mixture of low level and high level features. Often, the programmer has a choice of using low level or high level approaches.

C++ was developed for about two decades before it was standardized in the late 1990s. ANSI Standard C++ is the language that we will use.

Form and case

C and C++ are both case sensitive. That means that B and b are two different letters. If you write

  int x;
you have declared a variable x of type int. If you write
  Int x;
you probably get a compile error, unless you have created a type called Int.

C and C++ are free-form. That is, in most contexts the end of a line is just like a space. So there is no special meaning to a line. Typically, you put one statement per line, because it looks nicer than packing several to a line, or breaking a statement into several lines. Long statements are usually placed on several lines, however.

Comments

You can write comments in either C style or C++ style (in C++ programs). A C style comment begins with /* and ends with */. A C++ style comments begins with // and ends at the end of a line. (So C++ comments are one of the few violations of free-formness.) For example,

  /* Here is a C-style comment */

  // Here is a C++ style comment.

Names and reserved words

You can name things in C/C++ with names that begin with a letter or underscore, and contain letters, underscores and digits in any combination. For example,

   apostrophe
   the_list
   horse43
   x
are all names that can be given to things. Names cannot contain spaces. You should avoid names that begin with an underscore, since they are often used to name special things in the system.

Certain words are special in the language, and you cannot use them to name things.

Expressions and statements

An expression computes a value. For example, expression 3+5 has value 8. A statement is executed for an effect. For example, statement n = 1; puts value 1 into variable n.

In C/C++, an expression can both compute a value and have an effect. When an expression has an effect, that effect is called a side-effect. For example, if variable n has value 5, then expression n++ has value 5, but also has the side-effect of setting n to 6.

In C/C++, any expression can be a statement. So, for example, n++ is an expression, but you can write it as a statement:

  n++;
Writing a semicolon after an expression makes it a statement. When an expression is used as a statement, the expression is computed, but its value is ignored.

You can create one large statement, called a compound statement, from several smaller ones by enclosing the statements in {...}. For example,

  {
    n++;
    m++;
  }
is a compound statement. The statements in the compound statement are executed in the order in which they are written.

Simple data types

Integer types

Because C and C++ are low level languages, they provide several integer data types. Using these types, the programmer has very close control over how memory is used. Here are the integer data types.

Type Meaning
char A char is an integer stored in one byte. It is typically used to store the code for a character, but can be used to store any kind of integer. Some compilers say that a char is signed, meaning that it is taken to be an integer from -128 to 127. Some compilers say that a char is unsigned, meaning that it is an integer< from 0 to 255.
unsigned char This is the same as char, but holds numbers from 0 to 255.
short A short int is typically an integer stored in two bytes. It can hold values from -32,768 to 32,767. You can also write short int.
unsigned short Same as short, but holds numbers from 0 to 65,536.
long A long int is typically an integer stored in four bytes. You can also write long int.
unsigned long Same as long, but unsigned
int The type int is either the same as short or the same as long, at the compiler's discretion.
unsigned int Same as int, but unsigned.
long long The advent of 64 bit machines has led to a need for 8 byte integer variables. Type long long provides that. Not all compilers support this.

Operations on integers include the following.

Operation Meaning
+ addition
- subtraction
* multiplication
% x%y is the remainder when you divide x by y. Avoid cases where x or y are negative, since the result can depend on the machine that you are using.
/ x/y is the integer quotient when x is divided by y. For example, 6/2 = 3, but note that 6/4 = 1. The remainder is ignored.

Integer constants

Integer constants can be written in decimal (23) octal (071) or hexadecimal (0x3A). Character constants such as 'a' are integers -- 'a' is 97, the ascii code for character a. However, if you write 97 in your program, it is presumed to have type int. If you write 'a', it is presumed to have type char. Some useful special characters are '\n' (newline) and '\t' (tab). The ascii code for the backslash character is written '\\'.

Enumerated types

An enumerated type is an integer type with named members. You create such a type using an enum declaration. For example,

  enum Color {
    red, orange, yellow, green, blue, 
    indigo, violet
  };
creates a type called Color (in C++) or enum Color (in C or C++). The compiler defines red to be 0, orange to be 1, etc., through violet, which is defined to be 6.

Floating point types

The following types are used for real numbers, stored approximately (with roundoff error).

Type Meaning
float Real numbers stored in four bytes. These offer about 7 digits of precision.
double Real numbers stored in eight bytes. These offer about 15 digits of precision.
long double Real numbers stored in more than eight bytes. This is not available with all compilers

Operations on floating point types are the same as those for integers, except that % is not available, and division produces a floating point number. 2.0/3.0 yields 0.666666666666667.

Variables and assignment

Declaring variables

Before you use a variable, you must declare it, and indicate its type. To declare a variable x of type T, you write

   T x;
You can declare several variables at once. For example, to declare char variables c and d, and long variables x and y, you can write
  char c,d;
  long x,y;

When you write variable declarations outside a function definition, the variables are global, and can be shared by several functions. When you write them inside a function definition, the variables are local to that function, and cannot be used by any other functions. (But see pointers for an exception to this rule.)

You can declare variables inside compound statements. A variable declared in a compound statement is only accessible within the innermost compound statement that contains the declaration. For example, if you write

  {int x;
   ...
  }
then you can use x within this compound statement, but not outside. This is a convenient way to declare variables that are only used for a short part of a function.

In C++, a variable can be declared anywhere. In C, a variable can only be declared outside a function or as a function parameter or at the beginning of a compound statement (just after a left brace) and before any other statements.

Initial values and constants

When you create a variable, you can give it an initial value. For example,

  long n = 0;
not only allocates memory for variable n, but it sets that memory to 0. Outside a function, the initial value of a variable must be a constant, such as 0. Inside a function, the initial value of a variable can be given by any expression. For example, you can write
  long int n = a + b;
as long as the values of a and b are known.

Sometimes you want a "variable" that you never intend to change, after giving it an initial value. You can use modifier const for such variables. For example,

  const long int size = 40;
declares size to be 40, and does not allow size to be changed.

Caution: If you do not initialize a variable, it has a "junk" value. Do not use the value of a variable before you put something in the variable.

Assignment

To change the value of a variable, use an assignment statement, of the form x = e, where x is a variable and e is an expression. For example,

  n = a + 1;
computes a+1 and puts the result into variable n. Statement
  n = n + 1;
adds one to the value of variable n, and puts the result back into n.

In C/C++, an assignment is an expression. That is, it has a value. The value is the value just put into the variable. So, for example,

  n = 0
is an expression that has value 0, but also has a side-effect: it puts 0 in variable n.

Note: You only include the type of a variable when you are creating the variable. So

  long n = 0;
creates variable n and sets it to 0. But
  n =  0;
sets a previously created variable n to 0.

Abbreviations for assignments

There are abbreviations for frequently done kinds of assignments. Here are a few.

Abbreviation Meaning
n += k n = n + k
n -= k n = n - k
++n n = n + 1 (where the value of expression ++n is the value of n after the assignment)
n++ n = n + 1 (but the value of expression n++ is the value of n before the assignment)
--n n = n - 1 (where the value of expression --n is the value of n after the assignment)
n-- n = n - 1 (but the value of expression n-- is the value of n before the assignment)


Prev Next