Increment & Decrement Operators (++, - ) : Example (X Y && X Z) (!FLAG && T 10)
Increment & Decrement Operators (++, - ) : Example (X Y && X Z) (!FLAG && T 10)
Operators
There are four main classes of operators
1) Arithmetic 2) relational 3) logical 4) bitwise 5) other special
operators
1. Arithmetic operator : +,-,*,/,%
2. Assignment operators ( = )
variable = expression ;
(Multi assignment also possible such as x=y=z=20;)
3. Increment & Decrement operators ( ++, -- )
4. additional assignment operators ( +=, -=, *=, /=, %= )
Example meaning
a+=10 a=a+10
b-=c b=b-c
x*=10 x=x*10
5. unary operator - C include a class of operator that act upon a single operand
to produce a new value. Such a operators are known as unary operators
Example
-735 , -x - (b * b 4 * a * c)
priority
high ++, --, -(unary operator)
*, /, %
Low +, -
Bitwise operator are used to testing, setting (or) shifting actual bits in a byte (or)
word, which corresponds to char and integer type data and variants.
You cannot use bitwise operators on float, double, long double, void etc.
Bitwise AND, OR, NOT are governed by the same truth table as their logical
equivalents, except that they work bit by bit
XOR
P Q P^Q
0 0 0
1 0 1
0 1 1
1 1 1
the bit-shift operators <<, >> move all bits in a variable to the right (or) left
as specified.
Syntax : (1)shift-right
variable >> number of bit positions;
(2)shift-left
variable << number of bit positions;
Control Statements
enable us to specify the flow of program control
Two types of control statements
1. branching/ selection statement.
2. Looping statements
1. BRANCHING STATEMENTS
1. If statement
Simple if
Cascaded if (or) if else ladder
Nested if
2. switch statement
int a,b,c;
char opt,action[20];
opt=getchar();
scanf(%d %d,&a,&b);
switch(opt)
{
case + : c=a+b;
strcpy(action, sum );
break;
case - : c=a-b;
strcpy(action, difference );
break;
case * : c=a-b;
strcpy(action, product );
break;
default: strcpy(action, invalid action);
}
printf( the %s of %d and %d is %d,action,a,b,c);
3. Conditional operator (? :)
LOOP STATEMENTS
for, while,do
Break
for(i=1;i<5;i++)
{
for(j=1;j<5;i++)
{
if(j>i)break;
printf(\t%d,j);
}
printf(\n);
}
Continue:
for(i=1;i<5;i++)
{
for(j=1;j<5;i++)
{
if(i == j)continue;
printf(\t%d,j);
}
printf(\n);
}
exit() function : immediate termination of the entire program
void exit(int return_code);
1. Write a C Program to add two numbers without using addition operator
#include<stdio.h>
int main(){
int a,b;
int sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
//sum = a - (-b);
sum = a - ~b -1;
printf("Sum of two integers: %d",sum);
return 0;
}
Twist in bitwise complement operator in C Programming
For any integer n, bitwise complement of n will be -(n+1). To understand this, you
should have the knowledge of 2's complement