next up previous
Next: Expressions and statements Up: Scientific programming in C Previous: Introduction

Variables

Variable names in C can consist of letters and numbers in any order, except that the first character must be a letter. Names are case sensitive, so upper- and lower-case letters are not interchangeable. The underscore character (_) can also be included in variable names, and is treated as a letter. There is no restriction on the length of names in C. Of course, variable names are not allowed to clash with keywords that play a special role in the C language, such as int, double, if, return, void, etc. The following are examples of valid variable names in C:
x  c14  area  electron_mass  TEMPERATURE

The C language supports a great variety of different data types. However, the two data types which occur most often in scientific programs are integer, denoted int, and floating-point, denoted double. (Note that variables of the most basic floating-point data type float are not generally stored to sufficient precision by the computer to be of much use in scientific programming.) The data type (int or double) of every variable in a C program must be declared before that variable can appear in an executable statement.

Integer constants in C are denoted, in the regular fashion, by strings of arabic numbers: e.g.,

0  57  4567  128933
Floating-point constants can be written in either regular or scientific notation: e.g.,
0.01  70.456  3e+5  .5067e-16

Strings are mainly used in scientific programs for data input and output purposes. A string consists of any number of consecutive characters (including blanks) enclosed in double quotation marks: e.g.,

"red"  "Austin TX, 78723"  "512-926-1477"
Line-feeds can be incorporated into strings via the escape sequence \n: e.g.,
"Line 1\nLine 2\nLine 3"
The above string would be displayed on a computer terminal as
Line 1
Line 2
Line 3

A declaration associates a group of variables with a specific data type. As mentioned previously, all variables must be declared before they can appear in executable statements. A declaration consists of a data type followed by one or more variable names, ending in a semicolon. For instance,

int    a, b, c;
double    acc, epsilon, t;
In the above, a, b, and c are declared to be integer variables, whereas acc, epsilon, and t are declared to be floating-point variables.

A type declaration can also be used to assign initial values to variables. Some examples of how to do this are given below:

int    a = 3, b = 5;
double    factor = 1.2E-5;
Here, the integer variables a and b are assigned the initial values 3 and 5, respectively, whereas the floating-point variable factor is assigned the initial value $1.2\times 10^{-5}$.

Note that there is no restriction on the length of a type declaration: such a declaration can even be split over many lines, so long as its end is signaled by a semicolon. However, all declaration statements in a program (or program segment) must occur prior to the first executable statement.


next up previous
Next: Expressions and statements Up: Scientific programming in C Previous: Introduction
Richard Fitzpatrick 2006-03-29