Search:

Submit Article

# Operators and Expressions

Posted By: Mena Schmidt     Category: C Programming     Views: 21585

### Operators

• C supports rich set of operators
• =, +, -, *, &, etc
• An operator is a symbol that tells computer to perform certain math or logical manipulations
• They are used to manipulate data and variables
• They form a part of either math or logical expressions

### Categories of Operators

• Arithmetic
• Relational
• Logical
• Assignment
• Increment and Decrement
• Conditional
• Bitwise
• Special

### Arithmetic Operators

• All basic math operators provided!
• Unary and binary minus and ANSI plus
• Difference between / and %
• The integer expression
• The truncation to integer value in IE
• Real and Mixed mode arithmetic

### Relational Operators

• Taking decisions based on comparison
• When math expressions surrounds RO, they are evaluated before applying RO
 Op Meaning Op Meaning < Is less than <= Less than or equal to > Greater than >= Greater than or equal to == Equals != Not equal to

### Logical Operators

• Relational operators are combined using logical operators
• It yields value 1 or 0 like relational operator
• && is logical AND
• || is logical OR
• ! Means logical NOT
• Result is based on truth table of operators

### Assignment, Inc and Dec Ops

• =, +=, -=, *=, /= are assignment operators
• V op = exp is same as v = v op exp
• Op= here is known as shorthand assignment operator
• The ++ and - - operators
• The difference between prefix and postfix operators when used in assignment

### Conditional and Bitwise Ops

• x = (a > b) ? a : b
• Bitwise operators
 Operator Meaning & Bitwise AND | Bitwise OR ^ Bitwise Exclusive OR << Shift left >> Shift right ~ One's Complement

### Results of 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 1 0 0 0 0
• X is 13 so X is 0000 0000 0000 1101
• y is 25 so y is   0000 0000 0001 1001
• Z = x & y is     0000 0000 0000 1001 = 9

### Special Operators

• Comma (,), sizeof, pointer operators (& and *), member selection operators (. And ->)
• Value = (x=10, y=5, x+y);
• T=x,x=y,y=t;
• In for and while loops
• Sizeof(sum), sizeof (long int), sizeof (235L)
• C = ++a + b and c = a++ + b are different

### Evaluation of Expression

• Left to right unless parenthesis
• Priority of *, / and % is higher then +,-
• Two passes are needed to evaluate an exp
• Approximation for real numbers is problem
• Division by zero yields error
• Avoiding overflow and underflow errors
• Arithmetic type conversion to higher type

### Casting

• Need to force other than default conversion
• Ratio = female_no / male_no
• Examples
• A = (int) 21.3 / int (4.5)
• B = (double) sum / n
• Y = (int) (a+b)
• P = cos ((double(x))
• Priority and associativity
Share:

Didn't find what you were looking for? Find more on Operators and Expressions Or get search suggestion and latest updates.

 Mena Schmidt author of Operators and Expressions is from Frankfurt, Germany. View All Articles

 Please enter your CommentComment should be atleast 30 Characters.Please put code inside [Code] your code [/Code]. No Comment Found, Be the First to post comment!