Unit 3 Variables and Constants: Structure
Unit 3 Variables and Constants: Structure
Structure
3.0 Introduction
3.1 Objectives
3.2 Character Set
3.3 Identifiers and Keywords
3.3.1 Rules for Forming Identifiers
3.3.2 Keywords
3.4 Data Types and Storage
3.5 Data Type Qualifiers
3.6 Variables
3.7 Declaring Variables
3.8 Initialising Variables
3.9 Constants
3.9.1 Integer Constants
3.9.2 Floating Point Constants
3.9.3 Character Constants
3.9.4 String Constants
3.10 Symbolic Constants
3.11 Summary
3.12 Solutions / Answers
3.13 Further Readings
3.0 INTRODUCTION
As every natural language has a basic character set, computer languages also have a
character set, rules to define words. Words are used to form statements. These in turn
are used to write the programs.
Computer programs usually work with different types of data and need a way to store
the values being used. These values can be numbers or characters. C language has two
ways of storing number values—variables and constants—with many options for
each. Constants and variables are the fundamental elements of each program. Simply
speaking, a program is nothing else than defining them and manipulating them. A
variable is a data storage location that has a value that can change during program
execution. In contrast, a constant has a fixed value that can’t change.
This unit is concerned with the basic elements used to construct simple C program
statements. These elements include the C character set, identifiers and keywords, data
types, constants, variables and arrays, declaration and naming conventions of
variables.
3.1 OBJECTIVES
After going through this unit, you will be able to:
• define identifiers, data types and keywords in C;
• know name the identifiers as per the conventions;
• describe memory requirements for different types of variables; and
• define constants, symbolic constants and their use in programs.
When you write a program, you express C source files as text lines containing
characters from the character set. When a program executes in the target environment,
37
An Introduction to C it uses characters from the character set. These character sets are related, but need not
have the same encoding or all the same members.
Every character set contains a distinct code value for each character in the basic C
character set. A character set can also contain additional characters with other code
values. The C language character set has alphabets, numbers, and special characters as
shown below:
1. Alphabets including both lowercase and uppercase alphabets - A-Z and a-z.
2. Numbers 0-9
; : { , ‘ “ |
} > < / \ ~ _
[ ] ! $ ? * +
= ( ) - % # ^
@ & .
3.3.2 Keywords
Keywords are reserved words which have standard, predefined meaning in C. They
cannot be used as program-defined identifiers.
38
The lists of C keywords are as follows: Variables and
Constants
char while do typedef auto
int if else switch case
printf double struct break static
long enum register extern return
union const float short unsigned
continue for signed void default
goto sizeof volatile
Note: Generally all keywords are in lower case although uppercase of same names
can be used as identifiers.
C Language provides four basic data types viz. int, char, float and double. Using
these, we can store data in simple ways as single elements or we can group them
together and use different ways (to be discussed later) to store them as per
requirement. The four basic data types are described in the following table 3.1:
Memory requirements or size of data associated with a data type indicates the range of
numbers that can be stored in the data item of that type.
Unsigned bits use all bits for magnitude; therefore, this type of number can be larger.
For example signed int ranges from –32768 to +32767 and unsigned int ranges from
0 to 65,535. Similarly, char data type of data is used to store a character. It requires 1
byte. Signed char values range from –128 to 127 and unsigned char value range from
0 to 255. These can be summarized as follows:
Data type Size (bytes) Range
3.6 VARIABLES
Variable is an identifier whose value changes from time to time during execution. It is
a named data storage location in your computer’s memory. By using a variable’s
name in your program, you are, in effect, referring to the data stored there. A variable
represents a single data item i.e. a numeric quantity or a character constant or a string
constant. Note that a value must be assigned to the variables at some point of time in
the program which is termed as assignment statement. The variable can then be
accessed later in the program. If the variable is accessed before it is assigned a value,
it may give garbage value. The data type of a variable doesn’t change whereas the
value assigned to can change. All variables have three essential attributes:
• the name
• the value
• the memory, where the value is stored.
For example, in the following C program a, b, c, d are the variables but variable e is
not declared and is used before declaration. After compiling the source code and look
what gives?
main( )
{
int a, b, c;
char d;
a = 3;
b = 5;
c = a + b;
d = ‘a’;
e=d;
……….
……….
}
After compiling the code, this will generate the message that variable e not defined.
For example,
int a;
short int a, b;
40
int c, d; Variables and
Constants
long c, f;
float r1, r2;
int a = 10;
float b = 0.4 e –5;
char c = ‘a’;
a = 10;
b = 0.4 e –5;
c = ‘a’;
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
41
An Introduction to C
3.9 CONSTANTS
A constant is an identifier whose value can not be changed throughout the execution
of a program whereas the variable value keeps on changing. In C there are four basic
types of constants. They are:
1. Integer constants
2. Floating point constants
3. Character constants
4. String constants
Integer and Floating Point constants are numeric constants and represent numbers.
For example,
1 443 32767
are valid decimal integer constants.
Long Integer constants: These are used to exceed the magnitude of ordinary
integers and are appended by L.
For example,
50000U decimal unsigned.
1234567889L decimal long.
0123456L otal long.
0777777U otal unsigned.
A Floating Point number taking the value of 5 x 104 can be represented as:
5000. 5e4
5e+4 5E4
5.0e+4 .5e5
43
An Introduction to C The magnitude of floating point numbers range from 3.4E –38 to a maximum of
3.4E+38, through 0.0. They are taken as double precision numbers. Floating Point
constants occupy 2 words = 8 bytes.
Character constants have integer values associated depending on the character set
adopted for the computer. ASCII character set is in use which uses 7-bit code with 27
= 128 different characters. The digits 0-9 are having ASCII value of 48-56 and ‘A’
have ASCII value from 65 and ‘a’ having value 97 are sequentially ordered. For
example,
ESCAPE SEQUENCE
There are some non-printable characters that can be printed by preceding them with ‘\’
backslash character. Within character constants and string literals, you can write a
variety of escape sequences. Each escape sequence determines the code value for a
single character. You can use escape sequences to represent character codes:
• you cannot otherwise write (such as \n)
• that can be difficult to read properly (such as \t)
• that might change value in different target character sets (such as \a)
• that must not change in value among different target environments (such as \0)
For example,
• Replacement of value has to be done at one place and wherever the name
appears in the text it gets the value by execution of the preprocessor. This
saves time. if the Symbolic Constant appears 20 times in the program; it needs
to be changed at one place only.
3.11 SUMMARY
To summarize we have learnt certain basics, which are required to learn a computer
language and form a basis for all languages. Character set includes alphabets, numeric
characters, special characters and some graphical characters. These are used to form
words in C language or names or identifiers. Variable are the identifiers, which
change their values during execution of the program. Keywords are names with
specific meaning and cannot be used otherwise.
We had discussed four basic data types - int, char, float and double. Some qualifiers
are used as prefixes to data types like signed, unsigned, short, and long.
45
An Introduction to C
The constants are the fixed values and may be either Integer or Floating point or
Character or String type. Symbolic Constants are used to define names used for
constant values. They help in using the name rather bothering with remembering and
writing the values.
2. int rollno;
float total_marks, percentage;
46