0% found this document useful (0 votes)
7K views

Unit - 2 Operator: Sejal Patel Ass. Prof, PICA, PU

The document discusses various types of operators in the C programming language. It defines an operator as a symbol that tells the compiler to perform specific mathematical or logical functions. It then describes the following types of operators in C: arithmetic, relational, logical, bitwise, assignment, conditional, and increment/decrement. For each type, it provides examples to illustrate how they are used.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7K views

Unit - 2 Operator: Sejal Patel Ass. Prof, PICA, PU

The document discusses various types of operators in the C programming language. It defines an operator as a symbol that tells the compiler to perform specific mathematical or logical functions. It then describes the following types of operators in C: arithmetic, relational, logical, bitwise, assignment, conditional, and increment/decrement. For each type, it provides examples to illustrate how they are used.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Unit -2 Operator

SEJAL PATEL
Ass. Prof, PICA, PU
Definition
• An operator is a symbol that tells the compiler to
perform specific mathematical or logical functions.

• 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
 Conditional Operators
 Special Operator
 Increment and decrement operator
Arithmetic Operator
• An arithmetic operator performs mathematical operations
such as addition, subtraction and multiplication on numerical
values (constants and variables).
Example
#include <stdio.h>
main()
{ Output
int a = 21; Line 1 - Value of c is 31
int b = 10;
int c ; Line 2 - Value of c is 11
c = a + b; Line 3 - Value of c is 210
printf("Line 1 - Value of c is %d\n", c ); Line 4 - Value of c is 2
c = a - b; Line 5 - Value of c is 1
printf("Line 2 - Value of c is %d\n", c );
c = a * b;
printf("Line 3 - Value of c is %d\n", c );
c = a / b;
printf("Line 4 - Value of c is %d\n", c );
c = a % b;
printf("Line 5 - Value of c is %d\n", c );
}
Relational Operator
• A relational operator checks the relationship between two operands. If the relation is true,
it returns 1; if the relation is false, it returns value 0.
• Relational operators are used in decision making and loops Example A= 10 B =20
Example
#include <stdio.h>

