c programming operators 5

7
C Programming Operators Operators are the symbol which operates on value or a variable. For example: + is a operator to perform addition. C programming language has wide range of operators to perform various operations. For better understanding of operators, these operators can be classified as: Operators in C programming Arithmetic Operators Increment and Decrement Operators Assignment Operators Relational Operators Logical Operators Conditional Operators Bitwise Operators Special Operators Arithmetic Operators Operator Meaning of Operator + addition or unary plus - subtraction or unary minus * multiplication / division % remainder after division( modulo division) Example of working of arithmetic operators /* Program to demonstrate the working of arithmetic operators in C. */ #include <stdio.h> int main(){ int a=9,b=4,c; c=a+b;

Upload: robin-alom

Post on 18-Dec-2015

214 views

Category:

Documents


0 download

DESCRIPTION

Computer

TRANSCRIPT

C Programming OperatorsOperators are the symbol which operates on value or a variable. For example:+is a operator to perform addition.C programming language has wide range of operators to perform various operations. For better understanding of operators, these operators can be classified as:Operators in C programming

Arithmetic Operators

Increment and Decrement Operators

Assignment Operators

Relational Operators

Logical Operators

Conditional Operators

Bitwise Operators

Special Operators

Arithmetic OperatorsOperatorMeaning of Operator

+addition or unary plus

-subtraction or unary minus

*multiplication

/division

%remainder after division( modulo division)

Example of working of arithmetic operators

/* Program to demonstrate the working of arithmetic operators in C. */#include int main(){ int a=9,b=4,c; c=a+b; printf("a+b=%d\n",c); c=a-b; printf("a-b=%d\n",c); c=a*b; printf("a*b=%d\n",c); c=a/b; printf("a/b=%d\n",c); c=a%b; printf("Remainder when a divided by b=%d\n",c); return 0;}a+b=13a-b=5a*b=36a/b=2Remainder when a divided by b=1ExplanationHere, the operators +, - and * performed normally as you expected. In normal calculation,9/4equals to 2.25. But, the output is 2 in this program. It is because, a and b are both integers. So, the output is also integer and the compiler neglects the term after decimal point and shows answer 2 instead of 2.25. And, finallya%bis 1,i.e. ,whena=9is divided byb=4, remainder is 1.Suppose a=5.0, b=2.0, c=5 and d=2In C programming,a/b=2.5 a/d=2.5c/b=2.5 c/d=2Note:% operator can only be used with integers.Increment and decrement operatorsIn C,++and--are called increment and decrement operators respectively. Both of these operators are unary operators, i.e, used on single operand.++adds 1 to operand and--subtracts 1 to operand respectively. For example:Let a=5 and b=10a++; //a becomes 6a--; //a becomes 5++a; //a becomes 6--a; //a becomes 5 Difference between ++ and -- operator as postfix and prefixWheni++is used as prefix(like:++var),++varwill increment the value ofvarand then return it but, if++is used as postfix(like: var++), operator will return the value of operand first and then only increment it. This can be demonstrated by an example:

#include int main(){ int c=2,d=2; printf("%d\n",c++); //this statement displays 2 then, only c incremented by 1 to 3. printf("%d",++c); //this statement increments 1 to c then, only c is displayed. return 0;}

Output24Assignment OperatorsThe most common assignment operator is=. This operator assigns the value in right side to the left side. For example:var=5 //5 is assigned to vara=c; //value of c is assigned to a5=c; // Error! 5 is a constant.OperatorExampleSame as

=a=ba=b

+=a+=ba=a+b

-=a-=ba=a-b

*=a*=ba=a*b

/=a/=ba=a/b

%=a%=ba=a%b

Relational OperatorRelational operators checks relationship between two operands. If the relation is true, it returns value 1 and if the relation is false, it returns value 0. For example:a>bHere,>is a relational operator. Ifais greater thanb,a>breturns 1 if not then, it returns 0.Relational operators are used in decision making and loops in C programming.OperatorMeaning of OperatorExample

==Equal to5==3 returns false(0)

>Greater than5>3 returns true (1)

=3 returns true (1)

5)) returns true.

!Logical NOTIf c=5 then, !(c==5) returns false.

ExplanationFor expression,((c==5) && (d>5))to be true, bothc==5andd>5should be true but, (d>5) is false in the given example. So, the expression is false. For expression((c==5)||(d>5))to be true, either the expression should be true. Since,(c==5)is true. So, the expression is true.Since, expression(c==5)is true,!(c==5)is false.Conditional OperatorConditional operator takes three operands and consists of two symbols ? and : . Conditional operators are used for decision making in C. For example:c=(c>0)?10:-10;Ifcis greater than 0, value ofcwill be 10 but, ifcis less than 0, value ofcwill be -10.Bitwise OperatorsA bitwise operator works on each bit of data. Bitwise operators are used in bit level programming.OperatorsMeaning of operators

&Bitwise AND

|Bitwise OR

^Bitwise exclusive OR

~Bitwise complement

Shift right

Bitwise operator is advance topic in programming . Learn more aboutbitwise operator in C programming.Other OperatorsComma OperatorComma operators are used to link related expressions together. For example:int a,c=5,d;The sizeof operatorIt is a unary operator which is used in finding the size of data type, constant, arrays, structure etc. For example:

#include int main(){ int a; float b; double c; char d; printf("Size of int=%d bytes\n",sizeof(a)); printf("Size of float=%d bytes\n",sizeof(b)); printf("Size of double=%d bytes\n",sizeof(c)); printf("Size of char=%d byte\n",sizeof(d)); return 0;}OutputSize of int=4 bytesSize of float=4 bytesSize of double=8 bytesSize of char=1 byteConditional operators (?:)Conditional operators are used in decision making in C programming, i.e, executes different statements according to test condition whether it is either true or false.Syntax of conditional operatorsconditional_expression?expression1:expression2If the test condition is true,expression1is returned and if falseexpression2is returned.Example of conditional operator#include int main(){ char feb; int days; printf("Enter l if the year is leap year otherwise enter 0: "); scanf("%c",&feb); days=(feb=='l')?29:28; /*If test condition (feb=='l') is true, days will be equal to 29. */ /*If test condition (feb=='l') is false, days will be equal to 28. */ printf("Number of days in February = %d",days); return 0;}OutputEnter l if the year is leap year otherwise enter n: lNumber of days in February = 29Other operators such as &(reference operator), *(dereference operator) and ->(member selection) operator will be discussed inpointerchapter.