You are on page 1of 17

JIMS Engineering Management Technical

Introduction to Imperative language(CO2)


Campus (JEMTEC), Greater Noida
• Imperative programming languages are composed of step-by-step
instructions (how) for the computer.
Basic Concepts of ‘C’ • They describe explicitly which steps are to be performed in what
order to obtain the desired solution at the end.
• By contrast, in declarative programming, the desired result (what) is
described directly.
Unit: 1 • The languages are relatively easy to learn, as the code can be read
like a step-by-step instruction. Therefore, programmers normally
BCA-103 learn an imperative language first as part of their training.
Programming Using ‘C’ Language • One disadvantage of imperative programming is that for more
Devanshu Dube complex problems to be solved, the amount of code quickly starts to
grow. It remains easy to read but becomes confusing due to its
BCA 1st Sem volume.

Devanshu Dube BCA 103 Unit 1


11/12/2022 4

Introduction to Imperative language(CO2)


Content
(contd.)

• Introduction to imperative language Advantages Disadvantages


• Syntax and constructs of a specific language (ANSI C) Easy to read Code quickly becomes very extensive and
thus confusing
• Types Operator
• Expressions with discussion of variable naming Relatively easy to learn Higher risk of errors when editing
• Hungarian Notation: Variable Names
Conceptual model (solution path) is very System-oriented programming means that
• Proper variable naming and Hungarian Notation easy for beginners to understand maintenance blocks application
development
• Data Type and Sizes (Little Endian Big Endian)
• Constants, Declarations
Characteristics of specific applications can Optimization and extension is more
be taken into account difficult

Devanshu Dube BCA 103 Unit 1


11/12/2022 Devanshu Dube BCA 103 Unit 1 2 11/12/2022 5

Content (contd.) Introduction to ANSI C (CO2)

• Arithmetic Operators, Relational Operators, Logical


Operators
• Type Conversion • Core • Developer • Place

• Increment Decrement Operators


• Bitwise Operators
• Assignment Operators and Expressions High level Design and
Developed at AT
& T’s Bell
• Precedence and Order of Evaluation programming
language
written by
Dennis Ritchie
Laboratory of
USA in 1972

Devanshu Dube BCA 103 Unit 1


11/12/2022 Devanshu Dube BCA 103 Unit 1 3 11/12/2022 6

1
Introduction to C Programming & components (CO2) Features of C(CO2)

High Level Programming Language with some low level


features.
C components
Portable Language.

Stable language.
Features of C
Introduction to C Programming Simple Language.

Large Number of Operators.


C Character Set
Less Number of Keywords (only 32 keywords)
Supports pointers to refer computer memory, arrays,
structures and functions.
C Tokens
Structured Programming Language.

Extensible Language
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 7 11/12/2022 10

History of C Programming(CO2) Components of C Language(CO2)


Languages Year Description
The Character set (a-z, A-Z, 0-9, {, }…)
ALGOL 1960 First language to use a block structure. Led the way for
development of structured programming concepts.
CPL 1963 Developed for wide range of application and more complex The data types (int, char, float, double)
than ALGOL.
BCPL 1967 Developed by Martin Richards. Type-less language which Constants (24, 45.67, ‘X’, “Hello”)
facilitated the direct access of memory. Useful for system
programmers. Variables (a, b)
B 1970 Developed by Ken Thompson. It is used to develop the first
version of UNIX.
C 1972 Developed by Dennis Ritchie that took concepts from ALGOL,
Keywords (int, for, if, else, break)
BCPL and B. It supports the concepts of data-types.
K&RC 1978 Known due to popularity by book written by Kernighan &
Ritchie.
ANSI C 1989 Standardize by ANSI.

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 8 11/12/2022 11

History of C Programming(CO2) C-Character Set(CO2)

Languages Year Description Alphabets (A,..,Z,a,..,z)

ANSI/ISO 1990 ANSI standard adopted by ISO and known as C89. Digits(0-9)
C
C95 1995 Some minor changes made in C89
Special
C99 1999 Significant changes were made to C95. Symbols(@,.,;,:)

C11 2011 Last changes in C99 were made in December 2011


White
Spaces(\n,\b)

Operators(+,-,/,%)

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 9 11/12/2022 12

2
C Tokens (CO2) Identifiers (CO2)

Identifiers are used to name various program elements such as


variables, functions and arrays.
Keywords
C program is Identifiers
Tokens are It is the C token can
constructed Constants Variable is the named memory location that is used to hold a
the basic smallest be
by using
building individual
combination
categorized Operators value that can be modified during the execution of the program.
blocks in C unit in the C in five
s of these Special
language. program. forms:
tokens. symbols

