×

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages. For example, in the following code snippet, the assignment operator is used to assign the value 10 to the variable x:

x=10;  // This statement will assign a value of 10 to x

This assignment statement means that the value of 10 is being stored in the variable x. The value of x can be accessed and used in various ways in the program, such as being used in an expression or being printed on the screen.

Operator in C language

In C programming, an operator is a symbol that performs an operation on one or more operands. Operators are used to manipulate variables and values in expressions and statements.

C has a wide range of operators, including arithmetic operators, relational operators, logical operators, assignment operators, and more.

Here is a brief overview of some of the most commonly used operators in C:

1. Arithmetic Operators: These operators are used to perform basic arithmetic operations, such as addition, subtraction, multiplication, and division.

For Example:

  • ‘+’ for addition
  • ‘-’ for subtraction
  • ‘*’ for multiplication
  • ‘/’ for division
  • ‘%’ for modulo (remainder after division)

2. Relational Operators: These operators are used to compare two values and determine their relationship. They return a Boolean value (either 1 or 0) indicating whether the comparison is true or false.

 For Example:   

  • ‘==’ for equal to
  • ‘!=’ for not equal to
  • ‘>’ for greater than
  • ‘<’ for less than
  • ‘>=’ for greater than or equal to
  • <= for less than or equal to

3. Logical Operators: These operators are used to perform logical operations, such as AND, OR, and NOT. They also return a Boolean value.

For Example:

  • && for AND
  • || for OR
  • ! for NOT

4. Bitwise Operators: bitwise operators are used to performing bit-level operations on integer values. These operators are often used to manipulate individual bits in a value, such as setting or clearing specific bits or testing the value of individual bits.

Here is a list of the bitwise operators available in C:

  • & (bitwise AND): It performs a bit-level AND operation on two integers.
  • | (bitwise OR): It performs a bit-level OR operation on two integers.
  • ^ (bitwise XOR): It performs a bit-level XOR operation on two integers.
  • ~ (bitwise NOT): It inverts all the bits of an integer.
  • << (left shift): It shifts the bits of an integer to the left by a specified number of positions.
  • >> (right shift): It shifts the bits of an integer to the right by a specified number of positions.

5. Assignment Operators: These operators are used to assign a value to a variable. The most basic assignment operator is the equal sign (=), which assigns the value on the right side of the operator to the variable on the left side. Other assignment operators include +=, -=, *=, and /=, which perform the operation and assignment in one step.

For Example:

  • a = b  the value of b assigns to a
  • a += b is equivalent to a = a + b
  • a -= b is equivalent to a = a - b
  • a *= b is equivalent to a = a * b
  • a /= b is equivalent to a = a / b
  • a%=b is equivalent to a=a%b

In C, the assignment operator can be used to assign a value to any type of variable, including basic types such as integers, floats, and characters, as well as more complex types such as arrays and structures.

It is important to note that the assignment operator always operates from right to left. In other words, the value on the right side of the operator is assigned to the variable on the left side.

For example, in the following code snippet, the value of x is assigned to the variable y:

int x = 10;
        int y;       
       y = x;

To comprehend all of the assignment operators available in C, read the example below.

1. If a += b, a = a + b is the result.

Here is a C program that demonstrates how to utilize the += compound assignment operator:

Example:

#include <stdio.h>
int main(void) 
{
int x = 10;
int y = 5;
x += y; // x is now equal to 15 (10 + 5)
printf("x = %d\n", x);
return 0;
}

This program declares two variables, x and y, and assigns the values 10 and 5 to them, respectively. Then, it uses the += operator to add y to x and assign the result to x. Finally, it prints the value of x to the console using the printf function.

When this program is run, the following output is produced:

Output:

x = 15

The += operator is often used in loops to increment a variable by a certain amount on each iteration.

Example:

#include <stdio.h>
int main(void) 
{
int i;
for (i = 0; i < 10; i += 2) {
printf("%d ", i);
}
return 0;
}

This program uses the += operator to increment the variable i by 2 on each iteration of the loop. It will output the following:

