Vous êtes sur la page 1sur 27

Language Fundamentals-IV

(Operators and Expressions)

First, master the fundamentals. Larry Bird

Chapter Outline Chapter Outline Introduction. Introduction. Types of operators Types of operators Assignment operator Assignment operator Binary arithmetic operators. Binary arithmetic operators. Unary arithmetic operators. Unary arithmetic operators. Relational operators. Relational operators. Logical operators. Logical operators. Conditional operator Conditional operator Bitwise operators. Bitwise operators. Special operators. Special operators. Operators precedence and Operators precedence and associativity. associativity. Evaluating an expression. Evaluating an expression. Type conversion in an Type conversion in an expression. expression. Conclusion. Conclusion.

I long to accomplish great and noble task, but it is my chief duty to accomplish small tasks as if they were great and noble. Helen Keller

Success is neither magical nor mysterious. Success is the natural consequence of consistently applying the basic fundamentals. Jim Rohn

5.1. Introduction
Operator: Operator is a special character (or symbol) that performs an operation using operands. Operand: Operand is a variable or constant that acts as input for an operation to be performed. Expression: Expression is a collection of operators and operands that, when evaluated, returns a value. Ex: a+b is an expression in which the operator is + and the operands are a and b. Unary operator: Unary operator is an operator that performs operation using only one operand at a time. e.g., ++ (increment) is a unary operator. Binary operator: Binary operator is an operator that performs operation using two operands at a time. e.g., > (greater than) is a binary operator. Ternary operator: Ternary operator is an operator that performs operation using three operands at a time. e.g., ?:(Conditional operator) is ternary operator.

5.2. Types of operators


C has a rich set of operators. Based on the type of operation performed, operators are broadly classified as follows: 1. Assignment operator. 2. Binary arithmetic operators. 3. Unary arithmetic operators. 4. Relational operators. 5. Logical operators. 6. Bitwise operators. 7. Special operators

5.2.1. Assignment operator


Assignment operator is used to assign (or give) a value to a variable or constant. The assignment operator is = (can be read as assigns to). The assignment operator can be used to form an expression as follows: Operand_1=Operand_2; Here, Operand_1 (also called as LValue) can be a variable or constant. Operand_2 (also called as RValue) can also be a variable or constant or an expression. When assignment operator is used, the RValue gets assigned to LValue. Ex: 1) a=10; /* value 10 gets assigned to variable a*/ 2) const float PI=3.1412f; /*value 3.1412 gets assigned to constant PI*/ 3) c=a+b; /* a+b is calculated and result gets assigned to c*/

http://pradeeppearlz.blogspot.com/ Expressions

Operators and

4) a=b=c; /* multiple assignment: value of c gets assigned to b, which in turn gets assigned to a*/ 5) a+b=10; /* invalid assignment statement*/ Program #1 Write a program to interchange (or swap) two values by using temporary variable. /* A program to interchange two numbers using temporary variable*/ #include<stdio.h> main() { long int a,b,temp; printf(\n Enter any two numbers:); scanf(%ld%ld,&a,&b); printf(\n Before swap a=%ld\tb=%ld,a,b); temp=a; /* First, assign value of a to temp*/ a=b; /* Later, assign value of b to a*/ b=temp; /* Finally, assign value of temp to b*/ printf(\n After swap a=%ld\tb=%ld,a,b); } Output: Enter any two numbers: 2943 34646 Before swap a=2943 b=34646 After swap a=34646 b=2943

5.2.2. Binary arithmetic operators


C language provides operators for basic arithmetic operations such as addition, subtraction, multiplication and division. There are five binary arithmetic operators and these operators act on two operands at a time. These are: Operator + * / % Returns Returns Returns Returns Returns the the the the the Operation sum of values of two operands. difference of values of two operands. product of values of two operands. quotient after division of two operands. remainder after division of two operands.

These operators can be used to form an arithmetic expression as follows: Operand_1 Binary_arithmetic_operator Operand_2 Ex: a+b, a-b, a*b, a/b, a%b The above expressions are called as binary arithmetic expressions. The operands may be of any one of data types. If both operands are integer values , then that expression is called as integer arithmetic expression. If both are floating-point values, then that expression is called as realarithmetic expression. If both are of different values, then that expression is called as mixed-mode arithmetic expression.

