Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Lesson 3

C Operators – Types and Examples


The C programming language comes with very rich built-in functions. There is a vast
use of operators in all programming languages. Operators are a useful and powerful
feature of the C/C++ programming language. Without them, the functionality of C is
useless. It makes it easy for a programmer to write codes very efficiently and easily.
What are Operators?

An operator is a symbol that operates on a variable or value. It is used for performing


certain operations like arithmetical, logical, relational, etc.

When a programmer wants to perform some type of mathematical operation then you
have to use operators.

The purpose of operators is mainly for mathematical and logical calculations.


Types of Operators in C

Arithmetic Operators in C. ...

Increment Operator in C. ...

Decrement Operator in C. ...

Assignment Operators in C. ...

Relational Operators in C. ...

Logical Operators in C. ...

Conditional Operator in C. ...

Bitwise Operator in C.

1. Arithmetic Operators in C

The purpose of this operator is to perform mathematical operations like addition,


subtraction, division, multiplication etc.

1|P ag e
Lesson 3

2|P ag e
Lesson 3

2. Increment Operator in C

Mainly used for incrementing the value of an integer. It is represented by the ‘++’
operator. ++x will increase the value of the variable x instantly. But if it is placed after
the variable then it gets increased before the execution of the next statement.

3. Decrement Operator in C
Mainly used for decrementing the value of an integer. It is represented by the ‘–’
operator. –x will decrease the value of the variable x instantly. But if it is placed after the
variable then it gets decreased before the execution of the next statement.

4. Assignment Operators in C
The purpose of this operator is to assign value to a variable. The most used assignment
operator is “=”.

3|P ag e
Lesson 3

Write a C program that accepts principle, rate of interest, time and compute the
simple interest.

4|P ag e
Lesson 3

6. Logical Operators in C
In the C programming language, Logical operators are mostly used for decision
making. A logical operator returns either 0 or 1 whether the condition is true or false.

Operator What it does


&& (Logical AND) True only if all conditions satisfy.
|| (Logical OR) True only if either one condition satisfies.
! (Logical Not) True only if the operand is 0.

7. Conditional Operator in C
Also known as Ternary operator. The main purpose of conditional operators is in
decision making statements. It is similar to an if-else statement.

Syntax of a conditional operator:-

Expression1? statement1: statement2;

Expression1 is a boolean expression, it can be either true or false.

If the Expression1 returns true then statement1 will get executed.

If the Expression1 returns false then statement2 will get executed.

5|P ag e
Lesson 3

Operator Precedence in C

In between operators, some have higher precedence and some have lower precedence.
For example, Division has a higher precedence than subtraction operator.

6|P ag e
Lesson 3

Operators which have higher precedence are at the top of the table. And operators
which have lower precedence are at the bottom.

Variables in C language
 a Variable is the name of the place in the memory in which we store the data or
information.
 Unlike constant, values stored in variables can be changed, we can change the
value of a variables during the execution of a program.

7|P ag e
Lesson 3

5 types of variables in C
i. Local variables
ii. Global variables
iii. Static variables
iv. Automatic variables
v. External variables

Local and Global Variable in C Language

 And in C language we cannot use any variable without declaring it.

Variable Declaration in C
Syntax type variable name;

Variable Declaration and initialization

Rules on choosing a variable name

 A variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9, and
the underscore character.
 The first character must be a letter or underscore.
 Blank spaces cannot be used in variable names.
 Special characters like #, $ are not allowed.
 C keywords cannot be used as variable names.
 Variable names are case sensitive.
 Values of the variables can be numeric or alphabetic.
 Variable type can be char, int, float, double, or void.

8|P ag e
Lesson 3

Local variables in c
 When we declare a variable inside a function or block, then it is called a Local
Variable.

 We can use this variable in that block or function in which that variable is
declared.

 The lifetime of a local variable is only equal to the lifetime of the function or
block in which it is declared, as soon as that function or block is destroyed, the
local variable is also destroyed and the space it has got in the memory is also
released.

output

sum of 10 and 20 is: 30


Advantages of Local variable

When we declare a variable inside a function, then its scope is limited to that variable
so that if we want, we can create a variable with the same name in two different
functions, this will not cause any problem in the program because the scope of both
will be limited to that function.

The memory that the local variable gets is available only till the function in which that
local variable is declared is run in the computer. As soon as the function ends, the
variable’s memory is released and that memory can be used by any other variable of
the program.

9|P ag e
Lesson 3

Disadvantage of Local variables


 The scope of a variable is limited so that a variable declared in a function cannot
be used by another function.
 The debugging process becomes a bit tricky.
 Data sharing is not possible in local variable.

Global variables in C

Global variables are declared outside the function or block. Generally, the Global
Variable is declared at the top of the program.

The lifetime of a global variable is equal to the lifetime of the entire program, and as
soon as the program ends, the global variable is also destroyed and its available space
in the memory is also released.

Output -:

value of a = 10, b = 20 and c = 30

constant in C
constant defines a variable whose value cannot be changed.
You can use the const Keyword.
Const double PI = 3.14;

10 | P a g e
Lesson 3

Const double P1 = 3.14;

PI =2.9; // Error here, PI is a symbol constant; its value cannot be changed.


example 1: # define preprocessor

# include <stdio.h>

#define PI 3.1415

Int main ()

float radius, area;

printf (“ Enter the radius: ”);

scanf (“%f”, & radius);

// Notice, the Use of PI

Area= P1 * radius*radius;

Printf (“Area =%.2f”, area);

return 0;

11 | P a g e

You might also like