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.