http://pradeeppearlz.blogspot.com/ Expressions

Operators and

When binary operations are applied to numeric arguments of different types, numeric promotion is performed before the operation takes place. The numeric promotion consists of converting the values of the operands to a common type. The rules for this type conversion are as follows: If one of the operands is a long double, then the other is converted a long double. Otherwise, if one of the operands is a double, then the other is converted to a double. Otherwise, if one of the operands is a float, then the other is converted to a float. Otherwise, if one of the operands is a long, then the other is converted to a long. Otherwise, if one of the operands is unsigned int, the other is converted to unsigned to int. Otherwise, both operands are converted to int values. Ex: 2.3 (a double value) +3 (an int value) =5.3 (a double result) The integer value in this expression gets promoted to double; addition is performed and the result is of type double is returned. Program #2 Write a program to perform all arithmetic operations /*program to perform all arithmetic operations*/ #include<stdio.h> main() { int a,b; printf(\nEnter any two numbers: ); scanf(%d%d,&a,&b); printf(\nAddition=%d,a+b); printf(\nSubtraction=%d,a-b); printf(\nMultiplication=%d,a*b); printf(\nQuotient=%d,a/b); printf(\nRemainder=%d,a%b); } Output: Enter any two numbers: 5 6 Addition=11 Subtraction=-1 Multiplication=30 Quotient=0 Remainder=5 Note: 1. The operator % does not work on the operands of data types: float or double or long double. 2. An unusual aspect of programming in C is the arithmetic which can be done with characters. Consider the following: char ch1,ch2; ch1=a; /*assign a to ch1*/ ch2=ch1+1; /*assign b to ch2*/

http://pradeeppearlz.blogspot.com/ Expressions

Operators and

The second assignment increases the ASCII (American Standard Code for Information Interchange) value for the given character, a numeric value, by 1 and sets the result to ch2. Thus, ch2 contains b. 3. x%y is always equals to (x-(x/y)*y). 4. The second operand of operators % and / must be non-zero. Program #3 Write a program to convert an alphabet from lowercase to uppercase /*program to convert the lowercase to uppercase*/ #include<stdio.h> main() { char ch; printf(\n Enter any lowercase alphabet:\n); scanf(%c,&ch); /* or ch=getchar();*/ ch=ch-32; printf(\n Uppercase alphabet:%c,ch); } Output: Enter any lowercase alphabet: m Uppercase alphabet:M 1)#include<stdio.h> main() { int x; x=-3+4-7*8/5%10; printf(x=%d,x); } 2)#include<stdio.h> main() { float a=1.5; int b=3; a=b/2+b*8/b-b+a/3; printf(a=%f,a); } 3)#include<stdio.h> main() { int x; x=-3*-4%-6/-5; printf(x=%d,x); } 4)#include<stdio.h> main() { printf(%d,4/3); printf(%d,4/-3); printf(%d,-4/3); printf(%d,-4/-3); } 5)#include<stdio.h> main() { printf(%d,4%3); printf(%d,4%-3); printf(%d,-4%3); printf(%d,-4%-3); } 6)#include<stdio.h> main() { float a=5,b=2; int c; c=a%b; printf(%d,c); } 7)#include<stdio.h> main() { float a=4; int i=2; printf(%f%d,i/a,i/a); printf(%d%f,i/a,i/a); } 8)#include<stdio.h> main() { int x; x=4%5+6%5; printf(x=%d,x); } 9)#include<stdio.h> main() { int x; x=3*4%5; printf(x=%d,x); }

OBSERVABLE

O U T P U T

7)#include<stdio.h> 9)#include<stdio.h> main() main() { { float a=4; http://pradeeppearlz.blogspot.com/ int q=2,d=3,st; int i=2; Expressions st=q*d/4-12/12+12/3*16/d; printf(%f%d,i/a,i/a); printf(st=%d,st); printf(%d%f,i/a,i/a); } }

8)#include<stdio.h> main() { Operators float a; a=4/2; printf(%f%f,a,4/2); }

and

5.2.3. Unary arithmetic operators


