Operators |
|
Assignment operator |
|
Assignment operators are used to assign the result of an expression to a variable. The most commonly used assignment operator is (=). |
|
eg: i=i+10; |
|
i=i+10 is an assignment expression which assigns the value of i+10 to i. |
|
Expression like i=i+10, i=i-5, i=i*2 etc. can be rewritten using shorthand assignment operators. |
|
e.g.: i=i+5 is equivalent to i+=5 |
i=i*(y+1) is equivalent to i*=(y+1) |
|
Operator Precedence: |
|
While executing an arithmetic statement which has two or more operators, we may have some problems about how exactly does it get executed. |
|
To answer these questions satisfactorily we have to understand the precedence of operators. |
|
Precedence defines the sequence in which operators are to be applied on the operands. Operators of same precedence are evaluated from left to right or right to left, depending upon the level. |
|
This is known as associativity property of an operator. |
|
Summary of precedence of associativity is given below: |
|
Description | Operator | Associativity |
Function Expression | ( ) | Left to Right |
Array Expression | [ ] | Left to Right |
Structure Operator | -> | Left to Right |
Structure Operator | . | Left to Right |
|
|
Description | Operator | Associativity |
Unary minus | - | Right to Left |
Increment/Decrement | ++/-- | Right to Left |
One's Compliment | ~ | Right to Left |
Negation | ! | Right to Left |
Address of | & | Right to Left |
Value at address | * | Right to Left |
Type cast | (type) | Right to Left |
Size in bytes | sizeof | Right to Left |
|
|
Description | Operator | Associativity |
Multiplication | * | Left to Right |
Division | / | Left to Right |
Modulus | % | Left to Right |
Addition | + | Left to Right |
Subtraction | - | Left to Right |
|
|
Description | Operator | Associativity |
Left Shift | << | Left to Right |
Right Shift | >> | Left to Right |
|
|
Description | Operator | Associativity |
Less Than | < | Left to Right |
Less Than Equal to | <= | Left to Right |
Greater than | > | Left to Right |
Greater than Equal to | >= | Left to Right |
|
|
Description | Operator | Associavity |
Equal to | == | Left to Right |
Not equal to | != | Left to Right |
|
|
Description | Operator | Associavity |
Bitwise AND | & | Left to Right |
Bitwise XOR | ^ | Left to Right |
|
|
Description | Operator | Associavity |
Bitwise OR | ^ | Left to Right |
|
|
Description | Operator | Associavity |
Logical AND | && | Left to Right |
Logical OR | || | Left to Right |
|
|
Description | Operator | Associavity |
Conditional | ?: | Right to Left |
|
|
Description | Operator | Associavity |
Assignment | = | Right to Left |
Assignment | *= /= %= | Right to Left |
Assignment | += -= &= | Right to Left |
Assignment | ^= |= | Right to Left |
Assignment | <<= >>= | Right to Left |
|
|
Description | Operator | Associavity |
Comma | , | Right to Left |
|
|
|
|
|
Operators |
|
Type modifier |
|
The basic data types may have modifier preceding them to indicate special properties of the object being declared. |
|
These modifiers change the meaning of the basic data types to suit the specific needs. |
|
These modifiers are unsigned, signed, long and short. It is also possible to give these modifiers in combination, e.g., unsigned long int. |
eg:- |
Modifier for char Data Type |
|
main() |
{ |
char ch=291; |
printf("%d\t%c\n",ch,ch); |
} |
|
output:- 35 |
|
Here ch has been defined as a char ,and char cannot take a value bigger than +128.That is why assigned value of ch is 291 and is considered to be 35 (291-128-128). |
|
Data type | Range | Bytes occupied | Format |
signed char | -128 to +127 | 1 | %c |
unsigned char | 0 to 255 | 1 | %c |
short signed int | -32768 to 32767 | 2 | %d |
short unsigned int | 0 to 65535 | 2 | %u |
long signed int | -2147483648 to +2147483647 | 4 | %ld |
long unsigned int | 0 to 4294967295 | 4 | %lu |
float | -3.4e38 to 3.4e38 | 4 | %f |
double | -1.7e4932 to +1.7e308 | 8 | %lf |
long double | -1.7e4932 to 1.7e4932 | 10 | %lf |
|
|
|
|
|
Expressions |
|
Evaluation of expression |
|
An expression is a combination of variables, constants and operators arranged according to the syntax of the language. |
|
C can handle any complex expression with ease. |
|
It is little bit different from algebraic expression. |
|
Algebraic Expressions | C Expressions |
axb-cxd | a*b-c*d |
(m+n)(a+b) | (m+n)*(a+b) |
|
|
Evaluation of expression: |
|
We can evaluate an expression by using assignment statement. As |
|
Variable = Expression. |
|
e.g. : |
|
Temp = ((f * cos(x)/sin (y))+(g * sin(x)/cos(y))) |
|
All relevant variables must be assigned values before the evaluation of the expression. |
|
Type conversion in expression: |
|
To effectively develop C programs, it would be necessary for you to understand the rules that are used for the implicit conversion of operands. |
|
If an expression contains an operation between an int and a float, the int would be automatically promoted to a float before carrying out of operation. |
|
|
|
|
Expressions |
|
Automatic type conversion |
|
If the operands are of different types the lower type is automatically converted to thehigher type before the operation proceeds |
The result is of the higher type |
|
Given below is the sequence of rules that are applied by evaluating expressions. |
|
Operator 1 | Operator 2 | Result |
Long Double | any | Long Double |
Double | any | Double |
Float | any | Float |
Unsigned Long Int | any | Unsigned Long In |
Long Int | any | Long Int |
Unsigned Int | any | Unsigned Int |
|
|
Final result of an expression to the type of the variable on the left of the assignment signed before assigning the value to it. |
|
However, the following changes are introduced during the final assignment: |
|
1. Float to Int causes truncation of the fractional part. |
|
2. Double to float causes rounding of digits. |
|
3. Long int to int causes dropping of the excess higher order bits |
|
Type Casting: |
|
Casting a value is a forcing a type conversion in a way that is different from the automatic conversion and this process is called type cast. |
This should be clear from the following examples: |
|
An example of automatic conversion: |
|
 |
|
Output of automatic conversion: |
|
 |
|
The answer turns out to be 1.000000 and not 1.5 this is because, 6 and 4 are both integers, and hence 6/4 yields an integer, 1. |
|
This 1 when stored in a is converted to 1.000000. But what if you don't want the question to be truncated. One solution is to make either x or y as a float. |
|
The general form of casting is: |
|
(type_desired) expression; |
where type_desired : standard C data types and |
expression: constant, variables or an expression. |
|
An example of type casting: |
|
 |
|
Output of type casting example: |
|
 |
|
Type Definition using typedef |
|
C allows us to create data types via the typedef statement. |
|
The format of typedef statement is: |
|
typedef data type new_type_name; |
|
Example: |
|
typedef int units; |
units bat1,bat2; /*this statement is equivalent to int bat1,bat2*/ |
|
|
|
|
|
|
|
|
No comments:
Post a Comment