04 operatorcompu

Upload: er-arvind-nagda

Post on 14-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 04 Operatorcompu

    1/8

    C Programming-Operator

    C ProgrammingOperator

    Topics Covered:

    1. What is operator? List the different categories of operator in C2. Arithmetic Operators

    3. Integer Arithmetic4. Real Arithmetic

    5. Mixed mode arithmetic

    6. Relational Operators

    7. Logical Operators8. Assignment Operator

    9. Increment and Decrement Operator

    10. Bitwise Operator 11. Special Operator

    12. Comma Operator

    13. sizeof Operator 14. Evaluation of Expression

    15. Automatic type conversion

    16. Casting a value17. Operator precedence and associativity

    What is operator? List the different categories of operator in COperator is a symbol that tells computer to perform mathematical and logical task.

    C operators can be classified into following 8 categories:

    1. Arithmetic Operators

    2. Relational Operators3. Logical Operators4. Assignment Operators

    5. Increment and Decrement Operators

    6. Conditional Operators7. Bitwise operators

    8. Special Operators

    Arithmetic Operators: -C provides basic arithmetic operators *, +, -, /, and %. These all operators can work with only

    built-in data type (int, float, char, double, etc.)1. Addition Operator: -

    It has two format, one is unary plus and other is binary plus. Unary plus only works withonly one operand. Unary plus was not supported in older version of C.

    e.g. a = +b;

    a = +20;

    Here a and b are two variables and it is simply known as operand and 20 is

    constant

    Another format is binary plus in which two operands are usede.g. a = b + c;

    a = 20 + 30;2. Subtraction Operator: -

    It has also two format, one is unary minus which multiply your operand with 1.

    e.g. a = -b;

    a = -30;

    If initially b value is 20 then a value is going to be 20 * -1 i.e. 20

    By www.sharebca.com Share BCA.ComA

    place to learn and share all about BCA

    www.twitter.com/HARDIK_RATHOD

    follow me on twitter07/09/13 Page 1 of 8

    http://www.sharebca.com/http://www.twitter.com/HARDIK_RATHODhttp://www.sharebca.com/http://www.twitter.com/HARDIK_RATHOD
  • 7/29/2019 04 Operatorcompu

    2/8

    C Programming-Operator

    Another format is binary minus in which two operands are used

    e.g. a = b ca = 34 30

    3. Multiplication Operator: -

    It has only binary format means you can be used with only two operands.

    e.g. a = b * c

    a = 5 * 3

    4. Division Operator: -

    Division operator has also only binary format means operator in between two operands.

    e.g. a = b / c

    a = 6 / 3

    5. Modulo Operator: -

    Modulo operator is used to determine the remainder after division operation. Modulo

    operator cannot be used for the float value. If any of the operand is float then compilation

    error is generated.e.g. a = 6 % 4

    It will give the answer 2 since after dividing 6 by 4 we are getting 2 as remaindera = 7.8 % 4It will give compilation error because 7.8 is float value.

    Also if we are using modulo operator for the negative value then sign of the answer is

    sign of the first operand.e.g. a = -10 % 3 = - 1 (Sign of 1st operand 10 is minus so answer is minus)

    a = - 10 % -3 = -1 (Sign of 1st operand 10 is minus so answer is minus)

    a = 10 % -3 = 1 (Sign of 1st operand 10 is plus so answer is plus)

    Integer Arithmetic: -When both the operands in as single arithmetic expression (such as a + b) are integers, then this

    expression is known as the Integer Expression and the operation is known as the Integer

    arithmetic.

    The result of the integer arithmetic is always the integer.

    e.g. 5 + 6 = 11

    34 3 = 313 * 3 = 9

    4 / 2 = 2

    9 / 5 = 1 (Fractional part is truncated)8 % 5 = 3 (remainder value)

    During the division operation in the result the fractional part is truncated. If one of the operand is

    negative then the direction of truncation is machine dependente.g. -6 / 7 = 0 or -1 (Depend on the machine)

    Similarly in modulo operation if first operand (dividend) is negative then the result is negative

    other wise result is positivee.g. -14 % 3 = -2

    -14 % -3 = -2

    14 % -3 = 2

    Real Arithmetic: -If in arithmetic operation both the operand is real then the expression is known as the real

    expression and the operation is known as the real arithmetic

    In real arithmetic result is always the real.e.g. 4.5 + 443.3 = 447.8

    We cannot use modulo operator with the real arithmetic.

    Mixed mode arithmetic: -

    By www.sharebca.com Share BCA.ComA

    place to learn and share all about BCA

    www.twitter.com/HARDIK_RATHOD

    follow me on twitter07/09/13 Page 2 of 8

    http://www.sharebca.com/http://www.twitter.com/HARDIK_RATHODhttp://www.sharebca.com/http://www.twitter.com/HARDIK_RATHOD
  • 7/29/2019 04 Operatorcompu

    3/8

    C Programming-Operator

    If in arithmetic operation one operand is integer and another is real then this expression is known

    as the mixed mode expression and operation is known as the mixed mode arithmetic.e.g. 4.5 + 45 = 49.5

    In mixed mode arithmetic result is real

    Relational Operators: -Using the relational operators can do the comparison of two quantities. The expression containingthe relational operator is known as the relational expression.

    e.g. a < b is relational expression

    The result of the relational operation is either true (1) or false (0).

    e.g. 10 < 20 is true20 < 10 is false

    C supports six different relational operators:

    Operator Meaning

    < Less than Greater than

    >= Greater than or equal to

    == Equal to!= Not equal to

    Logical Operators: -C provides following logical operators:

    Operator Meaning

    && And

    || Or ! Not

    These operators are used when we want to check more than one condition

    e.g. a > b && a = = 10The mean of above expression is that the value of the a should be greater than b and

    its value should be 10.

    An expression, which contains logical operators, is known as the logical expression or compound

    relational expression. The result of the logical expression is also either true or false.

    Assignment Operator: -Assignment operators are used to assign some value to the variable. There is only one assignmentoperator that is =

    e.g. a = 30;

    Above expression assign value 30 to a.

    Also assignment operator can be used for the shorthand assignment operators. It is used to perform

    arithmetic operation with the variable itself.

    Simple Expression Shorthand Operators

    a = a + 1 a += 1

    a = a 23 a -= 23

    a = a * 4 a * = 4a = a / 5 a /= 5

    a = a % 10 a %= 10

    The advantages of shorthand operators are:1. It is easy to write since we have to not write the operand two times.

    2. It is easy to read

    3. It is more efficient

    By www.sharebca.com Share BCA.ComA

    place to learn and share all about BCA

    www.twitter.com/HARDIK_RATHOD

    follow me on twitter07/09/13 Page 3 of 8

    http://www.sharebca.com/http://www.twitter.com/HARDIK_RATHODhttp://www.sharebca.com/http://www.twitter.com/HARDIK_RATHOD
  • 7/29/2019 04 Operatorcompu

    4/8

    C Programming-Operator

    Increment and Decrement Operator: -C provides two operator increment operator ( ++ ) and decrement operator (--). Increment operator

    is used to add 1 into the variable previous value. Decrement operator is used to subtract 1 from thevariable previous value.

    e.g. i++ // If initially i value is 10 then after this instruction execution it becomes 11

    i-- // If initially i value is 10 then after this instruction execution it becomes 9

    Increment operator has two formats: one is prefix and another is postfix operator.

    i++ and ++i

    When first one is used in assignment it first perform the assignment and then increment the value

    of i, while later one increment the value of i and then performs assignment.

    e.g i=10;a= i++;printf(i = %d \t a = %d\n,i,a);

    This statement print the outputi = 11 a = 10

    i=10;a= ++i;printf(i = %d \t a = %d\n,i,a);This statement print the output

    i = 11 a = 11

    Same thing is true for the decrement operator.

    Bitwise Operator: -

    C provides special operators known as bitwise operators for manipulation (Calculation) of data atbit level. These operators are used for testing the bits, or shifting them right or left. These operators

    cant be used with the float or double.

    Operator Meaning

    & bitwise And

    | bitwise Or

    ^ bitwise exclusive or

    > shift right

    ~ 1s complement

    Special Operator: -C supports some other operators, which have some specific purpose and mean. Those operators are

    known as the special operator. Some special operators are comma operator, sizeof operator,

    address of operator (&), member selection operator (. and ->), pointer operator (*)

    Comma Operator: -Comma operator is used to link the expression together and entire expression is evaluated from left

    to right and the right-most expression value is assigned to left side of the assignment operator.

    e.g v = (x= 10, y = 5, x + y);

    In above expression first x has assigned value 10, then y has assigned value 5, and thenaddition operation of x and y is performed and the result is assigned to the v.

    sizeof Operator: -It is operator, which gives the bytes occupied by the operand in memory. Your operand may beconstant, variable or any other qualifier.

    e.g. m = sizeof(int); // Give the size of integer data type

    By www.sharebca.com Share BCA.ComA

    place to learn and share all about BCA

    www.twitter.com/HARDIK_RATHOD

    follow me on twitter07/09/13 Page

    4 of 8

    http://www.sharebca.com/http://www.twitter.com/HARDIK_RATHODhttp://www.sharebca.com/http://www.twitter.com/HARDIK_RATHOD
  • 7/29/2019 04 Operatorcompu

    5/8

    C Programming-Operator

    k = sizeof(14); // Give the no. of bytes occupied by simple value 14

    l = sizeof(p); // Give the no. of bytes occupied by variable p

    This operator is generally used to determine the size of the array and the structure, which is not

    known to programmer.

    Evaluation of Expression: -In C program any mathematical expression is evaluated by the precedence of the operator. Thepriority of the *, / and % operators is higher so first those expressions are evaluated from left to

    right. Then next lower priority operators are + and which are also executed from left to right

    side.

    Automatic type conversion: -C provides different data type and its arithmetic operation. If any expression two operands are

    different type then lower type is converted into higher type operand and the result is higher

    type. It is known as automatic type conversion.

    Following rules are applied during type conversion:

    1. If one of the operand is long double then other operands will be converted into long double

    and result is long double.2. Else if one of the operand is double, the other will be converted into double and the result is

    double.

    3. Else if one of the operand is float, the other will converted to float and the result will be

    float.

    4. Else if one of the operand is unsigned long int, the other will be converted to unsigned longint and the result will be unsigned long int.

    5. Else if one of the operand is long int and the other is unsigned int then

    a. If unsigned int can be converted to long int, the unsigned int operand will be converted

    to long int and the result is long int.

    b. Else both operands will be converted to unsigned long int and the result will be

    unsignedlong int.

    6. Else if one of the operands is long int, the other will be converted to long int and the result

    will be long int.7. Else if one of the operand is unsigned int, the other will be converted to unsigned int and

    the result will be unsigned int.

    After evaluating the mathematical expression, the result is converted to the type of the variable onthe left side of the assignment operator (=). Following rules are applied during the assigning the

    right side value to left side.

    1. When the float value is assigned to int, the fractional part is truncated.

    2. When the double value is assigned to float, rounding of digit is performed.3. When long int is assigned to the int, the excess (vFirini) bits are dropped (Deleted).

    Casting a value: -C compiler automatically performs the type conversion during evaluation of expression. But

    sometimes we want to force a type conversion in different way then the automatic conversion. Forthat we convert the variable from one type to another type during evaluation of expression. This is

    known as casting a value.

    The general format of the cast is:

    (type-name) expression

    By above format the given expression is converted into type specified in bracket. Here the type-name should be standard C data type.e.g. x = 2 / 3

    Here we get the result 0 because 2 and 3 are integer so the result is integer. But by casting

    we can get the result 0.666667x = (float) 2 /3

    Here 2 is integer but converted into the float during evaluation by casting.

    By www.sharebca.com Share BCA.ComA

    place to learn and share all about BCA

    www.twitter.com/HARDIK_RATHOD

    follow me on twitter07/09/13 Page 5 of 8

    http://www.sharebca.com/http://www.twitter.com/HARDIK_RATHODhttp://www.sharebca.com/http://www.twitter.com/HARDIK_RATHOD
  • 7/29/2019 04 Operatorcompu

    6/8

    C Programming-Operator

    Operator precedence and associativity: -Each operator in has precedence and on the basis of that precedence expression is evaluated.

    e.g. 5 + 7 * 2 = 19 true= 24 false

    because the precedence of the multiplication is higher so first multiplication is

    performed than addition is performedIn C programming language following operators are used and there precedence is based on their

    rank.

    Operator Description Rank

    ( ) Function call 1

    [ ] Array element 1

    + Unary plus 2

    - Unary minus 2

    ++ Increment 2

    -- Decrement 2

    ! Logical not operator 2& Address operator 2

    sizeof Size of the variable 2

    (type) Casting 2

    * Multiplication 3

    / Division 3

    % Modulus 3

    + Binary addition 4

    - Binary subtraction 4

    > Right shift 5

    < Less than 6< = Less than or equal to 6

    > Greater than 6

    > = Greater than or equal to 6

    = = Equal to 7

    != Not equal to 7

    & Bitwise end 8

    ^ Bitwise exlusive or (XOR) 9

    | Bitwise or (OR) 10

    && Logical end 11

    || Logical or 12

    = Assignment 13+=, -=, *=, /= Shorthand operator 14

    , Comma opearator 15

    Problems

    1. Write a program to input following item:Quantity sold

    Unit price

    Basic salary

    A sales person getting the 200 Rs. commissions on every item sold. Also he is getting the 2%

    bonus amount on the total selling amount at the end of month. At last print his commission,bonus amount and (basic salary + bonus + commission).

    2. Write a program to solve quadratic equationax2 + bx + c = 0

    In solving equation first determine the value of delta

    delta = b2 - 4acIf delta < 0 there is infinite number of solution.

    By www.sharebca.com Share BCA.ComA place to learn and share all about BCA

    www.twitter.com/HARDIK_RATHOD

    follow me on twitter07/09/13 Page

    6 of 8

    http://www.sharebca.com/http://www.twitter.com/HARDIK_RATHODhttp://www.sharebca.com/http://www.twitter.com/HARDIK_RATHOD
  • 7/29/2019 04 Operatorcompu

    7/8

    C Programming-Operator

    If delta = 0 then solution = -b / 2a

    If delta > 0 then there is two solutionsolution1 = (-b + sqrt(delta))/2a

    solution2 = (-b sqrt(delta))/2a

    3. Write a program to calculate the depreciation from following equation:

    depreciation = (purchase price scrap value) / years of service

    4. Write a program to calculate the distance travel by the vehicle from following equation:distance = ut + (at2)/2

    Where, u = initial speedt = time

    a = acceleration

    5. Write a program to calculate the simple interest and compound interest. Also determine thedifference between two differences.

    simpleSimple interest = p * r * n / 100;

    Compound interest = p * (1 + r/100) n - p

    6. Write a program to convert Fahrenheit to Celsius and vice-versaf = 1.8 * c + 32;

    7. Write a program to calculate the area of triangle from the perimeter and length of the side .perimeter = a + b + c; (a, b, and c are three sides of the triangle)s = perimeter / 2;

    area = sqrt( s * (s a) * (s b) * (s c));

    8. Write a program to calculate the area of triangle from the given base and heightarea = b * h /2; b = base and h = height

    9. Give the value of following arithmetic expression (It is possible your expression is invalid)

    I. 25/3 % 2 A. 0II. 14 % 3 A. 2

    III. 14 % -3 A. 2

    IV. 14 % -3 A. 2V. + 9 / 4 + 5 A. 7

    VI. 15.25 + - 5.0 A. 10.25

    VII. 7.5 % 3 A. Error VIII. (5/3) * 3 + 5 % 3 A. 5

    IX. 14 % 3 + 7 % 2 A. 3

    X. 21 % (int) 4.5 A. 1

    10. Find the error in following expression and it there is no error then print the appropriate

    answer.I. x = y = z = 0.5, 2.0, -5.75 A. x = 0.5, y = 0.5, z = 0.5

    II. m = ++a * 5; A. a value is incremented by 1 and thenmultiplied with 5 then assigned to

    m.

    III. y = sqrt(100); A. y = 10IV. p *= x /y; A. first x/y then z is multiplied by x/y and

    assigned to z

    V. s = /5; A. Compilation error VI. a = 5++ - 20 * 2; A. 38

    11. Determine the value of following expression if a=5, b=10 and c = -6

    I. a > b && a < c A. 0II. a < b && a > c A. 1III. a = = c || b > a A. 1

    IV. b > 15 && c < 0 || a > 0 A. 1

    V. (a/2.0 = = 0.0 && b/2.0 != 0.0) || c< -10 A. 0

    12. Out of the following option which option is wrong

    I. P+=++Q+ ++R

    By www.sharebca.com Share BCA.ComA

    place to learn and share all about BCA

    www.twitter.com/HARDIK_RATHOD

    follow me on twitter07/09/13 Page 7 of 8

    http://www.sharebca.com/http://www.twitter.com/HARDIK_RATHODhttp://www.sharebca.com/http://www.twitter.com/HARDIK_RATHOD
  • 7/29/2019 04 Operatorcompu

    8/8

    C Programming-Operator

    II. P+=++Q+++R

    III. P+=(++Q)+(++ R)IV. P+=++Q+(++R)

    13. What will be the output of the following statement?

    printf("%d",scanf("%d%d%d",&i,&j,&k));

    Ans. Less than or equal to 3 (Depends on the how many value your computer has read

    successfully)

    By www.sharebca.com Share BCA.ComA

    place to learn and share all about BCA

    www.twitter.com/HARDIK_RATHOD

    follow me on twitter07/09/13 Page 8 of 8

    http://www.sharebca.com/http://www.twitter.com/HARDIK_RATHODhttp://www.sharebca.com/http://www.twitter.com/HARDIK_RATHOD