Unary Arithmetic operators perform the arithmetic operations such as increment & decrement and change of sign on only one operand. There are 4 unary arithmetic operators: Operator Unary + (Positive) Unary - (Negative) ++ (Increment) - - (Decrement) Operation Keeps the sign of value unchanged. Changes the sign of value. Adds 1 to value of operand and assigns the result to that operand only. Subtracts 1 from the value of operand and assigns the result to that operand only. These operators can be used to form an expression as follows: Operand_1 Unary_arithmetic_operator (or) Unary_arithmetic_operator Operand_1 Both sign operators should be preceded by the operand where as the increment and decrement operators can be preceded or followed by the operand. Ex: int a=10; 1) +a /*keeps the sign of 10 as positive only*/ 2) a /* changes the sign of 10 to negative*/ 3) ++a or a++ /* equivalent to a=a+1; hence, a holds 11*/ 4) -a or a-- /* equivalent to a=a-1; hence, a holds 9*/ Program #4 Write a program to demonstrate positive and negative signs /*program to demonstrate positive and negative signs*/ #include<stdio.h> main() { int num; printf(\n Enter any number:); scanf(%d,&num); printf(\n Positive number=%d,+num); printf(\n Negative number=%d,-num); printf(\n Positive number=%d,+(+num)); printf(\n Negative number=%d,+(-num)); printf(\n Positive number=%d,-(-num)); printf(\n Negative number=%d,-(+num)); } Output: Enter any number: 123 Positive number=123 Negative number=-123 Positive number=123 Negative number=-123 Positive number=123 Negative number=-123

http://pradeeppearlz.blogspot.com/ Expressions

Operators and

Few words about increment and decrement operators: If ++ is used before the operand, then this increment operator is called as pre-increment operator. If ++ is used after the operand, then this increment operator is called as post-increment operator. The same treatment will be given to the --. The increment / decrement operator works as a simple increment / decrement by one, if it is not used as a part of an expression. In that case, there is no difference between operations of pre or post increment or decrement operators. Ex: int a=10; a++; printf(a=%d,a); Output: a=11 Ex: int a=10; ++a; printf(a=%d,a); Output: a=11

However, when these operators are used as a part of an expression then the difference can be noticed: The pre-increment operator first increments the value of operand and then returns that incremented value. The post-increment operator first returns the existing value of operand and then increments the value of the operand. The pre-decrement operator first decrements the value of operand and then returns that decremented value. The post-decrement operator first returns the existing value of operand and then decrements the value of the operand. Ex: int a=10; x=50+a++; printf(x=%d,a=%d,x,a); Output: x=60,a=11 Ex: int a=10; x=50+(++a); printf(x=%d,a=%d,x,a); Ouput: x=61,a=10

http://pradeeppearlz.blogspot.com/ Expressions

Operators and

1)#include<stdio.h> main() { int x=5; x++; printf(x=%d,x); ++x; printf(x=%d,x);


OBSERVABLE

4)#include<stdio.h> main() { int x=3,z; z=x---111; printf(x=%d z=%d,x,z); } 5)#include<stdio.h> main() { int x=3,z; z=x--- -1; printf(x=%d z=%d,x,z); } 6)#include<stdio.h> main() { int x=3,z; z=x----1; printf(x=%d z=%d,x,z); } 11)#include<stdio.h> main() { int x=4,y=3,z; z=x-- -y; printf(x=%dy=%dz= %d,x,y,z); }

7)#include<stdio.h> main() { int x=3,z; z=x++ + x++; printf(x=%d z= %d,x,z); } 8)#include<stdio.h> main() { int x=3,z; z=x++ + ++x; printf(x=%d z= %d,x,z); } 9)#include<stdio.h> main() { int x=3,z; z=x/++x; printf(x=%d z= %d,x,z); } 12)#include<stdio.h> main() { int a,b; a=-3 - -3; b=-3 - -(-3); printf(%d%d,a,b); }

} 2)#include<stdio.h> main() { int x=3,z; z=x++ +10; printf(x=%d z=%d,x,z); } 3)#include<stdio.h> main() { int x=3,z; z=++x +10; printf(x=%d z=%d,x,z); } 10)#include<stdio.h> main() { int i=3,j; j=++i*++i*++i; printf(i=%d j=%d,i,j); }

