C - Operators



In addition to the literals and identifiers (such as keywords, variables, function names etc.), operators are also an important part of C language. The operators in C are certain special symbols that are predefined to perform a certain operation on one or more operands. C language has a rich set of built-in operators, classified in different categories as arithmetic, comparison, bitwise etc.

C Operators

By definition, an operator performs a certain operation on operands. An operator needs one or more operands for the operation to be performed. The following figure explains the relation between the operator and the operand −

Operand

Depending on how many operands are required to perform the operation, operands are called as unary, binary or ternary operators. They need one, two or three operands respectively.

  • Unary operators: ++ (increment), -- (decrement), ! (NOT), ~ (compliment), & (address of), * (dereference)

  • Binary operators: arithmetic, logical and relational operators except !

  • Ternary operators: The ? operator

Operators in C are classified in the following categories −

Arithmetic Operators

We are most familiar with the arithmetic operators. These operators are used to perform arithmetic operations on operands. The most common arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). In addition, the modulo (%) is an important arithmetic operator that computes the remainder of a division operation. Arithmetic operators are used in forming an arithmetic expression. These operators are binary in nature in the sense they need two operands, and they operate on numeric operands, which may be numeric literals, variables or expressions. For example, in the expression.

a+b

Here + is an arithmetic operator. We shall learn more about arithmetic operators in C in a subsequent chapter.

Relational Operators

We are also acquainted with relational operators while learning secondary mathematics. These operators are used to compare two operands and return a boolean value (true or false). They are used in a boolean expression. The most common relational operators are less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=), equal to (==), and not equal to (!=). Relational operators are also binary operators, needing two numeric operands. For example, in the Boolean expression.

a>b

here > is a relational operator.

We shall learn more about with relational operators and their usage in one of the following chapters.

Logical Operators

These logical operators are used to combine two or more boolean expressions. We can form a compound Boolean expression by combining Boolean expression with these operators. An example of logical operator is as follows −

a>=50 && b>=50

The most common logical operators are AND (&&), OR(||), and NOT (!). Logical operators are also binary operators.

Bitwise Operators

Bitwise operators let you manipulate data stored in computer’s memory. These operators are used to perform bit-level operations on operands. The most common bitwise operators are AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). Here the ~ operator is a unary operator, while most of the other bitwise operators are binary in narure.

Assignment Operators

As the name suggests, an assignment operator ‘assigns’ or sets a value to a named variable in C. These operators are used to assign values to variables. The = symbol is defined as assignment operator in C, however it is not to be confused with its usage in mathematics.

Hence, the expression

a=5

assigns 5 to the variable a, but

5=a

Is invalid in C

The = operator, combined with the other arithmetic, relational and bitwise operators form augmented assignment operators. For example, the += operator is used as add and assign operator. The most common assignment operators are =, +=, -=, *=, /=, %=, &=, |=, and ^=.

Other Operators

Apart from the above, there are a few other operators in C that are not classified into any of the above categories. For example, the increment and decrement operators (++ and --) are unary in nature and can appear as a prefix or postfix to the operand. The operators that work with the address of memory location such as the address-of operator (&) and the dereference operator (*). The sizeof operator (sizeof) appears to be a keyword but really an operator. C also has the type cast operator (()) that forces the type of an operand to be changed. C also uses the dot (.) and the arrow (->) symbols as operators when dealing with derived data types such as struct and union.

The C99 version of C introduced a few additional operators such as auto, decltype.

A single expression in C may have multiple operators of different type. The C compiler evaluates its value based on the operator precedence and associativity of operators. For example, in the expression.

a+b*c

The multiplication operand takes precedence over the addition operator.

We shall understand these properties with examples in a subsequent chapter.

Many other programming languages, which are called C-family languages (such as C++, C#, Java, Perl and PHP) have an operator nomenclature that is similar to C.

Advertisements