This website includes Education Information like a programming language, job interview question, general knowledge.mathematics

Education log

PageNavi Results No.

Ads

Tuesday, April 21, 2020

assignment operator in c

assignment operator in c

In this tutorial, you will learn about different operators in C programming with the help of examples.

An operator is that works on a worth or a variable. For instance: a c programming language is an administrator to perform expansion.

C has a wide scope of operators to perform a different activity

Assignment operators are accustomed to allocating an incentive to a variable. The left side operand of the assignment administrator is a variable and the right side operand of the assignment administrator is a worth. The incentive on the correct side must be of similar information sort of the variable on the left side in any case the compiler will raise a mistake.

 

Various kinds of assignment operators are demonstrated as follows:

 

The following table lists the assignment operators supported by the C language −

Operator

Description

Example

=

Simple assignment operator. Assigns values from right side operands to left side operand

C = A + B will assign the value of A + B to C

+=

Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.

C += A is equivalent to C = C + A

-=

Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.

C -= A is equivalent to C = C - A

*=

Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.

C *= A is equivalent to C = C * A

/=

Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand.

C /= A is equivalent to C = C / A

%=

Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.

C %= A is equivalent to C = C % A

<<=

Left shift AND assignment operator.

C <<= 2 is same as C = C << 2

>>=

Right shift AND assignment operator.

C >>= 2 is same as C = C >> 2

&=

Bitwise AND assignment operator.

C &= 2 is same as C = C & 2

^=

Bitwise exclusive OR and assignment operator.

C ^= 2 is same as C = C ^ 2

|=

Bitwise inclusive OR and assignment operator.

C |= 2 is same as C = C | 2

 

Example:

 

// C program to demonstrate

// working of Assignment operators

 

#include <stdio.h>

 

int main()

{

 

            // Assigning value 10 to a

            // using "=" operator

            int a = 10;

            printf("Value of a is %d\n", a);

 

            // Assigning value by adding 10 to a

            // using "+=" operator

            a += 10;

            printf("Value of a is %d\n", a);

 

            // Assigning value by subtracting 10 from a

            // using "-=" operator

            a -= 10;

            printf("Value of a is %d\n", a);

 

            // Assigning value by multiplying 10 to a

            // using "*=" operator

            a *= 10;

            printf("Value of a is %d\n", a);

 

            // Assigning value by dividing 10 from a

            // using "/=" operator

            a /= 10;

            printf("Value of a is %d\n", a);

 

            return 0;

}


No comments:

Post a Comment