Declaration of variable:

• datatype var_name;
• e.g. int x;

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 13 11/12/2022 16

Keywords (CO2) Hungarian Notation (CO2)

• Hungarian is a naming convention for identifiers in code.


• Each identifier would have two parts to it, a type and a qualifier.
Keywords 1. type: the first characters of the identifier specify what type the object
have is an instance of. This is achieved by adopting part of the name of the
predefined type as a prefix on the identifier. The prefix is always entirely lower-
32 keywords
meaning Also known Always case.
Have fixed in C
and they are as Reserve written in
meaning. programmin 2. qualifier: the remainder of the name of the identifier describes what
used to words. lowercase.
g the variable is used for. The qualifier portion can be one word or a
perform
specific run-on word. The qualifier begins with a capital to distinguish it from
task. the type prefix.
Example:
String sNameFirst; //s is a prefix for String type
char cLetter; //c is a prefix for the char type

Devanshu Dube BCA 103 Unit 1


11/12/2022 14 11/12/2022 Devanshu Dube BCA 103 Unit 1 17

Keywords (CO2) Origin of Hungarian notation (CO2)

• Charles Simonya, chief architect at Microsoft is the originator


auto continue double float int short struct unsigned
of the Hungarian Notation standard, which is used extensively
in Microsoft Windows code.
break const else for long signed switch void • Simonya first used the notation in 1972.

• Since its inception in 1972, Hungarian Notation has been


case default enum goto register sizeof typedef volatile adopted by Xerox, Apple, 3Com, and of course Microsoft.

char do extern if return static union while

Devanshu Dube BCA 103 Unit 1


11/12/2022 15 11/12/2022 Devanshu Dube BCA 103 Unit 1 18

3
Rules for Naming Identifiers (CO2) Real or Floating Constants(CO2)

May consist of alphabets, digits and underscore ( _ ).

Must not start with a digit.

Keywords can not be used. A floating constant


consists of an The For
White spaces are not allowed. integer part, a Fraction example By Invalid
decimal point, a 0.02, - default real
part and constant
23.567, - floating
Only first 31 characters are significant. fractional part and integer 4.57E- literal s: 9.88.7,
exponent field part are 23, are 5.6E 23,
sequence
Examples: sum, a, x2, first_name, _SI. containing an e or of digits.
3.4e+25, double. • 3.4E9.
.98 4
E followed by an
Some Invalid identifiers integer.

Simple interest,@abc,12_dfr etc.

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 19 11/12/2022 22

Constants (CO2) Character constants(CO2)

known as
Character constants
literals. A character constant consists of a single character enclosed in
single quotes.
Types of For example ‘a’ , ‘D’ and ‘@’.
Constants are constants: Characters are stored using machine characters sets using ASCII
values that codes.
can’t change
during the Character Integer Floating Strings All escapes sequences are also characters sequences.
execution. constants constants constants constants
String constants
A string constant is a sequence of characters enclosed in double
quotes.
For example “a”, “ABC”.

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 20 11/12/2022 23

Integer Constants(CO2) Operators(CO2)

Wrong An operator is a symbol that specifies the mathematical, logical


A constant of
By default representatio or relational operation to be performed.
integer type For example :
integer literal n
consists of 1, 234, 567, - are of type • 123 456
sequence of 4563. int.
digits. • 12,34,567 C language supports different types of operators which can be
• $234 used with variables and constants to form expressions.

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 21 11/12/2022 24

4
Type of Operators(CO2) Characteristics of escape sequences(CO2)

Arithmetic operators Every combination starts with back slash(\)


Relational operators
Logical operators
They are non-printing characters.
Unary operators
Conditional operators
It can also be expressed in terms of octal digits or hexadecimal
Bitwise operators sequence.
Assignment operator
Comma operator Each escape sequence has unique ASCII value.
sizeof operator

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 25 11/12/2022 28

Backslash Character Constants(CO2) Structure of C program(CO2)

Documentation Section
An escape sequence is a sequence of characters that does not
represent itself when used inside a character or string literal, Preprocessor Directives
but is translated into another character. Global Declaration Section
void main()
used in formatting the output in output functions. {
Local Declaration Section
Statements
It represents only one character. However, it consists of two
characters. }
Other functions as required
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 26 11/12/2022 29

Backslash Character Constants(CO2) First C program(CO2)

Constants Hexadecimal Value Meaning


\a 07 audible alert (bell sound)
\b 08 back space /*C Program to Display Welcome message*/ Documentation Section

