Chapter 6 - C Program Data Types, Constants & Variables
Chapter 6 - C Program Data Types, Constants & Variables
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 ~ tilde > angle bracket or greater
. period _ under score than sign
; semicolon $ dollar sign ( left parenthesis
; colon % percent sign ) right parenthesis
? Question mark & ampersand ] right bracket
‘ apostrophe ^ caret [ left bracket
“ quotation mark * asterisk { left brace
! exclamation mark - minus sign } right brace
| vertical line + plus sign # number sign
/ slash < opening angle bracket (less than
\ back slash 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.
6.2.Trigraph characters
C Tokens
Special Symbols
Identifiers
6.3.3. Constants
Constants in C Program refers to fixed values that do not change during the execution of the
program. Figure 6.2 below shows the types of constants supported by C program
Constant
Integer Constants
An integer constant refers to a sequence of digits. There are three types of integers namely;-
1. Decimal integer: Digits 0 – 9 example +78, -321 Embedded spaces commas and non digit
characters are not permitted between digits
2. Octal integer: consists of any combination of digits from the set 0 trough 7, with a leading 0.
Example 037, 0, 0435, 0551
3. Hexadecimal integer: a sequence of digits preceded by 0x or 0X, they may also include
alphabets A through F or through f. A through F represents number 10 to 15. Examples 0X2,
0x9F, 0Xbcd
Real Constants
Real Constants are used to represent quantities that vary continuously, such as distance, height,
temperatures, prices etc. which integer constants are inadequate to represent. These quantities are
represented by numbers containing fractional parts example 12.58. Such numbers are called real
(floating point) constants. A real number may also be expressed in exponential (or scientific)
notation example the value 215.66 may be written as 2.1566e2 in exponential notation. e2 means
multiply by 102
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.
String Constants
A string constant is a sequence of characters enclosed in double quotes. They may be letters,
numbers, special characters and blank spaces. Example “hello’, “1987”, “5+5” etc. Note ‘X’ is not
the same as “X”. A string constant does not have a ASCII value.
‘\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
6.3.4. 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
Integer Character
Floating Point
Float double long double void
Table 6.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
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.
Types of Variable
1) Global Variable: Declared before the main() function 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.
2) Local Variable: 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;
}
6.6.1. User defined Type Declaration
C supports type definition that allows a programmer to define an identifier that would represent an
existing data type. These data types can later be used to declare variable. Format;
typedef type identifier;
where type refers to existing data type and identifier refers to the new name given to the data type.
The existing data type may belong to any class of type, including the user-defined ones. This
method only changes the name of the identifier and not the data type. Examples
typedef int units;
typedef float marks;
Once defined the new identifiers names can be to declare variable example
Programming Methodology – Chapter 4: C Program Constants, Variables, Data Types Page 6
units code;
marks x, y;
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.
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.
Program Example 6.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
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,