Declaration and Variables

Arrays

Remember this piece of code?

/* Comment: The program structure of a basic c++ program has the following. */

#include <iostream.h>

main () {

     int number1, number2, answer;

    number1=5;

    number2=7;

   answer=number1+number2;

   cout << "The answer is" << answer << endl;

}

We have talked about comments, now lets discuss declarations and variables.

Declarations

Declarations are used to describe the number and type of arguments passed to functions or subprograms. They are also used to make new variables.
The code piece: int number1, number2, answer.
This was used to make three integers: number1, number2, and answer.

Format of Variables

Identifiers (symbolic variables)
May be composed of letters, digits, and underscore
First character must be letter or underscore
Any letter, digit or underscore may follow
Upper and lower case letters are distinct
Examples: d, digit, Digit, num1, SumDigit, sum_digit, _num
Length is system dependent
Reserved words can not be used as a program identifier
Library identifiers may be used as programs
A variable is a declaration and definition
Types
int
short
long
unsigned
float
double
char
Separated by commas, or an initialization statement
Examples
int count;
float average, total;
Gives name and specifies use; may assign initial value
May be placed anywhere in program
Scope is within block in which identifier is declared
If declared "above" main, then it is a global variable

Other Data Types

Strings
Pointers
Structures
Arrays
 

Back Home Up Next