Chapter Four
Chapter Four
Chapter Objectives
By the end of the chapter the learner should be able to;
Describe the C program character set and Trigraph characters
Describe the C Program Tokens;
Constants, Keywords, Strings, Identifiers Special symbols and Operators
Declare and assign values to C variables.
Differentiate between Constants and Symbolic Constants
The characters that can be used to form words, numbers and expressions depend upon the computer on
which the program is run. The characters in C are grouped into the following categories;
1. Letters : Upper case A ……. Z and Lower casa a ……..z.
2. Digits: 0……..9
3. Special characters
, comma & ampersand
. period ^ caret
; semicolon * asterisk
; colon - minus sign
? Question mark + plus sign
‘ apostrophe < opening angle bracket (less than
“ quotation mark sign
! exclamation mark > angle bracket or greater than sign
| vertical line ( left parenthesis
/ slash ) right parenthesis
\ back slash ] right bracket
~ tilde [ left bracket
_ under score { left brace
$ dollar sign } right brace
% percent sign # number sign
C compiler ignores white spaces (Blank, Horizontal tab, Carriage return, New line Form feed) unless they
are a part of a string constant. White spaces may be used to separate words but are prohibited between the
characters of keywords and identifiers.
Trigraph characters
C has the concept of “trigraph” sequences to provide a way to enter certain characters that are not
available on some keyboards. Each trigraph sequence consists of three characters (two question marks
followed by another character
??= #number sign
??( [ left bracket
??) ] right bracket
??< { left brace
???> } right brace
??! | vertical line
??/ \ back slash
??/ ^ caret
??- ~ tilde
C Tokens
Every C programs word is classified as either a keyword or identifier. All keywords have the fixed
meaning and these meaning cannot be changed and acts as the building blocks for program statements.
List of ANSI C keywords are listed in table 3.2 below.
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Identifiers refer to the names of variables, functions ad arrays. These are user defined names and consists
of a sequence of letters and digits, with a letter as a first character.
4.4 Constants
Constants in C Program refers to fixed values that do not change during the execution of the program.
Figure 4.2 below shows the types of constants supported by C program
Constant
Numeric Character String
include<stdio.h>
include<conio.h>
main()
{
printf(“%d”, ‘a’);
getch();
}
The program will output 97as the output. Since each character constant represents an integer value, it is
possible to perform arithmetic operations on character constants.
‘\a’ audible alert (bell) ‘\t’ carriage return ‘\?’ question mark
‘\b’ back space ‘\v’ vertical tab ‘\\’ backslash
‘\f’ form feed ‘\” single quote ‘\0’ null
‘\n’ new line ‘\”’ double quote
Table 4.2. C program backslash constants
4.5 Variables
A variable is a data name that may be used to store a data value. Unlike constants that remain unchanged
during the execution of a program, a variable may take different values at different times during
execution. A variable name should be chosen in a meaningful way so as to reflect its function or nature in
the program. Example price, rate, total, amount etc. Naming follows the same rules as stated in the
identifiers section. Valid variable names include;
John, Value distance, Sum1 etc
Invalid variable names includes;
123, (area) % etc
4.6. Data types
C program is rich in its data types. Storage representations and machine instructions to handle constants
differ from machine to machine. ANSI C supports three classes of data types;
1. Primary (or fundamental) data type
2. Derived data types
3. User-defined data types
The derived data type and the user-defined data types will be discussed in later chapters
Integer Character
Signed unsigned type Char
int unsigned int signed char
short int unsigned short int unsigned char
long int unsigned long int
Floating Point
Table 4.5. Size and Range of basic data types on a 16-bit machine
Data type Range of values
char -128 to 127
int -32,768 to 32,768
float 3.4e-38 to 3.4e +38
double 1.7e-308 to 1.7e+308
4.6.2. Integer data types
Integers are whole numbers with a range of values supported by a particular machine. Integer data types
are defined in C as int. C supports three classes of integer storage, short int, int and long int in both
signed and unsigned forms.
Declaration of variables is done immediately after the opening brace ({) in the main() function body.
Variables can also be declared outside the main() function either before or after. When declared before
the main() function they are called Global variables and can be used in all the functions in the program.
Global variables do not need to be declared again in other functions and are called external variables.
Variables declared within a function are called local variables as they are only visible and meaningful
inside the function they are declared. Example of local variable declaration;
main()
{
int code;
float x, y;
char c;
statements;
}
Variables can be assigned an initial value when declaring it in a process called initialization, using the
format;
data type variable_name = constant
The control string contains the format of data being received, the ampersand & before the variable name
is an operator that specifies the variable name’s address. Example
scanf(“%d”, &marks);
When the computer encounters this statement, the programs stops and waits for the value marks to be
keyed in through the keyboard and the <enter key> pressed. “%d” signifies that an integer data type is
expected. The use of scanf() provides an interactive feature and makes the program more user friendly.
Program Example 4.1. Program to show use of Scan for interactive programming
#include<stdio.h>
#include<conio.h>
main()
{
int number;
printf(" Enter an integer number\n");
scanf("%d", &number);
Symbolic names are constants and not variables and thus do not appear in the declaration section. The
rules that apply to the #define statement which defines a symbolic constants are;
Symbolic names have the same form as variable names. Symbolic names are usually written in
CAPITAL letters to visually distinguish them from the normal variable names.
No black space between the # and word define is permitted
‘#’ must be the first character in the line
A black space is required between #define and symbolic name and between the symbolic name and
the constant.
#define statement must not end with a semicolon.
After definition, the symbolic name should not be assigned any other value within the program using
an assignment statement. Example MAX = 200; this is illegal
Symbolic names are NOT declared for data type. Its data type depends on the type of constant.
#define statements may appear anywhere in the program but before it is referenced in the program,
Usual practice is to place the #define statements at the beginning of the program.
Program Example 4.2. Program to calculate the Average of 10 number entered through the
keyboard
#include<stdio.h>
#include<conio.h>
#define N 10
main()
{
int count;
float sum, average, number;
sum = 0;
count = 0;
while(count < N)
{
printf("Enter any number ");
scanf("%f", &number);
count = count + 1;
}
average = sum/N;
printf(" N = %d Sum = %f", N, sum);
printf("Average = %f", average);
getch();
}
Out put
Chapter Review Questions
1. What is a variable and what is meant by the “value” of a variable?.
2. What is variable initialization?.
3. Find error in the following declaration statements
int x;
float letter, DIGIT;
double p, q;
n, m, z INTEGER;
4. Write a program to read two floating point numbers using scanf statement assign their sum to an
integer variable and then output the values of all the three variables,