×

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

Difference between rand() and srand() function in C

What is the rand()? The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

Bar3d() function in C Graphics

The bar3d function is used to create a 2-dimensional filled-in rectangular bar. We can also create three-dimensional shapes in C using helpful function. The first step in creating this 3D...

3 minutes read.

Storage Class in C

C storage class is used to define the scope variables and function. There are four various types of storage classes that are given below. auto: The auto keyword is the default...

1 minute read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 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.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 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.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

GPA Calculator in C

GPA stands for Grade Point Average. This GPA is used to measure the student's academic performance in educational institutes. By using this, segregation takes place and lets us know how...

4 minutes read.

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

4 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.