main() {

int a = 21;
int b = 10;
int c ;

if( a == b ) {
printf("Line 1 - a is equal to b\n" );
} else {
printf("Line 1 - a is not equal to b\n" );
/* Lets change value of a and b */
}
a = 5;
b = 20;
if ( a < b ) {
printf("Line 2 - a is less than b\n" );
if ( a <= b ) {
} else {
printf("Line 4 - a is either less than or equal to
printf("Line 2 - a is not less than b\n" ); b\n" );
} }
if ( a > b ) { if ( b >= a ) {
printf("Line 3 - a is greater than b\n" ); printf("Line 5 - b is either greater than or
} else { equal to b\n" );
printf("Line 3 - a is not greater than b\n" ); }
}
}
Output
Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is either less than or equal to b
Line 5 - b is either greater than or equal to b
Logical Operator
• An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making in
C programming.
• A= 1 and B = 0
Example
#include <stdio.h>
main() {
int a = 5;
int b = 20;
int c ;
if ( a && b ) {
Output:
printf("Line 1 - Condition is true\n" );
}
Line 1 - Condition is true
if ( a || b ) {
printf("Line 2 - Condition is true\n" ); Line 2 - Condition is true
} Line 3 - Condition is not true
/* lets change the value of a and b */
a = 0; Line 4 - Condition is true
b = 10;
if ( a && b ) {
printf("Line 3 - Condition is true\n" );
} else {
printf("Line 3 - Condition is not true\n" );
}

if ( !(a && b) ) {
printf("Line 4 - Condition is true\n" );
}
}
• Line 1 - Condition is true
• Line 2 - Condition is true
• Line 3 - Condition is not true
• Line 4 - Condition is true
Assignment Operators
• An assignment operator is used for assigning a value
to a variable. The most common assignment
operator is =
Example
#include <stdio.h>
main() {
int a = 21;
int c ;
c = a;
printf("Line 1 - = Operator Example, Value of c = %d\n", c );
c += a;
printf("Line 2 - += Operator Example, Value of c = %d\n", c );
c -= a;
printf("Line 3 - -= Operator Example, Value of c = %d\n", c );
c *= a;
printf("Line 4 - *= Operator Example, Value of c = %d\n", c );
c /= a;
printf("Line 5 - /= Operator Example, Value of c = %d\n", c );
c = 200;
c %= a;
printf("Line 6 - %= Operator Example, Value of c = %d\n", c );
}
Output
Line 1 - = Operator Example, Value of c = 21
Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - %= Operator Example, Value of c = 11
Bitwise Operators
• Bitwise operator works on bits and perform bit-by-bit
operation. The truth tables for &, |, and ^ are as follows:
Bitwise Operators
• Bitwise operator works on bits and perform bit-by-bit
operation. The truth tables for &, |, and ^ are as follows:
• 78: 1001110
• 30: 11110
• 51: 110011
• 75:1001011
Bitwise Operators
Binary Left Shift Operator
• << (left shift) Takes two numbers, left shifts
the bits of the first operand, the second operand
decides the number of places to shift. Or in other
words left shifting an integer “x” with an integer
“y” (x<<y) is equivalent to multiplying x with
2^y (2 raise to power y).
Binary Right Shift Operator
• >> (right shift) Takes two numbers, right
shifts the bits of the first operand, the second
operand decides the number of places to
shift.Similarly right shifting (x>>y) is equivalent
to dividing x with 2^y.

Important Points :
• The left shift and right shift operators should not be
used for negative numbers. The result of is
undefined behavior if any of the operands is a
negative number. For example results of both -1 <<
1 and 1 << -1 is undefined.
• If the number is shifted more than the size of
integer, the behavior is undefined. For example, 1
<< 33 is undefined if integers are stored using 32
bits. See this for more details.
• The left-shift by 1 and right-shift by 1 are equivalent
to multiplication and division by 2 respectively.
As mentioned in point 1, it works only if numbers
are positive.
Conditional operators
• They are also called as Ternary Operator .
• They also called as ?: operator
• Ternary Operators takes on 3 Arguments
Conditional operators
Syntax:
exp1 ? exp2 : exp3
Where exp1,exp2 and exp3 are expressions

• where
• expression1 is Condition
• expression2 is Statement Followed if Condition is True
• expression2 is Statement Followed if Condition is False
Working of the ? Operator:
Exp1 is evaluated first, if it is nonzero(1/true) then
the expression2 is evaluated and this becomes
the value of the expression,
If exp1 is false(0/zero) exp3 is evaluated and its
value becomes the value of the expression.
Ex: m=2;
n=3
r=(m>n) ? m : n;

Ex: m=2;
n=3
r=(m>n) ? m : n;

Same as if(m>n){
r=a;
}
else{
r=b;
}
• #include <stdio.h>
• #include<conio.h>
• void main()
• {
• int mark;
• printf("Enter mark: ");
• scanf("%d", &mark);
• printf(mark >= 40 ? "Passed" : "Failed");
• getch();
• }
Increment & Decrement Operators
C supports 2 useful operators namely
1. Increment ++
2. Decrement –-

The ++ operator adds a value 1 to the operand


The –- operator subtracts 1 from the operand

Example:
++a or a++
--a or a--
Rules for ++ & -- operators

1. These require variables as their operands


2. When postfix either ++ or – is used with the
variable in a given expression, the expression is
evaluated first and then it is incremented or
decremented by one
3. When prefix either ++ or – is used with the
variable in a given expression, it is
incremented or decremented by one first and
then the expression is evaluated with the new
value
Special operators
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.

This gives the size of the variable.


Sizeof ()
Example : size of (char) will give us 1.
C - Input & Output

• When we say Input, it means to feed some data into a


program.
• An input can be given in the form of a file or from the
command line.
• C programming provides a set of built-in functions to
read the given input and feed it to the program as per
requirement.

• When we say Output, it means to display some data on


screen, printer, or in any file.
• C programming provides a set of built-in functions to
output the data on the computer screen as well as to save
it in text or binary files.
Input /output
• printf scanf in C
• The printf() and scanf() functions are used for
input and output in C language. Both functions
are inbuilt library functions, defined in stdio.h
(header file).
printf() function

• The printf() function is used for output. It


prints the given statement to the console.
• The syntax of printf() function is given below:
• printf("format string",argument_list);
• The format string can be %d (integer), %c
(character), %s (string), %f (float) etc
scanf() function

• The scanf() function is used for input. It reads


the input data from the console
• scanf("format string",argument_list);
Format specifiers
First program
•The first line of the program #include <stdio.h> is a
preprocessor command,

#include <stdio.h> •which tells a C compiler to include stdio.h file before


going to actual compilation.
int main()
{ /* my first program •The next line int main() is the main function where
the program execution begins.
in C */
printf("Hello,World! •The next line /*...*/ will be ignored by the compiler
\n"); return 0; and

} • it has been put to add additional comments in the


program.

•So such lines are called comments in the program.

•The next line printf(...) is another function available


in C

• which causes the message "Hello, World!" to be


displayed on the screen.

•The next line return 0; terminates the main()


function and returns the value 0.
Semicolons

• In a C program, the semicolon is a statement


terminator. That is, each individual statement
must be ended with a semicolon. It indicates the
end of one logical entity.
• Given below are two different statements −
• printf("Hello, World! \n"); return 0;
Compiler
• A computer program which reads source code and outputs
assembly code or executable code is called compiler.

• A program that translates software written in source code into


instructions that a computer can understand Software used to
translate the text that a programmer writes into a format the
CPU can use.

• A piece of software that takes third-generation language


code and translates it into a specific assembly code.
Compilers can be quite complicated pieces of software.
Compiler
Compilation process
Process
• 1) C program (source code) is sent to preprocessor first. The preprocessor is
responsible to convert preprocessor directives into their respective values.
The preprocessor generates an expanded source code.

• 2) Expanded source code is sent to compiler which compiles the code and
converts it into assembly code.

• 3) The assembly code is sent to assembler which assembles the code and
converts it into object code. Now a simple.obj file is generated.

