BCA BCA-101
101 PROBLEM SOLVING USING
Programming in C C
For BCA
Prepared By:
Dr.Shaweta Sachdeva
Associate Professor
Syllabus
UNIT-I
Problem Solving Techniques: Understanding the problem, Developing algorithms and flowcharts, Writing pseudocode,
Structured solution planning, Testing and debugging strategies.
Introduction to C Programming: Features and applications of C, Origin of C at Bell Labs, Relationship with B and BCPL
languages and its influence on modern programming, Structure of a C program, Syntax and Semantics, Memory layout of a C
program, Input/Output functions, Indentation and comments.
Core Programming Concepts: Storage Classes, Keywords, Identifiers, Data types, Constants, Variables, Operators, Expressions,
Precedence and associativity, Type conversion (implicit explicit), Use of header files .
Computer Languages
• Machine language
• Assembly language
• High-level language
Machine Language
• Only language of a computer understood by it
without using a translation program
• Normally written as strings of binary 1s and 0s
Advantage
• Can be executed very fast
Limitations
• Machine Dependent
• Difficult to program
• Error prone
• Difficult to modify
Assembly/Symbolic Language
• Programming language that overcomes the limitations
of machine language programming by:
• Using alphanumeric mnemonic codes instead of
numeric codes
• Allowing storage locations to be represented in form
of alphanumeric addresses instead of numeric
addresses e.g. representing memory locations 1000,
1001, and 1002 as FRST, SCND, and ANSR
respectively
Sample assembly language program for adding two numbers and storing theresult
Assembler
• Software that translates as assembly language
program into an equivalent machine language
program of a computer
Advantages and disadvantages of Assembly
Language Over Machine Language
• Easier to understand and use
• Easier to locate and correct errors
• Easier to modify
Demerits
• Machine dependent
• Knowledge of hardware required
• Machine level coding
High-Level Languages
• Machine independent
• Do not require programmers to know anything
about the internal structure of computer on
which high-level language programs will be
executed
• Deal with high-level coding, enabling the
programmers to write instructions using
English words and familiar mathematical
symbols and expressions
Compiler
• Translator program (software) that translates a
high level language program into its equivalent
machine language program
• Compiles a set of machine language
instructions for every program instruction in a
high-level language
Interpreter
• Interpreter is a high-level language translator
• Takes one statement of a high-level language
program, translates it into machine language
instructions
• Immediately executes the resulting machine
language instructions
• Compiler simply translates the entire source
program into an object program and is not
involved in its execution
Interpreter
Merits and demerits
• Machine independent
• Easier to learn and use
• Fewer errors during program development
• Lower program preparation cost
• Better documentation
• Easier to maintain
Demerits
Lower execution efficiency
Less flexibility to control the computer’s CPU, memory and registers
Pseudocode
• A program planning tool where program logic is
written in an ordinary natural language using a
structure that resembles computer instructions
• “ Pseudo” means imitation or false and “ Code” refers
to the instructions written in a programming language.
Hence, pseudocode is an imitation of actualcomputer
instructions
• Because it emphasizes the design of the program,
pseudocode is also called Program Design Language
(PDL)
Overview of C
• C is a programming language developed at AT & T’s
Bell Laboratories of USA in 1972.
• It was designed and written by Dennis Ritchie.
• C is a structured programming language
• C supports functions that enables easy maintainability
of code, by breaking large file into smaller modules
• Comments in C provides easy readability
• C is a powerful language
Program structure
A sample C Program
#include<stdio.h>
int main( )
{
}
Preprocessor Statements:
• These statements begin with # symbol. They are
called preprocessor directives. These statements
direct the C preprocessor to include header files and
also symbolic constants in to C program.
• Some of the preprocessor statements are :
#include: for the standard input/output functions
• #define PI 3.141592 symbolic constant
Header files
• The files that are specified in the include section is
called as header file.
• These are precompiled files that has some functions
defined in them.
• We can call those functions in our program by
supplying parameters.
• Header file is given an extension .h
• C Source file is given an extension .c
Main function
• This is the entry point of a program
• When a file is executed, the start point is the main
function
• From main function the flow goes as per the
programmers choice.
• There may or may not be other functions written by
user in a program
• Main function is compulsory for any c program
Writing the first program
#include<stdio.h>
int main()
{
printf(“Hello world”);
getch();
return 0;
}
• This program prints Hello on the screen when weexecute
it
• getch() method pauses the Output Console until a key is
pressed.
The C Character Set
A character denotes any alphabet, digit or special symbol used to
represent information.
Constants, Variables and Keywords
The alphabets,
numbers and special symbols when properly combined
form constants, variables and keywords. A constant isan entity that
doesn’t change whereas a variable is an entity thatmay change.
There are two ways to define constant in C programming.
• const keyword #include<stdio.h>
• #define preprocessor int main(){
const float PI=3.14;
PI=4.5;
#include<stdio.h> printf("The value of PI is: %f",PI)
int main(){ ;
const float PI=3.14; return 0;
printf("The value of PI is: %f", PI);}
return 0; } If you try to change the value of PI,
it will render compile time error.
The #define preprocessor is also used to
define constant.
The #define preprocessor directive is usedto
define constant or micro substitution. It can use
any basic data type.
Syntax: #define token value
#include <stdio.h>
#define PI 3.14
void main()
{
printf("%f",PI);
}
Constants
C Variables
An entity that may vary during program execution is called a variable.
Variable names are names given to locations in memory.
Declaration: int age;
Initialization: int age=24;
C Keywords
Keywords are the words whose meaning has already been explained to the C
compiler (or in a broad sense to the computer). The keywords cannot be used
asvariable names because if we do so we are trying to assign a new
meaning to the keyword, which is not allowed by the computer.
Return type and value of printf function
printf is a library function of stdio.h, it is used to display messages as
well as values on the standard output device (monitor). printf
returns an integer value, which is the totalnumber of printed
characters.
new line character ( "\n")
#include <stdio.h>
void main()
{ int res;
res = printf("Hello\n");
printf("Total printed characters are: %d\n",res); }
Output
Hello
Total printed characters are: 5
Return type and value of scanf function
scanf is a library function of stdio.h, it is used to take inputfrom
the standard input device (keyboard). scanf returns an integer value,
which is the total number of inputs.
#include <stdio.h>
void main()
{ int x,y;
int res;
printf("Enter two number: ");
res=scanf("%d%d", &x,&y);
printf("Total inputs are: %d\n",res); }
#include <stdio.h>
int main()
{ printf("%d", printf("welcome to programming class"));
return 0;
}
Data Type Range Bytes Forma
t
signed char -128 to + 127 1 %c
unsigned char 0 to 255 1 %c
short signed int -32768 to +32767 2 %d
short unsigned int 0 to 65535 2 %u
signed int -32768 to +32767 2 %d
unsigned int 0 to 65535 2 %u
long signed int -2147483648 to 4 %ld
+2147483647
long unsigned int 0 to 4294967295 4 %lu
float -3.4e38 to 4 %f
+3.4e38
double -1.7e308 to 8 %lf
+1.7e308
long double -1.7e4932 to 10 %Lf
+1.7e4932
Note: The sizes and ranges of int, short and long are compiler dependent. Sizes in this figure are
for 16-bit compiler.
By default char (signed char) int is (signed int)
I byte = 8 bits either 0 or 1
In case of signed one bit unsinged 2n-1
is reserved for sign
+/- 28-1 = 256-1=255
-2n-1 to 2n-1 -1 So range is 0 to 255 for
-27 to 27-1
= -128 to 127
unsinged cha
Unsinged integer
If int takes 2 bytes then 2 bytes = 16 bits
Formula is 2n-1
216-1 = 65536-1=65535
Range is 0 to 65535
singed
0 to 65535 integer
-2n-1 to 2n-1 -1
-215 to 215 -1
-32768 to +32767
Identifiers
Definition: Names used to identify variables, functions, arrays, etc.
Rules:
o Must begin with a letter or underscore.
o Can contain letters, digits, and underscores.
o Case-sensitive (Variable and variable are different).
Operators and Expressions
An operator specifies an operation to be performedthat
yields a value. The variables, constants can bejoined by
various operators to form an expression.
An operand is a data item on which an operatoracts.
Some operators require two operands, while others act
upon only one operand. C includes alarge number of
operators that fall under severaldifferent categories,
which are as follows
Expression
Expression is a combination of operators, constants, variables
and function calls. The expression can be arithmetic, logical or
relational
x+y //arithmetic operation
a = b+c //uses two operators ( =') and ( + )
a>b //relational expression
a== b //logical expression
func(a, b) //function call
• Arithmetic operators
• Assignment operators .
• Increment and Decrement operators
• Relational operators.
• Logical operators
• Conditional operator
• Comma operator
• sizeof operator
• Bitwise operators
• Other operators
Binary Arithmetic Operators
a= 17, b=4
Increment And Decrement Operators
C has two useful operators increment ( ++ ) and decrement( - - ).
These are unary operators because they operate on a single
operand. The increment operator ( ++ ) incrementsthe value of the
variable by 1 and decrement operator ( - - )decrements the value of
the variable by 1.
++x is equivalent to x = x + 1
--x is equivalent to x = x-I
These operators are of two types
• Prefix increment / decrement - operator is written beforethe
operand (e.g. ++x or - -x )
Postfix increment / decrement - operator is written after the
operand (e.g. x++ or x - - )
Prefix Increment / Decrement
Here first the value of variable is incremented / decremented then the
new value is used in the operation .
X=3 (x whose value is 3.)
The statement y = ++x; means first increment the value of x by 1, then
assign the value of x to y .
equivalent to these two statements (x = x+1; y = x; )
Hence now value of x is 4 and value of y is 4.
The statement y = - -x ;
means first decrement the value of x by 1 then assign the value of x to y
This statement is equivalent to these two's statements
x = x -1 ; y= x;
Hence now value of x is 3 and value of y is 3.
Postfix Increment / Decrement
Here first the value of variable is used in the operation and then
increment/decrement is perform.
Let us take a variable whose value is 3.
The statement y = x++; means first the value of x is assigned to y and then
x is incremented.
statement is equivalent to these two statements y = x; x = x+1;
Output :
x is 4 and value of y is 3.
y = x- -; means first the value of x is assigned to y and then x is decremented.
This statement is equivalent to these two statements
y = x;
x = x-I;
Output is x is 3 and value of y is 4.
a++ + b (a=4, b=3)
Post Increment/decrement in the context of equation - First use the value inthe
equation and then increment the value
Pre increment/Decrement in context of equation -
First increment the value and then use in the equation after completion of the
equation
Relational Operators
Relational operators are used to compare values of two expressions depending on their
relations. An expression that contains relational operators is called relational
expression. If the relation is true "then the value of relational expression is 1 and if the
relation is false then the value of expression is 0
a=9, b=5
Logical Or Boolean Operators
The expression that combines two or more expressions is termed as a
logical expression. For combining these expressionswe use logical
operators. These operatorsreturn 0 for false and 1 for true. C has three
logical operators.
AND ( &&) Operation (This operator gives the net result true if
both the conditions are true, otherwise the result is false. )
a = 10, b = 5, c = 0
nonzero
values are
regarded as
true and
zero valueis
regarded as
false
OR ( II) Operator
This operator gives the net result false, if both the conditions havethe
value false, otherwise the result is true.
a = 10, b = 5, c =0
Consider the logical expression(a >= b) I I (b > 15)This
gives result true because one condition is true
Not ( ! ) Operator
This is a unary operator and it negates the value of the condition. If the value of
the condition is false then it gives the result true. If the value of the condition is
true then it gives the result false.
a = 10, b = 5, c = 0
! ( a = = 10 )
The value of the condition (a= =10) is true.
NOT operator negates the value of the condition. Hence the result is false.
Assignment Operator
x=8
y=5
s = x+y-2 /* Value of expression x+y-2 is assigned tos*/
y=x /* Value of x is assigned to y*/
Operator Description Example
= Simple assignment operator. Assigns values from right C = A + B will assign the value
side operands to left side operand of A + B to C
+= Add AND assignment operator. It adds the right
C += A is equivalent to C = C +
operand to the left operand and assign the result to the
A
left operand.
-= Subtract AND assignment operator. It subtracts the
right operand from the left operand and assigns the C -= A is equivalent to C = C - A
result to the left operand.
*= Multiply AND assignment operator. It multiplies the
C *= A is equivalent to C = C *
right operand with the left operand and assigns the
A
result to the left operand.
/= Divide AND assignment operator. It divides the left
operand with the right operand and assigns the result C / A is equivalent to C = C / A
to the left operand.
%= Modulus AND assignment operator. It takes modulus
C %= A is equivalent to C = C
using two operands and assigns the result to the left
%A
operand.
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2
Conditional Operator
Conditional operator is a ternary operator ( ? and : )
which requires three expressions as operands. This
written as
Test Expression ? expression1 : expression2
a < b ? printf("a is smaller") : printf("b is smaller");
max = a > b ? a : b;
Precedence And Associatively of operators
Precedence: The order in which operators are evaluated in an
expression.
Consider the following expression
2+3*5
If addition is performed before multiplication then resultwill
be 25
and if multiplication is performed before addition thenthe
result will be 17.
Associativity: The direction in which an expression is evaluated
when two operators have the same precedence.
When an expression contains two operators of equal priority the tie
between them is settled using the associativity of the operators.
Associativity can be of two types—Left to Right or Right to Left.
x = a+b < c
+ operator has higher precedence than < and =, and < has more
precedence than =, so first a+b will be evaluated, then < operator will
be evaluated, and atlast the whole value will be assigned to x.
If a = 2,b = 6, c = 9 then final value of x will bex *= a+ b
(+)operator has higher precedence than *=, so a+b will
be evaluated before compound assignment. This isinterpreted as x =
x*(a+b) and not as x = x*a+b.
x = 5, a = 2, b = 6
Ans=40;
x = a<=b II b==c
Here order of evaluation of operators will be
<=, = =, II,=. If initial values area = 2, b = 3,
c = 4,
then final value of x will 'be 1.
.
What if Two operators are of same precedence
For example-5
+ 16 / 2 * 4
Here / and * have higher precedence than + operator, so theywill be
evaluated before addition. But / and * have sameprecedence, so
which one of them will be evaluated first still remains a problem. If /
is evaluated before *, then the result is37 otherwise the result is 7.
Similarly consider this expression
20 - 7 - 5 - 2 -1
Here we have four subtraction operators, which of course have the
same precedence level. If we decide to evaluate from left to right
then answer will be 5, and if we evaluatefrom right to left the
answer will be 17.
To solve these types of problems, an associativity property is assignedto
each operator. Associativity of the operators within same group is same.
All the operators either associate from left to right or from rightto left
5 + 16 / 2 *4 since / and * operators associate from left to right
will be evaluated before * and the correct result 37.
20 - 7 - 5 - 2 -1 The subtraction operator associates from left to
right
so the value of this expression is 5
The assignment operator associates from right to left. Suppose we
have a multiple assignment expression like this x=y=z=5
initially the integer value 5 is assigned to variable z and then value
of expression z = 5 is assigned variable y. The value of expression
z = 5 is 5, so 5 is assigned to variable y and now the value of
expression y =z = 5 becomes 5.
Now value of this expression is assigned to x and the value of
hole
expression x = y = z = 5 becomes 5.
Format Specifiers
Definition: Used in printf() and scanf() functions to specify the type of
data.
Common Specifiers:
o %d: Integer.
o %f: Floating-point number.
o %c: Character.
o %s: String.
Thankyou…