O U T P U T

5.2.4. Relational operators


The relational operators are used to compare two operands. There are six relational operators available. All these operators are binary operators. These are used to form an expression that is either true or false. The following table lists all the relational operators. Operator == (is equal to) != (is not equal to) > (is greater than) >=(is greater than or equal to) < (is lesser than) <= (is lesser than or equals to) A relational expression is of the following form: Operation Checks whether Checks whether Checks whether Checks whether first first first first operand operand operand operand is is is is equal to second operand or not. not equal to second operand or not. greater than the second operand. greater or equals to second operand.

Checks whether first operand is lesser than the second operand. Checks whether first operand is lesser or equals to the second operand.

http://pradeeppearlz.blogspot.com/ Expressions

Operators and

Operand_1 relational_operator Operand_2 Where operand_1 and Operand_2 are variables or constants or arithmetic expressions. Ex: int i=7; Expression f>5 (i+f)<=10 c==119 c!=p c>=10*(i+f) Value 1 (true) 0 (false) 1 (true) 1 (true) 0 (false)

float f=5.5; char c=w;

Program #4 Write a program to demonstrate relational operators /*program to demonstrate relational operators*/ #include<stdio.h> main() { int num1,num2; num1=20; num2=30; printf(\n Is equal to condition=%d,(num1==num2)); printf(\n Is not equal to condition=%d,(num1!=num2)); printf(\n Is greater than condition=%d,(num1>num2)); printf(\n Is greater than or equal to condition=%d,(num1>=num2)); printf(\n Is lesser than condition=%d,(num1<num2)); printf(\n Is lesser than or equal to condition=%d,(num1<=num2)); } Output: Is equal to condition=0 Is not equal to condition=1 Is greater than condition=0 Is greater than or equal to condition=0 Is lesser than condition=1 Is lesser than or equal to condition=1

5.2.5. Logical operators (or) Compound relational operators


The logical operators are used to combine the relational expressions and evaluate them. There are three logical operators. These work on the values of operands that are either true or false. Both Logical AND and Logical OR operators are binary operators whereas Logical NOT is only unary operator. The following table lists them:

Operator && (Logical AND)

Operation Performs Logical AND operation on two operands.

http://pradeeppearlz.blogspot.com/ Expressions

Operators and

|| (Logical OR) ! (Logical NOT)

Performs Logical inclusive OR operation on two operands. Reverses the value of operand.

http://pradeeppearlz.blogspot.com/ Expressions

10

Operators and

The logical operators are used to form logical expressions as shown below: 1) Logical AND expression: Expression_1 && Expression_2 Where Expression_1 and Expression_2 are any expression those return either true or false. When logical AND operator is encountered, first Expression_1 is tested. It returns either true or false. Secondly, the expression_2 is tested. It returns either true or false. Then both of these expressions values are combined to give the value of logical expression as given below: Expression_1 1 1 0 0 Ex: int a=10,b=30,c; c=((a>b)&&(a!=b)); expression using &&. 2) Logical OR expression: Expression_1 || Expression_2 Where Expression_1 and Expression_2 are any expression those return either true or false. When logical AND operator is encountered, first Expression_1 is tested. It returns either true or false. Secondly, the expression_2 is tested. It returns either true or false. Then both of these expressions values are combined to give the value of logical expression as given below: Expression_1 1 1 0 0 Ex: int a=10,b=30,c; c=((a>b)||(a!=b)); expression using ||. 3) Logical NOT expression: !(Expression_1) where Expression_1 is any expression that returns either true or false. When logical NOT operator is encountered, first the Expression_1 is tested. It returns either true or false. Upon the value of this Expression_1, Logical NOT returns the value given below: Expression_1 1 !(Expression_1) 0 Expression_2 1 0 1 0 Expression_1 || Expression_2 1 1 1 0 Expression_2 1 0 1 0 Expression_1 && Expression_2 1 0 0 0

Ans: c=1

Note: If the left operand yields false value, the right operand is not evaluated by a compiler in a logical

Ans: c=1