\n 0A new line #include<stdio.h>


Preprocessor Directives
\r 0D carriage return #include<conio.h>
\f 0C form feed void main()
\t 09 horizontal tab {
\v 0B vertical tab printf(“Welcome to the world of C.”);
getch(); Statements
\’ 27 single quote
\’’ 22 double quote }
\? 3F question mark Welcome to the world of C.
\\ 5C backslash
\nnn any octal number
\xhh Any hexadecimal number

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 27 11/12/2022 30

5
Fundamental Data types(CO2) Data Type(CO2)

• Fundamental data types Primary or


fundamental or int , char, float
primitive or built-in double, void
and or basic data types.
Standard I/O ANSI C supports
array, function,
Three classes of Derived data types
pointer
Data Types

enum, typedef
User Defined data
,structure,
types
union

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 31 11/12/2022 34

Pre-processor Directives Data Modifiers(CO2)

Data type modifiers


Pre-processor works under the control of set of instructions, are keywords used to
called preprocessor directives. change the current
properties of data
types.
These are executed before the compilation process.
short
size
These statements begin with # symbol. Modifiers
Type for long
Data
Examples: modifiers
: signed
sign
• #define Modifiers
unsigned
• #include
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 32 11/12/2022 35

Data Types(CO2) Primary Data Types(CO2)


Basic DATA TYPE with type FORMAT SIZE RANGE
DATA modifier SPECIFIER
TYPE
char %c 1 Byte -128 to 127
signed char
char
A data type is used to declare variables that specifies unsigned char %c 1 Byte 0 to 255

• Size of memory allocated to variables. int %d 2 Byte -32768 to 32767


signed int %i Or Or
• Define the range of allowed values. short %hd 4 Byte -2,147,483,648 to
• Operations that can be performed on those values. int short int 2147,483,647
short signed int

Storage representation of different data types is different in unsigned %u 2 Byte 0 to 65535


unsigned int %x Or Or
memory. short unsigned int %o 4 Byte 0 to 4,294,967,295

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 33 11/12/2022 36

6
Primary Data Types(CO2) Explicit Type Conversion or Type Casting(CO2)
Basic DATA DATA TYPE with type FORMAT SIZE RANGE
TYPE modifier SPECIFIER
long 4 Byte -2,147,483,648 to
long int %ld 2147,483,647
x=(int) 8.5 • 8.5 is converted to integer 8 by
int long signed int %li truncation
unsigned long %lu 4 Byte 0 to 4,294,967,295
long unsigned int
a = (int) 23.6/(int)7.2 • Evaluated as 23/7 and the result
float %f 4 Byte +(1.17*10-38 to 3.4*1038 ) would be 3
float %e or %E And
%g or %G -(1.17*1038 to 3.4*1038 )
b= (double)sum/n • Division is done in floating point
mode
double %lf 8 Byte +(2.22*10-308 to 1.79*10308)
And

-(2.22*10308 to 1.79*10308) c= (int)(a+b) • Result of a+b is converted to


double
long double %Lf 10 Byte +(3.4*10-4932 to 1.1*104932)
intger.
And

-(3.4*10-4932 to 1.1*104932) p= (int)a+b • a is converted to integer and then


added to b.
void -- -- -- --
Devanshu Dube BCA 103 Unit 1
11/12/2022 Devanshu Dube BCA 103 Unit 1 37 11/12/2022 40

Implicit Type Conversion(CO2) Type Conversion or Type Casting(CO2)

int main()
{
int x = 10; // integer x
char y = 'a'; // character c
float z;

This automatic // y implicitly converted to int. ASCII


In this the data
conversion is // value of 'a' is 97
type/Variable of
known as x = x + y;
lower type is
implicit type
converted to a
conversion or
higher type. // x is implicitly converted to float
promotion.
z = x + 1.0;

OUTPUT :
printf("x = %d, z = %f", x, z);
x = 107, z = 108.000000
return 0;
}

Devanshu Dube BCA 103 Unit 1


11/12/2022 38 11/12/2022 Devanshu Dube BCA 103 Unit 1 41

Explicit Type Conversion(CO2) Type Casting or Type Conversion(CO2)

When we want to convert a type forcibly in a way that is


different from automatic type conversion, we need to go for
explicit type conversion.
Type casting is a way to convert a variable from one
data type to another data type. For example, if you want to
Lower to higher and higher to lower both type conversion can store a long value into a simple integer then you can type cast
be done in type casting. long to int.

There are two types of Type Casting


