Loading...
Wednesday 26 July 2017

Operators and Expressions in C

What are Operators: 
Any symbol or word which perform some operation are called operators, the values or variables on which operators perform operation are called Operands. 
Following are the main types of Operators supported in C:
  1. Arithmetic / Mathematical Operators: Perform mathematical operations 
      • +     Addition
      • -      Subtraction 
      • *     Multiplication 
      • /      Division 

  1. Comparison / Relational Operators : compare values and return answer true or false. 
      • <     Less than
      • >     Greater Than
      • <=   Less than or equal to 
      • >=   Greater than or equal to 
      • !=    Not Equal to   
  2. Logical Operators: Checks condition and returns answer true or false. 
      • &&   Logical And 
      • ||       Logical Or 
      • !       Logical Not 
  1. Unary Operators
  • Suppose we have a variable "a" which is assigned a value of 5 for increment or decrements 1 we can use unary operators. 
  • Pre Incremnt: ++a
  • Pre Decrement --a
  • Post Increment: a++
  • Post Decrement: a--
  1. Ternary Operators: short cut of if else ,check condition, if condition is true the value after question mark will be evaluated else value after : will be evaluated.  
          conditon?exp1:exp2
  1. Bitwise Operators: Operators which works on bits 
  • Binary And                           & 
  • Binary Or                               |
  • Binary Exclusive OR             ^
  • Binary Ones Complement     ~
  • Binary Left Shift Operator     <<
  • Binary Right Shfit Operator   >>
  1. Sizeof Operator : It is also know as sizeof() function it takes a variables return its size
Kinds of Operators:
Operators can be divided into three categories according to the number of operands: 
1. Binary Operators: Operators which takes two operands  such as Arithmetic Operators, Comparison Operators, logical operators takes two values. 
2.  Unary Operators: Operators which takes  one operand only , such as increment ++ and decrement -- operators. 
3. Ternary Operators: Operators which takes three operands there are only two operators which are called Ternary Operators they are ? and : 

Some example programs for Operators and Expressions: 


INPUT THROUGH SCANF()


main()
{
int a,b;
clrscr();
printf("Enter value for a ");
scanf("%d",&a);
printf("Enter value for b");
scanf("%d",&b);
printf("%d + %d= %d\n",a,b,a+b);
getch();
}


INPUT THOUGH GETCH()

main()
{
char ch;
clrscr();
printf("Press any key\n");
ch = getch();
printf("You Pressed %c",ch);
getch();
}

Comparisional  Operators / Relational Operators

#include<stdio.h>
int main()
{
int a, b, c, sum;
printf(“\n Enter any three numbers: ”);
scanf(“%d %d %d”, &a, &b, &c);
sum = a + b + c;
printf(“\n Sum = %d”, sum);
return 0;
}

Logical Operators
main()
{
int iit=50,win=6,word=6;

//excel=90,ppt=80,access=67;

clrscr();
printf("%d\n",iit>=33&&win>=33&&word>=33);
printf("%d\n",iit>=33||win>=33||word>=33);
printf("%d\n",!(iit>33));

getch();
}
Operator Precedence
main()
{

clrscr();
printf("%d",7+3*2/2);

getch();
}


Average
main()
{
int day1=600,day2=600,day3=800,day4=600,day5=1000,ave;
clrscr();
ave = (day1+day2+day3+day4+day5)/5;
printf("Sum of Values is:\t%d\n",day1+day2+day3+day4+day5);
printf("Average is \t%d\n",ave);
getch();
}
Sales
main()
{
char item[30];
int qty,price, total;
clrscr();
printf("<<<<<<<<<<Sales Report>>>>>>>>>>>\n\n");
printf("Enter Item");
scanf("%s",&item);
printf("\nPrice of Item");
scanf("%d",&price);
printf("\nQty of Items");
scanf("%d",&qty);
total = qty * price;
clrscr();
printf("<<<<<<<<<<Sales Report>>>>>>>>>>\n\n");
printf("??????? Output Result ??????????\n");
printf("\n\nItem: \t%s",item);
printf("\nRate:\t%d",price);
printf("\nQuantity:\t%d",qty);
printf("\nToal is:\t%d",total);
getch();

}

C Programming Operators and Expressions
Operators and Expressions in C 

0 Comments:

Post a Comment

 
TOP