C Operators: Arithmetic, Logical, Conditional and more

4 programming

C Programming Operators

An operator is a symbol which operates on a value or a variable. For example: + is an operator to perform addition.

C programming has wide range of operators to perform various operations. For better understanding of operators, these operators can be classified as:

C Arithmetic Operators

An arithmetic operator performs mathematical operations such as addition, subtraction and multiplication on numerical values (constants and variables).

Example #1: Arithmetic Operators

The operators +, – and * computes addition, subtraction and multiplication respectively as you might have expected.

In normal calculation, 9/4 = 2.25 . However, the output is 2 in the program.

It is because both variables a and b are integers. Hence, the output is also an integer. The compiler neglects the term after decimal point and shows answer 2 instead of 2.25.

The modulo operator % computes the remainder. When a = 9 is divided by b = 4 , the remainder is 1. The % operator can only be used with integers.

Increment and decrement operators

C programming has two operators increment ++ and decrement — to change the value of an operand (constant or variable) by 1.

Increment ++ increases the value by 1 whereas decrement — decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand.

Example #2: Increment and Decrement Operators

Here, the operators ++ and — are used as prefix. These two operators can also be used as postfix like a++ and a– . Visit this page to learn more on how increment and decrement operators work when used as postfix.

C Assignment Operators

An assignment operator is used for assigning a value to a variable. The most common assignment operator is =

Example #3: Assignment Operators

C Relational Operators

A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.

Relational operators are used in decision making and loops.

Example #4: Relational Operators

C Logical Operators

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming.

Example #5: Logical Operators

Explanation of logical operator program

  • (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true).
  • (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
  • (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
  • (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
  • !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).
  • !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).

Bitwise Operators

During computation, mathematical operations like: addition, subtraction, addition and division are converted to bit-level which makes processing faster and saves power.

Bitwise operators are used in C programming to perform bit-level operations.

Other Operators

Comma Operator

Comma operators are used to link related expressions together. For example:

The sizeof operator

The sizeof is an unary operator which returns the size of data (constant, variables, array, structure etc).

Example #6: sizeof Operator

C Ternary Operator (?:)

A conditional operator is a ternary operator, that is, it works on 3 operands.

Conditional Operator Syntax

The conditional operator works as follows:

  • The first expression conditionalExpression is evaluated first. This expression evaluates to 1 if it's true and evaluates to 0 if it's false.
  • If conditionalExpression is true, expression1 is evaluated.
  • If conditionalExpression is false, expression2 is evaluated.

Example #7: C conditional Operator

Other operators such as & (reference operator), * (dereference operator) and -> (member selection) operator will be discussed in C pointers.

Check out these examples to learn more:

C Programming

C Introduction

C Flow Control

C Functions

C Programming Arrays

C Programming Pointers

C Programming Strings

Structure And Union

C Programming Files

Additional Topics

Receive the latest tutorial to improve your programming skills

You have successfully subscribed to our newsletter.

Get Latest Updates on Programiz

You have successfully subscribed to our newsletter.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.