Ch-2 Constants, Variables and Data Types
Ch-2 Constants, Variables and Data Types
© KJS, IT | DDU
• Program: Executing a sequence of precise instructions to
process the given data.
• Syntax Rules: Combination of certain symbols and words
according to some rigid rules to form instructions.
2
C Character Set & Tokens
• Letters OR Alphabets
• (A, B, … , Z) & (a, b, … , z)
• Digits
© KJS, IT | DDU
• 0, 1, 2, … , 9
• Special Characters
• :(Colon), ;(Semicolon), ‘(single quotes), “(double quotes), +, -, *, /,
@, &(ampersand), %, ^, etc
• White Spaces
• Blank, Horizontal Tab, Newline 3
Trigraph Sequences
• For non-English keyboards,
ANSI C introduced the
concept of trigraphs.
• It helps to include certain
© KJS, IT | DDU
characters from program.
4
Keywords in C
auto double int struct
break else long switch
case enum register typedef
© KJS, IT | DDU
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while 5
Identifiers
• User defined names used in programs for providing identity to
variables, arrays and functions.
• Identifiers are made up of letters (uppercase and lowercase),
digits and underscore.
© KJS, IT | DDU
• Identifiers are case sensitive. Hence, max and Max are two
different identifiers for system.
• Keywords listed in previous slide can not be used as
identifiers.
6
Constants
© KJS, IT | DDU
7
Integer Constants
• Whole number values which are static or fixed.
• Positive and negative both numbers are allowed.
• Invalid Integer constant Example: 12,800 & 800$ & 15 756 etc.
© KJS, IT | DDU
• Octal examples: (0177)8= (127)10, (05)8=(5)10, (0731)8=(473)10
• Hexadecimal Examples: (0x32ab)16=(12971)10,
(0X92F)16=(2351)10, (0X12E)16=(302)10
© KJS, IT | DDU
• Can be represented in scientific format as well. In scientific
notation 314.28 is same as 3.1428e+2.
• Examples: 6.8e+4, -8.92e-1, 1.7e+2, 8.19E3
9
Character Constants
• Represent single character
• Enclosed between single quotes.
• Examples: ‘A’, ‘a’, ‘?’, ‘@’, ‘7’, ‘:’, etc.
© KJS, IT | DDU
• Characters follow ASCII (American Standard Code for
Information Interchange) value from its standard table.
• Character Constant Example in programming.
10
© KJS, IT | DDU
11
String Constants
• It is a sequence of character constants.
• Enclosed between double quotes.
• Examples: “Akshar”, “1982”, “D”, “hello, world.”, etc.
© KJS, IT | DDU
• Note: Here, “D” and ‘D’ are not same. And similarly “5”, ‘5’
and 5 are different.
12
Escape Sequences
• Special characters in addition to normal printable characters.
Constant Meaning
\a Bell
© KJS, IT | DDU
\b Back Space
\f Form Feed
\n New Line
\t Horizontal Tab
\v Vertical Tab
\’ and \” ans \? And \\ Special Characters Printing 13
\0 Null
Variables
• Represent quantities that change with time.
• Variable have a unique identifier associated with them called
variable name.
© KJS, IT | DDU
• The variables are used to store values that continuously
change during span of program.
• Variables follow ANSI(American National Standards Institute)
rules for variable names.
• Most of the C programming language related rules are
decided by ANSI committee. 14
Rules for Variable Names
• Only uppercase, lowercase letters, underscore and digits are
allowed.
• Variable name always begins with letter.
© KJS, IT | DDU
• Only first 32 characters of name are significant.
• Keywords can not be used as variable names.
• Variable names are case sensitive. Count and count are two
different variable names.
15
Variable Name Examples
- Validate Correctness
© KJS, IT | DDU
Variable Name
max
max4
max 3
Avg_num
double 16
1temp
Variable Name Examples
Variable Name Valid/Invalid Remark
max Valid
max4 Valid
© KJS, IT | DDU
max 3 Invalid Space is not allowed
Avg_num Valid
double Invalid Can’t use keyword
1temp Invalid Should start with letter
17
Some More Examples
Validate Correctness
© KJS, IT | DDU
18
Some More Examples
© KJS, IT | DDU
19
Data Types in C
© KJS, IT | DDU
20
Data Types in C (According to Book)
© KJS, IT | DDU
21
Data Types in C (At Present)
© KJS, IT | DDU
22
Variable Declaration
• Variables are used to store data values into memory spaces
identifies by variable names.
• Before storing a value in memory space, variables must be
defined with a valid variable name.
© KJS, IT | DDU
• Syntax: data_type vname1, vname2, vname3, vnameN ;
• Example: float marks, average, temperature ; OR
int no1, choice, count ;
• In C, variable must be declared at the beginning of the
program inside main function. 23
Assigning Values to Variable
• Assignment operator ‘=‘ is used to assign value to variables.
• One can assign vales to variable at the time of declaration or
at a later stage in program.
© KJS, IT | DDU
Example: Example:
© KJS, IT | DDU
• Example: #define PIE 3.1415
#define MAX 5
#define print printf
• Generally uppercase letters are used to define symbolic
constants. Its not a rule though! 25
Symbolic Constants VS Variables
• When we create a variable, special memory space is assigned
for that value in memory that can be changed later on.
• While with the symbolic constants, not only we can’t change
its value, but there is no memory space for it.
© KJS, IT | DDU
• Compiler simply replaces symbolic constants with its
alternatives before execution.
Symbolic Constant: Variables:
Memory Representation
#define PIE 3.1415 int a, b=18;
a b
#define clr clrscr a=15; 26
47 18
#define p printf a=a+32;
Constant Variables in C
• Other than symbolic constants, C can have constants in
program defined using keyword const.
• Any attempt to modify such variables results in compilation
error.
© KJS, IT | DDU
• Syntax: const data_type var_name = value ;
E.g. const int max = 100 ;
• If you do not initialize const, C compiler will initialize as 0.
27
Enumerated Data Types:
• Used to define more than one symbolic constants.
• Syntax: enum group_name { const_name1=value,
const_name2, const_name3=value2, … , const_nameN }
© KJS, IT | DDU
• E.g: enum week { SUN=1, MON, TUE, WED, THU, FRI, SAT }
• Enum data type can be compared using if statements.
• Enum supports different data types as its values.
• It is more categorized and managed than symbolic constatns.
28
Constant Handling
Symbolic Constants Enumerated Data Types Constants
Works as find and Extension of symbolic Resembles with
replace. constants with more than variables, but can’t be
one type of similar values. modified.
© KJS, IT | DDU
Managed by Managed by preprocessor. Managed by compiler.
preprocessor.
No memory storage. No memory storage. Stored in memory.
Can’t be modified Can’t be modified Can’t be modified
No defaults. Value must Default starts with 0 if not Default is 0 if not
be provided. specified. specified. 29
#define PIE 3.1415 enum status {online,offline} const int max=100;
User Defined Type Declaration
• C supports a feature called “type definition”.
• It allows user to define an identifier that would represent an
existing data type.
© KJS, IT | DDU
typedef type identifier; New alias to be used
keyword Existing data type
• Examples:
typedef int units; units batch1, batch2;
typedef float marks; marks class1[50], class2[50];
30
Storage Classes
• Storage classes basically include the scope, visibility and
lifetime which help us to trace the existence of a particular
variable during the runtime of a program.
• There are four different storage classes in a C program:
© KJS, IT | DDU
• auto
• register
• static
• extern
31
Storage
Classes
© KJS, IT | DDU
32
Example
• Global/External Variables
• Automatic Variables
• Static Variables (?)
• Register Variables (?)
© KJS, IT | DDU
33
THANK YOU!