0% found this document useful (0 votes)
33 views17 pages

Basics 2

Uploaded by

tejnakhosla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views17 pages

Basics 2

Uploaded by

tejnakhosla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

C

PROGRAMMI
NG
TYPE CASTING
When values of different types are mixed in an
expression, they are converted to the same type
before performing the particular operation. This
process of converting values for one type to another
type is known as type conversion.

C language facilitates type conversion in following two


forms:
 Implicit type conversion

 Explicit type conversion


IMPLICIT TYPE CONVERSION

An implicit type conversion is automatically performed


by the compiler without programmer’s intervention.
For implicit type conversion, the C language follows
certain rules that are introduced below:
At the time of evaluation of expression, when an
operation is performed on operands of different
ranks, then operand of lower rank is converted to
operand of higher rank. The operand of larger size is
given a higher rank than that of smaller size.
E.g.
int i, j;
float x, y;
x / i + y * j;
while evaluating “x/i”, integer operand “i” will be
converted to float type as “x” is float type and float
has higher rank than int. Similar is the case with “y*j”.

In the assignment statements, depending on the


difference of rank of operands, the C system tries to
either promote or demote the right operand so as to
make its rank match the left operand (variable).
CONVERSION RANK

Real

Integ
er

11. long
double
Charact 10. double
8. unsigned 9. float
er long int
7. long int
6.signed int
5. int
4. unsigned
short
3. short
PROMOTION
Promotion is the process that takes place when value of
an operand of lower rank is assigned to a variable of
type that has a higher rank.
E.g.
int i;
float f;
f=425; //value of f will be 425.0
DEMOTION
Demotion is the process that takes place when a value
of an operand of higher rank is assigned to a variable
of type that has lower rank.
E.g.
int i;
float f;
f=425.76;
i=f; //value of i will be 425
EXPLICIT TYPE CONVERSION

An explicit type conversion is user defined that forces


an operand or expression to be of specific type. The
process of explicit type conversion is also known as
type casting.

Syntax is:
type(operand) or type(expression)

E.g. (float) x
Will convert variable x of type int to float type.
A2Q4. TO SORT THE ARRAY

#include<stdio.h>
#include<conio.h>
void main()
{
int limit,i,a[10],j,temp;
clrscr();
printf("Enter total no of values: ");
scanf("%d",&limit);
printf("Enter the values: ");
for(i=1;i<=limit;i++)
scanf("%d",&a[i]);
for(i=1;i<=limit;i++)
{
for(j=1;j<=limit-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n\nThe sorted list is: \n");
for(i=1;i<=limit;i++)
printf("\n%d",a[i]);
getch();
}
TYPEDEF
#include<stdio.h>
#include<conio.h>
Void main()
{
typedef int myvar;
myvar val1,val2;

Printf(“Enter 2 values”);
Scanf(“%d%d”,&val1,&val2);
getch();
}
ENUMERATION CASE 1
#include<stdio.h>
#include<conio.h>
Void main()
{
enum day{mon,tue,wed,thu,fri,sat,sun};

printf(“%d”,tue);
}

Output : 1
ENUMERATION CASE 2
#include<stdio.h>
#include<conio.h>
Void main()
{
Enum day{mon=1,tue,wed,thu,fri,sat,sun};

printf(“%d”,tue);
}

Output : 2
ENUMERATION CASE 3
#include<stdio.h>
#include<conio.h>
Void main()
{
enum day{mon,tue,wed=7,thu,fri,sat,sun};

printf(“%d”,thu);
}

Output : 8
INCREMENT AND DECREMENT
OPERATOR
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=20,c;
c=++a;
printf(“%d”,c); //Ans: 11

c=b++;
printf(“%d”,c); //Ans: 20
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=20,c;
c=++a + ++b; //ans. 11+21=32
c=++a + b++; //ans. 11+20=31
c=a++ + ++b; //ans: 10+21=31
c=a++ + b++; //ans. 10+20=30
printf(“%d”,c);
getch();
}

You might also like