Syntax
• Implicit type conversion
• (type name) expression; • Explicit type conversion
• For e.g.
• x = (float)y;
• Where x is of type float
11/12/2022
Devanshuand
Dube yBCA
is 103
of type
Unit 1 int.
39 11/12/2022
Devanshu Dube BCA 103 Unit 1
42

7
Implicit Type Conversion(CO2) Standard I/O(CO2)

Standard input or stdin is used for taking input from devices


such as the keyboard as a data stream
This automatic
In this the data
conversion is
type/Variable of Standard output or stdout is used for giving output to a device
known as
lower type is
converted to a
implicit type such as a monitor.
conversion or
higher type.
promotion.

For using I/O functionality, programmers must


include stdio header-file within the program.

Devanshu Dube BCA 103 Unit 1


11/12/2022 43 11/12/2022 Devanshu Dube BCA 103 Unit 1 46

Explicit Type Conversion(CO2) Standard I/O(CO2)

When we want to convert a type forcibly in a way that is


different from automatic type conversion, we need to go for Reading Character from the keyboard:
explicit type conversion. • getchar() function can be used to read a single character
Syntax:
Lower to higher and higher to lower both type conversion can
var_name = getchar();
be done in type casting.
Example:
#include<stdio.h>
Syntax
void main()
{
• (type name) expression; char title;
• For e.g. title = getchar();
• x = (float)y; }

• Where x is of type float


11/12/2022
Devanshuand
Dube yBCA
is 103
of type
Unit 1 int.
44 11/12/2022 Devanshu Dube BCA 103 Unit 1 47

Explicit Type Conversion or Type Casting(CO2) Standard I/O(CO2)

Writing Character to the monitor

x=(int) 8.5 • 8.5 is converted to integer 8 by • putchar() function can be used to display a single character
truncation on the screen
Syntax:
a = (int) 23.6/(int)7.2 • Evaluated as 23/7 and the result
would be 3 putchar(var_name);

b= (double)sum/n • Division is done in floating point Example:


mode
#include<stdio.h>
c= (int)(a+b) • Result of a+b is converted to void main()
intger. {
char title =‘t’;
p= (int)a+b • a is converted to integer and then putchar(title);
added to b. }

Devanshu Dube BCA 103 Unit 1


11/12/2022 45 11/12/2022 Devanshu Dube BCA 103 Unit 1 48

8
Standard I/O(CO2) Errors(CO2)

Formatted input function takes an input data a specific format. Examples


It allows by scanf() function

Syntax: Syntax Error Logical Error


Runtime
Error

scanf(“control string”,&arg1,&arg2..);
void main()
Example: {
printf(“ value of x floataverage(floata,floatb)
int a;
#include<stdio.h> %d”,x): {
printf("type an integer");
void main() { /* x is not declared and return a+b/2;
int var1; semicolon missing*/ scanf("%d", a);.
/* should be (a + b) / 2 */
scanf("%d ", &var1, ); } }
}

11/12/2022 Devanshu Dube BCA 103 Unit 1 49 11/12/2022 Devanshu Dube BCA 103 Unit 1 52

Standard I/O(CO2) Object Code and Executable Code(CO2)

Formatted output function displays a data in a specific format


on the screen. It allows by printf() function

Syntax:

printf(“control string”,arg1,arg2..);
Source code Object code Executable Code
Example:
• The input program of • The output program • Executable code is
compiler is known as of compiler is generated after
#include<stdio.h>
source code. known as object linking all the object
void main() { code module after
• It can be in any high
int var1= 60; compilation.
level programming • It is machine
printf("%d ", var1, ); language language code.
} • It is human readable • It is machine
code. readable code
11/12/2022 Devanshu Dube BCA 103 Unit 1 50 11/12/2022 Devanshu Dube BCA 103 Unit 1 53

Errors(CO2) Translation of Codes(CO2)

There are three type


of errors can occur
in the c
Programming.

Syntax Logical Runtime


Error Error Error

Occurs due Found Code links and


Found at to incorrect after compiles just
Occurs due to fine, but
incorrect compile logic used in execution
time C– i.e may during
syntax used in execution, it
C- statements. statements give
by user. incorrect does
output something
illegal.

11/12/2022 Devanshu Dube BCA 103 Unit 1 51 11/12/2022 Devanshu Dube BCA 103 Unit 1 54

9
Variables and Memory allocation(CO2) Program(CO2)

/* Area of Circle*/
#include<stdio.h>
A variable is a name which is assigned to a data value at storage #include<conio.h>
location in computer’s memory. void main()
{
OUTPUT:
float r, area;
Enter radius:
Variables are used to store various kinds of data during program clrscr(); 2
executions. printf(“enter radius\n”); area of circle is =6.28
scanf(“%f”,&r);
area=22*r*r/7;
Variables name assigned before it use and it can be letter , digit printf(“area of circle is=%f”,area);
and an underscore(_). getch();
}

