You are on page 1of 33

BCA103T – Problem Solving Techniques using C Page 1 of 33

Syllabus
BCA103T – Problem Solving Techniques using C

Total Teaching Hours : 60 No of Hours / Week : 04


Unit-I
Introduction to Programming Concepts: Software, Classification of Software, Modular
Programming, Structured Programming, Algorithms and Flowcharts with examples.
Overview of C Language: History of C, Character set, C tokens, Identifiers, Keywords, Data types,
Variables, Constants, Symbolic Constants , Operators in C, Hierarchy of Operators, Expressions,
Type Conversions and Library Functions. [ 12 Hours ]
Unit-II
Managing Input and Output Operation: Formatted and Unformatted I/O Functions
Decision making, branching and looping: Decision Making Statements - if Statement, if–else
statement, nesting of if-else statements, else–if ladder, switch statement, ?: operator, Looping -
while, do-while, for loop, Nested loop, break, continue, and goto statements.
Functions: Function Definition, prototyping, types of functions, passing arguments to functions,
Nested Functions, Recursive functions. [ 12 Hours ]
Unit-III
Arrays: Declaring and Initializing, One Dimensional Arrays, Two Dimensional Arrays, Multi
Dimensional Arrays - Passing arrays to functions.
Strings: Declaring and Initializing strings, Operations on strings, Arrays of strings, passing strings to
functions. Storage Classes - Automatic, External, Static and Register Variables. [ 12 Hours ]
Unit-IV
Structures - Declaring and Initializing, Nested structure, Array of Structure, Passing structures to
functions, Unions, typedef, enum, Bit fields. Pointers – Declarations, Pointer arithmetic, Pointers
and functions, Call by value, Call by reference, Pointers and Arrays, Arrays of Pointers, Pointers and
Structures. Meaning of static and dynamic memory allocation, Memory allocation functions.
[ 12 Hours ]
Unit-V
Files - File modes, File functions, and File operations, Text and Binary files, Command Line
arguments. C Preprocessor directives, Macros – Definition, types of Macros, Creating and
implementing user defined header files. [ 12 Hours ]

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 2 of 33

LAB Program List


BCA103P – C Programming Lab

PART – A
1) Write a C Program to find the roots of the given quadratic equation using if-else if statement.
2) Write a menu driven C program using switch-case to find: (a) Sum of the digits of number
(b) Factorial of N.
3) Write a C program to find cos (x) using series cos(x) = 1 – x2/2! + x4/4! -………xn/n! ]
4) Write a Program to find whether a given number is prime number are not
5) Write a C program to arrange the given set of numbers in ascending and descending order.
6) Write a C program to find product of two N x M matrices.
7) Write a C program to calculate NCR= N!/ R! * (N-R)! Using function.
8) Write a C program to display Fibonacci series using recursive function.
9) Write a C program to concatenate two strings using pointers.
10) Write a C program to copy content of one file to another file.
PART – B
During practical examination the External and Internal examiners may prepare exam question
paper related to theory syllabus apart from Part-A. (A minimum of 10 Programs has to be
prepared).

Note :
a) The candidate has to write both the programs One from Part-A and other from Part-B and
execute one program as of External examiner choice.
b) A minimum of 10 Programs has to be done in Part-B and has to be maintained in the
Practical Record.
c) Scheme of Evaluation is as follows:
Writing two programs - 10 Marks
Execution of one program - 10 Marks
Formatting the Output - 05 Marks
Viva - 05 Marks
Record - 05 Marks
Total - 35 Marks

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 3 of 33

Unit-I
Introduction to Programming Concepts
A computer is defined as follows:

A computer is a programmable machine that receives input, stores and


manipulates data, and provides output in a useful format.
In regards to today‟s computers, the “machine” part of the computer is called
the hardware, while the “programmable” part is called the software.

Software means computer instructions or data. Or

Software is a set of programs, which is designed to perform a well defined


function.

A program is a sequence of instruction written to solve a particular problem.

System Software

System software is the software that communicates directly with the computer.
It provides the interface between user and the computer.
This software can be divided into 3 main categories.
 Operating System (OS)

 Utilities

 Language Translators

Applications Software

Applications software is software written to be “applied” to a particular task,


such as creating text documents or graphics. These programs are interpreted
and run by the Operating System. Applications software can be categorized
into many types.

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 4 of 33

Here are several categories of popular software.


 Word Processing
 Presentation Graphics
 Spreadsheets
 Database Management
 Web Authoring
 Browsers