Note: If the left operand yields true value, the right operand is not evaluated by a compiler in a logical

http://pradeeppearlz.blogspot.com/ Expressions

11

Operators and

0 Ex: int a=20,b=30,c=40,d; d=!(a>b&&b>c); ans: d=1

1)#include<stdio.h> main() { int x,y,z; x=y=z=1; z=++x||++y&&++z; printf(%d %d %d,x,y,z); } 2)#include<stdio.h> main() { int x,y,z; x=y=z=1; z=++x&&++y&&++z; printf(%d %d %d,x,y,z); } 3)#include<stdio.h> main() { int x,y,z; x=y=z=1; z=++x&&++y||++z; printf(%d %d %d,x,y,z); } 10)#include<stdio.h> main() { int x=11,y=6,z; z=x==5||y!=4; printf(z=%d,z); }

4)#include<stdio.h> main() { int x,y,z; x=y=z=-1; z=++x||++y&&++z; printf(%d %d %d,x,y,z); } 5)#include<stdio.h> main() { int x,y,z; x=y=z=-1; z=++x&&++y&&++z; printf(%d %d %d,x,y,z); } 6)#include<stdio.h> main() { int x,y,z; x=y=z=-1; z=++x&&++y||++z; printf(%d %d %d,x,y,z); } 11)#include<stdio.h> main() { int x=10,y=-20; x=!x; y=!y; printf(%d %d,x,y); }

7)#include<stdio.h> main() { int x=10,y=5,p,q; p=x>9; q=x>3&&y!=3; printf(%d %d,p,q); } 8)#include<stdio.h> main() { int a=30,b=40,x; x=(a!=10)&&(b=50); printf(x=%d,x); } 9)#include<stdio.h> main() { int a=100,b=200,c; c=(a==100||b>200); printf(c=%d,c); }

OBSERVABLE

O U T P U T

12)#include<stdio.h> main() { int x=0,y=1; y=!x; x=!y; printf(%d %d,x,y); }

http://pradeeppearlz.blogspot.com/ Expressions

12

Operators and

5.2.6. Conditional operator


The conditional operator consists of two symbols: the question mark (?) and the colon (:). This is the only operator in C that acts on three operands at a time. Hence, this operator is also called as ternary operator or trinary operator. This operator acts in the same way as the ifelse statement acts. Conditional operator can be used as follows: Condition? Expression_1: Expression2; In this syntax, Condition is any expression to be tested and it returns either true or false. Usually, it is either a relational expression or compound relational expression. Expression_1 and Expression_2 are any statements that are to be executed. When the conditional operator is encountered, first, the Condition is tested. It returns either true or false. If the condition is true, Expression_1 is executed. If the condition is false, Expression_2 is executed. This is shown as in the following chart:

False Condition

True

Expression_2

Expression_1

Next_statement
Program #4 Write a program to find biggest of three numbers /*program to find biggest of three numbers*/ #include<stdio.h> main() { int num1,num2,num3,big; printf(\n Enter any three numbers:); scanf(%d%d%d,&num1,&num2,&num3); big=((num1>num2)&&(num1>num3)?num1:num2>num3?num2:num3; printf(\n Biggest of three numbers=%d,big); } Output: Enter any three numbers: 190 452 43 Biggest of three numbers=452

http://pradeeppearlz.blogspot.com/ Expressions

13

Operators and

http://pradeeppearlz.blogspot.com/ Expressions

14

Operators and

OBSERVABLE

O U T P U T

1)#include<stdio.h> main() { int x=3,y=4,z=4; printf(ans=%d,z>=y&&y>=x?1:0); }

4)#include<stdio.h> main() { int k=12,n=30; k=(k>5&&n=4?100:200); printf(k=%d,k); } 5)#include<stdio.h> main() { int c=0,d=5,e=10,a; a=c>1?d>1||e>1?100:200:300; printf(a=%d,a); } 6)#include<stdio.h> main() { int a=10,b=10; printf(ans=%d,a>b?a*a:b/b); }

2)#include<stdio.h> main() { int x=3,y=4,z=4; printf(ans=%d,z>=y>=x?1:0); }

3)#include<stdio.h> main() { int i=-4,j,num=10; j=i%-3; j=(j?0:num*num); printf(j=%d,j); }