11/12/2022 Devanshu Dube BCA 103 Unit 1 55 11/12/2022 Devanshu Dube BCA 103 Unit 1 58

Declaration and Initialization(CO2) Program(CO2)


/* Area of Triangle*/
#include<stdio.h>
• Syntax: #include<conio.h>
Declaration: • <data type> <variable Name>; #include<math.h>
void main()
• int num; {
Example: • char name;
float a,b,c, area,s; OUTPUT:
Enter sides of triangle:
• Two type of initialization clrscr();
Initialization:
3
printf(“Enter sides of triangle\n”); 3
scanf(“%f%f%f”, &a, &b,&c); 2
• int num=10; //at time of declaration area of triangle is = 0.44
Example1: s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(“area of Triangle is=%f”,area);
Example2 : • int num;
• num=10; // after the declaration getch();
}
11/12/2022 Devanshu Dube BCA 103 Unit 1 56 11/12/2022 Devanshu Dube BCA 103 Unit 1 59

Programs(CO2) Program(CO2)

/* Swap Two Numbers using third variable*/


#include<stdio.h>
#include<conio.h>
Write a program of find area of circle. void main()
{
int a,b,temp;
Write a program of find area of triangle. OUTPUT:
clrscr();
Enter two numbers
printf (“enter two numbers\n”); 20
scanf (“%d %d ” ,&a ,&b ); 30
After swap numbers are:
Write a program of find swap two numbers using third variable. temp=a;
a=30
a=b; b=20
b=temp;
Write a program of swap two numbers without using third printf(“After swap numbers are:\n a=%d\nb=%d”,a,b);
variable getch();
}
11/12/2022 Devanshu Dube BCA 103 Unit 1 57 11/12/2022 Devanshu Dube BCA 103 Unit 1 60

10
Program(CO2) Type of operators(CO2)

/* Swap Two Numbers without using third variable*/


#include<stdio.h>
#include<conio.h> Arithmetic Relational Logical
void main() operators operators operators
{
int a,b;
OUTPUT:
clrscr();
Enter two numbers
printf (“enter two numbers\n”); 20 Bitwise Conditional Unary
scanf (“%d %d ” ,&a ,&b ); 30 operators operators operators
After swap numbers are:
a=a+b;
a=30
b=a-b; b=20
a=a-b;
printf(“After swap numbers are:\n a=%d\nb=%d”,a,b); Assignment Comma sizeof
getch(); operator operator operator
}
Devanshu Dube BCA 103 Unit 1
11/12/2022 Devanshu Dube BCA 103 Unit 1 61 11/12/2022 64

Operators in C language(CO2) Arithmetic operators(CO2)

For a=9, b=3


Operators Arithmetic Relational Logical
and Type operators Operators Operators Operation Operator Example Result

Multiply * a*b 27
Other
Increment Bitwise operator(con
/Decrement Assignment Divide / a/b 3
Operators Operators ditional ,
operators comma and
size) Addition + a+b 12
Operator
precedence Operator quiz Subtraction - a-b 6
and
Precedence quiz Modulus % a%b 0
associativity

Devanshu Dube BCA 103 Unit 1


11/12/2022 Devanshu Dube BCA 103 Unit 1 62 11/12/2022 65

Operators and Type(CO2) More Arithmetic operators(CO2)

For a=-10, b=3

Operation Operator Example Result

Divide / a/b -3
An operator is a symbol that specifies the mathematical, logical
or relational operation to be performed.
Modulus % a%b -1

For a=10 ,b=-3


C language supports different types of operators which can be
used with variables and constants to form expressions.
Divide / a/b -3
Modulus % a%b 1

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 63 11/12/2022 66

11
Relational operators(CO2) Assignment operators(CO2)

Operation Operator Example Result Operation Operator Example Assignment

Less than < 3<5 1

Greater than > 7>9 0 Assignment = Assign values to the left variable a=6
LESS THAN OR EQUAL TO <= 100<=100 1

Greater Than or Equal to >= 50>=100 0 Note: a=6;


Equal to == 20==20 1 a is called lvalue should be variable

Not Equal to != 20!=20 0 6 is rvalue can be variable ,constants and constant expression.

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 67 11/12/2022 70

Logical Operators(CO2) Bitwise Operators(CO2)

Operation Operator Example Result Operation Operator Example Result

