Sunday 22 February 2015

Operators in C


Operators:

1.    Arithmetic Operator
2.    Unary Operator
3.    Relational and Logical Operator
4.    Assignment Operator
5.    Conditional Operator

1.    Arithmetic Operator:
+, -, *, /, %

2.    Unary Operator:
++, --, SizeOf
++ = Increment Operator
-- = Decrement Operator
Prefix increment = ++i
Postfix increment = i++
Prefix decrement = --i
Postfix decrement = i--

3.(a). Relational Operator:

>, <, >=, <=, = =, !=

3.(b). Logical Operator:Char3

&&, ||, !
&& = Logical AND
|| = Logical OR
! = Logical NOT

4.Assignment Operator:

+=, -=, *=, /=, %=

5. Conditional Operator OR Ternary Operator: Chap5

?, :

Arithmetic Operator:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“Enter a values:”);
scanf(“%d”,&a);
printf(“Enter b values:”);
scanf(“%d”,&b);
c=a+b;
printf(“Answer=%d”, c);
getch();
}

Unary Operator:

PreFix Increment:

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
i=2;
clrscr();
printf(“i=%d\n”,i);
printf(“i=%d\n”,++i);
printf(“i=%d\n”,i);
getch();
}

Output:
I=2
I=3
I=3

PostFix Increment:

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
i=2;
clrscr();
printf(“i=%d\n”,i);
printf(“i=%d\n”,i++);
printf(“i=%d\n”,i);
getch();
}

Output:
I=2
I=2
I=3

PreFix Decrement:

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
i=2;
clrscr();
printf(“i=%d\n”,i);
printf(“i=%d\n”,--i);
printf(“i=%d\n”,i);
getch();
}

Output:
I=2
I=1
I=1

PostFix Decrement:

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
i=2;
clrscr();
printf(“i=%d\n”,i);
printf(“i=%d\n”,i--);
printf(“i=%d\n”,i);
getch();
}

Output:
I=2
I=2
I=1

SizeOf:

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“Integer=%d\n”,sizeof(int));
printf(“Float=%d\n”,sizeof(Float));
printf(“Char=%d\n”,sizeof(Char));
printf(“Double=%d\n”,sizeof(Double));
printf(“Long Int=%d\n”,sizeof(long int));
getch();
}

Output:
Integer   =     2
Float      =     4
Char      =     1
Double  =     8
Long Int       =     4    

Relational Operator:

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
x=30;
y=5;
clrscr();
printf(“The value of x==y is %d\n”,x==y);
printf(“The value of x>=y is %d\n”,x>=y);
printf(“The value of x<=y is %d\n”,x<=y);
printf(“The value of x!=y is %d\n”,x!=y);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
x=30;
y=5;
clrscr();
printf(“The value of x+y is %d\n”,x+y);
printf(“The value of x-y is %d\n”,x-y);
printf(“The value of x*y is %d\n”,x*y);
printf(“The value of x/y is %d\n”,x/y);
getch();
}

Assignment operator:

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
x=6*5;
y=8+7;
z=6-3;
clrscr();
printf(“X=%d\n”,x);
printf(“y=%d\n”,y);
printf(“z=%d\n”,z);
getch();
}

No comments:

Post a Comment