5.2.7. Bitwise operators


Bitwise operators perform operations on bits of data. These operators are used for testing, complementing or shifting bits to the right or left. Usually, bitwise operators are not useful in cases of float and double. There are six bitwise operators as shown below:

Operator & (Bitwise AND) | (Bitwise inclusive OR) ^(Bitwise exclusive OR) ~ (Ones complement) << (Left shift) >> (Right shift)

Operation Returns 1 if both corresponding bits are 1; otherwise 0. Returns 0 if both corresponding bits are 0; otherwise 1. Returns 0 if both corresponding bits are 0 or 1; otherwise 1. Returns 1, if bit is 0 and returns 0, if bit is 1. Shifts least significant bit by specified number of times. Shifts most significant bit by specified number of times.

All these operators are binary operators except ones complement operator. Ones complement operator is a unary operator.

http://pradeeppearlz.blogspot.com/ Expressions

15

Operators and

Bitwise AND (&): The operator & performs a bitwise AND between two operands. It compares each bit of the left operand with the corresponding bit of the right operand. For each bit, the result is 1, if both the compared bits are 1; otherwise, the result is 0 as shown below: Compared bits 0&0 0&1 1&0 1&1 Interview question #1 What is masking? Masking is an operation in which the desired bits of a binary number or bit pattern are set to zero. The operator & (Bitwise AND) is very useful for this purpose. To mask particular bits in a binary number, do the following: 1. Create a new number that has binary pattern with 0s in the positions that you want to mask and 1s in other positions. 2. Perform bitwise AND operation between the number that is to be masked and created number. Result 0 0 0 1

Bitwise Inclusive OR (|) The operator | performs a bitwise inclusive OR between two operands. It compares each bit of the left operand with the corresponding bit of the right operand. For each bit, the result is 0, if both the compared bits are 0; otherwise, the result is 1 as shown below: Compared bits 0|0 0|1 1|0 1|1 Result 0 1 1 1

http://pradeeppearlz.blogspot.com/ Expressions

16

Operators and

Bitwise Exclusive OR (^) The operator ^ performs a bitwise exclusive OR between two operands. It compares each bit of the left operand with the corresponding bit of the right operand. For each bit, the result is 0, if both the compared bits have the same value; otherwise, the result is 1 as shown below: Compared bits 0^0 0^1 1^0 1^1 Bitwise Right shift operator (>>) The right shift operator >> moves the bits to the right based on specified number of positions. The general form of right shift expression is: Constant >> n (or) variable >>n where n is the number of bit positions to be shifted. Ex: int a=10; x=a>>3; The right shift operator shifts the bits in the variable a thrice. Bitwise Right shift operator (<<) The left shift operator << moves the bits to the left based on specified number of positions. The general form of left shift expression is: Constant << n (or) variable <<n where n is the number of bit positions to be shifted. Ex: int a=10; x=a<<3; The left shift operator shifts the bits in the variable a thrice. Ones complement operator (~) Ones complement operator is a unary operator. It acts on one operand at a time. Ones complement operator converts all the 1-bits to 0 and all 0-bits to 1 of the binary pattern of the operand. The ones complement operator should be preceded with operand as follows: ~operand1 Where operand1 is a variable or a constant. Result 0 1 1 0

http://pradeeppearlz.blogspot.com/ Expressions

17

Operators and

Ex:

int a=10; x=~a; Interview question #2 What are uses of shift operators? To divide an integer by 2n, a right shift by n bit positions is applied. To multiply an integer by 2n, a left shift by n positions is applied. Program #5 Write a program to convert an uppercase alphabet to lowercase alphabet /*program to convert an uppercase alphabet to lowercase alphabet*/ #include<stdio.h> int main() { char alpha; printf(\n Enter any uppercase alphabet:); scanf(\n%c,&alpha); printf(\n Lower case alphabet=%c, alpha | 32); return 0; } Output: Enter any uppercase alphabet: A Lower case alphabet=a

