Assignment Operator in C
0 266
The assignment operator (=) sets a variable's value to a specified expression.
It takes the value on its right and assigns it to the variable on its left.
Syntax:
variable_name = value;
The assignment operator is fundamental in C programming.
It assigns the value of the expression on the right-hand side to the variable on the left-hand side.
Examples:
1 Basic Assignment
Assigning an integer value to a variable.
// Program to Assigning an integer value to a variable. #include<stdio.h>int main() { int num; num = 10; printf("The value of num is: %d\n", num); return 0; }
Output:
The value of num is: 10
2Assignment with Arithmetic Operation
Assigning the result of an arithmetic operation to a variable.
// program for Assignment with Arithmetic Operation #include<stdio.h>int main() { int a = 5, b = 3, result; result = a + b; printf("The result of %d + %d is: %d\n", a, b, result); return 0; }
Output:
The result of 5 + 3 is: 8
3Assignment with Conditional Operation
Assigning a value based on a condition.
// Program for Assigning a value based on a condition. #include<stdio.h>int main() { int marks = 75; char grade; grade = (marks >= 60) ? 'A' : 'B'; printf("The grade is: %c\n", grade); return 0; }
Output:
The grade is: A
4Multiple Assignments
Assigning values to multiple variables in one line.
// program for Assigning values to multiple variables in one line. #include<stdio.h>int main() { int x, y, z; x = y = z = 10; printf("x = %d, y = %d, z = %d\n", x, y, z); return 0; }
Output:
x = 10, y = 10, z = 10
Share:
Comments
Waiting for your comments