Sunday, August 9, 2020

Operators in C languages

C supports a rich set of built-in operators. We have already used several of them,such as =,+,-,*,& and <. An operator is a symbol that tells the computer to reform certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variables. They usually form a part of the mathematical or logical expressions.

C operators can be classified into a number of categories. They include:

1. Arithmetic Operators

2. Relational Operators

3. Logical Operators

4. Assignment Operators

5. Increment and Decrement Operators

6. Conditional Operators

7. Bitwise Operators

8. Special Operators

An expression is a sequence of operands and operators that reduces to a single value. For example,

10+15

Is an expression whose value is 25. The value can be any type other than void.

1.      1. Arithmetic Operators:-  C provide all the basic different operators. The operators +,-,*, and / all work the same way as they do in other languages. These can operate on any built-in data type allowed in C. The unary minus operator, in effect, multiplies its single operand by -1. Therefore, a number preceded by a minus sign changes its sign.

1.1 Integer Arithmetic:- When both the operands in single arithmetic expression such as a+b are integers, the expression is called an integer expression, and the operation is called integer arithmetic.  Integer Arithmetic always yields , and the operation is called integer arithmetic. Integer arithmetic always yields an integer value. The largest integer value depends on the machine, as pointed out earlier. In the above examples, if a and b are integers, then for a=14 1.1   and b=4 we have the following results:                                                                                            
              a-b=10

       a+b=18

       a*b=56

       a/b=3(decimal part truncated)

       a%b=2(remainder of division) 

1.   1.2 Real Arithmetic:-An arithmetic operation involving  only real operands is called real arithmetic. A real operand may assume values either in decimal or exponential  notation. Since floating point values are rounded to the number of significant digits permissible, the final value is an approximation of the correct result. If x,y, and z are floats, then we will have: x=6.0/7.0=0.857143,y=1.0/3.0=0.333333, z=-2.0/3.0=-0.666667. The operator % can not be used with real operands .

  1.3 Mixed-mode Arithmetic:-When one of the operands is real and the other is integer,the expression is called a mixed-mode arithmetic expression. If either operand is of the real type, then only the real operation is performed and the result is always a real number.Thus 15/10.0=1.5, whereas 15/10=1.

1.     2. Relational Operators:- We often compare  two depending  on their relation, take certain decisions. For example, we may compare the age of two persons , or the price of two items, and so on. These comparisons can be done with the help of relational operators.

1.      3.Logical Operators:- In addition to the relational operators. C has the following three logical operators.

            &&         meaning logical    AND

              ||           meaning logical    OR

              !              meaning logical    NOT

2.     4. Assignment Operators:- Assignment Operators are used to assign the result of an expression to a variable. We have seen the usual assignment operator,’=’ . In addition, C has a set of ‘shorthand ’ assignment operators of the form v op= exp; where  v is a variable, exp is an expression and op is a C binary arithmetic operator.

1.       5.Increment and Decrement Operators:- C allows two very useful operators not generally found in other languages. These are the increment and decrement operators:- ++ and – . The operator ++ adds 1 to the operand,while  -- subtracts 1. Both are unary operators and takes the following form:

             ++m;  or m++;

             --m;   or  m--;   

     6.    Conditional Operators:- A ternary operator pair “?:” is available in C to construct conditional expressions of the form:-  exp 1 ? exp 2: exp 3.  Where exp 1, exp 2, and  exp 3 are expressions.The operator  ? : works as follows  : exp 1 is evaluated first. If it is non zero(true), then  the expression exp 2  is evaluated and becomes the value of the expression. If exp 1 is false. exp 3 is evaluated and its value becomes the value of the expression. Note that only one of the expressions (either exp 2 and exp 3 )is evaluated. For example,consider the following statements.

                 a=10;

                 b=15;

                 x= (a>b)? a:b;   

In this example, x will be assigned the value of b. This can be achieved using the if..else statements as follows:

                If(a>b)

                 x=a;

                 else

                 x=b;

7. Bitwise Operators:-C has a distinction of supporting special operators known as bitwise operators for manipulation of data at bit level. These operators are used for testing the bits or shifting them right and left. Bitwise Operators may not be applied to float or double.


 8. Special Operators:- C supports some special operators of interest such as comma operator, sizeof operator, pointer operators(& and *) and member selection operators(. and ->).

8.1 The Comma Operators:- The comma operators can be used to link the related expressions together. A comma-linked list of expressions is evaluated left to right and the value of right-most expression is the value of the combined expression. For example, the statement value=(x=10, y=5,x+y);  first assigns the value 10 to x then assigns 5 to y  and finally assigned 15(i.e 10+5)to value . Since comma operator has lowest precedence of all operators, the parentheses are necessary. Some applications of comma operator are:

In for loops :

        for(n=1, m=10, n<m; n++, m++)

In while loops:

        While(c=getchar(), c!=’10’)

Exchanging Values

      t=x,x=y,y=t;

8.2 The sizeof Operator:- The sizeof is a compile time operator and, when used with an operand, it returns the number of bytes the operand occupies. The operand may be a variable, a constant or data type qualifier.

Examples: m=sizeof(sum);

                    n=sizeof(long int);

                    k=sizeof(235L);

The sizeof  operator is normally used to determine the lengths of arrays and structures when their sizes are not known to the programmer. It is also used to allocate memory space dynamically to variable during execution of a program . 




No comments:

Post a Comment

Detail of Software and Hardware

  Topic   There are available only two things in computer system one is software and another is hardware.   Both is opposite of each other...