C / C++

Operators in C : effective ways to manipulate variables

Hello all , hope you are doing great. Here is my ninth blog for you and third on C programming on topic Operators in C before that we have covered about C programming and datatypes of C . As we know operators are most used things in programming so we have to understand them well as it is basic for all programming languages . So here we start

What is Operators?

operators
Operators

Symbols for performing mathematical, relational, bitwise, conditional, or logical operations are available in C’s operators feature. There are many built-in operators in the C programming language that can be used to carry out different tasks as required by the application. These are typically used in program to manipulate data and variables and are included in mathematical, logical, or conditional statements.

In other terms, it is a symbol that instructs the compiler to carry out particular mathematical, logical, or conditional operations. It functions on a value or a variable as a symbol. For instance, the addition and subtraction operators in any C program are + and -. There are numerous operators in C that can practically carry out any operation. All operations can be carried out with this.

It is simply a symbol that is used to perform operations on variables and values . There can be many types of operations like arithmetic, logical, bitwise, etc.

There are following types to perform different types of operations in C language.

  • Arithmetic
  • Relational
  • Shift
  • Logical
  • Bitwise
  • Ternary or Conditional
  • Assignment
  • Miscellaneous

Arithmetic Operator

Mathematical operations like addition (+), subtraction (-), multiplication (*), division (/), and modulus (%) are all performed using this. All operations on numerical values are carried out through it (constants and variables).

OperatorNameDescriptionExample
+AdditionAdds together two valuesx + y
SubtractionSubtracts one value from anotherx – y
*MultiplicationMultiplies two valuesx * y
/DivisionDivides one value by anotherx / y
%ModulusReturns the division remainderx % y
++IncrementIncreases the value of a variable by 1++x
DecrementDecreases the value of a variable by 1–x

Example

Let’s examine an example of arithmetic operations in C below, assuming that variables a and b both have the values 10 and 20, respectively.

program:-

// Examples of arithmetic in C

#include <stdio.h>

int main()

{

    int a = 25 ,b = 20, c;    

    c = a+b;

    printf(“a+b = %d \n”,c);

    c = a-b;

    printf(“a-b = %d \n”,c);

    c = a*b;

    printf(“a*b = %d \n”,c);

    c = a/b;

    printf(“a/b = %d \n”,c);

    c = a%b;

    printf(“Remainder when a is divided by b = %d \n”,c);   

    return 0;

}

Output:-

a+b = 45

a-b = 5

a*b = 500

a/b = 1

Remainder when a divided by b = 2 .

The +, -, and * are those that compute addition, subtraction, and multiplication, respectively, and are displayed in the program. Normal math says that 25/20 Equals 1.25. However, with the aforementioned application, the output is 1. This is due to the fact that variables a and b are both integers. Consequently, an integer should also be the output. As a result, the compiler ignores the term following the decimal point and displays 2 rather than 2.25 as the program’s output.

A modulo operator can only be used with integers. 

It is possible to determine the remainder of any integer using the modulo operator (%). The remaining after dividing 25 by 20 is 5. Either one of the operands should be a floating-point number if we want the output of our division operator to be expressed in decimal values.

Suppose a = 7.0, b = 2.0, c = 5, and d = 3, the output will be:

// When either one of the operands is a floating-point number

a/b = 3.50  

a/d = 2.33 

c/b = 1.66  

// when both operands are integers 

c/d = 1

Assignment Operator

The primary function of an assignment in a program is to assign a value to a variable. To assign the outcome of an expression to a variable, these are used. In order to assign values to any variable, this is essential. = is the most popular assignment operator.

For C programming, a variety of shorthand assignments are available in the C language. The C language supports the following assignment, which are listed in the table below:

OperatorExampleSame As
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x – 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
&=x &= 3x = x & 3
|=x |= 3x = x | 3
^=x ^= 3x = x ^ 3
>>=x >>= 3x = x >> 3
<<=x <<= 3x = x << 3

Examples

The below example explains the working of assignment operator.

Program:-

// Examples of assignment

#include <stdio.h>

int main()

{

    int a = 7, b;

    b = a;      // b is 7

    printf(“b = %d\n”, b);

    b += a;     // b is 14 

    printf(“b = %d\n”, b);

    b -= a;     // b is 7

    printf(“b = %d\n”, b);

    b *= a;     // b is 49

    printf(“b = %d\n”, b);

    b /= a;     // b is 7

    printf(“c = %d\n”, c);

    b %= a;     // b = 0

    printf(“b = %d\n”, b);

    return 0;

}

Output:-

b = 7

b = 14 

b = 7

b = 49 

b = 7

b = 0

Increment/Decrement Operator

In C programming, there are only really two operators that can increase (++) or decrease (-) a variable’s value. It may alter the value of a constant or variable operand by 1. Decreasing and increasing Operators are very practical and are typically used to reduce calculations. These two operators can only operate on a single operand because they are unary operators. For instance, ++x and x++ denote x=x+1, while —x and x denote x=x-1.