Output:

0 2 4 6 8

As you can see, using the += operator to perform compound assignments in C is a useful and convenient method.

2. If a -= b, a = a - b is the result.

Here is a C program that demonstrates how to utilize the -= compound assignment operator:

Example:

#include <stdio.h>
int main(void) 
{
int x = 10;
int y = 5;
x -= y; // x is now equal to 5 (10 - 5)
printf("x = %d\n", x);
return 0;
}

This program declares two variables, x and y, and assigns the values 10 and 5 to them, respectively. Then, it uses the -= operator to subtract y from x and assign the result to x. Finally, it prints the value of x to the console using the printf function.

When this program is run, the following output is produced:

Output:

x = 5

The -= operator is often used in loops to decrement a variable by a certain amount on each iteration.

Example:

#include <stdio.h>
int main(void) 
{
int i;
for (i = 10; i > 0; i -= 2)
{
printf("%d ", i);
}
return 0;
}

This program uses the -= operator to decrement the variable i by 2 on each iteration of the loop. It will output the following:

Output:

10 8 6 4 2

As you can see, using the -= operator to perform compound assignments in C is a useful and convenient method.

3. If a *= b, a = a * b is the result.

Here is a C program that demonstrates how to utilize the *= compound assignment operator:

Example:

#include <stdio.h>
int main(void) 
{
int x = 10;
int y = 5;
x *= y; // x is now equal to 50 (10 * 5)
printf("x = %d\n", x);
return 0;
}

This program declares two variables, x and y, and assigns the values 10 and 5 to them, respectively. Then, it uses the *= operator to multiply x by y and assign the result to x. Finally, it prints the value of x to the console using the printf function.

When this program is run, the following output is produced:

Output:

x = 50

The *= operator is often used to repeatedly multiply a variable by a certain amount. Example:

#include <stdio.h>
int main(void) 
{
int x = 2,i;
for (i = 0; i < 4; i++) 
{
x *= 2;
printf("%d ", x);
}
return 0;
}

This program uses the *= operator to double the value of x on each iteration of the loop. It will output the following:

Output:

4 8 16 32

As you can see, using the *= operator to perform compound assignments in C is a useful and convenient method.

4. If a /= b, a = a / b is the result.

Here is a C program that demonstrates how to utilize the /= compound assignment operator:

Example:

#include <stdio.h>
int main(void) 
{
int x = 10;
int y = 5;
x /= y; // x is now equal to 2 (10 / 5)
printf("x = %d\n", x);
return 0;
}

This program declares two variables, x and y, and assigns the values 10 and 5 to them, respectively. Then, it uses the /= operator to divide x by y and assign the result to x. Finally, it prints the value of x to the console using the printf function.

When this program is run, the following output is produced:

Output:

x = 2

The /= operator is often used to repeatedly divide a variable by a certain amount.

Example:

#include <stdio.h>
int main(void) 
{
int x = 16,i;
for (i = 0; i < 4; i++)
{
x /= 2;
printf("%d ", x);
}
return 0;
}

This program uses the /= operator to reduce the value of x on each iteration of the loop. It will output the following:

Output:

8 4 2 1

As you can see, using the /= operator to perform compound assignments in C is a useful and convenient method.

5. If a %= b, a = a % b is the result.

Here is a C program that demonstrates how to utilize the %= compound assignment operator:

Example:

#include <stdio.h>
int main(void) 
{
int x = 10;
int y = 3;
x %= y; // x is now equal to 1 (10 % 3)
printf("x = %d\n", x);
return 0;
}

This program declares two variables, x and y, and assigns the values 10 and 3 to them, respectively. Then, it uses the %= operator to compute the remainder of x divided by y and assign the result to x. Finally, it prints the value of x to the console using the printf function.

When this program is run, the following output is produced:

Output:

x = 1

The %= operator is often used to repeatedly perform a modulo operation on a variable. For example:

Example:

