Sie sind auf Seite 1von 13

Assignment Operator in C Programming Language :

1. Assignment Operator is Used to Assign Value to a Variable.


2. Assignment Operator is Denoted by Equal to Sign , =.
3. Assignment Operator is Binary Operator i.e It Operates on Two Operands.
4. Assignment Operator have Two Values L-Value and R-Value.
5. Assignment Operator Copies R-Value into L-Value.
6. Assignment Operator have Lower Precedence than any Other Operator and Higher
Precedence than Comma Operator. [View this Precedence Table ]

Different Ways of Using Assignment Operator :
Way 1 : Assignment Operator used to Assign Value
#include<stdio.h>

int main()
{
int value;
value = 55;

return(0);
}
Way 2 : Assignment Operator used To Type Cast.
Assignment Operator can Type Cast Higher Values to Lower Values or Lower Values to
Higher Values
int value;
value = 55.450;
printf("%d",value);
Way 3 : Assignment Operator in If Statement
if(value = 10)
printf("True");
else
printf("False");


Arithmetic Operator in C Programming Language
1. C Programming Supports 5 Arithmetic Operators.
2. Arithmetic Operators are used for Arithmetic Calculation.
Supported Arithmetic Operators :
5 arithmetic operators are shown in the following table. Arithmetic operators are used to
perform arithmetic operations in c programming.
Operator Meaning Example
+ Addition Operator 10 + 20 = 30
- Subtraction Operator 20 10 = 10
* Multiplication Operator 20 * 10 = 200
/ Division Operator 20 / 10 = 2
% Modulo Operator 20 % 6 = 2
Live Example : C Program to Verify Arithmetic Operator
and Operation
#include <stdio.h>

int main()
{
int num1,num2;
int sum,sub,mult,div,mod;

printf("\nEnter First Number :");
scanf("%d",&num1);
printf("\nEnter Second Number :");
scanf("%d",&num2);

sum = num1 + num2;
printf("\nAddition is : %d",sum);

sub = num1 - num2;
printf("\nSubtraction is : %d",sub);

mult = num1 * num2;
printf("\nMultiplication is : %d",mult);

div = num1 / num2;
printf("\nDivision is : %d",div);

mod = num1 % num2;
printf("\nModulus is : %d",mod);

return(0);
}
Output :
Enter First Number : 10
Enter Second Number : 5

Addition is : 15
Subtraction is : 5
Multiplication is : 50
Division is : 2
Modulus is : 0
Precedence Power : Which Operator have Highest
Priority ?
If we consider all the arithmetic operators then we can say that Multiplication and division
operator have highest priority than addition and subtraction operator. Following table clearly
explains the priority of all arithmetic operators in C programming
Priority Rank Operator Description Operator Associativity
1 Multiplication * Left to Right
1 Division / Left to Right
1 Modulo % Left to Right
2 Addition + Left to Right
2 Subtraction - Left to Right

Increment Operator in C Programming :
1. Increment operator is used to increment the current value of variable by adding
integer 1.
2. Increment operator can be applied to only variables.
3. Increment operator is denoted by ++.
Different Types of Increment Operation :
In C Programming we have two types of increment operator i.e Pre-Increment and Post-
Increment Operator.
A. Pre Increment Operator in C Programming :
Pre-increment operator is used to increment the value of variable before using in the
expression. In the Pre-Increment value is first incremented and then used inside the
expression.
b = ++y;
In this example suppose the value of variable y is 5 then value of variable b will be 6
because the value of y gets modified before using it in a expression.
B. Post Increment Operator in C Programming :
Post-increment operator is used to increment the value of variable as soon as after executing
expression completely in which post increment is used. In the Post-Increment value is first
used in a expression and then incremented.
b = x++;
In this example suppose the value of variable x is 5 then value of variable b will be 5
because old value of x is used.
Live Program : C Program to Perform Increment
Operation in C Programming
#include<stdio.h>

void main()
{
int a,b,x=10,y=10;

a = x++;
b = ++y;

printf("Value of a : %d",a);
printf("Value of b : %d",b);
}
Output :
Value of a : 10
Value of b : 11
Tip #1 : Increment Operator should not be used on
Constants
We cannot use increment operator on the constant values because increment operator
operates on only variables. It increments the value of the variable by 1 and stores the
incremented value back to the variable,
b = ++5;
Or
b = 5++;

