Skip to main content

C Operators , precedence 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
Assignmentoperators 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 operations ( OR, AND, EX-OR)

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.
Example: ( university question)
Write a program for evaluating the function Y using ternary operator.
Y=1 x >5
Y=0 x <=5
#include <stdio.h>
int main()
{
    int x=6;
    int Y=(x>5)?1:0;
    printf("%d",Y);
}
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;
}
Output:
Value of (a + b) * c / d is :90
Value of ((a + b) * c) / d is :90
Value of (a + b) * (c / d) is :90
Value of a + (b * c) / d is :50

Largest among 3 numbers using ternary(conditional) operator (university question)
#include <stdio.h>
int main ()
{
    int a,b,c,larg;
    printf("Enter the three numbers.... !\n");
    scanf("%d%d%d",&a,&b,&c);
    larg=a>b?(a>c?a:c):(b>c?b:c);
    printf("Largest=%d\n",larg);
}

Output:
Enter the three numbers.... !
2 3 5
Largest=5

Comments

Popular posts from this blog

KTU Mandatory C programs for Laboratory and Solutions

LIST OF LAB EXPERIMENTS 1. Familiarization of Hardware Components of a Computer 2. Familiarization of Linux environment – Programming in C with Linux 3. Familiarization of console I/O and operators in C     i) Display “Hello World”     ii) Read two numbers, add them and display their sum     iii) Read the radius of a circle, calculate its area and display it 4. Evaluate the arithmetic expression ((a -b / c * d + e) * (f +g))   and display its solution. Read the values of the variables from the user through console 5. Read 3 integer values, find the largest among them. 6. Read a Natural Number and check whether the number is prime or not 7. Read a Natural Number and check whether the number is Armstrong or not 8. Read n integers, store them in an array and find their sum and average 9. Read n integers, store them in an array and search for an element in the    array using an algorithm for Linear Search 10.Read n integers, store them in an array and sort the elements in t

PROGRAMMING IN C KTU EST 102 THEORY AND LAB NOTES

PROGRAMMING IN C  KTU  EST 102  THEORY AND LAB   COMMON FOR ALL BRANCHES About Me Syllabus Theory Syllabus Lab Model Question Paper EST 102 Programmin in C University Question Papers  and evaluation scheme   EST 102 Programming in C  Introduction( Lab) Introduction to C programming Linux History and GNU How to create a bootable ubuntu USB stick Installing  Linux Install Linux within  Windows Virtual Box and WSL Linux Basic Features and Architecture Basic Linux Commands Beginning C Programming Compiling C programs using gcc in Linux Debugging C program using gdb Module 1: Basics of computer hardware and software          Module-1 Reading Material Basics of Computer Architecture Hardware and Software System Software and Application Software  Programming Languages ( High level, Low level and Machine Language) and Translators ( Compiler, Interpreter, Assembler) Algorithm, Flowcharts and Pseudo code Program Development Structured Programming Basics of hardware ( video) Know about Motherboar

Arrays in C-single and multi dimensional arrays- list and matrix

An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list(vector); A two dimensional array is like a table(matrix). We can have more dimensions. Always, Contiguous (adjacent) memory locations are used to store array elements in memory. Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element. Declaring  Single Dimensional Arrays Array variables are declared identically to variables of their data type, except that the variable name is followed by one pair of square [ ] brackets for mentioning dimension of the array.  Dimensions used when declaring arrays in C must be positive integral constants or constant expressions.  In C99, dimensions must still be positive integers, but variables can be used, so long as the variable has a positive value at the time the array is declared. ( Space is allo