When written before or after any operand, ++ and differ slightly from one another.

When the operator is used as a prefix, it increases the operand by 1 and assigns the result to the variable on the left. When used as a post-fix, however, it first assigns the value to the variable on the left, returning the original value before incrementing the operand by 1.

OperatorDescription
++This increment operator increases the integer value by 1.
This decrement operator decreases the integer value by 1.

Examples

Here is an example demonstrating the working of increment and decrement operator:

Program:-

// Examples of increment and decrement

#include <stdio.h>

int main()

{

    int a = 11, b = 90;

    float c = 100.5, d = 10.5;

    printf(“++a = %d \n”, ++a);

    printf(“–b = %d \n”, –b);

    printf(“++c = %f \n”, ++c);

    printf(“–d = %f \n”, –d);

    return 0;

}

Output:-

++a = 12

–b = 89

++c = 101.500000

–d = 9.500000

The increment and decrement operators ++ and — have been used as prefixes in the code example above. Keep in mind that when necessary, these two operators can also be used as postfixes like a++ and a—.

Relational / Comparison Operator

In a programme, relational operators are used expressly to compare two quantities or values. It examines the connection between two operands. Returning either 1 or 0 depending on whether the supplied relation is true or false. In making decisions and carrying out loop activities, relational operators are frequently utilised.

All of these that C supports are listed in the table below. In this case, we assume that variable A contains 15 and variable B contains 25.

OperatorDescriptionExample
==It is used to check if the values of the two operands are equal or not. If the values of the two operands are equal, then the condition becomes true.(A == B) is not true.
!=It is used to check if the values of the two operands are equal or not. If the values are not equal, then the condition becomes true.(A != B) is true.
>It is used to check if the value of left operand is greater than the value of right operand. If the left operand is greater, then the condition becomes true.(A > B) is not true.
<It is used to check if the value of left operand is less than the value of right operand. If the left operand is lesser, then the condition becomes true.(A < B) is true.
>=It is used to check if the value of left operand is greater than or equal to the value of right operand. If the value of the left operand is greater than or equal to the value, then the condition becomes true.(A >= B) is not true.
<=It is used to check if the value of left operand is less than or equal to the value of right operand. If the value of the left operand is less than or equal to the value, then the condition becomes true.(A <= B) is true.

Example

Below is an example showing the working of them:

Program:-

// Example of relational

#include <stdio.h>

int main()

{

    int x = 8, y = 10;

   printf(“%d == %d is False(%d) \n”, x, y, x == y);

   printf(“%d != %d is True(%d) \n “, x, y, x != y);

   printf(“%d > %d is False(%d)\n “,  x, y, x > y);

   printf(“%d < %d is True (%d) \n”, x, y, x < y);

   printf(“%d >= %d is False(%d) \n”, x, y, x >= y);

   printf(“%d <= %d is True(%d) \n”, x, y, x <= y);

    return 0;

}

Output:-

8 == 10 is False(0)

8 != 10 is True(1)

8 > 10 is False(0)

8 < 10 is True(1)

8 >= 10 is False(0)

8 <=10 is True(1) 

All these work in the same manner as described in the table above.

Logical Operator

Three logical operators are available in the C programming language when more than one condition needs to be tested in order to reach a conclusion. These are the logical operators:

  • &&, which denotes logical AND
  • || which denotes logical OR
  • ! referring to logical NOT

In the C programming language, these returns either 0 or 1, depending on whether the expression is true or false. In C programming, logical operators are typically employed for decision-making.

All of these that the C programming language supports are included in the table below. Here, we’re assuming that variable A contains seven and variable B contains three.

OperatorDescriptionExample
&&This is the AND in C programming language. It performs logical conjunction of two expressions. (If both expressions evaluate to True, then the result is True. If either of the expression evaluates to False, then the result is False)((A==7) && (B>7)) equals to 0
||It is the NOT in C programming language. It performs a logical disjunction on two expressions. (If either or both of the expressions evaluate to True, then the result is True)((A==7) || (B>7)) equals to 1
!It is the Logical NOT in C programming language. It is used to reverse the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false and vice versa.!(A && B) is true

Example

Following is the example that easily elaborates the working of the relational operator:

Program:-

// Working of logical operators

#include <stdio.h>

int main()

{

    int a = 15, b = 15, c = 20, results;

    results = (a == b) && (c > b);

    printf(“(a == b) && (c > b) is %d \n”, results);

    results = (a == b) && (c < b);

    printf(“(a == b) && (c < b) is %d \n”, results);

    results = (a == b) || (c < b);

    printf(“(a == b) || (c < b) is %d \n”, results);

    results = (a != b) || (c < b);

    printf(“(a != b) || (c < b) is %d \n”, results);

    results = !(a != b);

    printf(“!(a != b) is %d \n”, results);

    results = !(a == b);

    printf(“!(a == b) is %d \n”, results);

    return 0;

}

Output:-

(a == b) && (c > b) is 1 

