Punctuators
[] Brackets () Parentheses
{} Braces , Comma
… Ellipses * Asterisk
= Equal sign # Pound sign
Arithmetic Operators
+ Unary plus
- Unary minus
+ Addition
- Subtraction
* Multiplication
/ Division
Logical Operators
Logical Operators use True / False properties of expressions to return a true or false value. In C the true result of an expression is non zero. When a non zero value is subjected to a logical operation, the value is converted to 1. False values are always 0.
&& Logical AND
|| Logical OR
! Unary NOT
Increment and Decrement Operators
C includes several unique operators. Two of them are ++ (increment) and – (decrement) operators.
They can be placed before (prefix) or after (postfix) a variable.
++ Increment
-- Decrement
Eg:-
/* Logical Operators */
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
printf("Enter an integer:");
scanf("%d",&a);
b = (a>0);
printf("a = %d\n",a);
printf("b = %d\n",b);
c = a||b;
printf("c = %d\n",c);
c = a && b;
printf("c = %d\n",c);
}
/* Prefix */
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
a = 2;
printf("a = %d\n",a);
printf("++a = %d\n",++a);
printf("a = %d\n",a);
printf("--a = %d\n",--a);
printf("a = %d\n",a);
}
Comma [.] Operator
Expressions can be separated by commas. In C each comma-separated expression will be evaluated and the value returned from the group is the value of the right most expression.
/* Comma Operator */
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
a = 2;
printf("%d\n",a);
printf("%d%d\n",a,++a);
printf("%d%d%d\n",a,++a,a);
printf("%d%d%d\n",a,--a,a);
printf("%d%d\n",a,a++);
printf("%d%d\n",a,a--);
printf("%d%d%d%d\n",a,a++,a--,a);
}
Assignment Operators
C has a number of assignment Operators. Each assignment statement itself returns a value. The value that
it returns is the value that is assigned to the variable on the left side of the assignment. So, in C you will be
able to use an expression such as x = y = z = 0; The assignment operator has right to left associativity.
= Assignment += Addition assignment
-= Subtraction assignment *= Multiplication assignment
/= Division assignment %= Modulus assignment
<<= Shift left assignment >>= Shift right assignment
&= Bitwise AND assignment != Bitwise OR assignment
^= Bitwise XOR assignment
/* Assignment Operators */
#include<stdio.h>
#include<conio.h>
int main()
{
int a, b;
unsigned int p, q;
float c;
a = b = 2;
a += 3;
printf("a =%d\n",a);
b *= a;
printf("b =%d\n",b);
p = q = 61234;
printf("p = %u\n",p);
c = p/= 100;
printf("c =%.2f\n",c);
q %= 10;
printf("q = %u\n",q);
}
Conditional Operator [? : ]
The conditional operator tests an expression and returns the result of one of two other expressions depending on the true / false value of the first.
Syntax: <Expression1> ? <Expression 2>:<Expression 3>
The evaluation tests the first expression. If that value is true, the resulting value is that of the second expression; otherwise the resulting value is the third expression
/* ? : Operator*/
#include<stdio.h>
#include<conio.h>
int main()
{
float u,p,q,d;
u=200.00;
printf("Quantity purchase:");
scanf("%f",&q);
d=q>=50 ? u*20.0/100 :0;
p=q*(u-d);
printf("Final price =%.2f",p);
}
Bitwise Shift Operators
E1 << E2 - Left shift E1 - Shift expression
E1 >> E2 - Right shift E2 - Additive expression
The operands must be of Integral type and the operation is invalid
a) if the addtive expression is negative
b) If addtive expression is greater than or equal to the width of the shift expression.
Bitwise Operators Logical
The bitwise operators work on the principles of Boolean Logic – a branch of mathematics that assumes two state (True or False) variables.
/* Bitwise Logical Operators */
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
a=65;
b=16;
c= a & b;
printf("c=%d \n",c);
c= a | b;
printf("c=%d \n",c);
c=~a;
printf("c=%d\n", c);
c=a^b;
printf("c=%d\n", c);
}
No comments:
Post a Comment