c language - operators

Upload: nagajaneeha

Post on 04-Apr-2018

244 views

Category:

Documents


2 download

TRANSCRIPT

  • 7/29/2019 C language - operators

    1/53

    Operators

    Kinds of Operators

    Precedence of Operators

    Type Casting

  • 7/29/2019 C language - operators

    2/53

    Classifications

    Arithmetic operators

    Relational operators

    Logical operators

    Assignment operators

    Increment and decrement operators

    Conditional operators

    Bitwise operators

    Special operators

  • 7/29/2019 C language - operators

    3/53

    Arithmetic Operators

    Operator for arithmetic operation Unary operator : +, -

    Binary term operator : +, -, *, /, %

    % is purely integer operator.

    7/4 = 1 ; 4%7 = 4;9.0 / 2= 4.5; 7%4 = 3;

    9/4 = 2 ; 9/2.0 = 4.5;

    4/7 = 0; 9.0/2.0 = 4.5;

    9.0 % 2;

    9%2.0;

    9.0%2.0;

    a-b a+b a%b

    a*b -a*b a/b

  • 7/29/2019 C language - operators

    4/53

    Integer arithmetic

    Note: In modulo division, the sign of the resultis always the sign of the first operand

    (dividend).

    6/3 = 2

    -6/3 = -2

    6/-3 = -2

    -6/-3 = 2

    7%3 = 1

    -7%3 = -1

    7%-3 = 1

    -7%-3 = -1

  • 7/29/2019 C language - operators

    5/53

    Precedence of arithmetic operators

    Left to right evaluationEx:

    1. a = 9, b = 12, c = 3

    x = a b/3 + c*2 - 1

    High priority * / %

    Low priority + -

  • 7/29/2019 C language - operators

    6/53

    Precedence of arithmetic operators

    Order of evaluation can be changed byintroducing parentheses

    Ex:

    1. a = 9, b = 12, c = 3

    x = a b/(3 + c)*(2 1)

    For nested parentheses, inner most

    parentheses will be first evaluated.

    Ex:

    x = 9 ((12/3) + 3*2) 1

  • 7/29/2019 C language - operators

    7/53

    Relational Operators

    Compare two value.

    Result : true (1)or false (0).

    Zero is false, non zero integer is true.

  • 7/29/2019 C language - operators

    8/53

    Relational Operators

  • 7/29/2019 C language - operators

    9/53

    Relational Operators

    Examples:

    7>=4 = 1; 7>=7 = 1;

    94 = 1

    7>9 = 0;

    7

  • 7/29/2019 C language - operators

    10/53

    Logical Operators

    Operators

    && Logical AND ! Logical NOT

    || Logical OR

    An expression which combines two or more relational

    expressions is called as logical expression or compoundrelational expression.

    a < b && b < c

  • 7/29/2019 C language - operators

    11/53

    Logical Operators

    Truth tableOp1 Op2 Op1 && Op2 Op1 || Op2

    Non zero Non zero 1 1

    Non zero 0 0 1

    0 Non zero 0 1

    0 0 0 0

  • 7/29/2019 C language - operators

    12/53

    Assignment Operators

    Ex:

    Expr1 = Expr1 op Expr2 Expr1 op= Expr 2

    x = x * y + 1; x *= y + 1;

    x = x * (y+1)

    sum = sum + i ; sum += i ;

  • 7/29/2019 C language - operators

    13/53

    Increment & Decrement Operators

    Operator++, --

    Prefix operator (evaluation and then assignment)

    Postfix operator (assignment and then evaluation)

    Cannot use at expression, only at variable

    Cannot apply at real type

    n = 1;

    x = ++n; // x=2, n=2

    n = 1;

    x = n++; // x=1, n=2

    (a + b)++ // error

  • 7/29/2019 C language - operators

    14/53

    Increment & Decrement Operators

    Examples:

    1. a[i++] = 10;

    2. m = n++ -j+10;

    => a[i] = 10;

    i = i+1

  • 7/29/2019 C language - operators

    15/53

    What is the output of the following C

    segments: ( Be careful )

    x = 0 ;

    if ( x++ )

    y = 1;

    else

    y = -1;

    printf (x=%d,y=%d\n, x,y);

  • 7/29/2019 C language - operators

    16/53

    What is the output of the following C

    segments: ( Be careful )

    x = 0 ;

    if (++x)

    y = 1;

    else

    y = -1;

    printf (x=%d,y=%d\n, x,y);

  • 7/29/2019 C language - operators

    17/53

    The Conditional Operator

    Expr1 ? Expr2 : Expr3 (3 Terms Operator)

    m = a > b ? (c > a ? c : a) : (c > b ? c : b) ;

    max = x > y ? x : y ;

    if (x > y)

    max = x;

    else

    max = y;

  • 7/29/2019 C language - operators

    18/53

    Bitwise Operators

    Operand should be integer type

  • 7/29/2019 C language - operators

    19/53

    Bitwise Operators

    Precedence

    Operator Precedence

    ~

    > >>>

    &

    ^

    |

    (H)

    (L)

  • 7/29/2019 C language - operators

    20/53

    Bitwise Operators

    Bitwise AND

    10012 & 00112 = 00012

    To extract the special area in variable by masking thatarea

    Bit OR

    10012 | 00112 = 10112

    Exclusive OR

    10012 ^ 00112 = 10102

    1s Complement

    ~ 000010102 = 111101012

  • 7/29/2019 C language - operators

    21/53

    Bitwise Operators

    Bitwise Shift Operator

    Shift left()

    x > y = x / 2y

  • 7/29/2019 C language - operators

    22/53

    Special operators

    1. The Comma operator:

    used to link the related expressions together.

    Evaluated left to right has lowest precedence, parentheses are

    necessary.

    Ex:value = (x=10,y=5,x+y);

  • 7/29/2019 C language - operators

    23/53

    Special operators

    2. The sizeof operator:

    It returns the number of bytes the operand

    occupies.

    the operand can be a variable, constant or a

    data type qualifier.

    Ex:

    m = sizeof(sum);

    n = sizeof(long int);

    k = sizeof(235L);

    Category Operator Associativity

  • 7/29/2019 C language - operators

    24/53

    Category Operator Associativity

    Postfix () [] -> . ++ - - Left to right

    Unary+ - ! ~ ++ - - (type) * &

    sizeofRight to left

    Multiplicative * / % Left to rightAdditive + - Left to right

    Shift > Left to right

    Relational < >= Left to right

    Equality == != Left to right

    Bitwise AND & Left to right

    Bitwise XOR ^ Left to right

    Bitwise OR | Left to right

    Logical AND && Left to right

    Logical OR || Left to right

    Conditional ?: Right to left

    Assignment= += -= *= /= %= >>=

  • 7/29/2019 C language - operators

    25/53

    Operator Precedence

    Example:

    3 + 4*5 6/2 + 7*8

    3 + 20 - 6/2 + 7*83 + 20 - 3 + 7*8

    3 + 20 - 3 + 56

    23 -3 +5620 + 56

    76

  • 7/29/2019 C language - operators

    26/53

    Operator Precedence

    Example:

    x = y += z -= 4

    x = y += (z -= 4)x = (y += (z -= 4))

    (x = y += (z -= 4))

  • 7/29/2019 C language - operators

    27/53

    Operator Precedence

    a = x + y - z ; // Left Association

    b = -x ; // Right Association

    c = -x++ ;

    d = -++x ;

    e = -x + z ;

    e = ++z + z--;

    f= p++ + p++;

  • 7/29/2019 C language - operators

    28/53

    Type conversionAutomatic type conversion

    Operands of lower type is automatically

    converted to higher type and result is of

    higher type.

    1. Float to int causes truncation of fractional

    part.

    2. Double to float causes rounding of digits.

    3. Long int to int causes dropping of excess

    higher order bits.

    T i

  • 7/29/2019 C language - operators

    29/53

    Type conversion

    Example:

    int i,x;

    float f;

    double d;long int l;

    Expression:

    x = l / i + i * f - d

    long

    long

    float

    float

    float

    float

    int double

    double

  • 7/29/2019 C language - operators

    30/53

    Type ConversionCasting a value

    The process of local conversion is known as casting a

    value.

  • 7/29/2019 C language - operators

    31/53

    Type ConversionCasting a value

    Examples:

    x = (int)7.5

    A = (int)21.3/ (int)4.5

    B = (double)sum/n

    Y = (int)(a + b)

    P = cos ((double)x)

  • 7/29/2019 C language - operators

    32/53

    Managing I/O: Input-Output

    Statements, formatted I/O.

  • 7/29/2019 C language - operators

    33/53

    getchar()

    Used to read a single character from the

    standard input unit (keyboard)

    #include

    Ex: char name;

    name=getchar();

    Variable name = getchar();

  • 7/29/2019 C language - operators

    34/53

    Character test functions

    char character;

    character=getchar();

    isalpha(character): it assumes a non-zero

    value(TRUE) if the argument character

    contains an alphabet, otherwise it assumes

    zero.

    isdigit(character)

  • 7/29/2019 C language - operators

    35/53

    Character test functions

    Function Test

    isalnum(c) Is c an alphanumeric character?

    isalpha(c) Is c an alphabetic character?

    isdigit(c) Is c a digit?islower(c) Is c a lower case letter?

    isprint(c) Is c a printable character?

    ispunct(c) Is c a punctuation mark ?

    isspace(c) Is c a white space character?

    isupper(c) Is c an upper case letter?

  • 7/29/2019 C language - operators

    36/53

    Character test functions

    char c;c=getchar();

    if(isalpha(c)>0)

    printf(letter);

    else

    if(isdigit(c)>0)

    printf(digit);

    else

    printf(not alphanumeric);

  • 7/29/2019 C language - operators

    37/53

    putchar()

    Used to write characters one at a time to the

    terminal

    Ex: answer = Y;

    putchar(answer);

    putchar(variable name );

  • 7/29/2019 C language - operators

    38/53

    putchar()

    Ex:

    char alphabet;

    alphabet = getchar();

    if(islower(alphabet))

    putchar(alphabet);

  • 7/29/2019 C language - operators

    39/53

    Formatted Input: Scanf () function

    Its an input function, used to read values in

    run time.

    Its a call by address function.

    It is predefined function in stdio.h

    If & is missing, variable contains garbage value.

    scanf (control string, &var1, &var2,-------);

  • 7/29/2019 C language - operators

    40/53

    Field specifications

    The field specification for reading an integer

    number is

    % wc

    %conversion specification

    w field width of the number to be read

    d data type character in integer modec character mode

    s string mode

    % wd % ws

    I /O Formats

  • 7/29/2019 C language - operators

    41/53

    I /O Formats

    % is called format specifier.

    %d : Decimal integer

    %0 : Octal integer

    %0x : Hexadecimal integer

    Data type format

    Int %d

    Float %f

    Char %c

    Long %ld

    Double %lf

    Unsigned %u

    String %s

  • 7/29/2019 C language - operators

    42/53

    Scanf () function

    Example2: int a, b, c;

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

    Example3: int d, m, y;

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

    Example4: int x; float y; char ch;

    scanf(%d %f %c, & x, &y, &ch);Order of the variables and order of formats must

    match.

  • 7/29/2019 C language - operators

    43/53

    Try this..

    1. scanf(%2d %5d, &a,&b);

    Input1: 50 31426

    Input2: 31426 50

    2. scanf(%d %*d %d, &a, &b);Input: 123 456 789

    skipped because of*

  • 7/29/2019 C language - operators

    44/53

    gets()

    recieves a string from keyboard until the user

    presses enter key.

    Spaces and tabs are acceptable as part of

    input string.

    f(% ) ( )

  • 7/29/2019 C language - operators

    45/53

    scanf(%s, a); gets(a);

    If user enters sankar dayal

    sharma, only sankar is stored in

    array a.

    a

    entire sankar dayal sharma is

    stored in array a.

    a

    scanf() reads characters until

    user press space bar.

    gets() reads characters until

    user presses enter key.

    scanf() reads only one word. gets() function can read

    multiword strings.

    gets(arrayname)

    Formatted function Unformatted function

    Sankar \0

    Sankar dayal sharma \0

  • 7/29/2019 C language - operators

    46/53

    Formatted output: Printf()

    It is an output function.

    Its a call by value function.

    It is predefined function in stdio.h.

    Example: int a = 25;

    Printf(%d,a);

    printf(control string, arg1, arg2,-------);

  • 7/29/2019 C language - operators

    47/53

    printf()

    Type the following and observe the results:

    int x = 25; float y = 10.68f; char ch = g;

    1. printf(%d %f %c,x,y,ch);

    2. printf(%d\n %f\n %c,x,y,ch);

    3. printf(%d\t %f \t%c,x,y,ch);

    4. printf(%d, %f, %c,x,y,ch);\n and \t cannot be used in scanf() but can be

    used in printf.

    printf(result is %d\n,x*2+y);

  • 7/29/2019 C language - operators

    48/53

    scanf() printf()

    Input function Output function

    To read values at run time To display values

    Call by address function. Call by value function.

    deals with keyboard deals with monitor

    We cant write \n and \t Not allowed

    It can contain onlyvariables.

    It can contain variables,messages and expressions.

  • 7/29/2019 C language - operators

    49/53

    puts()

    It outputs a string to the screen.

    puts(GRIET);

  • 7/29/2019 C language - operators

    50/53

    puts()

    Type the following and observe the results:

    1. Printf(hyd\n);

    Printf(sec\n);

    Printf(cyb\n);

    2. Puts(hyd);

    Puts(sec);Puts(cyb);

    3 printf(%d );

  • 7/29/2019 C language - operators

    51/53

    3. printf( %d , . );

    printf(%d,A);

    printf(%d,7);printf(%d,\0);

    4. printf(%c,\0);

    5. float a = 123.6789;

    printf(%.2f,a);

    printf(%.3f,a);

    printf(%f,a);

  • 7/29/2019 C language - operators

    52/53

    6. int a=4567;

    printf(%7d,a);

    printf(%.07d,a);

    printf(%2d,a);

    printf(%d,a);

    Try this..Display 07:08:09

  • 7/29/2019 C language - operators

    53/53

    7. printf(\\n);

    printf(\\);

    printf(\);

    printf(%%);

    printf();

    printf(%);printf(\%);