#include <stdio.h>
int main(void) 
{
int x = 7,i;
for (i = 0; i < 4; i++) 
{
x %= 3;
printf("%d ", x);
}
return 0; 
}

This program uses the %= operator to compute the remainder of x divided by 3 on each iteration of the loop. It will output the following:

Output:

1 1 1 1 

As you can see, using the %= operator to perform compound assignments in C is a useful and convenient method.

Some More Compound Assignments Operator

There are a few more compound Assignments operator which we use with the help

some of the bitwise operators.

1. Left shift assignment operator (<<=)

The left shift assignment operator (<<=) is a compound assignment operator that combines the left shift operator (<<) and the assignment operator (=). It shifts the bits of a binary number to the left and assigns the result back to the same variable.

The <<= operator has a lower precedence than most other operators.

Here is a C program that demonstrates how to utilize the <<= compound assignment operator:

Example:

#include <stdio.h>
int main(void) 
{
int x = 10;
int y = 2;
x <<= y; // x is now equal to 40 (10 << 2)
printf("x = %d\n", x);
return 0;
}

This program declares two variables, x and y, and assigns the values 10 and 2 to them, respectively. Then, it uses the <<= operator to shift the bits of x left by y positions and assign the result to x. Finally, it prints the value of x to the console using the printf function.

When this program is run, the following output is produced:

Output:

x = 40

The <<= operator is often used to perform bit shifting operations on variables.

Example:

#include <stdio.h>
int main(void) 
{
int x = 7;
int i;
for (i = 0; i < 4; i++) 
{
x <<= 1;
printf("x = %d\n", x);
}
return 0;
}

This program uses the <<= operator to shift the bits of x left by 1 position on each iteration of the loop. It will output the following:

Output:

x = 14
x = 28
x = 56
x = 112

2. Right Shift Assignment Operator (>>=)

The >>= operator is a compound assignment operator in the C programming language. It is used to shift the bits of a variable right by a certain number of positions and assign the result to the variable.

Here is a C program that demonstrates how to utilize the >>= compound assignment operator:

Example:

#include <stdio.h>
int main(void) 
{
int x = 40;
int y = 2;
x >>= y; // x is now equal to 10 (40 >> 2)
printf("x = %d\n", x);
return 0;
}

This program declares two variables, x and y, and assigns the values 40 and 2 to them, respectively. Then, it uses the >>= operator to shift the bits of x right by y positions and assign the result to x. Finally, it prints the value of x to the console using the printf function.

When this program is run, the following output is produced:

Output:

x = 10

The >>= operator is often used to repeatedly perform a bit shift operation on a variable.

Example:

#include <stdio.h>
int main(void) 
{
int x = 56;
int i;
for (i = 0; i < 4; i++) 
{
x >>= 1;
printf("x = %d\n", x);
}
return 0;
}

This program uses the >>= operator to shift the bits of x right by 1 position on each iteration of the loop. It will output the following:

Output:

x = 28
x = 14
x = 7
x = 3

3. Bitwise AND Assignment Operator (&=)

The &= operator is a compound assignment operator in the C programming language. It is used to perform a bitwise AND operation on a variable and assign the result to the variable.

Here is a C program that demonstrates how to utilize the &= compound assignment operator:

Example:

#include <stdio.h>
int main(void) 
{
int x = 10; // binary representation: 1010
int y = 5; // binary representation: 0101
x &= y; // x is now equal to 0 (1010 & 0101)
printf("x = %d\n", x);
return 0; 
}

This program declares two variables, x and y, and assigns the values 10 and 5 to them, respectively. Then, it uses the &= operator to perform a bitwise AND operation on x and y and assign the result to x. Finally, it prints the value of x to the console using the printf function.

When this program is run, the following output is produced:

Output:

x = 0

The &= operator is often used to repeatedly perform a bitwise AND operation on a variable.

Example:

#include <stdio.h>
int main(void) 
{
int x = 15; // binary representation: 1111
int i;
for (i = 0; i < 4; i++) {
x &= 7; // binary representation: 0111
printf("x = %d\n", x);
}
return 0;
}

