2. lecture Basics of C (2)
2. lecture Basics of C (2)
Types
int
E.g. int a; a =5;
float
E.g. float miles; miles=5.6;
double
E.g. double big; big =312E7;
char
E.g. char letter; letter =‘y';
Modifying the Basic Data Types
The lists of modifiers are:
signed
unsigned
short
long
signed, unsigned, short, long can be used with int.
signed, unsigned can be used with char.
long can be used with double.
The amount of storage allocated is not fixed. ANSI has
the following rules
short int <=int <=long int
float <=double <=long double
User-Defined Data Types
Syntax:
typedef existing_data_type new_name_for_existing_data_type;
Operators
a = 15 = 0000 1111
Bitwise Shift
Operators b = 7 = 0000 0111
AND = 0000 0111
One’s Complement
Operator a = 15 = 0000 1111
b = 7 = 0000 0111
OR = 0000 1111
Bitwise Logical
a = 15 = 0000 1111
Operators b = 7 = 0000 0111
Bitwise AND (&) XOR = 0000 1000
Bitwise OR (|)
Bitwise NOT (^)
Bitwise Operators 41
Bitwise Shift Operators
Left Shift (<<)
Right Shift (>>)
a 0000 1111
Left Shift 0111 1000
by 3
a = 12 = 0000 1100
~a = 1111 0011
Bitwise Operators
Two’s Complement
a = -13 = 1111 0011
~a = 0000 1100
+1
13 = 0000 1101
Modify Operators in C
Pre-[increment | decrement]
Substitution
Evaluation
Assignment
Post-[increment | decrement]
PSE.AP 45
Task
Write output of following program:
Priority Operators
1st *, /, %
Hierarchy of Operations 2nd
3 rd
+, -
=
Precedence - which of the operator is to be executed first, BODMAS,
bracket is operated first.
Associativity is the preference of the operators (of same precedence level),
when there is tie between them. e.g. suppose we have
z=a+b*c
Then, operation occurs as z=a+(b*c) as *higher precedence than +
But when the case is like
Z=a*b+c/d, operation occurs as
Z=(a*b)+(c/d) i.e. multiplication is done first. Then (c/d) is evaluated. Then only
the two values are added. It happens so because *and / have higher
precedence than +. However, as *and / have same precedence level, *is
evaluated first, as the associativity of the arithmetic operators is from left to right,
hence whichever operator occurs first, that is evaluated.
Arithmetic Expression:
Z=a+2*b/c*d+23+a*a; a =2, b =3, c =4, d =5 then z =?
Operator Precedence & Associativity
Operator Category Operators Associativity
Arithmetic Multiply, Divide, & *, /, % L->R
Remainder
if (condition) {
; // Do nothing
}
if (error)
; // TODO: Add error handling here
Comma Operator
The comma operator in C allows multiple expressions to be evaluated in a single
statement, with the value of the entire expression being the value of the last
expression. It is represented by the comma symbol (,).