Decrement Operator in C Programming :
1. Decrement operator is used to decrease the current value of variable by subtracting
integer 1.
2. Like Increment operator, decrement operator can be applied to only variables.
3. Decrement operator is denoted by .
Different Types of Decrement Operation :
When decrement operator used in C Programming then it can be used as pre-decrement or
post-decrement operator.
A. Pre Decrement Operator in C Programming :
Pre-decrement operator is used to decrement the value of variable before using in the
expression. In the Pre-decrement value is first decremented and then used inside the
expression.
b = --var;
Suppose the value of variable var is 10 then we can say that value of variable var is firstly
decremented then updated value will be used in the expression.
B. Post Decrement Operator in C Programming :
Post-decrement operator is used to decrement the value of variable immediatly after
executing expression completely in which post decrement is used. In the Post-decrement old
value is first used in a expression and then old value will be decrement by 1.
b = var--;
Value of variable var is 5. Same value will be used in expression and after execution of
expression new value will be 4.
Live Program : C Program to Perform Decrement
Operation in C Programming
#include<stdio.h>

void main()
{
int a,b,x=10,y=10;

a = x--;
b = --y;

printf("Value of a : %d",a);
printf("Value of b : %d",b);

}
Output :
Value of a : 10
Value of b : 9
Tip #1 : Decrement Operator should not be used on
Constants
We cannot use decrement operator in the following case -
b = --5;
Or
b = 5--;

Relational Operator in C
In C Programming we can compare the value stored between two variables and depending on
the result we can follow different blocks using Relational Operator in C. In C we have
different relational operators such as -
Operator Meaning
> Greater than
>= Greater than or equal to
<= Less than or equal to
< Less than
Live Example of Relational Operator :
#include<stdio.h>
int main()
{
int num1 = 30;
int num2 = 40;
printf("Value of %d > %d is %d",num1,num2,num1> num2);
printf("Value of %d >=%d is %d",num1,num2,num1>=num2);
printf("Value of %d <=%d is %d",num1,num2,num1<=num2);
printf("Value of %d < %d is %d",num1,num2,num1< num2);
return(0);
}
Output :
Value of 30 > 40 is 0
Value of 30 >=40 is 0
Value of 30 <=40 is 1
Value of 30 < 40 is 1
We all know that C does not support boolean data type so in order to compare the equality we
use comparison operator or equality operator in C Programming.
Operator Meaning
== Is equal to
!= Is not equal to
Live Example Of Equality Operator in C
#include<stdio.h>
int main()
{
int num1 = 30;
int num2 = 40;
printf("Value of %d ==%d is %d",num1,num2,num1==num2);
printf("Value of %d !=%d is %d",num1,num2,num1!=num2);
return(0);
}
Output :
Value of 30 ==40 is 0
Value of 30 !=40 is 1

Logical Operator in C Programming
Whenever we use if statement then we can use relational operator which tells us the result of
the comparison, so if we want to compare more than one conditions then we need to use
logical operators. Suppose we need to execute certain block of code if and only if two
conditions are satisfied then we can use Logical Operator in C Programming.
Operator Name of the Operator
&& And Operator
|| Or Operator
! Not Operator
Let us look at all logical operators with example -
#include<stdio.h>
int main()
{
int num1 = 30;
int num2 = 40;

if(num1>=40 || num2>=40)
printf("Or If Block Gets Executed");

if(num1>=20 && num2>=20)
printf("And If Block Gets Executed");

if( !(num1>=40))
printf("Not If Block Gets Executed");

return(0);
}
Output :
Or If Block Gets Executed
And If Block Gets Executed
Not If Block Gets Executed
Explanation of the Program :
Suppose OR Operator is used inside if statement then if part will be executed if any of the
condition evaluated to be true.
if(num1>=40 || num2>=40)
In the above if statement first condition is false and second condition is true , so if part will be
executed.
if(num1>=20 && num2>=20)
In the above if statement both the conditions are true so if statement will be executed, AND
Operator will look for truthness of first condition, If it founds the true condition then it will
look for 2nd condition. If 2nd Condition founds to be true then it will look for next condition.
Logical Operator Chart :
Operator Applied Between Condition 1 Condition 2 Final Output
AND True True True

