Operators in C with examples


Operators in C with examples

In this tutorial, you will learn the operators in c with examples one by one. C language uses different types of operators as given below

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Increment and decrement operators
  • Assignment operators
  • Conditional operator
  • Bitwise operators

Arithmetic Operators:- Arithmetic operators are used to perform various kinds of arithmetic operations.

Also read, constant in C with example

Arithmetic operators used in C language are given below

OperatorMeaningExampleResult
+addition5+27
subtraction5-23
*multiplication5*210
/division5/22
%modulus operator to get remainder in integer division5%21

Note that when the two operands (5 and 2) are integers, the result will be an integer. To obtain the real value or a floating-point number in the result, at least one of the operands must be float or double.

Below is an example to find float or real value
5.0/2.0 or
5/2.0 or
5.0/2 will produce the result of 2.5
Below is an example to find the remainder of modulus operations
7 % 4 will produce the result 3
An expression consisting of numeric variables, constants, and arithmetic operators is called arithmetic expression. The value produced by evaluating an arithmetic expression will always be numeric i.e. integer, float or double, etc.

Relational operators:- Relational operators are used to compare the values of operands or expressions to obtain the result in a boolean value i.e. true or false.

Relational operator used in C language are given below

OperatorMeaningExampleResult
<less than5<2false
>greater than5>2true
<=less than or equal to5<=2false
>=greater than or equal to5>=2true
==equal to5==2false
!=not equal to5!=2true

Note that the boolean value “true” is represented by 1 in C language and false is represented by 0.

Logical operators:- Logical operators are used to combining more relational operations to form a complex expression called logical expression. A value obtained by evaluating a logical expression will always be logical i.e. either true or false.

Logical operator used in C language are given below

OperatorMeaningExampleResult
&&logical and(5<2) && (5>3)false
||logical or(5<2) || (5>3)true
!logical not!(5<2)true

Note that the logical not(!) is a unary operator which requires only one operand. it is also referred to as an inverter that converts the value of operands from true to false and vice versa.

Increment and Decrement operators(++ and –):- Increment operators are used to increase the value of an integer variable by 1. The decrement operator is used to deduct the value of an integer variable by 1.

Consider the following example

m=15;
m++ or ++m will produce the same result m=16
m– or –m will produce the same result m=14;
Note that m++ or m– are referring to the postfix increment and decrement operation and ++m or –m are referring to the prefix increment and decrement operation.

Consider the following example
m=15;
k = m++; // assigns the value 15 to k and increases the value of m to 16 thereafter.
m=15;
k = ++m; // increases the value of m to 16 and then assigns 16 to k;

Assignment Operators:- Assignment operators are used to perform arithmetic operations while assigning a value to a variable.

Assignment operators used in C language are given below

OperatorExampleEquivalent ExpressionResult
+=m+=10m = m+1025
-=m -= 10m = m-105
*=m*=10m = m*10150
/=m/=10m = m/101
%=m%=10m = m%105

Conditional Operator (?:) or Ternary Operator:-

A conditional operator is used to check a condition and select a value depending on the value of the condition. Normally, the selected value will be assigned to a variable that has the following form
variable = (condition)? value 1:value 2;
when this operator is executed by the computer, the value of the condition is evaluated. If it is true then value 1 is assigned to the variable, otherwise, value 2 is assigned to the variable.

Consider the following example

big = (a>b) ? a:b
In this operation, the computer checks the value of the condition (a>b); if it is true ‘a’ is assigned to big; otherwise, b is assigned to big. This is similar to the usual method of writing a condition which is shown below

if(a>b)
big = a;
else
big = b;

Bitwise operators:- Bitwise operators are used to perform operations at the binary digit level. These operators are not commonly used and are used only in special applications where optimized use of storage is required.

Bitwise operators used in C language are given below

OperatorMeaning
<<Shifts the bits to the left
>>Shifts the bits to the right
~bitwise inversion
&bitwise logical and
|
|
bitwise logical or
^bitwise exclusive or

Additional Operators used in C language

There are another two operations that are used in the c program.

  • sizeof operator.
  • comma operator.
    sizeof operator:- sizeof operator is used to finding the number of bytes occupied by a variable or data type in the computer memory.
    Some examples are given below
    int m, x[50];
    declares an array of 50 elements and a variable m.
    sizeof(float);
    returns 4 which is the number of bytes occupied by float type data.
    sizeof(m);
    returns 2 which is the number of bytes occupied by the integer variable m.
    sizeof(x);
    returns 100 which is the number of bytes occupied by the array x.

