Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this document? Why not share!

- C programming project by navin thapa by Navinthp 15704 views
- Project report by meenalpandey 12526 views
- C Programming Project by Vijay Mohire 586 views
- Mini projects in C by LovelyCoding.Org by Sarfaraj Alam 3001 views
- C language mini project By Arbab Ahmed by Arbab Ahmed 250 views
- Student Data Base Using C/C++ Final... by Haqnawaz Ch 24747 views

No Downloads

Total views

2,907

On SlideShare

0

From Embeds

0

Number of Embeds

3

Shares

0

Downloads

108

Comments

0

Likes

1

No embeds

No notes for slide

- 1. PUNJAB COLLEGE OF TECHNICAL EDUCATION LUDHIANA REPORT ON OPERATORS IN C SUBMITTED TO SUBMITTED BY Mr. BALRAJ SINGH ARLETE MARIA GAGANDEEP SINGH JASMEEN KAUR 1
- 2. Introduction When you learn to program in a high-level language like C (although C is fairly low- level, as high-level languages go), the idea is to avoid worrying too much about the hardware. You want the ability to represent mathematical abstractions, such as sets, etc. and have high level language features like threads, higher-order functions, exceptions. High-level languages, for the most part, try to make you as unaware of the hardware as possible. Clearly, this isn't entirely true, because efficiency is still a major consideration for some programming languages. C, in particular, was created to make it easier to write operating systems. Rather than write UNIX in assembly, which is slow process (because assembly code is tedious to write), and not very portable (because assembly is specific to an ISA), the goal was to have a language that provided good control-flow, some abstractions (structures, function calls), and could be efficiently compiled and run quickly. 2
- 3. Operations in c can be classified into the following categories: 1. Arithmetic operators 2. Conditional operators 3. Bitwise operators 4. Relational operators 5. Logical operators 6. Assignment operators 7. Increment and decrement operators 8. Special operators 3
- 4. 1. Arithmetic Operators C provides all the basic arithmetic 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 plus operator, in effect, multiplies its single operand by -1. Therefore, a number preceded by minus sign changes its sign. Operator Meaning + Addition or unary plus - Subtraction or unary minus * Multiplication / Division % Modulo division Integer division truncates any fraction part. The modulo division operation produces the reminder of any integer division. a+b a-b a*b a/b a%b -a * b Here a & b are variables and are known as operands. The modulo division operator % cannot be used on floating point data. Note that C does not have an operator for exponentiation. Old versions of C does not support unary plus but ANSI C supports it. Arithmetic operators include 1.1 Integer Arithmetic 2.2 Real Arithmetic 3.3 Mixed-mode Arithmetic 4
- 5. 1.1 Integer Arithmetic When we take two operands in a single expression then the expression formed is known as Integer expression, and the operation is called integer arithmetic. The largest integer value depends upon machine. Example: If x and y are integer then for x=19 and y=11 we can have the following result x -y = 8 and x+y = 30 1.2 Real Arithmetic An arithmetic operation involving only real operator is known as real arithmetic. Real operand can assume decimal and exponential notation. Since floating values are rounded to the number of significant digits permissible, the final value is an approximation of the correct result. If a, b, c are float then we have a=8.0/11.0=0.727272 Note- The operate % cannot 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 the real operation is performed and the result is always a real number. Example: 16/10.0= 1.6 5
- 6. 2. Conditional Operator A ternary operator pair “?:” is available in C to construct conditional expressions of the form : exp1 ? exp2: exp3 whereas exp1, exp2 and exp3 are expressions. The operator ?: works as follows: Exp1 is evaluate first. If it is nonzero (true), then the expression exp2 is evaluated and becomes the value of the expression. If exp1 is false, exp3 is evaluated and its value becomes the value of the expression. Note: Only one of the expressions (either exp2 or exp3) is evaluated. Example: x=16 y=25; x= (a>b) ? a : b; x will be assigned the value of y. This can be achived using the if... else statement as follows: if (a>b) x=a; else x=b; 6
- 7. 3. 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 or left. Bitwise operators may not be applied to float or double. Generic Bitwise Operations One of the unique features of C language as compared to other high-level language is that it allows direct manipulation of individual bits within a word. Bit –level manipulations are used in setting a particular bit or group of bits to 1 or 0. They are also used to perform certain numerical computations faster. C supports the following operators: 3.1 Bitwise logical operators a) Bitwise AND(&) b) Bitwise OR(|) c) Bitwise exclusive OR(^) 3.2Bitwise shift operators a) Left shift b) Right shift 3.3 One’s complement operator Result of Logical Bitwise Operations op1 op2 op1&op2 Op1|op2 Op1^op2 1 1 1 1 0 1 0 0 1 1 0 1 0 1 1 0 0 0 0 0 7
- 8. 3.1 Bitwise logical operators There are three logical bitwise operators. They are: a) Bitwise AND (&) b) Bitwise OR (|) c) Bitwise exclusive OR (^) a) Bitwise AND (&) The bitwise AND operator is represented by a single ampersand (&) and is surrounded on both sides by integer expressions. The result of ANDing operation is 1 if both the bits have a value of 1; otherwise it is 0. Example: Let us consider two variables x & y whose values are 13 and 25. The binary representation of these two variables are: x-- 0000 0000 0000 1101 y-- 0000 0000 0001 1001 If we execute statement z = x & y z-- 0000 0000 0000 1001 b) Bitwise OR (|) The bitwise is represented by the symbol | (vertical bar) and is surrounding by two integer operands. The result of OR operation is 1 if at least one of the bits has a value of 1; otherwise it is zero. Example: Consider the variables x & y discussed above. x 0000 0000 0000 1101 y 0000 0000 0001 1001 x/y 0000 0000 0001 1101 c) Bitwise Exclusive Or(^) The bitwise exclusive OR is represented by the symbol ^. The result of exclusive OR is 1 if one of the bits is 1; otherwise it is 0. Example: x 0000 0000 0000 1101 y 0000 0000 0001 1001 x^y 0000 0000 0001 010 8
- 9. 3.2) Bitwise Shift Operator The shift operators are used to move bit patterns either to the left or the right. The shift operators are represented by the symbols << and >> and are used in the following form: Left shift: op<<n Right shift: op>>n Op is the integer expression that is to be shifted and n is the number of bit position to be shifted. The left-shift operation causes all the bits in the operand op to be shifted to the left by n position. The leftmost n bits in the original bit pattern will be lost and the rightmost n bit position that is vacated will be filled with 0s. Similarly, the right-shift operation causes all the bits in the operand op to be shifted to the right by the n position. The rightmost bit n will be lost. The leftmost n bit position that is vacated will be filled with zero, if the op is unsigned integer. If the variable to be shifted is signed, then the operation is machine dependent. Example: x is unsigned integer whose bit pattern is 0100 1001 1100 1011 Then, vacated position x<<13=0100 1110 0101 1000 x>>3=0000 1001 0011 1001 vacated position 3.3) One’s Complement Operation The complement operator ~ is a unary operator and inverts all the bits represented by the operand. That is, 0s becomes 1s and 1s becomes 0s. Example: x=1001 0110 1100 1011 ~x=0110 1001 0011 0100 9
- 10. 4. Relational Operators Often it is required to compare the relationship between operands and bring out a decision and program accordingly. This is when the relational operator comes into picture. C supports the following relational operators. Operator Meaning < is less than <= is less than or equal to > is greater than >= is greater than or equal to == is equal to != is not equal to It is required to compare the marks of 2 students, salary of 2 persons; we can compare those using relational operators. A simple relational expression contains only one relational operator and takes the following form: exp1 relational operator exp2 where exp1 and exp2 are expressions, which may be simple constants, variables or combination of them. Examples of relational expressions and evaluated values. 6.5 <= 25 TRUE -65 > 0 FALSE 10 < 7 + 5 TRUE Relational expressions are used in decision making statements of C language such as if, while and for statements to decide the course of action of a running program. 10
- 11. 5. Logical Operators C has the following logical operators; they compare or evaluate logical and relational expressions. Operator Meaning && Logical AND || Logical OR ! Logical NOT 5.1 Logical AND (&&) This operator is used to evaluate 2 conditions or expressions with relational operators simultaneously. If both the expressions to the left and to the right of the logical operator is true then the whole compound expression is true. Example a > b && x = = 10 The expression to the left is a > b and that on the right is x == 10 the whole expression is true only if both expressions are true i.e., if a is greater than b and x is equal to 10. 5.2 Logical OR (||) The logical OR is used to combine 2 expressions or the condition evaluates to true if any one of the 2 expressions is true. Example a < m || a < n The expression evaluates to true if any one of them is true or if both of them are true. It evaluates to true if a is less than either m or n and when a is less than both m and n. 5.3 Logical NOT (!) The logical not operator takes single expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it just reverses the value of the expression. Example !(x >= y) The NOT expression evaluates to true only if the value of x is neither greater than or equal to y. 11
- 12. 6. Assignment Operators Assignment operators are used to assign the result of an expression to a variable. The Assignment Operator evaluates an expression on the right of the expression and substitutes it to the value or variable on the left of the expression. Example: x=a+b Here the value of a + b is evaluated and substituted to the variable x. In addition; C has a set of shorthand assignment operators of the form. var oper = exp; Here var is a variable, exp is an expression and oper is a C binary arithmetic operator. The operator oper = is known as shorthand assignment operator. 7. Increment and decrement Operators The increment and decrement operators are one of the unary operators which are very useful in C language. They are extensively used in for and while loops. The syntax of the operators is given below 1. ++variable name 2. variable name++ 3. – –variable name 4. variable name– – The increment operator ++ adds the value 1 to the current value of operand and the decrement operator – – subtracts the value 1 from the current value of operand. ++variable name and variable name++ mean the same thing when they form statements independently, they behave differently when they are used in expression on the right hand side of an assignment statement. Example: x= 5; y = ++x; (prefix) In this case the value of y and x would be 6 12
- 13. Suppose if we rewrite the above statement as x= 5; y = x++; (post fix) Then the value of y will be 5 and that of m will be 6. A prefix operator first adds 1 to the operand and then the result is assigned to the variable on the left. On the other hand, a postfix operator first assigns the value to the variable on the left and then increments the operand. Note- Increment and Decrement operators are unary operators and they require variable as their operands. The precedence associatively of ++ and - - operators are the same as those of unary and unary - - . 8. Special operator C supports some special operators of interest such as comma operator, size of operator, pointer operators (& and *) and member selection operators (. and ->). 8.1) The Comma Operator The comma operator can be used to link related expressions together. Comma-linked lists of expressions are evaluated left to right and value of right most expression is the value of the combined expression. Example: value = (x = 10, y = 5, x + y); First assigns 10 to x and 5 to y and finally assigns 15 to value. Since comma has the lowest precedence in operators the parenthesis is necessary. In for loops: for (n=1, m=10, n <=m; n++,m++) Exchanging values t = x, x = y, y = t; 13
- 14. 8.2) The sizeof Operator The operator size of gives the size of the data type or variable in terms of bytes occupied in the memory. The operand may be a variable, a constant or a data type qualifier. Example m = sizeof (sum); n = sizeof (long int); k = sizeof (235L); The size of 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 variables during the execution of the program. 14
- 15. Precedence Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others. Example: The multiplication operator has higher precedence than the addition operator: x = 7 + 3 * 2; /* x is assigned 13, not 20 */ The previous statement is equivalent to the following: x = 7 + ( 3 * 2 ); Using parenthesis in an expression alters the default precedence. For example: x = (7 + 3) * 2; /* (7 + 3) is evaluated first */ Associativity Associativity relates to precedence, and resolves any ambiguity over the grouping of operators with the same precedence. In the following statement, the rules of C specify that a * b is evaluated first: y = a * b / c; In a more complicated example, associativity rules specify that b ? c : d is evaluated first in the following example: a ? b ? c : d : e; The associativity of the conditional operator is right-to-left on the line. The assignment operator also associates right-to-left; for example: int x = 0 , y = 10, z = 8; x = y = z; /* x has the value 8, not 10 */ 15
- 16. Table including Precedence and Associativity Description Operator Rank Associativity Function call () 1 Left to right Array element reference [] Unary plus + 2 Right to left Unary minus - Increment ++ Decrement -- Logical negation ! Ones complement ~ Address & Size of an object Sizeof Multiplication * 3 Left to right Division / Modulus % Addition + 4 Left to right Subtraction - Left shift << 5 Left to right Right shift >> Less than < 6 Left to right Less than equal to <= Greater than > Greater than equal to >= Equality == 7 Left to right Inequality |= Bitwise AND & 8 Left to right Bitwise XOR ^ 9 Left to right Bitwise OR | 10 Left to right Logical AND && 11 Left to right Logical OR || 12 Left to right Conditional operator ?: 13 Right to left Assignment operator = 14 Right to left *=/=%= +=-=&= ^=|= << = >>= Commas operator , 15 Left to right 16

No public clipboards found for this slide

Be the first to comment