Program #6 Write a program to swap two integers with out using temporary variable /*program to swap two integers without using temporary variable*/ #include<stdio.h> int main() { int a,b; printf(\n Enter any two numbers:); scanf(%d%d,&a,&b); printf(\n Before swap a=%d\tb=%d,a,b); a=a^b; b=a^b; a=a^b; printf(\n After swap a=%d\tb=%d,a,b); return 0; } Output: Enter any two numbers: 10 20 Before swap a=10 b=20 After swap a=20 b=10

http://pradeeppearlz.blogspot.com/ Expressions

18

Operators and

Examples:

http://pradeeppearlz.blogspot.com/ Expressions

19

Operators and

5.2.8. Special operators


1. sizeof operator. 2. comma operator. 3. short-hand operators. 4. Referencing and dereferencing operators. sizeof operator: It is an unary operator. It returns the number of bytes occupied by the operand in memory. The operand may be a variable, a constant, an expression or simply a datatype. Ex: printf(%d,sizeof(10)); /*prints 2 or 4*/ printf(%d,sizeof(int)); /*prints 2 or 4*/ int x=30; float y=45.355; printf(%d,sizeof(y)); /*prints 4*/ printf(%d,sizeof(x+y)); /*prints 4*/ Comma Operator: A comma operator can be used as a separator or a terminator in the following statements: int a,b,c; c=a,a=b,b=c; This operator can also be used to link related expressions together. A comma-linked list of expressions is evaluated from left to right and the value of right-most expression is the value of combined expression. Ex: value=(x=10,y=20,x+y); ans: value=30

Short-hand Operators: These operators are formed by combining both binary arithmetic operators and bitwise operators with assignment operator. Hence, these are also called as compound assignment operators. The short-hand operators include: +=, -=, *=, /=, %= &=, |=, ^=, <<=, >>=. All of these operators are binary operators. So, these operands should act on two operands at a time. Operand_1 short-hand operator Operand_2 Usually, Operand_1 should be a variable and Operand_2 can be a variable or a constant. When compound assignment operator is encountered, the desired operation is performed between Operand_1 and Operand_2 and the result gets stored in Operand_1. Ex: a+=2 means a=a+2 int a=20; a+=2; ans: a=22

http://pradeeppearlz.blogspot.com/ Expressions

20

Operators and

Refencing and dereferencing operators: Referencing operator (&) is the operator that returns the address of operand in the memory. Dereferencing operator (*) is the operator that returns the value at the address that is pointed by the referencing operator. Ex: int a=10; printf(\n Address of a=%u,&a); /*prints address of a*/ printf(\n Value of a=%d,*(&a)); /*prints value of a */

1)

Are the following two statements same (yes /no)

a<=20?b=30:c=20; (a<=20)?b:c=30;
OBSERVABLE

2) We want to round of x, a float to an int value. The correct way to do so would be: a) y=(int) (x+0.5); b) y=int (x+0.5); c) y=(int )x+0.5; d) y=(int)((int)x+0.5) 1)#include<stdio.h> main() { printf(%d %d %d,sizeof(3.14f),sizeof(3.14),sizeof(3.14l)); } 2)#include<stdio.h> main() { printf(%d,sizeof(4)/sizeof(2.0)); printf(%d,sizeof(2.0)/sizeof(4)); }

O U T P U T
5.2.

Operators precedence and associativity

Precedence of an operator is the priority of it in the evaluation of an expression. Associativity of an operator is its order of evaluation in an expression. The evaluation continues according to the associativity individually as well as when all the operators in the expression have the same precedence.

http://pradeeppearlz.blogspot.com/ Expressions

21

Operators and

The following table lists the operators, their precedence and associativity. Precedence 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Operands 2 1 2 2 2 2 2 2 2 2 2 2 3 2 2 < () Operators [] . -> Associativity Left-to-Right Right-to-Left Left-to-Right Left-to-Right Left-to-Right >= Left-to-Right Left-to-Right Left-to-Right Left-to-Right Left-to-Right Left-to-Right Left-to-Right Left-to-Right Right-to-Left Left-to-Right

! ~ ++ -- + - * & (type) sizeof * / % + << <= == & ^ | && || ? : = *= /= %= += -= &= |= ^= <<= >>= , >> > !=

5.3. Evaluating an expression


