C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Assignment Operators

C Programming

In this tutorial we will learn about assignment operators in C programming language.

We use the assignment operators to assign the result of an expression to a variable.

In the following example we are assigning integer value 10 to a variable score of data type integer.

int score = 10;

Shorthand Assignment Operators

Lets say we have an integer variable and we have initially assigned 10 to it. Then lets say we increase the value by 5 and assign the new value to it.

//declare and set value
int x = 10;

//increase the value by 5 and re-assign
x = x + 5;

Another way to write the code x = x + 5 is by using the shorthand assignment += as shown below.

//declare and set value
int x = 10;

//increase the value by 5 and re-assign
x += 5;

Following is a list of shorthand assignment operators.

Simple assignmentShorthand assignment
x = x + 1x += 1
x = x - 1x -= 1
x = x * (n + 1)x *= (n + 1)
x = x / (n + 1)x /= (n + 1)
x = x % yx %= y

In the following example we are taking a value x from user then we are taking a new value y from user. Then we are adding y to x and assigning the result to x. Finally we are printing the new value stored in variable x.

#include <stdio.h>
int main(void)
{
  int x, y;
  
  printf("Enter the value of x: ");
  scanf("%d", &x);
  
  printf("Enter the value of y: ");
  scanf("%d", &y);
  
  // add y to x and re-assign
  x = x + y;
  
  printf("New value of x: %d\n", x);
  return 0;
}

Output

Enter the value of x: 10
Enter the value of y: 20
New value of x: 30

Another way of adding y to x and re-assigning the new value to x is by using the shorthand assignment operator +=.

#include <stdio.h>
int main(void)
{
  int x, y;
  
  printf("Enter the value of x: ");
  scanf("%d", &x);
  
  printf("Enter the value of y: ");
  scanf("%d", &y);
  
  // add y to x and re-assign
  x += y;
  
  printf("New value of x: %d\n", x);
  return 0;
}