When this program is run, the following output is produced:

Output:

x = 7
x = 7
x = 7
x = 7

4. Bitwise Exclusive OR Assignment Operator (^=)

The ^= operator is a compound assignment operator in the C programming language. It is used to perform a bitwise XOR operation on a variable and assign the result to the variable.

Here is a C program that demonstrates how to utilize the ^= compound assignment operator:

Example:

#include <stdio.h>
int main(void) 
{
int x = 10; // binary representation: 1010
int y = 5; // binary representation: 0101
x ^= y; // x is now equal to 15 (1010 ^ 0101)
printf("x = %d\n", x);
return 0;
}

This program declares two variables, x and y, and assigns the values 10 and 5 to them, respectively. Then, it uses the ^= operator to perform a bitwise XOR operation on x and y and assign the result to x. Finally, it prints the value of x to the console using the printf function.

When this program is run, the following output is produced:

Output:

x = 15

The ^= operator is often used to repeatedly perform a bitwise XOR operation on a variable.

Example:

#include <stdio.h>
int main(void) 
{
int x = 15; // binary representation: 1111
int i;
for (i = 0; i < 4; i++) 
{
x ^= 7; // binary representation: 0111
printf("x = %d\n", x);
}
return 0;
}

This program uses the ^= operator to XOR the value of x with 7 on each iteration of the loop. It will output the following:

Output:

x = 8
x = 15
x = 8
x = 15

6. Bitwise Inclusive OR Assignment Operator (|=)

The |= operator is a compound assignment operator in the C programming language. It is used to perform a bitwise OR operation on a variable and assign the result to the variable.

Here is a C program that demonstrates how to utilize the |= compound assignment operator:

Example:

#include <stdio.h>
int main(void) 
{
int x = 10; // binary representation: 1010
int y = 5; // binary representation: 0101
x |= y; // x is now equal to 15 (1010 | 0101)
printf("x = %d\n", x);
return 0;
}

This program declares two variables, x and y, and assigns the values 10 and 5 to them, respectively. Then, it uses the |= operator to perform a bitwise OR operation on x and y and assign the result to x. Finally, it prints the value of x to the console using the printf function.

When this program is run, the following output is produced:

Output:

x = 15

The |= operator is often used to perform a bitwise OR operation repeatedly on a variable.

Example:

#include <stdio.h>
int main(void) 
{
int x = 8; // binary representation: 1000
int i;
for (i = 0; i < 4; i++) 
{
x |= 7; // binary representation: 0111
printf("x = %d\n", x);
}
return 0;
}

This program uses the |= operator to OR the value of x with 7 on each iteration of the loop. It will output the following:

Output:

x = 15
x = 15
x = 15
x = 15

Assignment Operator using with character in C

In C, we can also use the assignment operator = to assign a character value to a character variable like this:

char ch = 'A';

Here, the character 'A' is being assigned to the character variable ch.

You can also use the assignment operator to assign a value to a character array, which is a series of characters stored in consecutive memory locations. For example:

char str[20] = "Hello, World!";

This will assign the character string "Hello, World!" to the character array str. Keep in mind that character arrays in C are null-terminated, meaning that the end of the string is marked by a special character '\0' (ASCII value 0). This character is automatically added to the end of the string when you use string literals (like "Hello, World!") to initialize a character array.

Conclusion:

The assignment operator (=) and compound assignment operators are crucial components of C programming, to sum up. While the compound assignment operators execute an operation on a variable and then assign the result to the variable, the assignment operator is used to assign a value to a variable. C supports a number of compound assignment operators, such as +=, -=, *=, /=, %=, =, >>=, &=, =, and |=. Each of these operators applies a particular operation to a variable, such as addition, subtraction, multiplication, division, modulus, bit shifting, bitwise AND, bitwise XOR, or bitwise OR. Any C programmer must be able to utilize these operators and know when to use them.


Related Topics

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

Pointer in C

Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr Symbol Description & (ampersand sign) Address of an operator...

1 minute read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.