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

ceri188

ceri188

ceri188

KUPU178

HOKI311

RATU311

KUPU178

RATU311

RATU311

kupu178

kupu178

HOKI311

togel

kupu178

ratu311

mix parlay

ceri188

slot online

kupu178

THOR311

KUPU178

RATU311

THOR311

THOR311

THOR311

THOR311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

kupu178

hoki311

THOR311

hoki311

togel online

kupu178

slot mahjong

ceri188

slot server thailand

ratu311

ceri188

ceri188

RATU311

punjabi virasat

mimcord.com

Centrum Medyczne

www.vrcorporate.in

phbalance.vn

THOR311

KUPU178

daftar slot gacor

hoki311

togel hongkong

kupu178

slot mahjong

ratu311

slot gacor

kupu178

kupu178

ceri188

kupu178

ceri188

THOR311

KUPU178

KUPU178

KUPU178

RATU311

kupu178

RATU311

RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

slot online

kupu178

thor311

thor311

kupu178

kupu178

RATU311

KUPU178

THOR311

THOR311

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

ratu311

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

ceri188

THOR311

THOR311

RATU311

toto macau

KUPU178

slot gacor

HOKI311

sabung ayam online

RATU311

SLOT LUCKY NEKO

CERI188

Slot Gacor

KUPU178

RATU311

RATU311

Link Spaceman

CERI188

Slot Gacor Online

THOR311

Mahjong ways

KUPU178

THOR311

THOR311

Live Casino

HOKI311

CERI188

CERI188

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

CERI188

HOKI311 SLOT

TOTO TOGEL TOKYO

CERI188

CERI188 SITE

CERI188

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

Toto Togel

KUPU178

Slot Mahjong

CERI188

CERI188

CERI188

Slot Sweet Bonanza 2500

KUPU178

SLOT ONLINE

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 10 GAME SLOT TERBAIK

KUPU178

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

SITUS BOLA

CERI188

KUPU178

THOR311

RATU311

THOR311

THOR311

HOKI311

KUPU178

RATU311

THOR311

CERI188

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

Slot Gacor Online

KUPU178

Mix Parlay

CERI188

CERI188

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

RATU311

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

KUPU178

KUPU178

RATU311

CERI188

RATU311: Alternatif karnpuracollege

RATU311

RATU311

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311 PRO

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

THOR311

RATU311 NONTON BOLA

RATU311

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

CERI188

RATU311 judi bola

KUPU178

situs judi bola

HOKI311

RATU311

THOR311

HOKI311

KUPU178

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

slot thailand

judi bola

KUPU178

slot gacor

slot gacor

yakuza303

slot gacor

RATU311

THOR311