Module 1 Continued
Module 1 Continued
BASICS OF C
What will you learn?
Storage Classes
Type Casting
Operators and Types
Operators Precedence and Associativity
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.
the data has been lost. In this case demotion takes place. When
fractional part has been lost. Similarly, if we convert the double type to float
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
#include<stdio.h>
clrscr();
getch();
}
EXPLICIT TYPE
CONVERSION
Type Casting is also known as Force Conversion. Type
casting is done when a higher data type is converted to lower
data type. But this conversion is under the control of the programmer,
not under the control of compiler.
Example:
float salary = 10000.00
int sal;
sal = (int)salary;
When the floating point numbers are converted to integers then
the decimal points are truncated. Therefore, the data is lost. So in order
to avoid such data loss always convert the lower data type to higher
data type but not the vice versa.
EXAMPLE FOR EXPLICIT TYPE
CONVERSION():
//C Program for explicit type conversion
#include<stdio.h>
char value;
clrscr();
getch();
}
OPERATORS OF C
An Operator is a symbol that performs some mathematical or logical
operation. In C there are different categories of Operators:
1. Arithmetic ( +, -, *, /, %)
7. Conditional (? : )
8. Special Operators
ARITHMETIC
OPERATORS
An arithmetic operator performs mathematical operations such as Addition,
subtraction, multiplication, division and modulo division are the arithmetic operations
that are supported by C and there are represented with the symbols +, -, *, /, %
Syntax:
respectively.
operand1 arithmetic_operator operand2
void main()
{
int a=9,b=4;
printf("Addition of a, b is : %d\n", a+b);
//13
printf("Subtraction of a, b is : %d\n", a-b);
//5
printf("Multiplication of a, b is : %d\n", a*b);
//36
printf("Division of a, b is : %d\n", a/b);
//2
printf("Modulus of a, b is : %d\n", a%b);
//1
}
PRIORITY OF ARITHMETIC
OPERATORS
High Priority: * / %
Low Priority: + -
2+3–5*2
2 + 3 – 10
5 – 10
-5
RELATIONAL
OPERATORS
Relational operators are used to compare the
relationship between two operands.
logical_operator operand
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
// C Program to demonstrate the working of logical operators
#include <stdio.h>
void main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) equals to %d \n", result); // 1
result = (a == b) && (c < b);
printf("(a == b) && (c < b) equals to %d \n", result); // 0
result = (a == b) || (c < b);
printf("(a == b) || (c < b) equals to %d \n", result); // 1
result = (a != b) || (c < b);
printf("(a != b) || (c < b) equals to %d \n", result); // 0
result = !(a != b);
printf("!(a == b) equals to %d \n", result); // 1
result = !(a == b);
printf("!(a == b) equals to %d \n", result); // 0
}
ASSIGNMENT OPERATOR
Assignment operator are used to assign the value
or an expression or a value of a variable to another variable.
Assignment Operator is denoted by =
Syntax: variable = expression;
Expression
a
TYPES OF ASSIGNMENT
Single Assignment Ex: a = 10;
#include <stdio.h>
void main()
{
int a = 5, c;
c = a;
printf("c = %d \n", c); // 5
c += a; // c = c+a
printf("c = %d \n", c); // 10
c -= a; // c = c-a
printf("c = %d \n", c); // 5
c *= a; // c = c*a
printf("c = %d \n", c); // 25
c /= a; // c = c/a
printf("c = %d \n", c); // 5
c %= a; // c = c%a
printf("c = %d \n", c); // 0
}
INCREMENT AND
DECREMENT
Increment operator is denoted with the symbol ++. This
operator is used to increase the value of variable by 1.
1. Pre – Increment
2. Post – Increment
#include <stdio.h>
void main()
{
int a = 10, b = 100;
}
BITWISE OPERATORS
Bitwise Operators are those that perform operations at the bit level.
These operators include bitwise AND, bitwise OR, bitwise XOR, and shift
operators.
Examples:
& Bitwise AND 0110 & 0011 0010
| Bitwise OR 0110 | 0011 0111
^ Bitwise XOR 0110 ^ 0011 0101
<< Left shift 01101110 << 2
10111000
>> Right shift 01101110 >> 3
00001101
~ One's complement ~0011 1100
#include <stdio.h>
void main()
{
int m = 10,n = 20 ;
printf("AND_opr value = %d\n",m&n ); //0
printf("OR_opr value = %d\n",m|n ); //30
printf("NOT_opr value = %d\n",~m); //-11
printf("XOR_opr value = %d\n",m^n ); //30
printf("left_shift value = %d\n", m << 1); //20
printf("right_shift value = %d\n", m >> 1); //5
}
CONDITIONAL
OPERATOR
The conditional operator first evaluates an expression for a true or false
value and then executes one of the two given statements depending
upon the result of the evaluation of condition.
Syntax:
#include<stdio.h>
#include<conio.h>
int m=5,n=4;
clrscr();
getch();
}
SPECIAL OPERATORS
There are some special operators available in C such as,
1.Comma operator
2.Sizeof operator
Syntax:
sizeof(operand);
Example:
#include<stdio.h>
#include<conio.h>
void main()
float b;
a=sizeof(char);
c=sizeof(b);
getch();
}
MEMBER OPERATORS
These are used to access members of structures and
unions.
Example:
Binary operator: It requires two operands like a+b, a-b, a*c etc.
evaluated in an expression.
a += b *= c -= 5
(a += (b = b * (c = c – 5)))
(a = a + (b = b * (c = c – 5)))
Example program to illustrate operator precedence
Example program to illustrate operator precedence (contd…)
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
<storage_class> <data_type>
<variable_Name>;
STORAGE CLASSES
SUMMARY
Storage Memory
Initial Value Scope Life
Class Unit
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 ( )
…………….. {
…………….. ……………
} …………….
}