Constant Variables Chapter-4
Constant Variables Chapter-4
Constant Variables Chapter-4
CONSTANTS
,VARIABLES AND DATA
TYPES
C Character set
•
It denotes any alphabet, digit or special
symbol used to represent information.
a. Alphabets
b. Digits
c. Special Characters
d. White Spaces
•
ALPHABETS
Uppercase letters A-Z
Lowercase letters a-z
DIGITS 0, 1, 2, 3, 4, 5, 6, 7,
8, 9
•
SPECIAL CHARACTERS
~ ` !@#%^&“‘( )[ ] { }+-*/\ _
=;:
, . < >?|
White spaces
Includes space ,tab (\t,\v)
New line (\n)
C TOKENS
•
The smallest individual elements or units
in a program are called as Tokens. C has
following tokens.
•
Identifiers
•
Keywords
•
Constants
•
Operators
•
Special characters
Keywords
•
There are some reserved words in C,
called keywords.
•
All the keywords have some pre-defined
meaning and can be used only for the
purpose intended.
•
All the keywords must be written in lower
case letters.
auto double int struct
break else long switch
case enum register typedef
char extern return union
continue for signed void
do if static while
default goto sizeof volatile
const float short unsigned
Identifiers
•
Identifier refers to name given to entities
such as variables, functions, structures
etc.
•
The rules for writing identifiers are
A valid identifier can have letters (both
uppercase and lowercase letters), digits and
underscores.
The underscore symbol is used in middle of
the variable.
example
•
Sum
•
Average
•
Total_marks
•
X1
•
Y1
•
pincode
Constants
•
The value of a constant cannot be
changed during execution of the program,
neither by the programmer nor by the
computer.
Integer Constant
•
An Integer constant consists of sequence
of digits.
•
Three types of integer constants
Decimal (0-9) (e-x) 345,890
Octal (0-7) (e-x) 051,044
Hexadecimal(0-9,A,B,C,D,E,F)
(e-x) oABC,oA1
Real Constants
•
Quantities which are represented by
numbers with fractional part are called real
constants.
Rules for real constant
1) It must have decimal point.
2) It can be either positive or negative.
3) No commas or blanks are allowed with in
real constant.
E-x 0.98, 9.876
Character constant
E-x
“red”, “phone”
Escape sequence
•
These are special backslash character
constants.
•
It is denoted by backslash followed by a
particular character.
•
E-x
•
\n - new line,
•
\t - horizontal tab
Variables
•
A variable is an identifier used to store a
single data item.
The rules for naming a variable are
•
Variable name starts with letter followed
by letters, digits or combination of both.
•
No special character except the
underscore symbol.
•
Maximum length of a variable name
should not exceed 32 characters.
Data types
•
Integer (int)
•
Character (char)
•
Floating point (float)
•
Double precision floating point (double)
Data type Size in bytes
int 2 bytes
Char 1 byte
Float 4 bytes
Integers
•
Integer is used to store whole numbers
variable name =
constant/variable/expression;
E-x
a=0;
MACROS
•
A macro is a fragment of code which has
been given a name. Whenever the name
is used, it is replaced by the contents of
the macro.
•
#define is a preprocessor directive used to
define the macro.
•
The general form of macro definition is
#define name replacement-string
E-x
#define max_marks 100
#define pi 3.14
#define college “new horizon”
Rules for macros
•
They are usually defined at the beginning
of the program.
•
#define statement must not terminated
with ;
•
When the program is compiled, every
occurrence of the macro name is replaced
with its value.
Advantages of macro
•
Macros make the program more clear.
•
Macros enhance the understandability of
the program.
•
They make the program easily modifiable.