Language Translators

Whatever language or type of language we use to write our programs, they


need to be in machine code in order to be executed by the computer. There are
3 main categories of translator used,

Assembler
An assembler is a program that translates the mnemonic codes used in
assembly language into the bit patterns that represent machine operations.
Assembly language has a one-to-one equivalence with machine code, each
assembly statement can be converted into a single machine operation.

Compiler
A compiler turns the source code that you write in a high-level language into
object code (machine code) that can be executed by the computer.
The compiler is a more complex beast than the assembler. It may require
several machine operations to represent a single high-level language
statement. As a result, compiling may well be a lengthy process with very large
programs.
Interpreter
Interpreters translate the source code at run-time. The interpreter translates
statements one-at-a-time as the program is executed.
Interpreters are often used to execute high-level language programs whilst
they are being developed since this can be quicker than compiling the entire

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 5 of 33

program. The program would be compiled when it is complete and ready to be


released.
Interpreters are also used with high-level scripting languages like PHP,
Javascript and many more. These instructions are not compiled and have to be
interpreted either by the browser (in the case of Javascript) or by interpreters
on the server (in the case of PHP).
Some programming languages make use of both compilers and interpreters. If
you were to write a Java program in a text editor, when you came to compile it
with the Java compiler, you would actually be creating something called
bytecode.
Bytecode can be thought of as an intermediate stage between source code and
object code. When a computer executes a Java program, library programs on
that machine interpret the bytecode. This allows Java to be platform-
independent - a user needs the correct run-time libraries for Java on their
machine in order to execute the programs.

Computer Science is the study of the theoretical foundations of information


and computation, and of practical techniques for their implementation and
application in computer systems.
So, computer science is all about taking in information and then performing
some computations & analysis to solve a particular problem or produce a
desired result, which depends on the application at hand.

A program is a sequence of instructions that can be executed by a computer to


solve some problem or perform a specified task.

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 6 of 33

Modular Programming
Modular programming is the process of subdividing a computer program into
separate sub-programs.

Structured Programming
A technique for organizing and coding computer programs in which a hierarchy of
modules is used, each having a single entry and a single exit point, and in which
control is passed downward through the structure without unconditional branches to
higher levels of the structure.
Three types of control flow are used: Sequence, Selection, and Iteration.

1. Sequence Control Structure (Sequential Flow):


The sequence structure simply executes a sequence of statements in the
order in which they occur.
The modules are executed one after the other.
2. Selection Control Structure ( Conditional Flow):
The selection structure tests a condition, then executes one sequence of
statements , depending on whether the condition is true or false. A
condition is any variable or expression that returns a Boolean value
(TRUE or FALSE).
3. Iteration Control Structure (Repetitive Flow).
The iteration structure executes a sequence of statements repeatedly as
long as a condition holds true.

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 7 of 33

Algorithm: an algorithm is a step-by-step procedure to solve a given


problem.

Write an Algorithm to add 3numbers

Step1: start

Step2: Input A,B,C

Step3: S=A+B+C

Step4: Output S

Step5:Stop

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 8 of 33

Flowchart: it is a graphical representation of an algorithm.

Flowcharts are usually drawn using some standard symbols as shown below.

Symbol Name Function


Indicates the starting or
ending of the program or
Start/Stop
process.

Denotes either an Input


Input/Output or Output operations.

Denotes decision(or
Decision branch) to be made.

Denotes a process to be
carried out.
Processing
Ex addition, subtraction,
division ….

Connectors On-page references

Used to invoke a
subroutine program or
Predefined Process
an interrupt program.

Flow Lines Show direction of flow.

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 9 of 33

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 10 of 33

Flow Chart examples

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 11 of 33

Overview of C Language
“C” is a structured programming language developed by Dennis Ritchie in 1972
at Bell Laboratories. C language was developed with UNIX operating system, so
it is strongly associated with UNIX, which is one of the most popular network
operating system in use today.

Features of “C” Language


1. It is robust language because of rich set of built-in functions and operators
that can be used to write any complex program.
2. It is efficient and fast because of its variant data-types and powerful
operation.
3. It is highly Portable i.e., programs written in one computer can be run on
another
4. It is well suited for structure program, thus allows the user to think about
the problem in the terms of functional blocks.
5. Debugging, testing and maintenance is easy
6. Ability to extend itself, we can continuously add our own functions to the
program.

7. C language is the most widely used language in operating systems and


embedded system development today.

C is called Middle Level Language.