Comma Operator:- The comma operator is used to linking related expressions to make the program more compact. Related expressions are evaluated from left to right and the value of the right most expression is returned as the value of the expression.
Consider the below example
temp = x;
x = y;
y = temp;

The above lines can be written as a single expression with the help of a comma operator as shown below
temp = x,x = y, y = temp;

Conclusion:- I hope this tutorial will help you to understand the overview of operators in C. If there is any doubt then please leave a comment below.


Leave a Comment

Live Casino Online

CERI188

slot gacor

slot gacor

live casino

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

CERI188

Slot Gacor Online

KUPU178

Slot Thailand

CERI188

CERI188

slot thailand

parlay

parlay

situs sabung ayam

CERI188 TOTO 4D

CERI188

THOR311 RTP

RATU311

THOR311

THOR311

THOR311

RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

RATU311

Mahjong Ways 2

CERI188

CERI188

sv388

CERI188

KUPU178 TOTO SLOT

CERI188

CERI188 TOTO Singapore

KUPU178

sabung ayam online

THOR311

parlay

RATU311

KUPU178

RATU311

RATU311

KUPU178

KUPU178

KUPU178

RATU311

CERI188

CERI188

KUPU178

RATU311: Alternatif karnpuracollege

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

RATU311

RATU311

RATU311

judi bola

live casino

sv388

sv388

sabung ayam online

sabung ayam online

Thor311 Terbaru

CERI188: Taruhan Bola Sbobet

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188

parlay

KUPU178

THOR311

RATU311

RATU311

CERI188

CERI188: Masuk Akun Slot Super Cepat

KUPU178: Slot Online Mahjong Bet 200

THOR311 PRO

THOR311

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

RATU311

KUPU178

THOR311

KUPU178

THOR311

RATU311

HOKI311

KUPU178

KUPU178

RATU311

THOR311 TOTO4D

KUPU178

Live Casino Roullate

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

RATU311

CERI188

DAFTAR KUPU178

LINK DAFTAR CERI188

CERI188

KUPU178

CERI188

CERI188

RATU311

KUPU178

THOR311 DOMINO QQ

judi bola online

Mix Parlay

CERI188

CERI188

RATU311

live casino online

RATU311

Mix Parlay

Slot PG Soft

CERI188

RATU311

RATU311

CERI188

THOR311

CERI188

THOR311

Game Thor311

THOR311

HOKI311

live casino

sv388

HOKI311

HOKI311

slot mahjong

Thor311 Alternatif

RATU311

HOKI311

HOKI311

HOKI311

HOKI311

CERI188

THOR311 RESMI

THOR311 AKSES

parlay

HOKI311

THOR311

RATU311 NONTON BOLA

CERI188 Link RTP LIVE

THOR311

Thor311 Live Casino

CERI188

akses VIP THOR311

THOR311

link daftar THOR311

THOR311

situs thor311

RATU311

HOKI311 Link SABUNG AYAM RESMI

HOKI311 Link QRIS EWALLET

RATU311

parlay

HOKI311 Link RTP Gacor

HOKI311 Link Apk Resmi

CERI188

KUPU178 Link Daftar

CERI188

CERI188

CERI188

CERI188

CERI188

HOKI311

RATU311

RATU311

HOKI311 Lapak Arena Slot

RATU311 judi bola

HOKI311 Link Spaceman Online

HOKI311 Link Daftar Resmi

CERI188 Toto SGP Online

KUPU178

HOKI311 TEMBAK IKAN ONLINE

KUPU178

HOKI311 Link Login Alternatif

HOKI311 Live Casino Online

HOKI311 Situs Slot Gacor

THOR311

THOR311

situs judi bola

CERI188

link login CERI188

link login CERI188

CERI188

THOR311

HOKI311

CERI188

parlay

THOR311

RATU311

HOKI311

HOKI311

HOKI311

RATU311

THOR311

KUPU178

HOKI311

KUPU178

KUPU178

HOKI311

parlay

slot gacor

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

HOKI311

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

RATU311

KUPU178

KUPU178

THOR311

KUPU178

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

KUPU178

KUPU178

HOKI311

THOR311

KUPU178

THOR311

THOR311

slot thailand

judi bola

THOR311

KUPU178

THOR311

THOR311

slot gacor

slot gacor

yakuza303

unobet77

slot gacor

slot gacor

RATU311

parlay

parlay

slot gacor

parlay

THOR311