(a == b) && (c < b) is 0 

(a == b) || (c < b) is 1 

(a != b) || (c < b) is 0 

!(a != b) is 1 

!(a == b) is 0 

  • (a == b) && (c > 15) evaluates to 1 because both the operands (a == b) and (c > b) are 1 (true).
  • (a == b) && (c < b) evaluates to 0 because of the operand (c < b) is 0 (false).
  • (a == b) || (c < b) evaluates to 1 because of the operand (a = b) is 1 (true).
  • (a != b) || (c < b) evaluates to 0 because both the operand (a != b) and (c < b) are 0 (false).
  • !(a != b) evaluates to 1 because the operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).
  • !(a == b) evaluates to 0 because the (a == b) is 1 (true). Hence, !(a == b) is 0 (false).

Bitwise Operator

Bit-by-bit operations are carried out by this, which operate on bits. Addition, subtraction, multiplication, division, and other mathematical operations are transformed to bit-level processing, which speeds up processing and makes it simpler to implement during computation and program compilation.

In C programming, bit-level operations are specifically carried out via bitwise operators. A specific operator for bit operations between two variables is supported by the C programming language.

The truth tables for &, |, and ^ is provided below:

pqp & qp | qp ^ q
00000
01011
11110
10011

Here, we will assume that A = 50 and B = 25 in binary format as follows.

A = 00110010

B = 00011001

—————–

A&B = 00010000

A|B  = 00111011

A^B = 00101011

~A  = 11001101

The table provided below demonstrates the bitwise supported by C. Assume variable ‘A’ holds 50 and variable ‘B’ holds 25.

OperatorDescriptionExample
&Binary AND. It copies a bit to the result if it exists in both the operands.(A & B) = 16, i.e. 00010000
|Binary OR. It copies a bit if and only if it exists in either operand.(A | B) = 59, i.e. 00111011
^Binary XOR. It copies the bit only if it is set in one operand but not both.(A ^ B) = 43, i.e. 00101011
~Binary One’s Complement . It is unary and has the effect of ‘flipping’ bits.(~A ) = ~(50), i.e,. -0111101
<<Binary Left Shift. The value of the left operands is moved left by the number of bits specified by the right operand.A << 2 = 200 i.e. 11001000
>>Binary Right Shift. The value of the left operands is moved right by the number of bits specified by the right operand.A >> 2 = 12 i.e., 00001100

Miscellaneous

Besides all the other operators discussed above, the C programming language also offers a few other important including sizeof, comma, pointer(*), and conditional or ternary (?:).

OperatorDescriptionExample
sizeof()The sizeof is a unary that returns the size of data (constants, variables, array, structure, etc).sizeof(a), where a is integer, will return 4.
sizeof(b), where b is float, will return 4.
sizeof(c), where c is double, will return 8.
sizeof(d), where d is integer, will return 1.
&It returns the address of a memory location of a variable.&a; returns the actual address of the variable.
It can be any address in the memory like 4, 70,104.
*Pointer to a variable.*a; It points to the value of the variable.
? :conditional or ternaryv(?: in combination) to construct conditional expressions.If Condition is true ? then value X : otherwise value Y will be returned as output.

Operator Precedence in C

One of the characteristics of the C programming language that helps to decide how an expression is evaluated in accordance with the available expressions and how terms in an expression are grouped is operator precedence. Some of them are given more priority than others, while others are given less priority. For instance, the multiplication operator has higher precedence than the addition in the C programming language.

Example

In the case of the phrase x = 7 + 4 * 2, x is given the value 15 rather than 22, as Multiplication * takes precedence over Addition +. As a result, it multiplies 4 by 2 first before adding 7 to the expression.

For a better understanding of operator precedence, a table is provided below. Those with the most precedence are displayed at the top of the table, while those with the lowest precedence are displayed at the bottom. The operators with greater precedence are evaluated first and the ones with lower precedence are evaluated later in an expression in a C program.

CategoryOperatorAssociativity
Postfix() [] -> . ++ – –Left to right
Unary+ – ! ~ ++ – – (type)* & sizeofRight to left
Multiplicative* / %Left to right
Additive+ –Left to right
Shift<< >>Left to right
Relational< <= > >=Left to right
Equality== !=Left to right
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Left to right

What is Precedence and Associativity ?

Precedence is the priority for grouping different types of operators with their operands.

Associativity is the left-to-right or right-to-left order for grouping operands to operators that have the same precedence.

Conclusions

Nearly all of the C operators have been properly illustrated in this essay on C operators. The article begins with a brief overview of C’s operators before delving deeper into each type. We have given a brief summary of each operator in the C programming language and covered the fundamentals of arithmetic, increment/decrement, assignment, relational, logical, bitwise, special, and operator precedence. For a better understanding of the subject, we have provided an example after the summary. There has also been discussion of some additional significant miscellaneous operators that are quite helpful in C programming.

We hoped that this post would help you understand more about C’s operators and how to use them to software development projects.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button