C Programming language is called as Middle Level Language because it
supports the feature of High Level Language and Low Level Language.

C is Called Middle Level Language Justify?


C Programming language is called as Middle Level Language because
(i) it behaves as High Level Language through Functions - gives a modular
programming and breakup, increased efficiency for re-usability
(ii) it gives access to the low level memory through Pointers. Moreover it does
support the Low Level programming i.e, Assembly Language.
As its a combination of these two aspects, its neither a High Level nor a Low
level language but a Middle Level Language.

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 12 of 33

Basic Structure of C programming

C language is very popular language among all the languages. The structure of
a C program is a protocol (rules) to the programmer, while writing a C
program. The general basic structure of C program is shown in the figure
below.

Program Code
/* Our first simple C basic program */
#include <stdio.h>
void main()
{
int A=5, B=10, C;
C=A+B;
printf(“The Value of C is %d”,C);
getch();
}

Explanation for C basic Program:

/* Our first simple C basic program */ Whatever is given inside the


command /* */ in any C program,
won‟t be considered for compilation
and execution.
#include, is known as pre-processor,
the main work of processor is to link
the program with the header file.
Header file is a collection of built-in
functions that helps us in our
program. Header files contain
definitions of functions and variables
#include <stdio.h> which can be incorporated into any C
program by pre-processor #include
statement.
To use any of the standard functions,
the appropriate header file must be
included. This is done at the
beginning of the C source file.
This is a preprocessor command that
includes standard input output header
file(stdio.h) from the C library before
compiling a C program.

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 13 of 33

void main() This is the main function from where


execution of C program begins.
{ This indicates the beginning of the
main function.
int A=5, B=10, C; Variable declarations, all the variables
should be declared before using them
in the program.
C=A+B; Expression, calculation statement.
printf(“The Value of C is %d”,C); printf command prints the output onto
the screen.
getch(); This command waits for any character
input from keyboard.
} This indicates the end of the main
function.

How to Executing a C Program


Step1: Open turbo C IDE(Integrated Development Environment), click on File and
click on New

Step2: After writing the program save it(press f2 to save your program)

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 14 of 33

In the above we can see the file name has GKSir.C , .C is the extension of C
programming, so whenever you save the program, save it with extension .C

Step3: To compile the program. Click on Compile or Press Alt+f9

Step4: To run the program, after free of errors. Click on Run or Press Ctrl+f9

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 15 of 33

Step5: Output

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 16 of 33

Constants,Variables and Datatypes

Character Set : The characters that can be used to form words and
expressions depends upon the computer to which the program is run.

The Characters in C are


1. Letters: A-Z, a-z, both upper and lower case.
2. Digits: 0-9.
3. Special character: +-*/%?<>~!@#$&(){}[]|\ etc
4. White spaces are newline, horizontal tab, blank space .

Keywords: Keywords are those words whose meaning is already defined by


the compiler. Keywords cannot be used as variable name. No header file is
needed to include the keywords. Keywords should be written in lower case
letters.

Keywords are also called has reserved words.

There are 32 keywords in C.

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 17 of 33

Identifiers: In C Programming, identifiers are names given to C entities, such


as variables, functions, structures etc. identifiers are created to give unique
name to C entities to identify it during the execution of program.

Example: #define NUM 10


#define A 20
int B;

“NUM”, “A” , B are user – defined identifiers.

Variables: A Variable is a name give to memory location where the actual data
is stored.
To indicate the memory location, each variable should be given a unique name
called identifier.

Rules for writing variable name in C


1. Variable name can be any combination of 1 to 31 alphabets, digits or
underscores.
2. The first character in the variable name must be an alphabet.
3. No blanks are allowed within a variable name.
4. No special symbol other than underscore can be used in a variable name.
5. Variable name should not be a keyword.

Constants: Constants are the terms that can't be changed during the
execution of a program.
Different types of C constants
Constant Type of value stored
Integer Constant Constant which stores integer value.
Floating Constant Constant which stores float value.

Character Constant Constant which stores character value.

String Constant Constant which stores string value.

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 18 of 33

Integer Constants: Sequence of number 0-9 without decimal points, fractional


part or any other symbols. It requires two or four bytes, can be +ve, -ve or
Zero the number without a sign is as positive.
Example: -10, +20, 40

Floating Constants: floating constants contains decimal value.


Example: 2.5, 5.521, 3.14 etc.

Character Constants: A single character constant is given within a pair of single


