Skip to main content

C Operators and Expressions


C – Operators and Expressions
  • The symbols which are used to perform logical and mathematical operations in a C program are called C operators.
  • These C operators join individual constants and variables to form expressions.
  • Operators, functions, constants and variables are combined together to form expressions.
  • Consider the expression A + B * 5. where, +, * are operators, A, B are variables, 5 is constant and A + B * 5 is an expression.

Types of C operators:

C language offers many types of operators. They are,
Types of Operators
Description
Arithmetic_operators These are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus
Assignment_operators These are used to assign the values for the variables in C programs.
Relational operators These operators are used to compare the value of two variables.
Logical operators These operators are used to perform logical operations on the given two variables.
Bit wise operators These operators are used to perform bit operations on given two variables.
Conditional (ternary) operators Conditional operators return one value if condition is true and returns another value is condition is false.
Increment/decrement operators These operators are used to either increase or decrease the value of the variable by one.
Special operators &, *, sizeof( ) and ternary operators.


Arithmetic Operators in C:

C Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus in C programs.
Arithmetic Operators/Operation
Example
+ (Addition)
A+B
– (Subtraction)
A-B
* (multiplication)
A*B
/ (Division)
A/B
% (Modulus)
A%B

Assignment operators in C:

  • In C programs, values for the variables are assigned using assignment operators.
  • For example, if the value “10” is to be assigned for the variable “sum”, it can be assigned as “sum = 10;”
  • There are 2 categories of assignment operators in C language. They are,
    1.Simple assignment operator ( Example: = )
    2. Compound assignment operators ( Example:+=, -=, *=, /=, %=, &=, ^= )
Operators
Example/Description
=
sum = 10;
10 is assigned to variable sum
+=
sum += 10;
This is same as sum = sum + 10
-=
sum -= 10;
This is same as sum = sum – 10
*=
sum *= 10;
This is same as sum = sum * 10
/=
sum /= 10;
This is same as sum = sum / 10
%=
sum %= 10;
This is same as sum = sum % 10
&=
sum&=10;
This is same as sum = sum & 10
^=
sum ^= 10;
This is same as sum = sum ^ 10

Relational operators in C:

Relational operators are used to find the relation between two variables. i.e. to compare the values of two variables in a C program.
Operators
Example/Description
>
x > y (x is greater than y)
<
x < y (x is less than y)
>=
x >= y (x is greater than or equal to y)
<=
x <= y (x is less than or equal to y)
==
x == y (x is equal to y)
!=
x != y (x is not equal to y)

Logical operators in C:

  • These operators are used to perform logical operations on the given expressions.
  • There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).
Operators
Example/Description
&& (logical AND)
(x>5)&&(y<5)
It returns true when both conditions are true
|| (logical OR)
(x>=10)||(y>=10)
It returns true when at-least one of the condition is true
! (logical NOT)
!((x>5)&&(y<5))
It reverses the state of the operand “((x>5) && (y<5))”
If “((x>5) && (y<5))” is true, logical NOT operator makes it false

Bit wise operators in C:

  • These operators are used to perform bit operations. Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits.

Below are the bit-wise operators in C language.

  • & – Bitwise AND
  • | – Bitwise OR
  • ~ – Bitwise NOT
  • ^ – XOR
  • << – Left Shift
  • >> – Right Shift

Truth table for bit wise operation&Bit wise operators:

truth-table

Conditional or ternary operators in C:

  • Conditional operators return one value if condition is true and returns another value is condition is false.
  • This operator is also called as ternary operator.
Syntax : (Condition? true_value: false_value);
Example : (A > 100 ? 0 : 1);
  • In above example, if A is greater than 100, 0 is returned else 1 is returned.This is equal to if else conditional statements.
Increment/decrement operators in C:
  • Increment operators are used to increase the value of the variable by one and decrement operators are used to decrease the value of the variable by one in C programs.
  • Syntax:
    Increment operator: ++var_name; (or) var_name++;
    Decrement operator: – -var_name; (or) var_name – -;
  • Example:
    Increment operator :++ i ; i ++ ;
    Decrement operator : – – i ; i – – ;

Special Operators in C:

