operators lect2

Upload: vijayadhanya

Post on 06-Apr-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Operators Lect2

    1/19

    OPERATORS AND EXPRESSIONS

  • 8/3/2019 Operators Lect2

    2/19

    Definition

    An operator is a symbol (+,-,*,/) that directs the computer to performcertain mathematical or logical manipulations and is usually used tomanipulate data and variables

    Ex: a+b

    Where + is operator and a, b are the operands.

  • 8/3/2019 Operators Lect2

    3/19

    Operators in C1. Arithmetic operators

    2. Relational operators

    3. Logical operators4. Assignment operators

    5. Increment and decrement operators

    6. Conditional operators

    7. Bitwise operators8. Special operators

  • 8/3/2019 Operators Lect2

    4/19

    Arithmetic operators

    Operator example Meaning

    + a + b Addition unary

    - a b Subtraction- unary

    * a * b Multiplication

    / a / b Division

    % a % b Modulo division- remainder

  • 8/3/2019 Operators Lect2

    5/19

    Example main()

    {

    int a=12,b=3,c;

    c=a%b;

    printf(%d,c);

    }

    Arithmetic operators can be classified as

    i) unary arithmetic-Requires only one operand. Eg: +x.

    ii) Binary arithmetic-Requires two operands. Eg: a+b.

    iii) Integer arithmetic-Requires two operands and integervalues. Eg: a+b. where a=6,b=4.

    iv) Floating point arithmetic-Requires both operands and floattype for arithmetic operations.

  • 8/3/2019 Operators Lect2

    6/19

    Relational Operators

    Operator Meaning

    < Is less than Is greater than

    >= Is greater than or equal to== Equal to

    != Not equal to

  • 8/3/2019 Operators Lect2

    7/19

    #include

    #include

    {Clrscr();

    Printf(\n condition:return values\n);

    Printf(\n 5!=5: %5d,5!=5);

    Printf(\n 5==5 : %5d, 5==5);

    }

  • 8/3/2019 Operators Lect2

    8/19

    Logical Operators

    Operator Meaning example

    && Logical AND (exp1)&&(exp2)

    || Logical OR (exp1)||(exp2)

    ! Logical NOT !(exp1)

    Logical expression or a compound relationalexpression-

    An expression that combines two or morerelational expressions

    Ex: if (a==b && b==c)

  • 8/3/2019 Operators Lect2

    9/19

    Truth Table

    a bValue of the expression

    a && b a || b

    0 0 0 00 1 0 1

    1 0 0 1

    1 1 1 1

    a < b && b < c

    1 2

    3

  • 8/3/2019 Operators Lect2

    10/19

    #include

    #include

    {

    int a,b,c;Clrscr();

    Printf(\n enter values\n);

    Scanf(%d%d%d,&a,&b,&c);

    If((a

  • 8/3/2019 Operators Lect2

    11/19

    Assignment operatorsSyntax:

    v=exp or value;

    v op = exp;Where v = variable,

    op = shorthand assignment operator

    exp = expression

    Ex: x=x+3

    x+=3

  • 8/3/2019 Operators Lect2

    12/19

    Shorthand Assignment operatorsSimple assignment

    operatorShorthand operator

    a = a+1 a + =1

    a = a-1 a - =1

    a = a* (m+n) a * = m+n

    a = a / (m+n) a / = m+n

    a = a %b a %=b

  • 8/3/2019 Operators Lect2

    13/19

    Increment & Decrement OperatorsC supports 2 useful operators namely

    1. Increment ++

    2. Decrement operators

    The ++ operator adds a value 1 to the operand

    The operator subtracts 1 from the operand

    ++a or a++

    --a or a--

  • 8/3/2019 Operators Lect2

    14/19

    Rules for ++ & -- operators

    1. These require variables as their operands

    2. When postfix either ++ or is used with the

    variable in a given expression, the expression isevaluated first and then it is incremented ordecremented by one

    3. When prefix either ++ or is used with the

    variable in a given expression, it is incrementedor decremented by one first and then theexpression is evaluated with the new value

  • 8/3/2019 Operators Lect2

    15/19

    Examples for ++ & -- operatorsLet the value of a =5 and b=++a thena = b =6Let the value of a = 5 and b=a++ thena =5 but b=6i.e.:1. a prefix operator first adds 1 to the operand and

    then the result is assigned to the variable on theleft

    2. a postfix operator first assigns the value to thevariable on left and then increments the operand.

  • 8/3/2019 Operators Lect2

    16/19

    Conditional operatorsSyntax:

    exp1 ? exp2 : exp3

    Where exp1,exp2 and exp3 are expressions

    Working of the ? Operator:Exp1 is evaluated first, if it is nonzero(1/true) then the expression2 is

    evaluated and this becomes the value of the expression,

    If exp1 is false(0/zero) exp3 is evaluated and its value becomes the valueof the expression

    Ex: m=2;

    n=3

    r=(m>n) ? m : n;

  • 8/3/2019 Operators Lect2

    17/19

    Bitwise operators

    These operators allow manipulation of data at the bit level

    Operator Meaning

    & Bitwise AND

    | Bitwise OR

    ^ Bitwise exclusive OR

    > Shift right

  • 8/3/2019 Operators Lect2

    18/19

    Special operators1. Comma operator ( ,)

    2. sizeof operator sizeof( )

    3. Pointer operators ( & and *)4. Member selection operators ( . and ->)

  • 8/3/2019 Operators Lect2

    19/19

    ExampleEvaluate the expression when a=4

    b=a- ++a

    =a 5=5-5

    =0