quote.
Example: „a‟, ‟8‟

String Constant: These are the sequence of character within double quote.
Example: “GKMV”, “India”, “4”

Escape Sequences
In C language there are some characters that are not treated as the same.
These character combinations that give different results such as a new line, a
tab space, or a backspace, are known as escape sequences.
Escape sequences in C language start with a backslash (\) and followed by a
character.

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 19 of 33

Data types
In C data types are defined as the data storage format that a variable can store
a data to perform a specific operation.
Variable(data) should be declared before it can be used in program. Data types
are the keywords, which are used for assigning a type to a variable.
C language is rich of data types.

Basic data types in C:


a) Character data type:
Character data type allows a variable to store only one character.
char keyword is used to refer character data type.
Storage size of char data type is 1 byte. We can store only one character
using character data type
The format specifiers used for char is %c
b) Integer data type:
Integer data type allows a variable to store numeric values.
int keyword is used to refer integer data type.
The storage size of int data type is 2 byte.
The format specifiers used for int is %d
c) Floating point data type:
Floating point data type allows a variable to store decimal values.
float keyword is used to refer floating point data type.
The storage size of float data type is 4 byte.
The format specifiers used for int is %f
We can use up-to 6 digits after decimal using float data type.

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 20 of 33

d) Void data type:


Void is an empty data type that has no value.
This can be used in functions and pointers.

Variable Declaration in C
A variable declaration provides assurance to the compiler that there is one
variable existing with the given type and name so that compiler proceed for
further compilation. A variable declaration has its meaning at the time of
compilation only, compiler needs actual variable declaration at the time of
linking of the program.
A variable declaration has the form:
type variable_list;
Here type must be a valid C data type. The variable_list may consist of one or
more identifier names separated by commas.
Some of the valid declaration are shown below.
int a, b;
char ch;
float sum, average;

In the above example,


a and b are the variable of integer data type.
ch is a variable of character data type.
sum and average are the variable of floating point data type.

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 21 of 33

Assigning values to variables

Variables may be initialized at the time of declaration by assigning a value to


them as show in the below example:

int a=10, b=5;


char ch=‟y‟;
float sum=0.0, average=12.45;

In the above example,


a and b are the variable of integer data type with value of 10 and 5
respectively.
ch is a variable of character data type with a value „y‟. character value must be
enclosed by single quote.
sum and average are the variable of floating point data type with a value 0.0
and 12.45 respectively.
Symbolic constant in c Language
A symbolic constant is name that substitute for a sequence of character that
cannot be changed. The character may represent a numeric constant, a
character constant, or a string. When the program is compiled, each occurrence
of a symbolic constant is replaced by its corresponding character sequence.
They are usually defined at the beginning of the program. The symbolic
constants may then appear later in the program in place of the numeric
constants, character constants, etc., that the symbolic constants represent.
For example
#define PI 3.141593
#define X 45

#define PI 3.141593 defines a symbolic constant PI whose value is 3.141593.


When the program is preprocessed, all occurrences of the symbolic constant PI
are replaced with the replacement text 3.141593.

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 22 of 33

Operators and Expression

An operator is a symbol that tells the compiler to perform specific mathematical


or logical manipulations. C language e is rich in built-in operators and provides
the following types of operators.

a)Arithmetic operators

C provides all the basic arithmetic operators, they are +, -, *, /, % Integer


division truncates any fractional part. The modulus division produces the
remainder of an integer division.
Assume variable A holds 7 and variable B holds 2 then:
Operator Description Example
A+B
+ Adds two operands
7 +2=9
A-B
- Subtracts second operands from first
7-2=5
operands
A*B
* Multiply both operands
7*2=14
A/B
/ Divide numerator by denominator
7/2=3
A%B
% Modulus Operator and remainder of after an
7%2=1
integer division

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 23 of 33

b) Relational operators

Relational operator‟s checks relationship between two operands. If the relation


is true, it returns value 1 and if the relation is false, it returns value 0.

Assume variable A holds 10 and variable B holds 15 then:

Operator Description Example


A==B
Checks if the value of two operands is equal
10==15
== or not, if yes then condition becomes true.
returns
False(0)
Checks if the value of two operands is equal A!=B
or not, if values are not equal then condition 10!=15
!=
becomes true. returns
True(1)
Checks if the value of left operand is greater A>B
than the value of right operand, if yes then 10>15
>
condition becomes true. returns
False(0)
Checks if the value of left operand is less than A<B
the value of right operand, if yes then 10<15
<
condition becomes true. returns
True(1)
Checks if the value of left operand is greater A>=B
than or equal to the value of right operand, if 10>=15
>=
yes then condition becomes true. returns
False(0)
Checks if the value of left operand is less than A<=B
or equal to the value of right operand, if yes 10<=15
<=
then condition becomes true returns
True(1)

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 24 of 33

