OPERATORS
An operator is a symbol that tells the compiler to perform specific
mathematical or logical functions. By definition, an operator performs a certain
operation on operands. An operator needs one or more operands for the
operation to be performed.
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
C language is rich in built-in operators and provides the following types of
operators −
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
We will, in this chapter, look into the way each operator works. Here, you will
get an overview of all these chapters. Thereafter, we have provided
independent chapters on each of these operators that contain plenty of
examples to show how these operators work in C Programming.
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, take a look at this simple expression −
a+b
Here "+" is an arithmetic operator. We shall learn more about arithmetic
operators in C in a subsequent chapter.
The following table shows all the arithmetic operators supported by the C
language. Assume variable A holds 10 and variable B holds 20 then −
Show Examples
Operato
Description Example
r
+ Adds two operands. A + B = 30
− Subtracts second operand from the first. A − B = -10
* Multiplies both operands. A * B = 200
/ Divides numerator by de-numerator. B/A=2
% Modulus Operator and remainder of after an integer division. B%A=0
++ Increment operator increases the integer value by one. A++ = 11
-- Decrement operator decreases the integer value by one. A-- = 9
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.
Show Examples
Operat Examp
Description
or le
(A == B)
Checks if the values of two operands are equal or not. If yes, then the
== is not
condition becomes true.
true.
Checks if the values of two operands are equal or not. If the values are (A != B)
!=
not equal, then the condition becomes true. is true.
(A > B)
Checks if the value of left operand is greater than the value of right
> is not
operand. If yes, then the condition becomes true.
true.
Checks if the value of left operand is less than the value of right (A < B)
<
operand. If yes, then the condition becomes true. is true.
(A >= B)
Checks if the value of left operand is greater than or equal to the value
>= is not
of right operand. If yes, then the condition becomes true.
true.
Checks if the value of left operand is less than or equal to the value of (A <= B)
<=
right operand. If yes, then the condition becomes true. is true.
Explore our latest online courses and learn new skills at your own pace. Enroll
and become a certified expert to boost your career.
Logical Operators
These 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.
Show Examples
Operat Examp
Description
or le
(A &&
Called Logical AND operator. If both the operands are non-zero, then
&& B) is
the condition becomes true.
false.
Called Logical OR Operator. If any of the two operands is non-zero, (A || B)
||
then the condition becomes true. is true.
Called Logical NOT Operator. It is used to reverse the logical state of !(A &&
! its operand. If a condition is true, then Logical NOT operator will make B) is
it false. true.
We will discuss more about Logical Operators in C in a subsequent chapter.
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.
Bitwise operator works on bits and perform bit−by−bit operation. The truth
tables for &, "|", and "^" are as follows −
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume A = 60 and B = 13 in binary format, they will be as follows −
A = 0011 1100
B = 0000 1101
------------------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The following table lists the bitwise operators supported by C. Assume variable
'A' holds 60 and variable 'B' holds 13, then −
Show Examples
Operat Examp
Description
or le
(A & B)
Binary AND Operator copies a bit to the result if it exists in both = 12,
&
operands. i.e., 0000
1100
| Binary OR Operator copies a bit if it exists in either operand. (A | B) =
61, i.e.,
0011
1101
(A ^ B)
Binary XOR Operator copies the bit if it is set in one operand but not = 49,
^
both. i.e., 0011
0001
(~A ) =
Binary One's Complement Operator is unary and has the effect of ~(60),
~
'flipping' bits. i.e,. -
0111101
A << 2 =
Binary Left Shift Operator. The left operands value is moved left by the 240 i.e.,
<<
number of bits specified by the right operand. 1111
0000
A >> 2 =
Binary Right Shift Operator. The left operands value is moved right by 15 i.e.,
>>
the number of bits specified by the right operand. 0000
1111
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.
The following table lists the assignment operators supported by the C language
−
Show Examples
Operat Exampl
Description
or e
C=A+
B will
Simple assignment operator. Assigns values from right side operands assign the
=
to left side operand value of
A + B to
C
C += A is
Add AND assignment operator. It adds the right operand to the left equivalent
+=
operand and assign the result to the left operand. to C = C
+A
C -= A is
Subtract AND assignment operator. It subtracts the right operand from equivalent
-=
the left operand and assigns the result to the left operand. to C = C -
A
*= Multiply AND assignment operator. It multiplies the right operand C *= A is
equivalent
with the left operand and assigns the result to the left operand. to C = C
*A
C /= A is
Divide AND assignment operator. It divides the left operand with the equivalent
/=
right operand and assigns the result to the left operand. to C = C /
A
C %= A
is
Modulus AND assignment operator. It takes modulus using two
%= equivalent
operands and assigns the result to the left operand.
to C = C
%A
C <<= 2
is same as
<<= Left shift AND assignment operator.
C = C <<
2
C >>= 2
is same as
>>= Right shift AND assignment operator.
C = C >>
2
C &= 2 is
&= Bitwise AND assignment operator. same as C
=C&2
C ^= 2 is
^= Bitwise exclusive OR and assignment operator. same as C
=C^2
C |= 2 is
|= Bitwise inclusive OR and assignment operator. same as C
=C|2
Hence, the expression "a = 5" assigns 5 to the variable "a", but "5 = a" is an
invalid expression 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
Misc Operators ↦ sizeof & ternary
are =, +=, -=, *=, /=, %=, &=, |=, and ^=.
Besides the operators discussed above, there are a few other important
operators including sizeof and ? : supported by the C Language.
Show Examples
Operato
Description Example
r
sizeof(a), where a is integer, will return
sizeof() Returns the size of a variable.
4.
&a; returns the actual address of the
& Returns the address of a variable.
variable.
* Pointer to a variable. *a;
If Condition is true ? then value X :
?: Conditional Expression.
otherwise value Y
Operators Precedence in C
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.
Show Examples
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right
Other Operators in C
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 following expression −
a+b*c
The multiplication operand takes precedence over the addition operator.
What is a Source Code?
Source code refers to high-level code or assembly code that is generated by
humans/programmers. Source code is easy to read and modify. It is written by the
programmer by using any High-Level Language or Intermediate language which is
human-readable. The source code contains comments that the programmer makes for
better understanding.
Source code is provided to a language translator which converts it into machine-
understandable code which is called machine code or object code. The computer
cannot understand direct source code, a computer understands machine code and
executes it. It is considered a fundamental component of a computer. In simple we
can say source code is a set of instructions/commands and statements which is
written by a programmer by using a computer programming language like C, C+
+, Java, Python, Assembly language, etc.So statements written in any programming
language are termed source code.
Key Features of Source Code
They are coded in high-level programming languages.
Frequently found in a format that can be read and understood by human beings.
Has to be assembled or set out to be usable on a machine.
Both are fixed and alterable by developers.
What is an Object Code?
Object code refers to low-level code that is understandable by machine. Object code
is generated from source code after going through a compiler or other translator. It is
in executable machine code format. Object code contains a sequence of machine-
understandable instructions that which Central Processing Unit understands and
executes.
The object file contains the object code. It is considered as one more machine code.
Some object file examples are common object file format (COFF), COM files, and
“.exe” files. It is the output of a compiler or other translator. We can understand source
code but we can not understand object code as it is not in plain text like source code
rather it is in binary formats.
Key Features of Object Code
It is coded in machine language which is in the form of binary language of 0s and
1s.
It can only be read by another computer or other programmed machines and is not
comprehensible by human beings.
Executed directly from the computer hardware and can be operated on with
complex logical as well arithmetic operations.
Created by linking the source code files to create an executable file with the help of
a compiler utility program.
Difference Between Source Code and Object Code
SOURCE CODE OBJECT CODE
Source code is generated by human or Object code is generated by compiler or
programmer. other translator.
Source code is high level code. Object code is low level code.
Source code is written in plain text by
Object code is translated code of source
using some high level programming
code. It is in binary format.
language.
Object code is not human
Source code is human understandable.
understandable.
Source code is not directly Object code is machine understandable
understandable by machine. and executable.
It is written in a high-level language like
It is written in machine language through
C, C++, Java, Python, etc., or assembly
compiler or assembler or other translator.
language.
It can be easily modified. It can not be modified.
It contains comments for better It does not contain comments for
understanding by programmer. understanding by machine.
It contains less number of statements It contains more number of statements
than object code. than source code.
SOURCE CODE OBJECT CODE
It is less close. towards machine. It is more close towards machine.
Performance of source code is less than Performance of object code is more than
object code as it is less close towards source code as it is more close towards
machine. machine.
Source code is input to compiler or any Object code is output of compiler or any
other translator. other translator.
Variables
In C, a variable is a named memory location that stores data values. Variables can hold
different types of data, such as integers, characters, strings, and floating-point
numbers.
Declaring variables
To declare a variable, use the syntax <data type> <variable-list>;
For example, int a, b, sum;
Use keywords like int for integers, float for floating-point values, and char for
characters
Naming variables
Variable names can contain letters, digits, and underscores
Variable names must start with a letter or underscore
Variable names are case-sensitive
Variable names cannot contain spaces or special characters like !, #, %
Reserved words (such as int) cannot be used as names
Using variables
Variables can be modified and reused many times during the program execution
The scope of a variable is the block or region in the program where it is declared,
defined, and used
Outside this region, the variable is treated as an undeclared identifier
Examples of variables
int age = 25; stores the number 25 in the variable named age
printf("%d", age)
Syntax
In C, we have to declare the type of data the variable would store along with the
name while creating a variable:
data_type variable_name;
This data_type decides how much memory a variable need. We can choose
the data types provided by C language to store different type of data in the
variable.
We can also create multiple variables in a single statement by separating them
using comma:
data_type v1, v2, v3 ….;
where v1, v2 … are the names for variables.
Example
int var; // integer variable
char a; // character variable
float fff; // float variable
The above syntax is called variable definition. In this, a variable is only named
and allocated some memory to it. No data is stored in it till now.
The data is first entered in the variable in the process called variable
initialization. It is nothing but assigning some initial value to it using assignment
operator (=).
variable_name = value;
It can also be combined with variable definition:
data_type variable_name; = value;
In C, variables can store data belonging to any of the types it recognizes.
Hence there are as many number of types of variables as the number of data
types in C.
Type & Description
Sr.No
char
1
Typically a single octet(one byte). It is an integer type.
int
2
The most natural size of integer for the machine.
float
3
A single-precision floating point value.
double
4
A double-precision floating point value.
void
5
Represents the absence of type.
C programming language also allows to define various other types of variables
such as Enumeration type, Pointer type, Array type, Structure type, Union type,
etc.