Professional Documents
Culture Documents
Types of operators :
1. Arithmetic Operator
2. Relational Operator
3. Logical Operator
4. Assignment Operator
5. Increment and Decrement Operator
6. Conditional Operators
7. Bitwise Operators
8. Special Operator
# include <stdio.h>
int main ()
Operator Meaning {
int months, days;
+ Addition or printf (“Enter days \n “);
Unary Plus scanf (“%d”, & days);
months = days/30;
- Subtraction or day = days%30;
Unary Minus printf ( “ Month =%d Days =%d”, months,days );
return 0;
* Multiplication }
#include <stdio.h>
Operator Meaning int main()
{
< Is less than int a,b;
#include <stdio.h>
Example : int main()
#include <stdio.h> {
int main() int b_salary;
{ printf (“ Enter employee basic salary =“);
int a,b; scanf(“%d”, &b_salary);
printf (“ Enter value of a ,b =“);
scanf(“%d%d%d”,&a,&b); if (b_salary> 0 && b_salary <=10000)
{
if (a>50 && b <100) printf (“ Entered basic salary is correct”);
{ }
printf (“ Correct value”); else
} {
else printf (“Incorrect basic salary”);
{ }
printf (“Entered values is not correct”); }
}
} 6
PPT CREATED BY : Prof. Rajesh K. Jha
C - Programming Language
Logical Operator. ( || ) Logical Operator. ( II )
a=a*(n+1) a*=n+1
a=a/(n+1) a/=n+1
a=a%b a%=b
-- Decrement
Operator 2. When postfix ++ ( or - - ) is used with a variable in an
++num Pre Increment expression, the expression is evaluated first using the
original value of the variable and then the variable is
Operator
incremented (or decremented) by one.
int main()
{
++ Increment int a,b,c,d;
Operator
a = 15;
-- Decrement
b = 10;
Operator c = ++ a - b;
++num Pre Increment
printf ( " a = %d b=%d c= %d \n ", a, b,c);
Operator
d =b++ + a;
Conditional or
Ex:3:
#include <stdio.h>
int main() Example :
{
int a = 11, b = 20, even=0; #include <stdio.h>
even= a%2 == 0 ? printf ("Even") : printf ("Odd"); int main()
return 0; {
} int num;
Ex: 4. printf("Enter your number=");
#include<stdio.h> scanf("%d",&num);
int main() (num>0)? (printf("True")) : (printf("False"));
{ return 0;
int a=10,b=30,z; }
z=(0)?a:b;
printf ("%d",z);
return 0;
}
Note : Zero always return false value.
Non-zero always return true values.
PPT CREATED BY : Prof. Rajesh K. Jha 13
C - Programming Language
Ex:6:
#include <stdio.h>
int main()
{
int a = 10, b = 20;
(a>b) ? printf("a is greater than b that is %d > %d", a, b)
: printf("a is greater than b that is %d > %d", a, b);
return 0;
}
X ~x
Operator Meaning
~ (not) : 0 1
& Bitwise AND
7 -> 0 1 1 1 1 0
-------------------
| Bitwise OR 8 <- 1 0 0 0
~7 = 8
~ Not (Bitwise
complement Note : not bitwise operator job is to
operator) complement each bit one by one.
<< Left shift
Ex:1: Ex:2:
1 1 1
#include <stdio.h> #include <stdio.h>
int main() int main() 1 0 0
{ {
int a=7, b=4; int a, b;
printf("Output = %d", a&b); printf (" Enter two number"); 0 1 0
return 0; scanf ("%d%d",&a,&b);
} 0 0 0
printf("Output = %d", a&b);
return 0;
Bitwise And (&) Operator: }
7 -> 0 1 1 1
4 -> & 0 1 0 0 Output :
-------------------
4 <- 0 1 0 0
Output : 7 & 4 = 4
PPT CREATED BY : Prof. Rajesh K. Jha 17
C - Programming Language
Ex:1: Ex:2:
Output : 7|4 = 7
Ex:1: Ex:2:
#include <stdio.h>
int main()
{ #include <stdio.h>
int a = 12, b = 25; int main()
{
printf("Output = %d", a^b); int a, b;
return 0; printf (" Enter two number=");
} scanf ("%d%d",&a,&b);
Ex:1: Ex:2: X ~x
0 1
35 = 00100011 (In Binary) #include <stdio.h>
1 0
int main()
{
Bitwise complement Operation of 35
int a;
~ 00100011 printf (" Enter value of a=“);
________ scanf ("%d",&a);
11011100 = 220 (In decimal)
printf("Output = %d",~b);
return 0;
}
Output :
0000 0011
0000 0110
Output : 6. formula follow : [3 * 21]
, (comma operator) :
The comma operator can be used to link the related sizeof operator :
expressions together. A comma-linked list of The sizeof is a compile time operator and, when
expressions are evaluated left to right and the value of used with an operand, it returns the numbers of bytes
right-most expression is the value of the combined the operand occupies.
expression.
value = (x=10, y=5, x+y);
The operand may be a variable, a constant or a data
Note : Comma operator has the lowest precedence of type qualifier.
all operators. That is the reason parentheses
are necessary. Ex:
m=sizeof (sum);
Below topic will be discuss in the chapter of pointer n= sizeof (long int);
and structure.
( & and * ) pointer operator : Pointer
(. And - > ) member selection operator : Structure
#include <stdio.h>
int main() { Size of variable a : 4
int a = 16; Size of int data type : 4
printf("Size of variable a : %d\n",sizeof(a)); Size of char data type : 1
printf("Size of int data type : %d\n",sizeof(int)); Size of float data type : 4
printf("Size of char data type : %d\n",sizeof(char)); Size of double data type :8
printf("Size of float data type : %d\n",sizeof(float));
printf("Size of double data type : %d\n",sizeof(double));
return 0;
}
evaluated from left to right using the rules of precedence of introducing parentheses into an expression. Consider the
operators. There are two distinct priority levels of arithmetic same expression with parentheses as shown below :
operators in C.
9 – 12/3 (3+3) * ( 2 -1 )
1. High Priority : * / %
2. Low priority : + - Whenever parentheses are used, the expressions within
parentheses assume highest priority. It two or more sets of
Ex:1: x = a – b /3 + c * 2 - 1 parentheses appear one after another as shown above, the
expression contained in the left most set is evaluated first
Statement becomes and the right-most in the last.
x = 9 – 12 / 3 + 3 * 2 -1 First pass
Step 1: 9-12/6 * (2-1)
Solution : Step 2: 9 -12/6 * 1
First Pass
Second pass
step 1 : x = 9 - 4 + 3 * 2 – 1
step 2 : x = 9 – 4 + 6 – 1 Step 3: 9 - 2 * 1
Step 4: 9 - 2
Second Pass Third pass
step 3 : x = 5 + 6 - 1 Step 5: 7
step 4 : x = 11 - 1
step 5 : x = 10
a=5<=8 && 6! =5
1. First parenthesized sub expression from left
to right are evaluated. #include <stdio.h>
int main()
2. If parentheses are nested, the evaluation {
begins with the innermost sub-expression. int a;
3. The precedence rules is applied in a=5<=8 && 6! =5
determining the order of application of printf (“%d”, a );
operators in evaluating sub-expressions. return 0;
}
4. The associativity rule is applied when two
or more operators of the same precedence
level appears in a sub-expression. Output : 1
5. Arithmetic expressions are evaluated from
left to right using the rules of precedence.
6. When parentheses are used, the
expressions within parentheses assume
highest priority.
Example Action
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
{ {
y=sqrt(x); z=pow(x,y);
} }
#include<stdio.h>
#include<conio.h>
#include <stdio.h>
int main()
#include <math.h>
{
float x,y;
int main()
printf ("Enter value of x=");
{
scanf ("%f",&x);
double num = 2.66;
y=sqrt(x);
int z;
printf("\n Value of y=%f",y);
}
z = ceil(num);
printf("Ceiling integer of %.2f = %d", num, z);
Program:2: pow()
return 0;
#include<stdio.h>
}
#include<conio.h>
int main()
{
int x,y,z;
printf ("Enter value of x and y=");
scanf ("%d%d",&x,&y);
z=pow(x,y);
printf("\n Value of z=%d",z);
}
In the C Programming Language, the #define directive allows the definition of macros within your source code.
These macro definitions allow constant values to be declared for use throughout your code.
Macro definitions are not variables and cannot be changed by your program code like variables. You generally
use this syntax when creating constants that represent numbers, strings or expressions.
#define directive line can not be terminate with ;
Syntax :
#define constant_name value_of_constant_name
#define num 10
#define name “OP Solution”
2.
#include <stdio.h>
#define company "OM Solution" #include <stdio.h>
#define year 2005
#define num1 10
int main() #define num2 20
{
printf("%s is company which is established on %d \n", company, int main()
year); {
return 0; int sum;
} sum =num1+num2;