Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).
Example 1: Arithmetic Operators
Increment and Decrement Operators
C programming has two operators increment ++
and decrement --
to change the value of an operand (constant or variable) by 1.
// Working of increment and decrement operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common assignment operator is =
Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.
Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming.
Bitwise Operators
During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power.
Bitwise operators are used in C programming to perform bit-level operations.
The sizeof operator
The sizeof
is a unary operator that returns the size of data (constants, variables, array, structure, etc).
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}
Example 1: Multiplication and Addition
Explanation:
Multiplication (*) has higher precedence than addition (+).
Execution:
First 3 * 2 = 6, then 5 + 6 = 11.
Result:
x = 11.
Example 2: Division and Modulus
int x = 20 / 4 % 3;
Explanation:
Division (/) and modulus (%) have the same precedence, so they are evaluated left to right.
Execution:
20 / 4 = 5.
5 % 3 = 2.
Result:
x = 2.
0 Comments