Hardware Reference
In-Depth Information
The sixth line assigns 3 to the variable
a
.
The seventh line assigns 5 to the variable
b
. The
eighth line computes the sum of variables
a
and
b
and assigns it to the variable
c
. You will see
that assignment statements are major components in C programs.
The ninth line calls the library function
printf()
to print the string
a
1
b
5 followed by the
value of
c
and move the cursor to the beginning of the next line.
The tenth line returns a 0 to
the caller of main()
.
The closing brace in the eleventh line ends the main() function.
Variables and constants are the basic objects manipulated in a program. Variables must be
declared before they can be used. A variable declaration must include the name and type of the
variable and may optionally provide its initial value. A variable name may start with a letter
(
A
through
Z
or
a
through
z
) or an underscore character followed by zero or more letters, digits,
or underscore characters. Variable names cannot contain arithmetic signs, dots, apostrophes,
C keywords, or special symbols such as @, #, ?, and so on. Adding the underscore character (_)
may sometimes improve the readability of long variables. Don't begin variable names with an
underscore, however, since library routines often use such names. C language is case sensitive.
Upper- and lowercase letters are distinct.
There are only a few basic data types in C:
void,
char,
int,
float,
and
double
.
A variable of type
void
represents nothing. The type
void
is used most commonly with functions and can indicate
that the function does not return any value or does not have incoming parameters. A variable
of type
char
can hold a single byte of data. A variable of type
int
is an integer, which is normally
the natural size (word length) for a particular machine. The type
float
refers to a 32-bit, single-
precision, floating-point number. The type
double
represents a 64-bit, double-precision, floating-
point number. In addition, there are a number of qualifiers that can be applied to these basic
types.
Short
and
long
apply to integers. These two qualifiers will modify the lengths of integers.
An integer variable is 16-bit by default for many C compilers including the CodeWarrior C and
the GNU C compiler. The modifier
short
does not change the length of an integer. The modifier
long
doubles a 16-bit integer to 32 bits. The keyword
unsigned
should be used if the variables
are never negative to improve the efficiency of the generated code.
All variables must be declared before their use. A declaration specifies a type and contains
a list of one or more variables of that type, as in
int i, j, k;
char cx, cy;
A variable may also be initialized when it is declared, as in
int i
5
0;
char echo
5
'y';
/* the ASCII code of letter
y
is assigned to variable
echo
. */
There are four kinds of constants:
characters, integers, floating-point numbers,
and
strings
.
A character constant is an integer, written as one character within single quotes, such as 'x'. A
Search WWH ::
Custom Search