Constants, Variables
and Data Types
Character Set
◼ Characters in C are grouped into following
characters.
Letters (a-z A-Z)
Digits (0-9)
Special Characters
White Spaces
◼ Blank space
◼ Horizontal tab
◼ Carriage return
◼ New Line
◼ Form feed
Trigraph Characters
◼ Many non-English keyboards do not support all
symbols.
◼ Trigraph character (??) is used with another
character to get the missing symbol.
◼ Examples
??= #
??( [
??< {
C Tokens
◼ In text, words and punctuation are said to
tokens.
◼ In C, the smallest individual units are known as
C tokens.
Keywords
◼ Every C word is either a Keyword or an
Identifier.
◼ Keywords have a fixed meaning and cannot be
changed.
◼ 32 Keywords, should be written in lowercase.
◼ Additional keywords may be added by different
compilers, refer C manual provided by them.
Keywords
Identifiers
◼ Identifiers refer to the name of variables,
functions and arrays.
◼ They are user-defined, consisting of letters
and digits.
◼ UPPER or lower case can be used.
◼ Underscore can be used between long
identifier (formed using 2 words).
Rules for Identifiers
◼ First character must be an alphabet (or an
underscore).
◼ Must consists of only letters, digits or
underscore.
◼ Only first 31 characters are significant.
◼ Cannot use a keyword.
◼ Must not contain white space.
Constants
Fixed values that do not change during the
execution of program.
Integer constants
◼ Sequence of digits
◼ 3 types
Decimal
Octal
Hexadecimal
Decimal Integers
◼ Set of digits (0-9), preceded by optional + or –
symbol
◼ Valid→ 123 -321 0 +78
◼ Invalid→ 15 750 20,000 $1000
Octal Integers
◼ Any combination of numbers from 0 to 7, with a
leading 0
◼ Example→ 037 0435 0551
Hexadecimal Integer
◼ Sequence of digits along with alphabets A
to F or a to f, preceded by 0x or 0X
◼ Example→ 0X2 0x9F 0Xbcd
◼ Octal and Hexa are rarely used.
◼ Largest value that can be stored depends
on machine.
Ina 16-bit machine, it is 32767 and
2147483647 in a 32-bit machine.
Real (Floating Point) Constants
◼ To represent real numbers such as
0.0083 -0.75 435.36
.95 +.5 -.71
◼ mantissa e exponent
215.65 → 2.1565e2
-0.000000368 → -3.68E-7
Examples of Numeric Constants.
Valid or Invalid
Single Character Constants
◼ A single character enclosed within a pair of
single quote marks
‘5’ ‘X’ ‘;’ ‘’
◼ Have integer values known as ASCII
values
printf(“%d”, ‘a’); → 97
printf(“%c”, 97); → a
String Constants
◼ Sequence of characters within double
quotes
“Hello” “1947” “X”
◼ ‘X’ and “X”, are not same.
◼ String constants don’t have any integer
value associated with them.
Program
◼ Write a C Program to check if a number is
positive or negative.
Backslash Character Constants
◼ Used in output functions
‘\a’ ‘\b’ ‘\n’ ‘\t’
◼ Escape sequence characters
Zero
Variables
◼ Name to store a data value.
◼ Takes different values.
◼ Chosen by programmer in a meaningful
way.
◼ Rules of identifiers.
Data Types
◼ To store a value and to specify the kind of
data.
◼ ANSI C supports 3 classes of Data types
Primary (Fundamental) data types
Derived data types
User-defined data types
Primary data types
◼ C supports 5 fundamental data types
int
char
float
double
void
Integer Types
◼ Signed values requires 1 bit for
representing the sign.
◼ Hence, for a 16 bit computer
-215 to +215-1
-32768 to +32767
-2n-1 to +2n-1-1
◼ 3 classes of Integer storage
short int
int Both signed and unsigned
long int
◼ The size of the above mentioned types
are different
Floating Point Types
◼ Float uses 32 bits on both 16 and 32 bit
machines.
◼ Gives an accuracy up to 6 digits
◼ Double uses 64 bits and gives accuracy
up to 14 digits.
◼ If not sufficient use long double which
uses 80 bits.
IEEE 754 Standard for 64 bit floating point
representation
Void and Character Types
◼ Void- has no values.
Function returning no values
◼ Characters require 8 bits
Signed -128 to 127
Unsigned 0 to 255
Declaration of variables
◼ Primary Type Declaration
data-type v1,v2,v3…vn ;
◼ Declaration can be made within main() or
before main().
Programs
◼ Write a C Program to check if a number is
even or odd.
◼ Write a C Program which use different
data types.
User-Defined Type Declaration
◼ C allows users to define an identifier that
represents an existing data type
typedef type identifier;
Existing type
typedef int marks;
Int m1,m2,m3,m4;
marks m1,m2,m3,m4;
◼ Increase the readability of the program.
enum
◼ enum identifier {value1, value2, ….valuen};
Enumeration Constants
◼ enum identifier v1,v2,v3,…vn;
Enumeration Variables
◼ Example:
enum day {Monday, Tuesday,…,Sunday};
enum day week_st,week_end;
week_st=Monday;
week_end=Friday;
Enum (Continued)
◼ Compile assigns integer digits to enum
constants starting from zero.
◼ To change the numbering,
◼ enum day {Monday=1, Tuesday,…,Sunday};
Storage Classes
◼ Storage classes provide information
regarding location and visibility of the
variable.
◼ Local variables and Global variables.
◼ Auto (garbage)- Local variable only to the function.
Default is auto
◼ static (0)- Local variable which exists and retains the
value even after transfer of control
◼ extern (0)- Global variable known to all the functions
◼ register- Local variable which is stored in the register
Storage Classes (Continued)
◼ auto int count;
◼ register char ch;
◼ static int x;
◼ extern long total;
Assigning values
◼ variable_name= constant;
a=10;
name=“Rahul”;
◼ Multiple assignments in a single statement
a=b=c=0;
num1=num2=MAX;
◼ variable_name=expression;
a=a+1;
sum=a+b;
Reading Data from Keyboard
◼ Using the scanf statement
scanf (“control string”, &var1,&var2,…);
◼ Control String- data to be received
◼ “&” symbol used to access variable’s
address
scanf (“%d %f %c”, &id, &sal, &ch);
Symbolic Constants
◼ Repeatedly using a value in the program
◼ Making modifications and understanding is
difficult
#define symbolic_name value_of_constant
#define STRENGTH 100
#define PI 3.14159
Constant Variables
◼ To make a variable constant we prefix the
const keyword
const int class_strength=40;
◼ Modifications are not allowed during the
program execution.
Volatile variable
◼ Allows the value of the variable to be
changed by an external source.
◼ Prefix the keyword volatile
volatile int number;
◼ Only external sources are allowed to
modify and not by its own program
volatile const int number=100;
Programs
◼ Write a C Program to display size of data
types.
◼ Write a C Program which uses typedef.
◼ Write a C Program which uses local and
global variables.
◼ Write a C Program which use symbolic
constants.
Operators and
Expressions
Operators and Expressions
◼ Operator- symbol that tells the computer to
perform mathematical or logical operation
◼ Expression- Operators with variables
◼ 8 categories of operators are supported in
C
◼ Arithmetic
◼ Relational
◼ Logical
◼ Assignment
◼ Increment and decrement
◼ Conditional
◼ Bitwise
◼ Special
Arithmetic Operators
Integer Arithmetic
◼ When both the operands in an expression
are integer then we say its integer
arithmetic operation
◼ 6/7 or -6/-7 will result to ZERO
◼ -6/7 may be ZERO or -1 (Machine
dependent)
◼ -14 % 3 = -2
Sign of the result is always the sign of
◼ -14 % -3 = -2 the first operand
◼ 14 % -3 = 2
Real Arithmetic
◼ Expression involving only real operands
X=6.0/7.0=0.857143
◼ % cannot be used with real operands
Mixed-mode Arithmetic
◼ One operand is real and the other operand
is int
15/10.0=1.5
◼ 1.5 because of the real no 10.0
15/10=1
Relational Operators
◼ Used when we are comparing values or
expressions.
◼ ae-1 relational operator ae-2
◼ ae-1 and ae-2 are evaluated first and then
they are compared.
◼ Relational expressions are valued in
decision statements
> is complement of <= !(x<y) x>=y
< is complement of >=
== is complement of !=
Logical Operators
◼ C has 3 logical operators
&& logical AND
|| logical OR
! Logical NOT
◼ An expression which contains 2 or more
relational expressions is logical expression or
compound relational expression
◼ a>b && x==10
Step 1: Evaluate a>b (True or False)
Step 2: Evaluate x==10 (True or False)
Step 3: Evaluate the whole expression (True
or False)
Assignment Operators
◼ Along with ‘=’, C supports shorthand
notation for assignment
v op =exp;
◼ v- variable
◼ exp- expression
◼ op- arithmetic operator
◼ v op = exp; ◼ x + = y+1;
◼ v = v op (exp); ◼ x = x + (y+1);
◼ A Simpler Example
x=x+1 is same as x+=1
◼ Advantage is more when the repeating
expression is large
value(5*j-2) = value (5*j-2) +delta;
Which can be rewritten as
value (5*j-2)+=delta;
Programs
◼ Write a C Program which use symbolic
constants. (#define PI 3.14)
◼ Write a C Program to use operators.
Increment & Decrement
Operators
◼ ++, - - ➔ Unary operators.
◼ ++ ➔ Increase value of variable by 1.
◼ Can only be applied to variables(Not on constants or
expressions)
◼ Usage: (Let v be a variable)
◼ 1. ++v ➔ Prefix Increment : same as v = v+1;
◼ 2. v++ ➔ Postfix Increment : same as v = v+1;
◼ 3. --v ➔ Prefix Decrement : same as v = v-1;
◼ 4. v-- ➔ Postfix Decrement : same as v = v-1;
60
Increment & Decrement
Operators
Examples:
◼ m = 5;
◼ y = ++m; (prefix)
◼ After Execution:
◼ y=6, m=6
◼ m = 5;
◼ y = m++; (postfix)
◼ After Execution:
◼ y=5, m=6
61
Increment & Decrement
Operators
Rules:
◼ ++,-- are Unary operators, these work only on
variables.
◼ Postfix ++(--)used in expression first value is used
in evaluation of expression, then
incremented(decremented).
◼ Prefix ++(--)used in expression first variable
incremented(decremented), the new value is used
in evaluation of expression,
◼ Precedence and associativity of ++ and – – are
same as that of unary + and unary –
62
Conditional Operator (?:)
◼ General Syntax: exp1?exp2:exp3;
◼ Exp1 is evaluated first.
◼ If exp1 evaluates to a non-zero
value(true) then exp2 is evaluated.
◼ If exp1 evaluates to a zero (false)
then exp3 is evaluated.
◼ Same as:
if(exp1)
exp2;
else
63
exp3;
Conditional Operator
◼ Example 1:
◼ a = 10; b=15;
◼ x = (a>b)?a:b; // x = 15; ➔ Finding
larger of two.
◼ Same as:
if(a>b)
x = a;
else
x = b;
◼ A concise way of doing if-else condition.
◼ Reduces number of lines of code.
64
Bitwise Operators
◼ Act on bit level in a data item (integer
variable, int constant etc.)
◼ Can’t work on floating point number (real
numbers)
◼ Operators are:
◼ << ➔Shift left
◼ >> ➔Shift right
◼ & ➔ Bitwise AND
◼ | ➔ Bitwise OR
◼ ^ ➔Bitwise Exclusive OR
65
Bitwise Operators(<<)
◼ Examples(shift left: <<): (Assume integer
variable is 1 byte size)
◼ int a = 5;
◼ a’s Bit Sequence in Memory: 0 0 0 0 0 1 0 1
◼ Operators are:
◼ b = a<<1; // Shift the bits one position left and
assign to b.
◼ b's Bit Sequence in Memory: 0 0 0 0 1 0 1 0
◼ a's bit sequence remains same as original.
◼ Values of a and b??
66
Bitwise Operators(>>)
◼ Examples(shift right >>): (Assume integer variable is 1
byte size)
◼ int a = 5;
◼ a‘s Bit Sequence in Memory: 0 0 0 0 0 1 0 1
◼ Operators are:
◼ b = a>>1; // Shift the bits one position right and assign to
b.
◼ b‘s Bit Sequence in Memory: 0 0 0 0 0 0 1 0
◼ a‘s bit sequence remains same as original.
◼ Values of a & b?
67
Bitwise Operators(&)
◼ Examples(Bitwise AND :&): (Assume integer
variable is 1 byte size)
◼ int a = 5,b = 6,c;
◼ a‘s Bit Sequence in Memory: 0 0 0 0 0 1 0 1
◼ b‘s Bit Sequence in Memory: 0 0 0 0 0 1 1 0
◼ c = a &b;
◼ c‘s Bit Sequence in Memory: 0 0 0 0 0 1 0 0
68
Bitwise Operators(|)
◼ Examples(Bitwise OR:|): (Assume integer
variable is 1 byte size)
◼ int a = 5,b = 6,c;
◼ a’s Bit Sequence in Memory: 0 0 0 0 0 1 0 1
◼ b’s Bit Sequence in Memory: 0 0 0 0 0 1 1 0
◼ c = a | b;
◼ c‘s Bit Sequence in Memory: 0 0 0 0 0 1 1 1
69
Bitwise Operators(^)
◼ Examples(Bitwise XOR:^): (Assume integer
variable is 1 byte size)
◼ int a = 5,b = 6,c;
◼ a's Bit Sequence in Memory: 0 0 0 0 0 1 0 1
◼ b's Bit Sequence in Memory: 0 0 0 0 0 1 1 0
◼ c = a ^ b;
◼ c‘s Bit Sequence in Memory: 0 0 0 0 0 0 1 1
70
Special Operators: Comma(,)
◼ Comma (,)
◼ Used to link related expressions together.
◼ Comma separated expressions execute from
left to right.
◼ Right most expression is the value of the
combined expression.
◼ E.g: v = (x=10, y=5, x+y);
◼ Value of v?
◼ E.g. for loop: for(i=0,j=1; i<n; i++,j++)
71
Special Operators: sizeof
◼ Size of operator(sizeof)
◼ Used to find the size(in bytes) of a data
type or variable or constant.
◼ int a = sizeof(int); // a??
◼ int b = sizeof(float); // b??
◼ int c = sizeof(a); // c??
◼ int d = sizeof(100); // d??
72
Arithmetic Expressions
AE is a combination of variables, constants and operators arranged as per syntax of the language
◼ Algebraic expression ◼ C Expression
a xb–c ➢ a*b-c
(m+n)(x+y) ➢ (m+n)*(x+y)
(ab)/c ➢ (a*b)/c
3x2 + 2x+1 ➢ 3*x*x+2*x+1
(x/y) + c ➢ x/y+c
73
Evaluation of expressions
◼ General format:
variable = expression;
◼ First expression is evaluated, the result is
assigned to variable in LHS.
◼ x = a * b - c;
◼ y = b / c *a;
◼ z = a – b / c + d;
74
Precedence of Arithmetic
Operators
◼ int a = 2,b =4,c=5,d=3;
◼ int e = a+b*c/d;
◼ The order of executing sub-expressions
affects the final result.
◼ C Language defines rules for deciding the
order execution in such complex expression.
◼ Precedence: refers to the priority of
operators w.r.t choosing order of execution.
◼ High Precedence: * / %
◼ Low Precedence: + -
75
Operator Precedence
◼ All operators are classified into different levels of
precedence.
◼ If expression involves multiple operators
( e.g: a+b*c-d), operation with higher precedence is performed
first.
◼ ‘b*c’ is done first in the example, since * has higher
precedence than +,-
◼ What if the precedence for two operators are same? ?
◼ I.e, in example a+b*c-d , + and – have the same
precedence.
◼ ➔ Associativity
76
Precedence & Associativity
◼ E.x. a+b*c-e*f
◼ E.x. if(a>b && c==d)
◼ E.x. a=5, b=10; !a<b (Note: ! has higher
precedence than <)
◼ (a*b)*(c-d)/(e+f)
◼ Precedence rules decide the order in which
different precedence level operators are applied.
◼ Associativity rules decide the order in which
operators in same precedence level are applied.
77
78
Precedence & Associativity
◼ Associativity :- Defines the direction of execution when
operators of same precedence level are involved.
◼ Two types :
Left-to-right associativity
Right-to-left associativity
◼ Arithmetic operators follow L-R associativity.
Ex: a + b * c - d
◼ Precedence rules can be bypassed with use of
parenthesis
◼ E.g. (a+b)*c-d
79
Precedence & Associativity of
most common operators
Operator Category Associativity
*,/,% Arithmetic L to R
operators
+,- Arithmetic L to R
operators
<,<=,>,>= Relational Ops L to R
==,!= Relational Ops L to R
&& Logical AND L to R
|| Logical OR L to R
=,*=,/=, %=,+=,-= Assignment & R to L
Shorthand
80
‘Type conversion’ in expressions
◼ Conversion of one data type to another.
◼ Done when expressions contains operands of
different types.
◼ E.g: 2+3*2.7+7
◼ Two types of type conversion
◼ 1. Implicit Type Conversion: Automatically done
by the compiler.
◼ Conversion happens according to the rules of the
language.
◼ General thumb rule for ‘automatic’ type
conversion: Conversion done such a way that,
there is no data/precision loss for the data.
81
Entire Data Types in C
82
Implicit Type Conversion
hierarchy
83
Type conversion in expressions
◼ 2. Explicit Type Conversion: Explicitly done
using casting operator
variable = (type)expression; // variable/expression
◼ Conversion happens according to the rules of the
language.
int a =10;
float b;
b = (float)a; // explicit type conversion
◼ Another example
int a = 5,b =3;
float c = a/b; // c = 1.0
float d = (float)a/b; // d = 1.666667
84
Examples of output statements
printf(“Hello world..!!”);
◼ Output: Hello world..!!
int a = 5,b=3;
float c = (float)a/b;
printf(“Result of %d / %d is %f”,a,b,c);
◼ Output: Result of 5 / 3 is 1.666667
printf(“Result of %d / %d is \n %f”,a,b,c);
◼ Output: Result of 5 / 3 is
1.666667
85
Examples of Input statements
int a,b;
float c;
char d;
double e;
scanf(“%d %d %f”,&a,&b,&c);
scanf(“%c”,&d);
scanf(“%lf”,&e);
Wrong way: scanf(“c=%c\n”,&d); // Wrong!!
86
Programs
◼ Write a C Program which uses “,” operator.
◼ Write a C Program to check the outcome
of the following condition.
if (a<b<c)
87
End of Unit 1
88