True False False

False True False
Operator Applied Between Condition 1 Condition 2 Final Output

False False False
OR True True True

True False True

False True True

False False False
NOT True - False

False - True

Conditional Operators [ ?: ] : Ternary Operator
Statement in C
1. They are also called as Ternary Operator .
2. They also called as ?: operator
3. Ternary Operators takes on 3 Arguments
Syntax :
expression 1 ? expression 2 : expression 3
where
expression1 is Condition
expression2 is Statement Followed if Condition is True
expression2 is Statement Followed if Condition is False

Conditional Statement in C Programming Lanuage
Meaning of Syntax :
1. Expression1 is nothing but Boolean Condition i.e it results into either TRUE or
FALSE
2. If result of expression1 is TRUE then expression2 is Executed
3. Expression1 is said to be TRUE if its result is NON-ZERO
4. If result of expression1 is FALSE then expression3 is Executed
5. Expression1 is said to be FALSE if its result is ZERO
Live Example : Check whether Number is Odd or Even
#include<stdio.h>

int main()
{
int num;

printf("Enter the Number : ");
scanf("%d",&num);

flag = ((num%2==0)?1:0);

if(flag==0)
printf("\nEven");
else
printf("\nOdd");
}
More Simply we can write this program as -
#include<stdio.h>

int main()
{
int num;

printf("Enter the Number : ");
scanf("%d",&num);

(num%2==0)?printf("Even"):printf("Odd");

}
Big Note : Ternary Operator
Operator that works on 3 operands is called as tertiary operator. Ternary operator . Read rules
of using Ternary Operator in C .
Comma Operator : Lowest Precedence | Returns Righmost
#include<stdio.h>
void main()
{
int a=1,b=2;
int k;
k = (a,b);
printf("%d",k);
}
1. Comma Operator has Lowest Precedence. [Priority] i.e evaluated at last .
2. Comma operator returns the value of the rightmost operand.
3. Comma Operator Can acts as -
o Operator : In the Expression
o Separator : Declaring Variable , In Function Call Parameter List

In the Above Example -
1 : Comma as Seperator
int a=1,b=2;
It can acts as Seperator in -
Function calls
Function definitions
Variable declarations
Enum declarations
2 : Comma as Operator
k = (a,b);

Different Typical Ways of Comma as Operator :
int a=1,b=2,c;
Way 1 :
c = (a,b);
c = Value Stores in b . = 2
Way 2 :
c = a,b;
c = Value Stores in a . = 1

Arrow (->) Operator : Accessing Structure members using
Pointer
In C Programming we use arrow operator to access structure member using the pointer
variable.
Syntax and Use of Arrow Operator :
struct student
{
char name[20],
int roll;
}*ptr;
Expalanation :
Whenever we declare structure variable then member can be accessed using the dot operator.
But when pointer to a structure is used then arrow operator is used.
struct student
{
char name[20],
int roll;
}std;
struct student
{
char name[20],
int roll;
}*ptr;
Access Structure Member Example 1 Example 2
Name is accessed using std.name ptr->name
Roll number is accessed using std.roll ptr->roll

C Programming Operator Precedence and Associativity

C Programming supports wide range of Operators . While
Solving the Expression we must follow some rules , Example :
while Solving the expression [ a + b *c ] , we should first
perform Multiplication Operation and then Addition . Similarly
in order to solve such complicated expression you should have
hands on Operator Precedence and Associativity of Operators.
Precedence of the Operator are Listed in the Following Table and
This Table is Summarized in decreasing Order of Priority i.e
Topmost Operator has Highest Priority and Bottommost
Operator has Lowest Priority.

Description Operator Associativity
Increment
Decrement
Negation
Unary Minus
++

!
-
R -> L
Sizeof sizeof
Multiplication
Division
Modulus
*
/
%
L -> R
Addition
Subtraction
+
-
L -> R
Shift Operator
<<
>>
L -> R
Less Than
Greater Than
Less Than and Equal to
Greater Than and Equal to
<
>
<=
>=
L -> R
Equal to
Not Equal To
==
!=
L -> R
Logical AND && L -> R
Logical OR || L -> R
Conditional ?: R -> L
Assignment
=
+=
-=
*=
/=
R -> L
Comma , L -> R
Notes :

Das könnte Ihnen auch gefallen