C - Operators and Expressions
C - Operators and Expressions
The symbols which are used to perform logical and mathematical operations in a C program are called C
operators.
These C operators join individual constants and variables to form expressions.
Operators, functions, constants and variables are combined together to form expressions.
Consider the expression A + B * 5. where, +, * are operators, A, B are variables, 5 is constant and A + B * 5 is
an expression.
TYPES OF C OPERATORS:
Arithmetic_operators These are used to perform mathematical calculations like addition, subtraction,
multiplication, division and modulus
Arithmetic Operators/Operation Example
Eg;
+ (Addition) A+B #include <stdio.h>
– (Subtraction) A-B int main(){
* (multiplication) A*B int a=40,b=20, add,sub,mul,div,mod;
/ (Division) A/B add = a+b;
% (Modulus) A%B sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n",
mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
}
Assignment_operators These are used to assign the values for the variables in C programs.
In C programs, values for the variables are assigned using assignment operators.
For example, if the value “10” is to be assigned for the variable “sum”, it can be assigned as “sum = 10;”
There are 2 categories of assignment operators in C language. They are,
1. Simple assignment operator ( Example: = )
2. Compound assignment operators ( Example: +=, -=, *=, /=, %=, &=, ^= )
Operators Example/Description
sum = 10;
= 10 is assigned to variable sum
sum += 10;
+= This is same as sum = sum + 10
sum -= 10;
-= This is same as sum = sum – 10
sum *= 10;
*= This is same as sum = sum * 10
sum /= 10;
/= This is same as sum = sum / 10
sum %= 10;
%= This is same as sum = sum % 10
sum&=10;
&= This is same as sum = sum & 10
sum ^= 10;
^= This is same as sum = sum ^ 10
Eg:
# include <stdio.h>
int main(){
int Total=0,i;
for(i=0;i<10;i++)
{
Total+=i; // This is same as Total = Toatal+i
}
printf("Total = %d", Total);
}
Relational operators These operators are used to compare the value of two variables.
Operators Example/Description Eg:
#include <stdio.h>
> x > y (x is greater than y)
4 int main()
5 {
< x < y (x is less than y)
6 int m=40,n=20;
7 if (m == n)
>= x >= y (x is greater than or equal to y) 8 {
9 printf("m and n are equal");
<= x <= y (x is less than or equal to y) 10 }
11 else
== x == y (x is equal to y) 12 {
13 printf("m and n are not equal");
14 }
!= x != y (x is not equal to y) }
Logical operators These operators are used to perform logical operations on the given two variables.
These operators are used to perform logical operations on the given expressions.
There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).
Operators Example/Description
(x>5)&&(y<5)
&& (logical AND) It returns true when both conditions are true
(x>=10)||(y>=10)
|| (logical OR) It returns true when at-least one of the condition is true
!((x>5)&&(y<5))
It reverses the state of the operand “((x>5) && (y<5))”
! (logical NOT) If “((x>5) && (y<5))” is true, logical NOT operator makes it false
Eg;
#include <stdio.h>
int main(){
int m=40,n=20;
int o=20,p=30;
if (m>n && m !=0) {
printf("&& Operator : Both conditions are true\n"); }
if (o>p || p!=20) {
printf("|| Operator : Only one condition is true\n"); }
if (!(m>n && m !=0)){
printf("! Operator : Both conditions are true\n"); }
else {
printf("! Operator : Both conditions are true. " \
"But, status is inverted as false\n"); }}
Bit wise operators These operators are used to perform bit operations on given two variables.
These operators are used to perform bit operations. Decimal values are converted into binary values which are
the sequence of bits and bit wise operators work on these bits.
Bit it wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise NOT), ^ (XOR), << (left shift)
and >> (right shift).
Truth Table For Bit Wise Operation & Bit Wise Operators:
Eg:
3 #include <stdio.h>
4 int main(){
5 int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
6 AND_opr = (m&n);
7 OR_opr = (m|n);
8 NOT_opr = (~m);
9 XOR_opr = (m^n);
10 printf("AND_opr value = %d\n",AND_opr
n",AND_opr );
11 printf("OR_opr value = %d\n",OR_opr
n",OR_opr );
12 printf("NOT_opr value = %d\n",NOT_opr
n",NOT_opr );
13 printf("XOR_opr value = %d\n",XOR_opr
n",XOR_opr );
14 printf("left_shift value = %d\n",
n", m << 1);
15 printf("right_shift value = %d\n",
n", m >> 1); }
16
Operator Operator/Description
Special operators
&, *, sizeof( ) and ternary operators.
Operators Description
#include <stdio.h>
int main(){
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}
C TOKENS:
C tokens are the basic buildings blocks in C language which are constructed together to write a C
program.
Each and every smallest individual units in a C program are known as C tokens.
C tokens are of six types. They are,
1. Keywords (eg: int, while),
2. Identifiers (eg: main, total),
3. Constants (eg: 10, 20),
4. Strings (eg: “total”, “hello”),
5. Special symbols (eg: (), {}),
6. Operators (eg: +, /,-,*)
Eg:
int main(){
int x, y, total;
x = 10, y = 20;
total = x + y;
printf ("Total = %d \n", total); }
where,
main – identifier
{,}, (,) – delimiter
int – keyword
x, y, total – identifier
main, {, }, (, ), int, x, y, total – tokens
Do you know how to use C token in real time application programs? We have given simple real time
application programs where C token is used. You can refer the below C programs to know how to use C
token in real time program.
IDENTIFIERS IN C LANGUAGE:
Each program elements in a C program are given a name called identifiers.
Names given to identify Variables, functions and arrays are examples for identifiers. eg. x is a name
given to integer variable in above program.
RULES FOR CONSTRUCTING IDENTIFIER NAME IN C:
1. First character should be an alphabet or underscore.
2. Succeeding characters might be digits or letter.
3. Punctuation and special characters aren’t allowed except underscore.
4. Identifiers should not be keywords.
KEYWORDS IN C LANGUAGE:
Keywords are pre-defined words in a C compiler.
Each keyword is meant to perform a specific function in a C program.
Since keywords are referred names for compiler, they can’t be used as variable name.
C language supports 32 keywords
float (10.456789)
Real or Floating point constants doule (600.123456789)
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\” Double quote
\’ Single quote
\\ Backslash
\v Vertical tab
\a Alert or bell
\? Question mark