You are on page 1of 3

Activity

DIFFERENT TYPES OF C OPERATORS

ARITHMETIC OPERATORS
PREFIX/POSTFIX OPERATORS
ASSIGNMENT OPERATORS
RELATIONAL OPERATORS
LOGICAL AND BOOLEAN OPERATORS
TERNARY OPERATORS

ARITHMETIC OPERATORS

() BRACKETS
* MULTIPLICATION
/ DIVISION
+ ADDITION
- SUBTRACTION
% MODULUS
= ASSIGNMENT

The program below demonstrates the arithmetic operators:

#include <stdio.h>
int main(){
int x=10, y=20;
int result;
int remainder;
printf(“x=%d\n”,x);
printf(“y=%d\n”,y);
result=x+y;
printf(“The sum of x+y is %d\n”,result);
result = x*2;
printf(“The product of x*2 is %d\n”,result);
result = y-10;
printf(“the difference of y-10 is %d\n”, result);
result=y/x;
printf(“the quotient of y/x is %d\n”,result);
remainder = x%4;
printf(“the remainder of x%4 is %d\n”, remainder);
return 0;}

PREFIX AND POSTFIX OPERATORS

INCREMENT ++ POSTFIX A++, A--


DECREMENT -- PREFIX ++A, --A

int main() {
int a=5,b;
b= ++a + 5;
printf("a=%d b=%d",a,b);
return 0;
}

int main() {
int a=5,b;
b= a++ + 5;
printf("a=%d b=%d",a,b);
return 0;
}

ASSIGNMENT OPERATORS

*= MULTIPLICATION ASSIGNMENT
/= DIVISION ASSIGNMENT
%= MODULUS ASSIGNMENT
+= ADDITION ASSIGNMENT
-= SUBTRACTION ASSIGNMENT
EXAMPLE:
Y=10;
Y+=10, Y=20
Y-=5, Y=15
Y*=4, Y=60
Y/=2, Y=30

int main() {
int x=10;
int y=x;
printf(“y=%d”,y);
y+=10;
printf(“y+=10; y%d”, y);
y-=5;
printf(“y-=5; y=%d”,y);
y*=4;
printf(“y*=4; y=%d”, y);
y/=2;
printf(“y/=2; y=%d,” y);
return 0; }

RELATIONAL OPERATORS

> GREATER THAN


>= GREATER THAN OR EQUAL
< LESS THAN
<= LESS THAN OR EQUAL
== EQUAL
!= NOT EQUAL

Int main(){
Int x=10, y=20;

printf(“x=%d, x);
printf(“y=%d, y);
if (x==y) printf(“true”);
else printf(“false”);

if(x!=y) printf(“true”);
else printf(“false”);

if(x>y) printf(“true”);
else(“false”);

if(x<y) printf(“true”);
else printf(“false”);

return0; }

LOGICAL/BOOLEAN OPERATOR

&& AND A<B && C<D TRUE, WHEN A<B AND C<D
|| OR A>B || C>D TRUE, WHEN A>B OR C>D
! NOT !A NOT A

Int main(){
Int x=7, y=10;
x++;
if(x<y||y<20) printf(“true”);
else printf(“false”);

y++;
if(x>y&&y>0) printf(“true”);
else printf(“false”);
return 0;}
TERNARY OPERATOR

It is a shorthand version of if-then-else statement

If(expression1)
Return expression2;
Else return expression3;

RADIX CHANGING

CHANGING DECIMAL INTEGER INTO OCTAL OR HEX

%o OCTAL
%x HEX

THE FORMAT SPECIFIERS (PLACEHOLDERS)

VARIABLE TYPE DISPLAY


%c char Single character
%d (%i) int integer
%e Float or double Exponential format
%f Float or double Decimal format
%o int Octal value
%x int Hex value
%p pointer Address stored in pointer
%s Array of char Sequence of characters

Activity:

Create a program that prints the sum, difference, product and quotient of the two given numbers A=7, and x=9.

You might also like