Using Operators in Your C Program

Assignment Operators

Assignment operators are used to assign a value to a variable.  The most obvious of these is the '=' sign. However, there are a few other that do different things that can save some time.

Operator Description Example
= Assigns the value from the right side of the operator to the variable on the left side. x = y; // Makes x equal to y.
+= Adds the value from the variable on the left side of the operator to the value on the right side, then assigns the result to the variable on the left side. x += y; // Makes x equal to x + y.
-= Subtracts the value from the right side of the operator from the value of the variable on the left, then assigns the result to the variable on the left side. x -= y; // Makes x equal to x - y.
*= Multiplies the value from the right side of the operator by the value of the variable on the left, then assigns the result to the variable on the left side. x *= y; // Makes x equal to x * y.
/= Divides the value of the variable on the left side of the operator by the value from the right, then assigns the result to the variable on the left side. x /= y; // Makes x equal to x / y.
%= Takes the modulus of the value of the variable on the left side of the operator by the value from the right, then assigns the result to the variable on the left side. x %= y; // Makes x equal to x % y.
<<= Shifts the binary value of the variable on the left side of the operator left by a number of bits equal to the value on the right side of the operator and assigns the result to the variable on the left side. x <<= 2; // Makes x equal to x << 2.
>>= Shifts the binary value of the variable on the left side of the operator right by a number of bits equal to the value on the right side of the operator and assigns the result to the variable on the left side. x >>= 2; // Makes x equal to x >> 2.
&= Performs a bitwise AND operation between the value of the variable on the left side of the operator and the value on the right side of the operator. x &= y; // Makes x equal to x & y.
|= Performs a bitwise OR operation between the value of the variable on the left side of the operator and the value on the right side of the operator. x |= y; // Makes x equal to x | y.
^= Performs a bitwise XOR operation between the value of the variable on the left side of the operator and the value on the right side of the operator. x ^= y; // Makes x equal to x^y.

Bitwise Operators

Bitwise operators perform operations on individual bits of a variable. These are useful when we start working with masks later on. First of all, to assign a binary value to a variable, we can usually use a syntax such as:

int x = 0b00110011 //This assigns the binary value of 0011 0011 to x
int y = 0b01100110 //This assigns the binary value of 0110 0110 to y

For the examples given in the table below lets assume the binary values  given to x and y assigned above.

Operator Description Example
& Bitwise AND operation x & y returns 0010 0010
| Bitwise OR operation x|y returns 0111 0111
^ Bitwise XOR operation x^y returns 0101 0101
~ Toggles all bits ~x returns 1100 1100
<< Shifts all bits left x<<2 returns 1100 1100
>> Shifts all bits right x>>2 returns 0000 1100

Comparison Operators

Comparison operators compare the values to the left and the right of the operator and return a true or false value. These are essential later when we talk about control structures. For the examples in the table below lets assume x = 1 and y = 2.

Operator Description Example
== Returns true if they are equal. Returns false if they are not equal. x == y returns false
!= Returns true if they are not equal. Returns false if they are equal. x != y returns true
< Returns true if the left side is less than the right side. Otherwise it returns false. x < y returns true
> Returns true if the left side is greater than the right side. Otherwise it returns false. x > y returns false
<= Returns true if the left side is less than or equal to the right side. Otherwise it returns false. x <= y returns true
>= Returns true if the left side is greater than or equal to the right side. Otherwise it returns false. x >= y returns false

Logical Operators

Logical operators are usually used in conjunction with comparison operators. Since comparison operators return true or false, lets assume x = true and y = false for the examples in the table below.

Operator Description Example
&& Returns true if both sides are true. Otherwise it returns false. x && y returns false
|| Returns true if either side is true. Returns false if both sides are false x || y returns true
! Inverts the logic level of the value it preceeds !x returns false

Mathematical Operators

You will be familiar with some of the mathematical operators, but there are a few new ones that need to be learned when we start programming. For the examples in the table below lets assume x = 1, y = 2, and z =3.

 

Operator Description Example
+ Adds the values on either side of the operator. x + y returns 3
- Subtracts the value on the right from the value on the left z - y returns 1
* Multiplies the values on either side of the operator. y * z returns 6
/ Divides the value on the left by the value on the right. y / x returns 2
% This is the modulus operator. It returns the remainder of the left side divided by the right side. z % y returns 1
++ Increases an integer value by 1. x++ makes x = 2
-- Decreases an integer value by 1. z-- makes z = 2

Operators used with Pointers

There are two more operators we need to introduce that will become very important later one. These are the operators we use when using pointers. A pointer is a variable that stores an address of another variable. This becomes useful when we need to branch to different parts of a program. We create a pointer in the same way as other variables except we place an asterisk just before the name of the variable. We use the ampersand sign just ahead of a variable name to access the address of the variable instead of the value stored by the variable. The program below demonstrates using these. Notice how "myPointer" is used when values are printed to the screen and what results we get.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *myPointer; //This line creates a pointer named "myPointer"
    int aNumber = 123456;

    myPointer = &aNumber; //This line assigns the address of aNumber to my Pointer
    printf("The address to aNumber is %d\n", myPointer);
    printf("The value stored at the address is %d\n\n", *myPointer);
    return 0;
}

This demo program output demonstrates the use of pointers in C programming by showing the difference between the address and the value of an int
Home | Products | Electronics | Programming | SpazzOuts | Contact
© Copyright 2014 - 2017 SpazzTech LLC. All Rights Reserved
Made with Adobe Muse