Day 2
Day 2
BASICS OF C
What will you learn?
Data Types
Storage Classes
Type Casting
Operators and Types
Operators Precedence and Associativity
DATA TYPES
BASIC DATA TYPES IN C
Data types are used to specify two things to the compiler:
Control string may also contain text, captions, identifiers or any other
text that is to be readable.
EXAMPLES:
int x;
scanf(“%d”, &x);
printf(“%d”, x);
EXAMPLES:
float x;
scanf(“%f”, &x);
printf(“%f”, x);
EXAMPLES:
It is possible to read and write multiple variables at the same time
char x;
float y;
int z;
scanf(“%c”, &x);
printf(“%c”, x);
TYPE CONVERSION
Type conversion means converting from one data type to another.
Type Conversion is done when the expression has variables of
different data types.
been lost. In this case demotion takes place. When demotion occurs, the data
part has been lost. Similarly, if we convert the double type to float type then
float x = 10.56;
int y = x;
Here, while assigning the value of x into y, y can store only 10 and the fractional
part has been lost. But this conversion taken place without the knowledge of the
1. Arithmetic ( +, -, *, /, %)
7. Conditional (? : )
8. Special Operators
ARITHMETIC OPERATORS
Addition, subtraction, multiplication, division and modulo division are the
arithmetic operations that are supported by C and there are represented with
the symbols +, -, *, /, % respectively.
2+3–5*2
2 + 3 – 10
5 – 10
-5
RELATIONAL OPERATORS
Relational Operator is also called Comparison operator used to
compare two values. Relational expressions return true or false depending
upon the relation between the two values.
Logical AND (&&): This Operator gives TRUE Only when the all the
conditions in the expression evaluates to TRUE
Logical OR (||): This operator gives TRUE if at least any one of the
condition is evaluated to TRUE
Logical NOT (!): This Operator acts as Negation. It makes TRUE as FALSE
and FALSE as TRUE
LOGICAL OPERATORS (CONTD.)
Truth Table for AND:
Expression
a
ASSIGNMENT OPERATORS
OPERATOR Expression DESCRIPTION
1. Pre – Increment
2. Post – Increment
Under this, ++ operator is post fixed with the variable name i.e.,
m++. This means that the assignment of incremented value to m
is postponed until the next statement.
INCREMENT (CONTD.)
Suppose m = 5, y = ++m //prefix notation
These operators include bitwise AND, bitwise OR, bitwise XOR, and shift
operators.
Syntax:
Associativity determines the order in which operators with same precedence are
executed.
1. Left to Right Associativity evaluates starting on the left and moving to the
right
2. Right to Left Associativity evaluates starting on the right and moving to the
left
While evaluating expressions, precedence is applied before Associativity.
LEFT TO RIGHT ASSOCIATIVITY:
The following shows an example of left to right Associativity:
3*8/4%4*5
As all operators of the expressions are of same level precedence
then Associativity determines how the sub expressions are grouped
together as follows:
((((3 * 8) / 4) % 4) * 5)
(((24 / 4) % 4) * 5)
((6 % 4) * 5)
(2 * 5)
10
The value of the expression is 10.
RIGHT TO LEFT ASSOCIATIVITY:
The following shows an example of right to left Associativity:
a += b *= c -= 5
(a += (b = b * (c = c – 5)))
(a = a + (b = b * (c = c – 5)))
STORAGE CLASSES
STORAGE CLASSES
Storage Classes are used to describe about the features of a
variable/function.
Those features include:
1. Storage class of a variable determines the storage area
2. Storage class of a variable how long the variable exists – life time
of the variable
3. Storage class of a variable specifies the scope of the variable
4. Storage class specifies the default value of a variable
C STORAGE CLASSES
C language supports the four storage classes:
1. Automatic
2. Register
3. Static
4. Extern
Output:
a=10 a=10 a=10 Storage Memory Initial
Scope Life
Class Unit Value
CPU
Register Garbage Local End of Block
Register
STATIC STORAGE CLASS
#include<stdio.h>
void main()
static int a = 10;
{
void subfun()
int i;
{
subfun();
printf(“a=%d”, a);
subfun();
a++;
subfun();
}
}
4. A variable declared with extern storage class has file scope that have
the properties of the static storage class.
Program1.c Program2.c
Definition File Reference Source File
#include<stdio.h> #include<stdio.h>
int a; #include “program1.c”
int main( ) extern int a;
{ int main ( )
…………….. {
…………….. ……………
} …………….
}