Lesson5- Language Fundamentals4

download Lesson5- Language Fundamentals4

of 27

Transcript of Lesson5- Language Fundamentals4

  • 7/28/2019 Lesson5- Language Fundamentals4

    1/27

    Language Fundamentals-IVLanguage Fundamentals-IV 5

    Chapter Outline

    Introduction.

    Types of operators

    Assignment operator

    Binary arithmetic operators.

    Unary arithmetic operators.

    Relational operators.

    Logical operators.

    Conditional operator

    Bitwise operators.

    Special operators.

    Operators precedence and

    associativity.Evaluating an expression.

    Type conversion in an

    expression.

    Conclusion.

    First, master the fundamentals. Larry Bird

    I long to accomplish great and noble task, but it ismy chief duty to accomplish small tasks as if theywere great and noble.

    Helen Keller

    Success is neither magical nor mysterious. Success isthe natural consequence of consistently applying thebasic fundamentals.

    Jim Rohn

    (Operators and Expressions)

  • 7/28/2019 Lesson5- Language Fundamentals4

    2/27

    5.1. IntroductionOperator: Operator is a special character (or symbol) that performs an operation using operands.

    Operand: Operand is a variable or constant that acts as input for an operation to be performed.

    Expression: Expression is a collection of operators and operands that, when evaluated, returns a value.

    Ex: a+b is an expression in which the operator is + and the operands are a and b.

    Unary operator: Unary operator is an operator that performs operation using only one operand at a

    time.

    e.g., ++ (increment) is a unary operator.

    Binary operator: Binary operator is an operator that performs operation using two operands at a time.

    e.g., > (greater than) is a binary operator.

    Ternary operator: Ternary operator is an operator that performs operation using three operands at a

    time.

    e.g., ?:(Conditional operator) is ternary operator.

    5.2. Types of operatorsC has a rich set of operators. Based on the type of operation performed, operators are broadly classified

    as follows:

    1. Assignment operator.

    2. Binary arithmetic operators.

    3. Unary arithmetic operators.

    4. Relational operators.

    5. Logical operators.

    6. Bitwise operators.

    7. Special operators

    5.2.1. Assignment operator

    Assignment operator is used to assign (or give) a value to a variable or constant. The assignment

    operator is = (can be read as assigns to). The assignment operator can be used to form an expression

    as follows:

    Here, Operand_1 (also called as LValue) can be a variable or constant. Operand_2 (also called as RValue)

    can also be a variable or constant or an expression.

    When assignment operator is used, the RValue gets assigned to LValue.

    Ex:

    1) a=10; /* value 10 gets assigned to variable a*/

    2) const float PI=3.1412f; /*value 3.1412 gets assigned to constant PI*/

    3) c=a+b; /* a+b is calculated and result gets assigned to c*/

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    2

    Operand_1=Operand_2;

  • 7/28/2019 Lesson5- Language Fundamentals4

    3/27

    4) a=b=c; /* multiple assignment: value of c gets assigned to b, which in turn gets assigned to a*/

    5) a+b=10; /* invalid assignment statement*/

    Program #1

    5.2.2. Binary arithmetic operators

    C language provides operators for basic arithmetic operations such as addition, subtraction, multiplication

    and division. There are five binary arithmetic operators and these operators act on two operands at a

    time.

    These are:

    Operator Operation+ Returns the sum of values of two operands.- Returns the difference of values of two operands.* Returns the product of values of two operands.

    / Returns the quotient after division of two operands.% Returns the remainder after division of two operands.

    These operators can be used to form an arithmetic expression as follows:

    Ex: a+b, a-b, a*b, a/b, a%b

    The above expressions are called as binary arithmetic expressions. The operands may be of any

    one of data types. If both operands are integer values , then that expression is called as integer

    arithmetic expression . If both are floating-point values, then that expression is called as real-

    arithmetic expression . If both are of different values, then that expression is called as mixed-mode

    arithmetic expression .http://pradeeppearlz.blogspot.com/ Operators andExpressions

    3

    Write a program to interchange (or swap) two values by using temporary variable. /* A program to interchange two numbers using temporary variable*/#includemain()

    { long int a,b,temp;printf(\n Enter any two numbers:);scanf(%ld%ld,&a,&b);printf(\n Before swap a=%ld\tb=%ld,a,b);temp=a; /* First, assign value of a to temp*/a=b; /* Later, assign value of b to a*/b=temp; /* Finally, assign value of temp to b*/printf(\n After swap a=%ld\tb=%ld,a,b);

    }Output: Enter any two numbers: 294334646Before swap a=2943 b=34646After swap a=34646 b=2943

    Operand_1 Binary_arithmetic_operator Operand_2

  • 7/28/2019 Lesson5- Language Fundamentals4

    4/27

    When binary operations are applied to numeric arguments of different types, numeric promotion

    is performed before the operation takes place. The numeric promotion consists of converting the values of

    the operands to a common type.

    The rules for this type conversion are as follows:

    If one of the operands is a long double , then the other is converted a long double.

    Otherwise, if one of the operands is a double , then the other is converted to a double.

    Otherwise, if one of the operands is a float , then the other is converted to a float.

    Otherwise, if one of the operands is a long , then the other is converted to a long.

    Otherwise, if one of the operands is unsigned int , the other is converted to unsigned to int.

    Otherwise, both operands are converted to int values.

    Ex: 2.3 (a double value) +3 (an int value) =5.3 (a double result)

    The integer value in this expression gets promoted to double; addition is performed and the result is of

    type double is returned.

    Program #2

    Note:

    1. The operator % does not work on the operands of data types: float or double or long double.

    2. An unusual aspect of programming in C is the arithmetic which can be done with characters.

    Consider the following:

    char ch1,ch2;

    ch1=a; /*assign a to ch1*/

    ch2=ch1+1; /*assign b to ch2*/

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    4

    Write a program to perform all arithmetic operations /*program to perform all arithmetic operations*/#include

    main(){

    int a,b;printf(\nEnter any two numbers: );scanf(%d%d,&a,&b);printf(\nAddition=%d,a+b);printf(\nSubtraction=%d,a-b);printf(\nMultiplication=%d,a*b);printf(\nQuotient=%d,a/b);printf(\nRemainder=%d,a%b);

    }Output: Enter any two numbers: 56Addition=11Subtraction=-1Multiplication=30Quotient=0Remainder=5

  • 7/28/2019 Lesson5- Language Fundamentals4

    5/27

    The second assignment increases the ASCII (American Standard Code for Information Interchange) value

    for the given character, a numeric value, by 1 and sets the result to ch2. Thus, ch2 contains b.

    3. x%y is always equals to (x-(x/y)*y).

    4. The second operand of operators % and / must be non-zero.

    Program #3

    OBSERVABLE

    O

    U

    T

    P

    U

    T

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    5

    Write a program to convert an alphabet from lowercase to uppercase

    /*program to convert the lowercase to uppercase*/#includemain(){

    char ch;printf(\n Enter any lowercase alphabet:\n);scanf(%c,&ch); /* or ch=getchar();*/ch=ch-32;printf(\n Uppercase alphabet:%c,ch);

    }

    Output: Enter any lowercase alphabet:mUppercase alphabet:M

    1) #includemain(){

    int x;x=-3+4-7*8/5%10;printf(x=%d,x);

    }

    2) #includemain(){

    float a=1.5;int b=3;a=b/2+b*8/b-b+a/3;printf(a=%f,a);

    }

    3) #includemain(){

    int x;x=-3*-4%-6/-5;printf(x=%d,x);

    }

    4) #includemain(){

    printf(%d,4/3);printf(%d,4/-3);printf(%d,-4/3);printf(%d,-4/-3);

    }

    5) #includemain(){

    printf(%d,4%3);printf(%d,4%-3);printf(%d,-4%3);printf(%d,-4%-3);

    }

    6) #includemain(){

    float a=5,b=2;int c;c=a%b;printf(%d,c);

    }

    7) #includ emain(){

    float a=4;int i=2;printf(%f%d,i/a,i/a);printf(%d%f,i/a,i/a);

    }

    8) #includemain(){

    float a;a=4/2;printf(%f%f,a,4/2);

    }

    #includen()

    int q=2,d=3,st;q*d/4-12/12+12/3*16/d;

    printf(st=%d,st);

    7) #includemain(){float a=4;int i=2;printf(%f%d,i/a,i/a);printf(%d%f,i/a,i/a);}

    8) #includemain(){int x;x=4%5+6%5;printf(x=%d,x);}

    9) #includemain(){

    int x;x=3*4%5;printf(x=%d,x);}

  • 7/28/2019 Lesson5- Language Fundamentals4

    6/27

    5.2.3. Unary arithmetic operators

    Unary Arithmetic operators perform the arithmetic operations such as increment & decrement and change

    of sign on only one operand. There are 4 unary arithmetic operators:

    Operator OperationUnary + (Positive) Keeps the sign of value unchanged.Unary - (Negative) Changes the sign of value.++ (Increment) Adds 1 to value of operand and assigns the result to that operand only.

    - - (Decrement) Subtracts 1 from the value of operand and assigns the result to that operandonly.

    These operators can be used to form an expression as follows:

    Both sign operators should be preceded by the operand where as the increment and decrement operators

    can be preceded or followed by the operand.

    Ex: int a=10;

    1) +a /*keeps the sign of 10 as positive only*/

    2) a /* changes the sign of 10 to negative*/

    3) ++a or a++ /* equivalent to a=a+1; hence, a holds 11*/

    4) -a or a-- /* equivalent to a=a-1; hence, a holds 9*/

    Program #4

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    6

    Operand_1 Unary_arithmetic_operator(or)

    Unary_arithmetic_operator Operand_1

    Write a program to demonstrate positive and negative signs /*program to demonstrate positive and negative signs*/#include

    main(){

    int num;printf(\n Enter any number:);scanf(%d,&num);printf(\n Positive number=%d,+num);printf(\n Negative number=%d,-num);printf(\n Positive number=%d,+(+num));printf(\n Negative number=%d,+(-num));printf(\n Positive number=%d,-(-num));printf(\n Negative number=%d,-(+num));

    }Output: Enter any number: 123Positive number=123Negative number=-123Positive number=123Negative number=-123Positive number=123Negative number=-123

  • 7/28/2019 Lesson5- Language Fundamentals4

    7/27

    Few words about increment and decrement operators:

    If ++ is used before the operand , then this increment operator is called as pre -increment operator. If

    ++ is used after the operand , then this increment operator is called as post -increment operator. The

    same treatment will be given to the --.

    The increment / decrement operator works as a simple increment / decrement by one, if it is not

    used as a part of an expression. In that case, there is no difference between operations of pre or postincrement or decrement operators.

    Ex: int a=10;

    a++;

    printf(a=%d,a);

    Ex: int a=10;

    ++a;

    printf(a=%d,a);Output: a=11 Output: a=11

    However, when these operators are used as a part of an expression then the difference can be

    noticed: The pre-increment operator first increments the value of operand and then returns that

    incremented value. The post-increment operator first returns the existing value of operand and then increments the

    value of the operand. The pre-decrement operator first decrements the value of operand and then returns that

    decremented value. The post-decrement operator first returns the existing value of operand and then decrements the

    value of the operand.

    Ex: int a=10;

    x=50+a++;

    printf(x=%d,a=%d,x,a);

    Ex: int a=10;

    x=50+(++a);

    printf(x=%d,a=%d,x,a);Output: x=60,a=11 Ouput: x=61,a=10

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    7

  • 7/28/2019 Lesson5- Language Fundamentals4

    8/27

    OBSERVABLE

    O

    U

    T

    PU

    T

    5.2.4. Relational operators

    The relational operators are used to compare two operands. There are six relational operators available.

    All these operators are binary operators. These are used to form an expression that is either true or false.

    The following table lists all the relational operators.

    Operator Operation== (is equal to) Checks whether first operand is equal to second operand or not.!= (is not equal to) Checks whether first operand is not equal to second operand or not.

    > (is greater than) Checks whether first operand is greater than the second operand.>=(is greater than or

    equal to)

    Checks whether first operand is greater or equals to second operand.

    < (is lesser than) Checks whether first operand is lesser than the second operand.

  • 7/28/2019 Lesson5- Language Fundamentals4

    9/27

    Where operand_1 and Operand_2 are variables or constants or arithmetic expressions.

    Ex: int i=7;

    float f=5.5;

    char c=w;

    Program #4

    5.2.5. Logical operators (or) Compound relational operators

    The logical operators are used to combine the relational expressions and evaluate them. There are threelogical operators. These work on the values of operands that are either true or false. Both Logical AND

    and Logical OR operators are binary operators whereas Logical NOT is only unary operator. The following

    table lists them:

    Operator Operation&& (Logical AND) Performs Logical AND operation on two operands.

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    Expression Valuef>5 1 (true)(i+f)=10*(i+f) 0 (false)

    9

    Operand_1 relational_operator Operand_2

    Write a program to demonstrate relational operators /*program to demonstrate relational operators*/#includemain(){

    int num1,num2;num1=20;num2=30;printf(\n Is equal to condition=%d,(num1==num2));printf(\n Is not equal to condition=%d,(num1!=num2));printf(\n Is greater than condition=%d,(num1>num2));printf(\n Is greater than or equal to condition=%d,(num1>=num2));printf(\n Is lesser than condition=%d,(num1

  • 7/28/2019 Lesson5- Language Fundamentals4

    10/27

    || (Logical OR) Performs Logical inclusive OR operation on two operands.! (Logical NOT) Reverses the value of operand.

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    10

  • 7/28/2019 Lesson5- Language Fundamentals4

    11/27

    The logical operators are used to form logical expressions as shown below:

    1) Logical AND expression:

    Where Expression_1 and Expression_2 are any expression those return either true or false.

    When logical AND operator is encountered, first Expression_1 is tested. It returns either true or

    false. Secondly, the expression_2 is tested. It returns either true or false. Then both of these expressions values are combined to give the value of logical expression as given below:

    Expression_1 Expression_2 Expression_1 && Expression_21 1 11 0 00 1 00 0 0

    Ex: int a=10,b=30,c;

    c=((a>b)&&(a!=b)); Ans: c=1

    Note: If the left operand yields false value, the right operand is not evaluated by a compiler in a logical

    expression using &&.

    2) Logical OR expression:

    Where Expression_1 and Expression_2 are any expression those return either true or false.

    When logical AND operator is encountered, first Expression_1 is tested. It returns either true or

    false. Secondly, the expression_2 is tested. It returns either true or false. Then both of these expressions

    values are combined to give the value of logical expression as given below:

    Expression_1 Expression_2 Expression_1 || Expression_21 1 11 0 10 1 10 0 0

    Ex: int a=10,b=30,c;

    c=((a>b)||(a!=b)); Ans: c=1

    Note: If the left operand yields true value, the right operand is not evaluated by a compiler in a logical

    expression using ||.

    3) Logical NOT expression:

    where Expression_1 is any expression that returns either true or false.

    When logical NOT operator is encountered, first the Expression_1 is tested. It returns either true

    or false. Upon the value of this Expression_1, Logical NOT returns the value given below:

    Expression_1 !(Expression_1)1 0

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    11

    Expression_1 && Expression_2

    Expression_1 || Expression_2

    !(Expression_1)

  • 7/28/2019 Lesson5- Language Fundamentals4

    12/27

    0 1Ex: int a=20,b=30,c=40,d;

    d=!(a>b&&b>c); ans: d=1

    OBSERVABLE

    OU

    T

    P

    UT

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    12

    1) #include

    main(){int x,y,z;x=y=z=1;z=++x||++y&&++z;

    printf(%d %d %d,x,y,z);}

    2) #includemain(){

    int x,y,z;x=y=z=1;

    z=++x&&++y&&++z;printf(%d %d %d,x,y,z);}

    3) #includemain(){

    int x,y,z;x=y=z=1;z=++x&&++y||++z;

    printf(%d %d %d,x,y,z);}

    4) #includemain(){

    int x,y,z;x=y=z=-1;z=++x||++y&&++z;

    printf(%d %d %d,x,y,z);}

    5) #includemain(){

    int x,y,z;x=y=z=-1;

    z=++x&&++y&&++z;printf(%d %d %d,x,y,z);}

    6) #includemain(){

    int x,y,z;x=y=z=-1;z=++x&&++y||++z;

    printf(%d %d %d,x,y,z);}

    7) #include

    main(){int x=10,y=5,p,q;p=x>9;q=x>3&&y!=3;printf(%d %d,p,q);}

    8) #includemain(){int a=30,b=40,x;x=(a!=10)&&(b=50);printf(x=%d,x);}

    9) #includemain(){int a=100,b=200,c;c=(a==100||b>200);printf(c=%d,c);}

    10) #includemain(){int x=11,y=6,z;z=x==5||y!=4;printf(z=%d,z);}

    11) #includemain(){int x=10,y=-20;x=!x;y=!y;printf(%d %d,x,y);}

    12) #includemain(){int x=0,y=1;y=!x;x=!y;printf(%d %d,x,y);}

  • 7/28/2019 Lesson5- Language Fundamentals4

    13/27

    5.2.6. Conditional operator

    The conditional operator consists of two symbols: the question mark (?) and the colon (:). This is the only

    operator in C that acts on three operands at a time. Hence, this operator is also called as ternary

    operator or trinary operator. This operator acts in the same way as the ifelse statement acts.

    Conditional operator can be used as follows:

    In this syntax,

    Condition is any expression to be tested and it returns either true or false. Usually, it is either a

    relational expression or compound relational expression.

    Expression_1 and Expression_2 are any statements that are to be executed.

    When the conditional operator is encountered, first, the Condition is tested. It returns either

    true or false. If the condition is true, Expression_1 is executed. If the condition is false, Expression_2

    is executed. This is shown as in the following chart:

    Program #4

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    13

    Condition? Expression_1: Expression2;

    Condition

    Expression_2 Expression_1

    TrueFalse

    Write a program to find biggest of three numbers /*program to find biggest of three numbers*/#includemain(){

    int num1,num2,num3,big;printf(\n Enter any three numbers:);scanf(%d%d%d,&num1,&num2,&num3);

    big=((num1>num2)&&(num1>num3)?num1:num2>num3?num2:num3;printf(\n Biggest of three numbers=%d,big);

    }Output: Enter any three numbers: 19045243Biggest of three numbers=452

    Next_statement

  • 7/28/2019 Lesson5- Language Fundamentals4

    14/27

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    14

  • 7/28/2019 Lesson5- Language Fundamentals4

    15/27

    OBSERVABLE

    OU

    T

    P

    U

    T

    5.2.7. Bitwise operators

    Bitwise operators perform operations on bits of data. These operators are used for testing,complementing or shifting bits to the right or left. Usually, bitwise operators are not useful in cases of

    float and double . There are six bitwise operators as shown below:

    Operator Operation& (Bitwise AND) Returns 1 if both corresponding bits are 1; otherwise 0.| (Bitwise inclusive OR) Returns 0 if both corresponding bits are 0; otherwise 1.^(Bitwise exclusive OR) Returns 0 if both corresponding bits are 0 or 1; otherwise 1.~ (Ones complement) Returns 1, if bit is 0 and returns 0, if bit is 1.> (Right shift) Shifts most significant bit by specified number of times.

    All these operators are binary operators except ones complement operator. Ones complement operator

    is a unary operator.

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    15

    1) #includemain(){int x=3,y=4,z=4;printf(ans=%d,z>=y&&y>=x?1:0);}

    2) #includemain(){int x=3,y=4,z=4;printf(ans=%d,z>=y>=x?1:0);}

    3) #includemain(){int i=-4,j,num=10;

    j=i%-3; j=(j?0:num*num);printf(j=%d,j);}

    4) #includemain(){int k=12,n=30;k=(k>5&&n=4?100:200);printf(k=%d,k);

    }

    5) #includemain(){int c=0,d=5,e=10,a;a=c>1?d>1||e>1?100:200:300;printf(a=%d,a);}

    6) #include

    main(){int a=10,b=10;printf(ans=%d,a>b?a*a:b/b);}

  • 7/28/2019 Lesson5- Language Fundamentals4

    16/27

    Bitwise AND (&):

    The operator & performs a bitwise AND between two operands. It compares each bit of the left operand

    with the corresponding bit of the right operand. For each bit, the result is 1, if both the compared bits are

    1; otherwise, the result is 0 as shown below:

    Compared bits Result

    0 & 0 00 & 1 0

    1 & 0 0

    1 & 1 1

    Interview question #1

    Bitwise Inclusive OR (|)

    The operator | performs a bitwise inclusive OR between two operands. It compares each bit of the left

    operand with the corresponding bit of the right operand. For each bit, the result is 0, if both the

    compared bits are 0; otherwise, the result is 1 as shown below:

    Compared bits Result

    0 | 0 0

    0 | 1 1

    1 | 0 1

    1 | 1 1

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    16

    What is masking?Masking is an operation in which the desired bits of a binary number or bit pattern are set to zero. The

    operator & (Bitwise AND) is very useful for this purpose. To mask particular bits in a binary number, do

    the following:

    1. Create a new number that has binary pattern with 0s in the positions that you want to mask and

    1s in other positions.

    2. Perform bitwise AND operation between the number that is to be masked and created number.

  • 7/28/2019 Lesson5- Language Fundamentals4

    17/27

    Bitwise Exclusive OR (^)

    The operator ^ performs a bitwise exclusive OR between two operands. It compares each bit of the left

    operand with the corresponding bit of the right operand. For each bit, the result is 0, if both the

    compared bits have the same value; otherwise, the result is 1 as shown below:

    Compared bits Result

    0 ^ 0 0

    0 ^ 1 1

    1 ^ 0 1

    1 ^ 1 0

    Bitwise Right shift operator (>>)

    The right shift operator >> moves the bits to the right based on specified number of positions. The

    general form of right shift expression is:

    where n is the number of bit positions to be shifted.Ex: int a=10;

    x=a>>3;The right shift operator shifts the bits in the variable a thrice.

    Bitwise Right shift operator (

  • 7/28/2019 Lesson5- Language Fundamentals4

    18/27

    Ex: int a=10;x=~a;

    Interview question #2

    Program #5

    Program #6

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    18

    What are uses of shift operators?To divide an integer by 2 n, a right shift by n bit positions is applied.

    To multiply an integer by 2 n , a left shift by n positions is applied.

    Write a program to convert an uppercase alphabet to lowercase alphabet /*program to convert an uppercase alphabet to lowercase alphabet*/#includeint main(){

    char alpha;printf(\n Enter any uppercase alphabet:);scanf(\n%c,&alpha);printf(\n Lower case alphabet=%c, alpha | 32);

    return 0;}

    Output: Enter any uppercase alphabet:ALower case alphabet=a

    Write a program to swap two integers with out using temporary variable /*program to swap two integers without using temporary variable*/#includeint main(){

    int a,b;printf(\n Enter any two numbers:);scanf(%d%d,&a,&b);printf(\n Before swap a=%d\tb=%d,a,b);a=a^b;b=a^b;a=a^b;printf(\n After swap a=%d\tb=%d,a,b);

    return 0;}Output: Enter any two numbers: 1020Before swap a=10 b=20After swap a=20 b=10

  • 7/28/2019 Lesson5- Language Fundamentals4

    19/27

    Examples:

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    19

  • 7/28/2019 Lesson5- Language Fundamentals4

    20/27

    5.2.8. Special operators

    1. sizeof operator.

    2. comma operator.

    3. short-hand operators.

    4. Referencing and dereferencing operators.

    sizeof operator: It is an unary operator. It returns the number of bytes occupied by the operand in

    memory. The operand may be a variable, a constant, an expression or simply a datatype.

    Ex: printf(%d,sizeof(10)); /*prints 2 or 4*/

    printf(%d,sizeof(int)); /*prints 2 or 4*/

    int x=30; float y=45.355;

    printf(%d,sizeof(y)); /*prints 4*/

    printf(%d,sizeof(x+y)); /*prints 4*/

    Comma Operator: A comma operator can be used as a separator or a terminator in the followingstatements:

    int a,b,c;

    c=a,a=b,b=c;

    This operator can also be used to link related expressions together. A comma-linked list of

    expressions is evaluated from left to right and the value of right-most expression is the value of

    combined expression.

    Ex: value=(x=10,y=20,x+y); ans: value=30

    Short-hand Operators: These operators are formed by combining both binary arithmetic operators

    and bitwise operators with assignment operator. Hence, these are also called as compound

    assignment operators.

    The short-hand operators include: += , -= , *= , /= , %=

    &= , |= , ^= , = .

    All of these operators are binary operators. So, these operands should act on two operands at a time.

    Usually, Operand_1 should be a variable and Operand_2 can be a variable or a constant.When compound assignment operator is encountered, the desired operation is performed between

    Operand_1 and Operand_2 and the result gets stored in Operand_1.

    Ex: a+=2 means a=a+2

    int a=20;

    a+=2; ans: a=22

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    20

    Operand_1 short-hand operator Operand_2

  • 7/28/2019 Lesson5- Language Fundamentals4

    21/27

    Refencing and dereferencing operators: Referencing operator (&) is the operator that returns the

    address of operand in the memory. Dereferencing operator (*) is the operator that returns the value

    at the address that is pointed by the referencing operator.

    Ex: int a=10;

    printf(\n Address of a=%u,&a); /*prints address of a*/

    printf(\n Value of a=%d,*(&a)); /*prints value of a */

    OBSERVABLE

    OU

    T

    P

    UT

    1) Are the following two statements same (yes /no)

    a

  • 7/28/2019 Lesson5- Language Fundamentals4

    22/27

    The following table lists the operators, their precedence and associativity.

    5.3. Evaluating an expression

    Evaluation of an expression is the process of calculating the result from it based on operators precedence

    and associativity.

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    Precedence Operands Operators Associativity

    1 2 ( ) [ ] . -> Left-to-Right

    2 1 ! ~ ++ -- + - * & (type) sizeof

    Right-to-Left

    3 2 * / % Left-to-Right

    4 2 + - Left-to-Right

    5 2 > Left-to-Right

    6 2 < >= Left-to-Right

    7 2 = = != Left-to-Right

    8 2 & Left-to-Right

    9 2 ^ Left-to-Right

    10 2 | Left-to-Right

    11 2 && Left-to-Right

    12 2 || Left-to-Right

    13 3 ? : Left-to-Right14 2 = *= /= %= += -=

    &= |= ^= =Right-to-Left

    15 2 , Left-to-Right

    22

  • 7/28/2019 Lesson5- Language Fundamentals4

    23/27

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    23

  • 7/28/2019 Lesson5- Language Fundamentals4

    24/27

    5.3. Type conversion in an expression

    Type conversion is the process of converting a value from one data type to another. This type conversion

    makes the operands in an expression belong to similar data type.

    If casting is done from lower size type to higher size type value, then it is called as broadening

    conversion . E.g., char to int.

    If casting is done from higher size type to lower size type value, then it is called as narrowing

    conversion , causes some loss of value.

    E.g. float to int causes truncation of fractional part.

    float to long causes truncation of fractional part.

    double to float causes rounding of digits.

    long int to int causes dropping of the excess high order bits.

    int to short causes dropping of the excess high order bits.

    This type conversion can be done either implicitly or explicitly.

    Implicit type conversion: C permits mixing of constants and variables of different data types in an

    expression. C automatically converts any intermediate values to the proper type so that expression can

    be evaluated with out loosing any significance. This automatic conversion is known as implicit type

    conversion.

    During evaluation, the lower type is automatically converted to the higher type, according to strictrules of type conversion. The rules for this type conversion are as follows:

    1. If one of the operands is a long double , then the other is converted a long double.

    2. Otherwise, if one of the operands is a double , then the other is converted to a double.

    3. Otherwise, if one of the operands is a float , then the other is converted to a float.

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    24

    short char

    int

    unsigned int

    long int

    unsigned long int

    float

    double

    long double

    Broadeningconversion

    Narrowingconversion

  • 7/28/2019 Lesson5- Language Fundamentals4

    25/27

    4. Otherwise, if one of the operands is an unsigned long , then the other is converted to an

    unsigned long.

    5. Otherwise, if one of the operands is a long , then the other is converted to a long.

    6. Otherwise, if one of the operands is unsigned int , the other is converted to unsigned int.

    7. Otherwise, both operands are converted to int values.

    Ex: int i=10,x;float f=3.4f;

    double d=7.4;

    long int l=214L;

    x=l/i+i*f-d

    Note : If the two operands in an assignment statement are of different data types, the right side operand

    is automatically converted to the data type of the left side.

    Ex: int a=10;

    float b;

    b=a;

    printf(%f,b);

    output:

    Ex: float a=10.98;

    int b;

    b=a;

    printf(%d,b);

    output:Program #4

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    25

    Write a program to print ASCII value of a character /*program to print ASCII value of a character*/#includemain(){

    char ch;int val;printf(\n Enter any character:);scanf(\n%c,&ch);val=ch;printf(\n ASCII value of \%c\ is %d,ch,val);

    }Output: Enter any character:aASCII value of a is 97

  • 7/28/2019 Lesson5- Language Fundamentals4

    26/27

    OBSERVABLEOUT PUT

    Explicit type conversion: When we want to do type conversion forcefully, then we use cast operator.

    Cast operator is used to convert value of an operand from one type to another explicitly by the user. This

    explicit type conversion is also called as casting or coercion .

    The usage of cast operator is as follows:

    Here, type_name is any standard data type. The expression may be a constant, variable or an

    expression. As this operator works on only one operand at a time, this is a unary operator.

    Ex: int a=10,b=25,c=32;

    float d;

    d=(a+b+c)/3;

    printf(%f,d);

    output: 22.000000

    Ex: int a=10,b=25,c=32;

    float d;

    d=(float)(a+b+c)/3;

    printf(%f,d);

    output: 22.333334From the above example, it is clear that the coercion helps us to avoid truncation in division.

    5.6. Conclusion

    C supports arithmetic, relational and logical operators like other programming languages. It supports

    increment and decrement operators for faster execution. Bitwise operators are supported in C, which are

    not available in other languages. In addition to simple assignments, C also provides compound

    assignments or short-hand assignments.

    Expressions are formulated with the help of operators and operands. These are evaluated to get

    the desired result. These are evaluated according to the precedence levels of the operators and their

    associativity. In evaluating mixed-type expressions, implicit conversion rules are followed. Explicit type

    conversions are possible with coercion or type casting.

    http://pradeeppearlz.blogspot.com/ Operators andExpressions

    26

    (type_name) expression

    2) #includemain(){printf(%d %o %x,72,72,72);}

    1) #includemain(){printf(%d %d %d,72,072,0x72);}

  • 7/28/2019 Lesson5- Language Fundamentals4

    27/27