c) Logical operators

Logical operators are used to combine expressions containing relation


operators.

Assume variable A holds 5 and variable B holds 2 then:

Operator Description Example


((A ==5) && (B>5))
&& Called Logical AND operator. If both the
returns false.
operands are non zero then the
condition becomes true.
((A ==5) && (B>5))
|| Called Logical OR Operator. If any of
returns true.
the two operands is non zero then the
condition becomes true.
!(A==5)
! Called Logical NOT Operator. Use to
returns false.
reverses the logical state of its
operand. If a condition is true then
Logical NOT operator will make false.

d) Assignment operators

The most common assignment operator is =. This operator assigns the value in
right side to the left side.

Simple assignment operator


The variable or value of right hand side is assigned to left hand side
variable.
Example:
A=B; (the variable B value will be assigned to variable A)
A=45; (the value 45 is assigned to variable A)
A=B=C;( the variable value C is assigned to B, the variable value B is
assigned to A)

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 25 of 33

Compound assignment operator

Operator Description Example


A= B
Simple assignment operator, Assigns
The value of B is
= values from right side operands to left
assigned to A. now A
side operand
get the new value.
Add AND assignment operator, It A += B
+= adds right operand to the left operand is equivalent to
and assign the result to left operand A = A +B
Subtract AND assignment operator, It A -= B
subtracts right operand from the left is equivalent to
-=
operand and assign the result to left A = A -B
operand
Multiply AND assignment operator, It A *= B
multiplies right operand with the left is equivalent to
*=
operand and assign the result to left A = A *B
operand
Divide AND assignment operator, It A /= B

divides left operand with the right is equivalent to


/=
operand and assign the result to left A = A/B

operand
Modulus AND assignment operator, It A %= B
%= takes modulus using two operands is equivalent to

and assign the result to left operand A = A%B

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 26 of 33

Difference between = and ==


= is an assignment operator, == is a relational operator.
Example for =
A=B
The value of B will be assigned to A, A gets a new value of B

Consider A=5 and B=8;


A=B
The value 8 will be assigned to A, A gets a new value as 8.

Example for ==
A==B
It checks whether the value of A and B are equal or not.
Consider A=5 and B=8;
A==B
It checks whether the value of A and B are equal or not.

e) Increment and Decrement Operators


There are two special unary operators in C, Increment operator ++ and
Decrement operator --.

The Increment operator causes its operand to be increased by 1.


The Decrement operator causes its operand to be decreased by 1.
The operand used with each of these operators must be a single variable.

The increment and decrement operators can be each be utilized two different
ways, depending on whether the operator is written before or after the
operand. If the operator precedes the operator(eg:++i) then it is called as pre-
increment operator. If the operator follows the operands(eg: i++) then it is
known as post-increment operator.

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 27 of 33

Pre-increment and Post-increment operator


In pre-increment operator, the operand will be incremented before it is utilized.
In post-increment operator, the operand will be incremented after it is utilized.

Examples:

a=10; b=20;
x=++a;
y=b++;
Here x=++a statement is equivalent to following two statement in sequence.
a=a+1;
x=a;
So the value of x and a will be 11.
y=b++ is equivalent to
y=b;
b=b+1;
so the value of y=20 and b=21.

Thus, pre-increment operator first increments the value of the operand and
then returns the value (the incremented one).
post-increment operator first returns the value and then increments the value
of the operand.

Consider an example below.


s=8;
printf(“s=%d”\n”, s);
printf(“s=%d”\n”, ++s);
printf(“s=%d”\n”, s);

The output will be generated as shown below


s=8
s=9
s=9

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 28 of 33

Consider an example below.


s=3;
printf(“s=%d”\n”, s);
printf(“s=%d”\n”, s++);
printf(“s=%d”\n”, s);

The output will be generated as shown below


s=3
s=3
s=4

Consider an example below.


m=10;
The hierarchy of operator precedence for unary operator is from Right-to Left.
So consider the printf() statement

Consider an example.
a=10, b=20;
b=a++;
a=++b;
printf(“a=%d\n b=%d”,a,b);
The output will be generated as shown below
a=11
b=11

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 29 of 33

