Basics 2
Basics 2
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.
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
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();
}