Bitwise AND & 5&6 4


Logical AND && (6>5) && (7>9) 0

Bitwise OR | 5|6 7
Logical OR || (6>5) || (7>9) 1
Bitwise XOR ^ 5^6 3
Logical NOT ! !(6>5) 1
Bitwise Left Shift << 5<<1 10
Note1: If first operand in False in case of logical AND operation than second
operand will not evaluated Bitwise Right Shift >> 5>>1 2
Note2: If first operand in True in case of logical OR operation than second
operand will not evaluated Bitwise Not /One’s Complement ~ ~6 -7

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 68 11/12/2022 71

Increment/Decrement operators(CO2) More Bitwise Operators(CO2)

Operation Operator Example Result


Operation Operator Example Post-fix/Pre-fix

Increment operator ++ Add one to the current value a++/++a Bitwise AND & 5&6 4
The result of AND is 1 only if both bits are 1

Decrement Operator -- Minus one to the current value a--/--a


5--------0000000000000101

Example1: int a=6,b;


b=a++ ; // first assign value than increment the value so b=6,a=7 6--------0000000000000110

Example2: int a=6,b;


b=++a; // first increment value than assign the value so b=7,a=7 Result: 4--------0000000000000100

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 69 11/12/2022 72

12
More Bitwise Operators(CO2) More Bitwise Operators(CO2)
Let us suppose the bitwise AND operation of two integers 12 and 25.

Operation Operator Example Result


12 = 00001100 (In Binary)
#include <stdio.h>
25 = 00011001 (In Binary) int main()
{ Bitwise XOR ^ 5^6 3
Bit Operation of 12 and 25 int a = 12, b = 25; The result of XOR is 1 if the two bits are different
printf("Output = %d", a&b);
00001100 return 0;
& 00011001 } 5--------0000000000000101
________
00001000 = 8 (In decimal) Output = 8 6--------0000000000000110

Result: 3--------0000000000000011

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 73 11/12/2022 76

More Bitwise Operators(CO2) More Bitwise Operators(CO2)


Let us suppose the bitwise AND operation of two integers 12 and 25.

Operation Operator Example Result


12 = 00001100 (In Binary)
#include <stdio.h>
25 = 00011001 (In Binary) int main()
Bitwise OR | 5|6 7 {
The result of OR is 1 if any of the two bits is 1 int a = 12, b = 25;
Bitwise XOR Operation of 12 and 25
printf("Output = %d", a^b);
00001100 return 0;
5--------0000000000000101 ^ 00011001 }
________
Output = 21
00010101 = 21 (In decimal)
6--------0000000000000110

Result: 7--------0000000000000111

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 74 11/12/2022 77

More Bitwise Operators(CO2) More Bitwise Operators(CO2)


Let us suppose the bitwise AND operation of two integers 12 and 25.

12 = 00001100 (In Binary) Operation Operator Example Result


#include <stdio.h>
25 = 00011001 (In Binary) int main()
{
Bitwise OR Operation of 12 and 25 int a = 12, b = 25;
Bitwise Left shift << 5<<1 10
printf("Output = %d", a|b);
00001100 return 0;
| 00011001 }
Takes two numbers, left shifts
________ the bits of the first operand,
Output = 29 5--------0000000000000101<<1
00011101 = 29 (In decimal) the second operand decides
the number of places to shift.

Result: 10--------0000000000001010

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 75 11/12/2022 78

13
More Bitwise Operators(CO2) More Bitwise Operators(CO2)

Operation Operator Example Result


Operation Operator Example Result

Bitwise Right shift << 5>>2 2


takes one number and inverts all bits of it
Bitwise complement ~ ~6 -7
Takes two numbers, right shifts
5--------0000000000000101>>1 the bits of the first operand, the
second operand decides the
number of places to shift. ~6--------0000000000000110( one’s complement)
->1111111111111001
Result: 10--------000000000000010

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 79 11/12/2022 82

More Bitwise Operators(CO2) More Bitwise Operators(CO2)


• The left shift and right shift operators should not be used for negative Let us suppose the bitwise AND operation of two integers 12 and 25.
numbers.
• If the second operand(which decides the number of shifts) is a negative 12 = 00001100 (In Binary)
number, it results in undefined behaviour in C. #include <stdio.h>
25 = 00011001 (In Binary) int main()
• For example results of both 1 <<- 1 and 1 >> -1 is undefined.
{
• Also, if the number is shifted more than the size of the integer, the int a = 12, b = 25;
Bitwise XOR Operation of 12 and 25
behaviour is undefined. For example, 1 << 33 is undefined if integers are printf("Output = %d", a^b);
stored using 32 bits. 00001100 return 0;
^ 00011001 }
________
Output = 21
00010101 = 21 (In decimal)

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 80 11/12/2022 83