• 4) The object code is sent to linker which links it to the library such as
header files. Then it is converted into executable code. A simple.exe file is
generated.

• 5) The executable code is sent to loader which loads it into memory and
then it is executed. After execution, output is sent to console.
Difference between compiler and
interpreter
Header file
• A header file is a file with extension .h which contains C function
declarations and macro definitions to be shared between several source
files.

• There are two types of header files: the files that the programmer writes and
the files that comes with your compiler.

• You request to use a header file in your program by including it with the C
preprocessing directive #include, like you have seen inclusion of stdio.h
header file, which comes along with your compiler.

• Including a header file is equal to copying the content of the header file but
we do not do it because it will be error-prone and it is not a good idea to
copy the content of a header file in the source files, especially if we have
multiple source files in a program.

• A simple practice in C or C++ programs is that we keep all the constants,


macros, system wide global variables, and function prototypes in the header
files and include that header file wherever it is required.
Syntax
• Include Syntax
• Both the user and the system header files are included using
the preprocessing directive #include.
• It has the following two forms −

• #include <file> This form is used for system header files. It


searches for a file named 'file' in a standard list of system
directories.

• #include "file" This form is used for header files of your own
program.

• It searches for a file named 'file' in the directory containing


the current file.
Types of header files
Input/Output
1 stdio.h
Functions
2 conio.h console input/output
3 assert.h Diagnostics Functions
Character Handling
4 ctype.h
Functions
5 cocale.h Localization Functions
6 math.h Mathematics Functions
Nonlocal Jump
7 setjmp.h
Functions
Signal Handling
8 signal.h
Functions
Variable Argument List
9 stdarg.h
Functions
10 stdlib.h General Utility Functions
11 string.h String Functions
12 time.h Date and Time Functions
Example Description
#include<stdio.h> // include for including headerfile
// std = stadndard
// io for input output //h headerfile
#include<conio.h> // con = console // io input output
Void main()
{
int a; // ; statement terminator
clrscr(); // clrscr clear screen
printf(“enter one no:”); // printing to console
scanf(“%d”,&a); // % format specifier
printf(“%d”,a)
getch(); // getch = get charachter

You might also like