C Assignment operator

Description

C Assignment operator is used to assign the value of an expression to a variable.

Syntax

var = expression.

Assignment shorthand

Consider the following line of code:

number = number + 10;

has a shorthand version:

number += 10;

This shorthand version can be any of the arithmetic operators:

Operator Shorthand
+ +=
--+
* *=
/ /+
% %/

The assignment operators, =, +=, -=, *=, /=, %=, have the lowest priority. The assignment operators are evaluated from right to left.

Example - C Assignment operator


#include <stdio.h>
//  w w  w.ja v  a2  s .c  o  m
main(){
    int i,j,k;
   
    i = 6;    
    j = 8;
    k = i + j;
   
    printf("sum of two numbers is %d \n",k);
}

The code above generates the following result.

Example 2


#include<stdio.h>
/*w w w. jav a  2  s . com*/
main( )
{
  int a = 1,b =2,c =3,d=4;

  a += b * c + d;
  printf("\n a = %d",a);
}

The code above generates the following result.

You can use other formats such as var += expression, which means var = var + expression.





















Home »
  C Language »
    Language Basics »




C Language Basics
Data Types
Operator
Statement
Array