More Bitwise Operators(CO2) Conditional operators(CO2)


• The left-shift and right-shift operators are equivalent to multiplication and
division by 2 respectively.
• It works only if numbers are positive.
Operation Operator Syntax Example Result

int main()
{
int x = 19; Condition ?: exp1?exp2:exp3 12>6?1:0 1
printf("x << 1 = %d\n", x << 1);
printf("x >> 1 = %d\n", x >> 1);
return 0;
} Note: if exp1 is true then exp2 will evaluates
if exp1 is false then exp3 will evaluates
Output:
x << 1 = 38
x >> 1 = 9

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 81 11/12/2022 84

14
sizeof() operators(CO2) Comma operator(CO2)
Comma Operator (,)
Using Comma Operator along with Assignment
Operation Operator Example Result #include<stdio.h>
int main()
{
Determines memory size size() size(int) 2 int i;
size(5) 2 i = 1,2,3;
printf("i:%d\n",i);
size(5+6) 2 return 0;
}
Note: Evaluates the memory size of variable, constant and Output :
expression passed to the operator. i:1
Explanation :
i = 1,2,3;
Above Expression contain 3 comma operator and 1 assignment operator.
If we check precedence table then we can say that “Comma” operator has lowest
precedence than assignment operator
So Assignment statement will be executed first .
Devanshu Dube BCA 103 Unit 1 1 is assigned to variable “i”.
11/12/2022 85
11/12/2022 Devanshu Dube BCA 103 Unit 1 88

Comma operator(CO2) Comma operator(CO2)


Comma Operator (,)
Operation Operator Example Result Using Comma Operator with Round Braces
#include<stdio.h>
int main()
separator , a=3,4,5; a=3 {
a=(3,4,5); a=5 int i;
a=(b+=a,++b); a=6 i = (1,2,3);
printf("i:%d\n",i);
if a=4 and b=1 return 0;
Explanation :
i = (1,2,3);
} Bracket has highest priority than any operator.
Inside bracket we have 2 comma operators.
Note: Evaluates the expression and leave for the result Output : Comma operator has associativity from Left to Right.
i:3 Comma Operator will return Rightmost operand
i = (1,2,3)
==> 1,2 will return 2
==> 2,3 will return 3
==> means (1,2,3) will return 3
==> Assign 3 to variable i.
Devanshu Dube BCA 103 Unit 1
11/12/2022 86
11/12/2022 Devanshu Dube BCA 103 Unit 1 89

Comma operator(CO2)
• Comma Operator (,)
– Comma Operator has Lowest Precedence i.e it is having lowest priority so it
is evaluated at last.
– Comma operator returns the value of the rightmost operand when multiple
comma operators are used inside an expression.
– Comma Operator Can acts as –
• Operator : In the Expression
• Separator : Declaring Variable , In Function Call Parameter List

Comma as Separator Comma as Operator


int num1 = 1, num2 = 2; res = (num1, num2);
It can acts as Seperator in – In this case value of
1.Function calls rightmost operator will be
2.Function definitions assigned to the variable. In
3.Variable declarations this case value of num2 will
4.Enum declarations be assigned to variable res.

11/12/2022 Devanshu Dube BCA 103 Unit 1 87 11/12/2022 Devanshu Dube BCA 103 Unit 1 90

15
Operator Precedence and Associativity(CO2) Operator Precedence and Associativity(CO2)
Operator Description Associativity Precedence Level
+ Addition operator
Left-to-right 4
- Subtraction operator
• Operator Precedence >> Right shift operator Left-to-right
5
<< Left shift operator
< Less than
<= Less than or equal operator
Left-to-right 6
and >
>=
Greater than
Greater than or equal to
== Equal to
Left-to-right 7
!= Not equal to

Associativity & Bitwise AND Left-to-right 8


^ Bitwise XOR Left-to-right 9
| Bitwise OR Left-to-right 10
&& Logical AND Left-to-right 11
|| Logical OR Left-to-right 12
?: Conditional operator Right-to-Left 13

Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 91 11/12/2022 94

Operator Precedence and Associativity(CO2) Operator Precedence and Associativity(CO2)


Operator Description Associativity Precedence Level

Precedence of operators determines the order of evaluation =


when they occur in the same expression which is called mixed *= /= %=
expression += -= Assignment operator Right-to-Left 14
&= ^= |=
<<= >>=
Associativity of operators determines the order in which
operators of equal precedence are evaluated when they occur
in the same expression
, Comma operator Left-to-right 15

