Unit 4 - Data Types and Keywords
Unit 4 - Data Types and Keywords
C Keywords
The C language reserves certain words that have special meanings to the language. Those
reserved words are sometimes called C keywords. You should not use the C keywords for your
own variable, constant, or function names in your programs.
Reserved Keywords in C
Keyword Description
auto Storage class specifier
break Statement
case Statement
char Type specifier
const Storage class modifier
continue Statement
default Label
do Statement
double Type specifier
else Statement
enum Type specifier
extern Storage class specifier
float Type specifier
for Statement
goto Statement
if Statement
int Type specifier
long Type specifier
register Storage class specifier
return Statement
short Type specifier
signed Type specifier
sizeof Operator
static Storage class specifier
struct Type specifier
switch Statement
typedef Statement
union Type specifier
unsigned Type specifier
void Type specifier
volatile Storage class modifier
while Statement
Don’t worry if you can’t remember all the C keywords the first time through.
You’ll become more familiar with them and start to use many of the keywords
through examples and exercises. Note that all C keywords are written in lowercase letters. As
I’ve mentioned, C is a case sensitive language. Therefore, int, as shown in the list here, is
considered as a C keyword,
but INT is not.
An object of the char data type represents a single character of the character set used by your
computer. For example, A is a character, and so is a. But 7 is a number. However, a computer
can only store numeric code. Therefore, characters such as A, a, B, b, and so on all have a
unique numeric code that is used by computers to represent the characters. Usually, a
character takes 8 bits (that is, 1 byte) to store its numeric code. For many computers, the ASCII
codes are the de facto standard codes to represent a character set. (ASCII, just for your
information, stands for American Standard Code for Information Interchange.)
The original ASCII character set has only 128 characters because it uses the lower 7 bits that
can represent 27 (that is, 128) characters. On IBM-compatible PCs, however, the character set
is extended to contain a total of 256 (that is, 28) characters.
Character Variables
char variablename;
where variablename is the name you provide in which to store values of this type.
If you have more than one variable to declare, you can either use the following format:
char variablename1;
char variablename2;
char variablename3;
or this one:
For example, the following statement declares MyCharacter and sets it to ‘A’:
Similarly, the following statements declare x, y, and z as character variables and then assign
values to them:
char x, y, z;
x = ‘A’;
y = ‘f’;
z = ‘7’;
Note the last assignment, z = ‘7’, sets z to equal the numeric value representing the
character ‘7’ in the character set — not the actual number 7.
You’ll learn more about the character variable and how to use it in your C programs later.
Character Constants
A character enclosed in single quotes (‘) is called a character constant. For instance,
‘A’, ‘a’, ‘B’, and ‘b’, are all character constants that have their unique numeric values in a given
character set. For instance, you may see the unique numeric values from the ASCII character
set.
It is important to remember that character constants are always surrounded by single quote
characters (‘) while a string of more than one character uses the double quote (“).
If this sounds confusing, just remember that single quotes go with single characters. You saw
an example of double quotes and character strings with the printf() function calls earlier.
From the ASCII character set, you will find that the unique numeric (decimal) values of ‘A’, ‘a’,
‘B’, and ‘b’ are 65, 97, 66, and 98, respectively. Therefore, given x as a character variable, and
given the ASCII character set, for instance, the following two assignment statements are
equivalent:
x = ‘A’;
x = 65;
x = ‘a’;
x = 97;
Printing Characters
You already know that the printf() function, defined in the C header file stdio.h, can
be used to print out messages on the screen. In this section,
you’re going to learn to use the character format specifier, %c, which indicates to the printf()
function that the argument to be printed is a character. You’ll learn more about the format
specifier in more detail later. (Here, you just get your feet wet.) The important thing to know for
now is that each format specifier
in the string you pass to printf() will correspond to one of the variables you pass in to the
function.
Lines 6 and 7 declare two character variables, c1 and c2, while lines 9 and 10 assign c1
and c2 with the character constants ‘A’ and ‘a’, respectively.
Note that the %c format specifier is used in the printf() function in lines 11 and 12,
which tells the computer that the contents contained by c1 and c2 should be printed as
characters. When the two statements in lines 11 and 12 are executed, two characters are
formatted and output to the screen, based on the numeric values contained by c1 and c2,
respectively.
The int keyword is used to specify the type of a variable as an integer. Integer numbers are also
called whole numbers, which have no fractional part or decimal point. Therefore, the result of an
integer division is truncated, simply because any fraction part is ignored.
Depending on the operating system and the C compiler you’re using, the length of an integer
varies. On most UNIX workstations, for example, an integer is 32 bits long, which means that
the range of an integer is from 2147483647 (that is, 231–1)
to -2147483648. The range of a 16-bit integer is from 32767 (that is, 215–1) to -32768.
Again, this can vary among different systems, so you can check the reference materials for your
compiler to be sure.
Some C compilers, such as Visual C++ 1.5, provide only the 16-bit integer, whereas
other 32-bit C compilers, such as Visual C++ 5.0, support the 32-bit integer.
int variablename;
Similar to the character declaration, if you have more than one variable to declare, you can
either use the format like this:
int variablename1;
int variablename2;
int variablename3;
or like this:
int variablename1, variablename2, variablename3;
Here variablename1, variablename2, and variablename3 indicate the places where you put the
names of int variables.
For example, the following statement declares MyInteger as an integer variable and
assigns it a value:
int A, a, B, b;
A = 37;
a = –37;
B = -2418;
b = 12 ;
Like the character format specifier (%c) that is used to format a single character, %d, called the
integer format specifier, is used to format an integer.
The major change I made in the above program was to replace the character format specifier
(%c) with the integer format specifier (%d).
Both format specifiers do basically the same thing — insert some data into the string you pass
to printf() — but the difference is in the way printf() displays that data. The %c specifier always
prints a character; the %d specifier always prints a number. Even when they refer to the exact
same data, it will be printed the way you indicate in the format specifier regardless of the actual
data type.
The two statements in lines 11 and 12 format the two character variables (c1 and c2) by using
the integer format specifier %d, and then print out two messages showing the numeric values
65 and 97 that represent, respectively, the characters A and a in the ASCII character set.
The floating-point number is another data type in the C language. Unlike an integer number, a
floating-point number contains a decimal point. For instance, 7.01 is a floating point number; so
are 5.71 and –3.14. A floating-point number is also called a real number.
A floating-point number is specified by the float keyword in the C language. Floating pointer
constants can be suffixed with f or F to specify float. A floating-pointer number without a suffix is
double by default. The double data type is introduced later.
Like an integer number, a floating-point number has a limited range. The ANSI standard
requires that the range be at least plus or minus 1.0 × 1037. In most cases, a floating-point
number is represented by taking 32 bits. Therefore, a floating-point number in C is of at least six
digits of precision. That is, for a floating-point number, there are at least six digits (or decimal
places) on the right side of the decimal point.
Unlike an integer division from which the result is truncated and the fraction is discarded, a
floating-point division produces another floating-point number. A floating-point division is carried
out if both the divisor and the dividend, or one of them, are floating point numbers.
For instance, 571.2 / 10.0 produces another floating-point number, 57.12. So do 571.2/ 10 and
5712 / 10.0.
float variablename;
Similar to the character or integer declaration, if you have more than one variable to declare,
you can either use the format like this:
float variablename1;
float variablename2;
float variablename3;
float a, b, c;
a = 10.38;
b = –32.7;
c = 12.0f;
You can also use the floating-point format specifier (%f) to format your output.
Inside the main() function, the two statements in lines 6 and 7 declare three integer
variables, int_num1, int_num2, and int_num3, and three floating-point variables,
flt_num1, flt_num2, and flt_num3.
Lines 9 and 10 assign the result of 32/10 to int_num1 and flt_num1, respectively;
32.0/10 to int_num2 and flt_num2 in lines 11 and 12, and 32/10.0 to int_num3 and
flt_num3 in lines 13 and 14.
Then, lines 16–21 print out the values contained by the three int variables and the three
floating-point variables. Note that %d is used for the integer variables, and the floating point
specifier (%f) is used for formatting the floating-point variables in the printf() function. Because
the truncation occurs in the integer division of 32/10, flt_num1 contains 3.000000, not 3.200000,
which you can see from the second line of the output. However, flt_num2 and flt_num3 are
assigned 3.200000 because both 32.0/10 and 32/10.0 are considered as the floating-point
division. But int_num2 and int_num3, as integer variables, discard respectively the fraction parts
of the floating-point divisions of 32.0/10 and 32/10.0. Therefore, you just see the integer
3 in both the third and fifth lines of the output.
In the C language, a floating-point number can also be represented by another data type, called
the double data type. In other words, you can specify a variable by the double keyword, and
assign the variable a floating-point number.
The difference between a double data type and a float data type is that the former uses
twice as many bits as the latter. Therefore, a double floating-point number is of at least 10 digits
of precision, although the ANSI standard does not specify it for the double data type.
Later you’ll learn to use the sizeof operator to obtain the length in bytes of a data type, such as
char, int, float, or double, specified on your computer system.
Using Scientific Notation
The C language uses scientific notation to help you write lengthy floating-point numbers.
In scientific notation, a number can be represented by the combination of the mantissa and the
exponent. The format of the notation is that the mantissa is followed by the exponent, which is
prefixed by e or E. Here are two examples:
mantissaeexponent
and
mantissaEexponent
Please note that mantissa and exponent above are both placeholders and you need to
replace them with numerical values. For instance, 5000 can be represented by 5e3 in scientific
notation. Likewise, -300 can be represented by -3e2, and 0.0025 by 2.5e-3.
Correspondingly, the format specifier, %e or %E, is used to format a floating-point number in
scientific notation. The usage of %e or %E in the printf() function is the same as %f.
Naming a Variable
There are a few things to keep in mind when creating your own variable names.
Remember that since variable names are identifiers, you must follow the rules for identifiers that
were outlined in the previous hour.
Just as function names should ideally reflect the task that the function performs, variable names
should describe the value stored in the variable and what purpose it serves in your program or
function. Most of the code samples so far have used single-letter variable names such as i, but
as you write larger programs and more complicated functions it becomes increasingly more
important to give your variables meaningful names.