f) Conditional operators or Ternary operator (? : ;)


It is one of the special operators supported by the C language. The conditional
operator evaluates an expression and returns one of two values based on
whether condition is TRUE or FALSE.
Syntax:
condition ? expression1 : expression2;

It is also called as Ternary operators because it uses 3 operators ( ? : ;)

If the condition is TRUE, it returns expression1 and if not it returns expression2.

Example:
x=10, y=15,
BIG=(x>y)? x:y;
printf(“Largest of two number= %d \n”, BIG);
the output is:
Largest of two number = 15

g) Bitwise Operators
Bitwise operator works on bits and performs 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 if A = 60; and B = 13; now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 30 of 33

A&B = 0000 1100


A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The Bitwise operators supported by C language are listed in the following table.
Assume variable A holds 60 and variable B holds 13, then:

Operator Description Example

Binary AND Operator copies a bit to the (A & B) will give 12, which
&
result if it exists in both operands. is 0000 1100

Binary OR Operator copies a bit if it (A | B) will give 61, which


|
exists in either operand. is 0011 1101

Binary XOR Operator copies the bit if it is (A ^ B) will give 49, which
^
set in one operand but not both. is 0011 0001

(~A ) will give -61, which


Binary Ones Complement Operator is
~ is 1100 0011 in 2's
unary and has the effect of 'flipping' bits.
complement form.

Binary Left Shift Operator. The left


operands value is moved left by the A << 2 will give 240 which
<<
number of bits specified by the right is 1111 0000
operand.

Binary Right Shift Operator. The left


operands value is moved right by the A >> 2 will give 15 which
>>
number of bits specified by the right is 0000 1111
operand.

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 31 of 33

h) sizeof() operators
The sizeof() operator returns the size of its operand in bytes. The sizeof()
operator always precedes its operand. The operand may be an expression or it
may be a cast.
Example:
Suppose i is an integer variable and c is a character type variable then
printf(“Size of integer : %d”,sizeof(i));
printf(“Size of character : %c”,sizeof(c));

the output generated:


Size of integer : 2
Size of character : 1

It is a unary operator which is used in finding the size of data type, constant,
arrays, structure etc.

i) Comma operators
Comma operators are used to link related expressions together.
For example:
int a,c=5,d;

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 32 of 33

The hierarchy of operator precedence’s

Category Operator Associativity


Unary ++ -- ! sizeof - Right to left
Arithmetic operators * /%+- Left to right
Relational operators < <= > >= Left to right
Equality == != Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional operators ?: Right to left
= += -= *= /= %=>>=
Assignment operators Right to left
<<= &= ^= |=
Comma , Left to right

Example 1: Determine the hierarchy of operations and evaluate the following


expression:
i= 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8
Stepwise evaluation of this expression is shown below:
i= 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8
i= 6 / 4 + 4 / 4 + 8 - 2 + 5 / 8 operation: *
i= 1 + 4 / 4 + 8 - 2 + 5 / 8 operation: /
i= 1 + 1+ 8 - 2 + 5 / 8 operation: /
i= 1 + 1 + 8 - 2 + 0 operation: /
i= 2 + 8 - 2 + 0 operation: +
i= 10 - 2 + 0 operation: +
i= 8 + 0 operation : -
i= 8 operation: +
Note that 6 / 4 gives 1 and not 1.5. This so happens because 6 and 4 both are
integers and therefore would evaluate to only an integer constant.
Similarly 5 / 8 evaluates to zero, since 5 and 8 are integer constants and hence
must return an integer value.

www.gkmvkalyan.blogspot.in
BCA103T – Problem Solving Techniques using C Page 33 of 33

Example 2: Determine the hierarchy of operations and evaluate the following


expression:
k=3/2*4+3/8+3
Stepwise evaluation of this expression is shown below:
k=3/2*4+3/8+3
k = 1 * 4 + 3 / 8 + 3 operation: /
k = 4 + 3 / 8 + 3 operation: *
k = 4 + 0 + 3 operation: /
k = 4 + 3 operation: +
k = 7 operation: +
Note that 3 / 8 gives zero, again for the same reason mentioned in the previous
example.

So far we have seen how the computer evaluates an arithmetic statement


written in C. But our knowledge would be incomplete unless we know how to
convert a general arithmetic statement to a C statement. C can handle any
complex expression with ease.
Some of the examples of C expressions are shown in below.

www.gkmvkalyan.blogspot.in

You might also like