*Note 1:
Parentheses are also used to group sub-expressions to force a
different precedence; such parenthetical expressions can be nested
The associativity defines the direction ,left to right or right to and are evaluated from inner to outer.
**Note 2:
left , in which the operator acts upon its operands Postfix increment/decrement have high precedence, but the actual
increment or decrement of the operand is delayed (to be
accomplished sometime before the statement completes execution).
So in the statement y = x * z++; the current value of z is used to
evaluate the expression (i.e., z++ evaluates to z) and z only
Devanshu Dube BCA 103 Unit 1 incrementedDevanshu
after allDube
else isBCA
done.
103 Unit 1
11/12/2022 92 11/12/2022 95

Operator Precedence and Associativity


Operator Precedence and Associativity(CO2) Operator
()
Description
Parentheses (function call) (*see note 1)
Associativity
left-to-right
Precedence
1
[] Brackets (array subscript)
Operator Description Associativity Precedence . Member selection via object name
Level -> Member selection via pointer
++ -- Postfix increment/decrement (**see Note 2)
() function call ++ -- Prefix increment/decrement right-to-left 2
[] square bracket (array subscript) +- Unary plus/minus
!~ Logical negation/bitwise complement
-> Arrow operator Left-to-right 1 (type)
*
Cast (convert value to temporary value of type)
Dereference
. Dot operator
&
sizeof
Address (of operand)
Determine size in bytes *Note
on this 1:
implementation
Parentheses are also used to group sub-expressions to force a3
+ Unary plus * / % Multiplication/division/modulus
different precedence; such parenthetical
left-to-right
expressions can be nested
+ - Addition/subtraction left-to-right 4
- Unary minus << >>
and are evaluated from inner to outer. left-to-right
Bitwise shift left, Bitwise shift right 5
++ -- increment /decrement operator < <=
**Note 2:
Relational less than/less than or equal to left-to-right 6
Postfix increment/decrement have high precedence, but the actual
! Logical NOT > >= Relational greater than/greater than or equal to
Relational is equal to/is increment
not equal to or decrement of the operandleft-to-right
is delayed (to be
~ Bitwise one’s complement Right-to-left 2 == !=
accomplished sometime before the statement
7
completes execution).
& Bitwise AND left-to-right 8
* Indirection operator (Value at address operator) ^ Bitwise exclusive OR
So in the statement y = x * z++; the current value of z is used to
left-to-right 9
& Address operator | Bitwise inclusive OR
evaluate the expression (i.e., z++ evaluates to z) and z only 10
left-to-right
incremented after all else is done.
(datatype) Type Casting && Logical AND left-to-right 11
|| Logical OR left-to-right 12
sizeof() Sizeof operator
?: Ternary conditional right-to-left 13
* Multiplication operator = Assignment right-to-left 14
+= -= Addition/subtraction assignment
/ Division operator Left-to-right 3 *= /= Multiplication/division assignment
% Modulus operator %= &= Modulus/bitwise AND assignment
^= |= Bitwise exclusive/inclusive OR assignment
Devanshu Dube BCA 103 Unit 1
11/12/2022 93 <<= >>= Bitwise shift left/right assignment
11/12/2022
, Devanshu Dube
Comma (separate expressions) BCA 103 Unit 1 left-to-right 15 96

16
Operator Precedence and Associativity(CO2)

• Example:
y=12*3%5-6+7/2
y=12*3%5-6+7/2
y=36%5-6+7/2
y=1-6+7/2
y=-5+7/2
y=-5+3
y=-2
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 97 11/12/2022 100

Summary

• Tokens are individual unit used in the c programs like


keywords , identifiers ,constants etc.

• Data type used to specify the storage of variable.

• Operators are symbols used to perform calculations

• Operator precedence and associativity are rule used to solve


mixed operations

• scanf() and printf() are the library functions are used for input
and output resp.

Devanshu Dube BCA 103 Unit 1


11/12/2022 98

References
• The C programming by Kernighan Brain W. and Ritchie Dennis
M., Pearson Education .
• Computer Basics and C Programming by V.Rajaraman , PHI
Learning Pvt. Limited – 2015.
• Computer Concepts and Programming in C by Vikas Gupta,
Wiley India Publication
• Computer Fundamentals and Programming in C. Reema
Thareja, Oxford Publication
• Computer Concepts and Programming in C, E Balaguruswami,
McGraw Hill

Devanshu Dube BCA 103 Unit 1


11/12/2022 99

17

You might also like