Evaluation of an expression is the process of calculating the result from it based on operators precedence and associativity.

http://pradeeppearlz.blogspot.com/ Expressions

22

Operators and

http://pradeeppearlz.blogspot.com/ Expressions

23

Operators and

5.3.

Type conversion in an expression

Type conversion is the process of converting a value from one data type to another. This type conversion makes the operands in an expression belong to similar data type. If casting is done from lower size type to higher size type value, then it is called as broadening conversion. E.g., char to int. If casting is done from higher size type to lower size type value, then it is called as narrowing conversion, causes some loss of value. E.g. float to int causes truncation of fractional part. float to long causes truncation of fractional part. double to float causes rounding of digits. long int to int causes dropping of the excess high order bits. int to short causes dropping of the excess high order bits.

long double double Broadening conversion float unsigned long int long int unsigned int int short char Narrowing conversion

This type conversion can be done either implicitly or explicitly. Implicit type conversion: C permits mixing of constants and variables of different data types in an expression. C automatically converts any intermediate values to the proper type so that expression can be evaluated with out loosing any significance. This automatic conversion is known as implicit type conversion. During evaluation, the lower type is automatically converted to the higher type, according to strict rules of type conversion. The rules for this type conversion are as follows: 1. If one of the operands is a long double, then the other is converted a long double. 2. Otherwise, if one of the operands is a double, then the other is converted to a double. 3. Otherwise, if one of the operands is a float, then the other is converted to a float.

http://pradeeppearlz.blogspot.com/ Expressions

24

Operators and

4. Otherwise, if one of the operands is an unsigned long, then the other is converted to an unsigned long. 5. Otherwise, if one of the operands is a long, then the other is converted to a long. 6. Otherwise, if one of the operands is unsigned int, the other is converted to unsigned int. 7. Otherwise, both operands are converted to int values. Ex: int i=10,x; float f=3.4f; double d=7.4; long int l=214L; x=l/i+i*f-d

Note: If the two operands in an assignment statement are of different data types, the right side operand is automatically converted to the data type of the left side. Ex: int a=10; float b; b=a; printf(%f,b); output: Program #4 Write a program to print ASCII value of a character /*program to print ASCII value of a character*/ #include<stdio.h> main() { char ch; int val; printf(\n Enter any character:); scanf(\n%c,&ch); val=ch; printf(\n ASCII value of \%c\ is %d,ch,val); } Output: Enter any character: http://pradeeppearlz.blogspot.com/ 25 a Expressions ASCII value of a is 97 Ex: float a=10.98; int b; b=a; printf(%d,b); output:

Operators and

OBSERVABLE

1)#include<stdio.h> main() { printf(%d %d %d,72,072,0x72); }

O U T P U T

2)#include<stdio.h> main() { printf(%d %o %x,72,72,72); }

Explicit type conversion: When we want to do type conversion forcefully, then we use cast operator. Cast operator is used to convert value of an operand from one type to another explicitly by the user. This explicit type conversion is also called as casting or coercion. The usage of cast operator is as follows:

(type_name) expression
Here, type_name is any standard data type. The expression may be a constant, variable or an expression. As this operator works on only one operand at a time, this is a unary operator. Ex: int a=10,b=25,c=32; float d; d=(a+b+c)/3; printf(%f,d); Ex: int a=10,b=25,c=32; float d; d=(float)(a+b+c)/3; printf(%f,d);

output: 22.000000 output: 22.333334 From the above example, it is clear that the coercion helps us to avoid truncation in division.

5.6. Conclusion
C supports arithmetic, relational and logical operators like other programming languages. It supports increment and decrement operators for faster execution. Bitwise operators are supported in C, which are not available in other languages. In addition to simple assignments, C also provides compound assignments or short-hand assignments. Expressions are formulated with the help of operators and operands. These are evaluated to get the desired result. These are evaluated according to the precedence levels of the operators and their associativity. In evaluating mixed-type expressions, implicit conversion rules are followed. Explicit type conversions are possible with coercion or type casting.

http://pradeeppearlz.blogspot.com/ Expressions

26

Operators and

http://pradeeppearlz.blogspot.com/ Expressions

27

Operators and

Vous aimerez peut-être aussi