Below are some of the special operators that the C programming language offers.
Operators
Description
&
This is used to get the address of the variable.
Example : &a will give address of a.
*
This is used as pointer to a variable.
Example : * a where, * is pointer to the variable a.
sizeof ()
This gives the size of the variable.
Example : sizeof (char) will give us 1.


Operator Precedence:
Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

PrecedenceOperatorDescriptionAssociativity
1++ --Suffix/postfix increment and decrementLeft-to-right
()Function call
[]Array subscripting
.Structure and union member access
->Structure and union member access through pointer
(type){list}Compound literal(C99)
2++ --Prefix increment and decrementRight-to-left
+ -Unary plus and minus
! ~Logical NOT and bitwise NOT
(type)Type cast
*Indirection (dereference)
&Address-of
sizeofSize-of[note 1]
_AlignofAlignment requirement(C11)
3* / %Multiplication, division, and remainderLeft-to-right
4+ -Addition and subtraction
5<< >>Bitwise left shift and right shift
6< <=For relational operators < and ≤ respectively
> >=For relational operators > and ≥ respectively
7== !=For relational = and ≠ respectively
8&Bitwise AND
9^Bitwise XOR (exclusive or)
10|Bitwise OR (inclusive or)
11&&Logical AND
12||Logical OR
13?:Ternary conditionalRight-to-Left
14=Simple assignment
+= -=Assignment by sum and difference
*= /= %=Assignment by product, quotient, and remainder
<<= >>=Assignment by bitwise left shift and right shift
&= ^= |=Assignment by bitwise AND, XOR, and OR
15,CommaLeft-to-right
Example:
#include <stdio.h>
main() {
   int a = 20;
   int b = 10;
   int c = 15;
   int d = 5;
   int e;
    e = (a + b) * c / d;      // ( 30 * 15 ) / 5
   printf("Value of (a + b) * c / d is :%d\n",  e );
   e = ((a + b) * c) / d;    // (30 * 15 ) / 5
   printf("Value of ((a + b) * c) / d is  :%d\n" ,  e );
   e = (a + b) * (c / d);   // (30) * (15/5)
   printf("Value of (a + b) * (c / d) is  :%d\n",  e );
   e = a + (b * c) / d;     //  20 + (150/5)
   printf("Value of a + (b * c) / d is  :%d\n" ,  e );
   return 0;
}


Comments

Popular posts from this blog

Files in C

A file is a repository of data that is stored in a permanent storage media, mainly in secondary memory. In order to use the files we should learn how to read information from a file and how to write information into a file. A very important concept in C is the stream.The stream is a common logical interface to the various devices( files).A stream is linked to a file while using an open operation. A stream is disassociated from a file while using a close operation. The current location, also referred to as the current position, is the location in a file where the next fie access will occur.There are two types of streams text and binary. The following are some difference between text and binary files ·Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable. ·A newline(\n) character is converted into the carriage return-linefeed combination before being written to the d…

KTU C programming question paper and evaluation scheme

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY SECOND SEMESTER B.TECH DEGREE EXAMINATION, MAY 2017 CS 100 COMPUTER PROGRAMMING (CS, IT) SCHEME OF EVALUATION
PART A 1 An identifier is a sequence of characters invented by the programmer or to identify or name a specific object. The sequence of characters may be letters, digits, and special character ‘_’known as an underscore Rules: i)Identifiers should start with alphabets. ii)Identifiers are case sensitive iii)A numeric digit should not be the first character iv)Identifier name should not be a keyword v)Identifier may be of any reasonable length 1mark






2mark 2 Associativity defines the direction, left to right or right to left in which operator act upon its operands Unary operators have associativity is from right to left. For examplein the expression &--x, pre decrement works first and then address of operator works Direction + example<

Linear and Binary Search in C

Searching is one of the most common problems that arise in computing. Searching is the algorithmic process of finding a particular item in a collection of items. A search typically answers either True or False as to whether the item is present. On occasion it may be modified to return where the item is found. Search operations are usually carried out on a key field. Consider searching for a given value k in an array A of size n. There are 2 basic approaches: sequential search and binary search.
Linear (Sequential) Search When data items are stored in a collection such as a list or array , we say that they have a linear or sequential relationship. Each data item is stored in a position relative to the others. In C array, these relative positions are the index values of the individual items. Since these index values are ordered, it is possible for us to visit them in sequence. This process gives rise to our first searching technique, the sequential search. Starting at the first item in …