Lesson5 - Language Fundamentals4
Lesson5 - Language Fundamentals4
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.
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
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); } }
and
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
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
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)
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
http://pradeeppearlz.blogspot.com/ Expressions
Operators and
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
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
http://pradeeppearlz.blogspot.com/ Expressions
12
Operators and
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
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); }
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
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)
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.
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 * / % + << <= == & ^ | && || ? : = *= /= %= += -= &= |= ^= <<= >>= , >> > !=
http://pradeeppearlz.blogspot.com/ Expressions
22
Operators and
http://pradeeppearlz.blogspot.com/ Expressions
23
Operators and
5.3.
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
O U T P U T
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