Pps Unit 1
Pps Unit 1
PPS UNIT 1
21CSS101J
PROGRAMMING FOR PROBLEM SOLVING
for
BE/BTECH I SEM
S.PRABU
Assistant Professor
C-TECH-SRM-IST-KTR
01
UNIT-1
Evolution of Programming & Languages - Problem solving through programming -
Writing algorithms & Pseudo code - Single line and multiline comments - Introduction to
C: Structure of the C program - Input and output statements. Variables and identifiers,
Constants, Keywords - Values, Names, Scope, Binding, Storage Classes - Numeric Data
types: integer, floating point Non-Numeric Data types: char and string - L value and R
value in expression, Increment and decrement operator - Comma, Arrow and Assignment
operator, Bitwise and Size-of operator - Arithmetic, Relational and logical Operators -
Condition Operators, Operator Precedence - Expressions with pre / post increment
operator
In the early days, Charles Babbage had made the device, but he was confused about
how to give instructions to the machine, and then Ada Lovelace wrote the instructions
for the analytical engine.
The device was made by Charles Babbage and the code was written by Ada Lovelace
for computing Bernoulli’s number.
First time in history that the capability of computer devices was judged.
02
1952: Autocode
Developed by Alick Glennie.
The first compiled computer programming language.
COBOL and FORTRAN are the languages referred to as Autocode.
1958: ALGOL
ALGOL stands for algorithmic Language.
The initial phase of the most popular programming languages of C, C++, and JAVA.
It was also the first language to implement the nested function and has a simple syntax
than FORTRAN.
The first programming language to have a code block like “begin” that indicates that
your program has started and “end” means you have ended your code.
1959: COBOL
It stands for Common Business-Oriented Language.
COBOL was one of the first programming languages designed for use in business and
finance.
It was created as part of a US Department of Defence effort to create a portable
programming language for data processing.
1964: BASIC
It stands for Beginners All-Purpose Symbolic Instruction Code.
In 1991 Microsoft released Visual Basic, an updated version of Basic
The first microcomputer version of Basic was co-written by Bill Gates, Paul Allen,
and Monte Davidoff for their newly-formed company, Microsoft.
03
1972: C
It is a general-purpose, procedural programming language and the most popular
programming language till now.
All the code that was previously written in assembly language gets replaced by the C
language like operating system, kernel, and many other applications.
It can be used in implementing an operating system, embedded system, and also on
the website using the Common Gateway Interface (CGI).
C is the mother of almost all higher-level programming languages like C#, D, Go,
Java, JavaScript, Limbo, LPC, Perl, PHP, Python, and Unix’s C shell.
After 1980s
The object oriented programming languages are developed
C++
Java
Dot Net
Python
04
05
Algorithm
A step by step procedure to solve a particular problem.
It is a ordered set of rules to solve a problem.
finite set of instructions which are being carried in a specific order to perform the
specific task
Need
Algorithms are necessary for solving complex problems efficiently and effectively.
06
Advantages of Algorithms
In an Algorithm the problem is broken down into smaller pieces or steps hence, it is
easier for the programmer to convert it into an actual program.
Due to stepwise representation, it is easy to understand.
Disadvantages of Algorithms:
Writing an algorithm takes a long time so it is time-consuming.
Branching and Looping statements are difficult to show in Algorithms(imp).
07
Example
Addition of two numbers
Step 1: Start
Step 2: Declare three variables a, b, and c.
Step 3: Enter the values of a and b.
Step 4: Add the values of a and b and store the result in the c variable,
i.e., c=a+b.
Step 5: Print c
Step 6: Stop
PSEUDO CODE
Methodology to represent the implementation of an algorithm.”
It is written in the form of annotations and informational text that is written in plain
English only.
Just like programming languages, it doesn't have any syntax, so it cannot be compiled
or interpreted by the compiler.
It is a methodology that allows the programmer to represent the implementation of an
algorithm.
Simply, we can say that it’s the cooked up representation of an algorithm.
Often at times, algorithms are represented with the help of pseudo codes as they can
be interpreted by programmers no matter what their programming background or
knowledge is.
08
09
Topic : 4 Comments
#include<stdio.h>
int main()
{
//printing information
printf("Hello C");
return 0;
}
/*
code
to be commented
*/
10
Content Code
Documentation // Addtion of 2 Numbers
#include <stdio.h>
Pre-processor Section
#define c 10
Global Declaration float pi=3.144;
Main function void main ( )
{
variable Declaration int a,b,c;
Input statement Scanf(“%d%d”, &a,&b);
Execution part c=a+b;
Output statement Printf(“Addition is: %d”,c);
}
void printdemo(void)
Sub Program
{ Printf(“Demo”); }
1. Documentation
This section consists of the description of the program, the name of the program, and
the creation date and time of the program.
It is specified at the start of the program in the form of comments. Documentation can
be represented as:
// description, name of the program, programmer name, date, time etc.
11
Pre-processor Section
All the header files of the program will be declared in the pre-processor section of the
program.
Header files help us to access other’s improved code into our code.
A copy of these multiple files is inserted into our program before the process of
compilation.
Example:
#include<stdio.h>
#include<math.h>
#define is also a preprocessor to define a particular value before the main function.
#include <stdio.h>
#define a 10
int main()
{
printf("%d",a);
return 0;
}
Main() Function
Every C program must have a main function.
The main() function of the program is written in this section.
Operations like declaration and execution are performed inside the curly braces of the
main program.
void main()
{
Statements;
}
12
Sub Programs
User-defined functions are called in this section of the program.
The control of the program is shifted to the called function whenever they are called
from the main or outside the main() function.
These are specified as per the requirements of the programmer.
Input and Output statement are used to read and write the data in C programming.
These are embedded in stdio.h (standard Input/Output header file).
Input means to provide the program with some data to be used in the program.
Output means to display data on screen or write the data to a printer or a file.
C programming language provides many built-in functions to read any given input and
to display data on screen when there is a need to output the result.
There are mainly two of Input/Output functions are used for this purpose. These are
discussed as:
Formatted I/O functions
Unformatted I/O functions
Format specifier: It is a way to tell the compiler what type of data is in a variable.
13
scanf() statement
The scanf() function is an input function.
It used to read the mixed type of data from keyboard.
You can read integer, float and character data by using its control codes or format
codes. The general syntax is as:
scanf(“format_specifier”,&var_name);
Example : scanf(“%d”,&a);
%d for integer, %f for float, %c for character
& : it is the address operator, tells the location of the variable.
a : is a integer variable.
14
printf() statement
This is an output function.
It is used to display a text message and to display the mixed type (int, float, char) of
data on screen.
The general syntax is as:
printf(“format_specifier”,var_name);
Example : printf(“%d”,a);
Format specifier: It is a way to tell the compiler what type of data is in a variable.
%d for integer, %f for float, %c for character
a : is a integer variable.
#include <stdio.h>
void main()
{
char a;
printf(“Enter a single character: ”);
a=getchar();
15
Output :
Enter a single character: e
The entered character is : e
#include <stdio.h>
void main()
{
char a;
printf(“Enter a single character: ”);
a=getch();
Output :
Enter a single character: The entered character is : r
Note: It does not need to press enter key to execute the next step, it immediately show
the result.
getchar( ) getch( )
1 Reading character from standard input Reading character from any input console
2 It use the buffer It doesn’t use buffer
the entered character returns after the the entered character returns immediately
3
enter key is pressed. without having to wait for the enter key
16
#include <stdio.h>
void main()
{
char a[10];
printf("Enter a string: ");
gets(a);
VARIABLE
variables are the storage place where some form of data is stored.
Different variables require different amount of memory on which a set of operations
is applied.
Example. Integer, float, double
How to declare variable (or) syntax for variable declaration
data_type variable_name;
int a;
A variable is a name associated with some memory location.
A variable is used to hold some value that can be changed according to the
requirement.
During program execution, there will be storing of some data in different locations of
memory in computer.
These memory locations are identified by their address like 56234.
17
CONSTANT
As its name suggests constants are the values which will never change during the
execution of program.
Those variables which cannot be modified once they are defined in the program.
syntax for constant declaration
const data_type variable_name = value;
Example: const int a=10;
A constant is used to hold the fixed values which we can retrieve later but cannot
change.
They have fixed values till the program’s life.
We can only assign value to the constant in the declaration.
KEYWORDS
Keywords are predefined, reserved words used in programming that have special
meanings to the compiler.
Keywords are part of the syntax and they cannot be used as an identifier.
There are total 32 keywords in C.
auto double int struct
break else long switch
case enum register typedef
char extern return union
continue for signed void
do if static while
default goto sizeof volatile
const float short unsigned
18
IDENTIFIER
19
The scope of a variable in C is the block or the region in the program where a
variable is declared, defined, and used.
Outside this region, we cannot access the variable and it is treated as an undeclared
identifier.
A storage class represents the visibility and a location of a variable. It tells from what
part of code we can access a variable.
A storage class in C is used to describe the following things:
• The variable scope.
• The location where the variable will be stored.
• The initialized value of a variable.
• A lifetime of a variable.
• Who can access a variable?
AUTO
The variables defined using auto storage class is called as local variables.
Auto stands for automatic storage class.
A variable is in auto storage class by default if it is not explicitly specified.
The scope of an auto variable is limited with the particular block only.
20
Once the control goes out of the block, the access is destroyed.
This means only the block in which the auto variable is declared can access it.
A keyword auto is used to define an auto storage class.
By default, an auto variable contains a garbage value.
Program example
#include <stdio.h>
int main( )
{
auto int j = 1;
{
auto int j= 2;
{
auto int j = 3;
printf ( " %d ", j);
}
printf ( "\t %d ",j);
}
printf( "%d\n", j);
}
Output : 3 2 1
EXTERN
21
Notice that the extern variable cannot be initialized it has already been defined in the
original file.
Result:
value of the external integer is = 48
here the enternal variable ‘i’ can be shared both the files. The value can be
modified in one file to another.
STATIC
The static variables are used within function/ file as local static variables.
They can also be used as a global variable
Static local variable is a local variable that retains and stores its value between
function calls or block and remains visible only to the function or block in which it is
defined.
Static global variables are global variables visible only to the file in which it is
declared.
Example: static int count = 10;
22
Keep in mind that static variable has a default initial value zero and is initialized
only once in its lifetime.
int main()
{
printf("%d ", fun());
printf("%d ", fun());
printf("%d ", fun());
return 0;
}
Output : 1 2 3
Static variables initialized once and retains the values, its never reset again.
23
}
int fun3()
{
count++;
return count;
}
int main()
{
printf("%d ", fun1());
printf("%d ", fun2());
printf("%d ", fun3());
return 0;
}
Output : 1 2 3
Here Static variable is globally declared and shared by different functions.
REGISTER
Register variables tell the compiler to store the variable in CPU register instead of
memory.
Frequently used variables are kept in registers and they have faster accessibility.
We can never get the addresses of these variables. “register” keyword is used to
declare the register variables.
#include <stdio.h>
int main()
{
register int a = 10;
auto int b = 8;
printf("The value of register variable a : %d \n",a);
printf("The value of auto variable b : %d \n",b);
printf("The sum of auto and register variable : %d\n",(a+b));
return 0;
}
24
Output:
The value of register variable a : 10
The value of auto variable b : 8
The sum of auto and register variable : 18
25
26
Type Cast
Type casting is the process in which the compiler converts one data type in a program
to another one.
It is otherwise called as Type Conversion.
Types
• Implicit Type Conversion
• Explicit Type Conversion
#include <stdio.h>
void main()
{
int a=5;
float b;
b=a;
printf("a= %d \n",a);
printf("b= %f",b);}
}
27
Output :
a= 5
b= 5.000000
b value is assigned with necessary zeros in fractional part.
#include <stdio.h>
void main()
{
int a = 15, b = 2;
float div;
// Division of a and b
div = (float)(a / b);
printf("The result is %f\n", div);
}
Output : The result is 7.000000
Here we explicitly convert the integer result into float.
28
Topic : 11 Operators
Operators can be defined as the symbols that help us to perform specific operation.
In other words, we can say that an operator operates the operands.
For example, ‘+’ is an operator used for addition, as shown below:
c = a + b;
Here, ‘+’ is the operator known as the addition operator, and ‘a’ and ‘b’ are operands.
The addition operator tells the compiler to add both of the operands ‘a’ and ‘b’.
Arithmetic Operators
Arithmetic operators are the symbols that can be used to perform common
mathematical operations such as addition, subtraction etc.
29
Example Program :
// Working of arithmetic operators
#include <stdio.h>
void main( )
{
int val1 = 10,val2 = 5; // input variables
int add,sub,mul,div,rem,inc,dec; //result variables
inc = ++val1;
printf("Increment is %d \n",inc);
dec = --val2;
printf("Decrement is %d \n",dec);
}
Output: Addition = 15
Subtraction = 5
Multiplication = 50
Division = 2
Remainder = 0
Increment is 11
Decrement is 4
30
Relational Operators
Example Program :
// Working of Relational operators
#include <stdio.h>
void main( )
{
int a = 100, b = 50; // input variables
if(a>b)
{
printf("Using > Symbol: ");
printf("A(%d) is Bigger\n",a);
}
if(a>=10)
{
printf("Using >= Symbol: ");
printf("A(%d) is Greater than or Equalt to 100\n",a);
}
if(b<a)
{
printf("Using < Symbol: ");
31
printf("B(%d) is SMALLER\n",b);
}
if(b<=50)
{
printf("Using <= Symbol: ");
printf("B(%d) is Less than or Equalt to 50\n",b);
}
if(a==100)
{
printf("Using == Symbol: ");
printf("A(%d) is Equal to 100\n",a);
}
if(b!=100)
{
printf("Using != Symbol: ");
printf("B(%d) is NOT Equal to 100\n",b);
}
}
Output
Logical Operators
Logical operators are used to determine the logic between variables or values:
Logical Operators are used to combining two or more conditions/constraints or to
complement the evaluation of the original condition in consideration.
The result of the operation of a logical operator is a Boolean value either true or false.
32
Example Program :
// Pgm for validating entered data. Application of logical operator
#include <stdio.h>
void main()
{
int age, mark;
printf ("Enter your age: ");
scanf ("%d", &age);
printf ("Enter your mark: ");
scanf ("%d", &mark);
33
Output:
Enter your age: -56
Enter your mark: 234
The entered Mark is invalid: it is out of range
The entered Age is InValid
Bitwise Operators
The bitwise operators are the operators used to perform the operations on the data at
the bit-level.
These operators work at a low level, directly manipulating the binary representation
of data.
It consists of two numbers, either 0 or 1.
To speed up calculations, it is mostly used in numerical computations.
We have different types of bitwise operators in the C programming language.
The following is the list of the bitwise operators:
34
AND : This operator results in 1 when the values of both the bits are 1.
OR : This operator results in 1 when the any one of the bits is 1.
XOR : This operator results in 0 when the both bits are same.
1 when both bits are unique
NOT : The calculation is done with the help of expression – (n+1).
L Shift : It is used to shift all the bits to the left by a specified number of bits.
R Shift : It is used to shift all the bits to the right by a specified number of bits.
35
Output:
Result of Bitwise AND Operator is: 1
Result of Bitwise OR Operator is: 7
Result of Bitwise XOR Operator is: 6
Result of Bitwise NOT Operator is: -6
Result of Bitwise Lshift Operator is: 20
Result of Bitwise Rshift Operator is: 0
Assignment Operators
It is the operator used to assign the right side operand or variable to the left side
variable.
Equals (=) operator is the most commonly used assignment operator.
For example: int i = 10;
• the value 10 is assigned in to varibale i.
The below table displays all the assignment operators present in C Programming with
an example.
36
= x = 25 Value 25 is assigned to x
Example Program
printf("a=%d,b=%d ",a,b);
c = a;
printf("Assignment of b=a is %d \n",c);
printf("a=%d,b=%d ",a,b);
a+=b; // a=a+b;
printf("(a=a+b)Value of a now = %d\n", a);
printf("a=%d,b=%d ",a,b);
a-=b; // a=a-b;
printf("(a=a-b)Value of a now = %d\n", a);
printf("a=%d,b=%d ",a,b);
a*=b; // a=a*b;
printf("(a=a*b)Value of a now = %d\n", a);
37
printf("a=%d,b=%d ",a,b);
a/=b; // a=a/b;
printf("(a=a/b)Value of a now = %d\n", a);
printf("a=%d,b=%d ",a,b);
a%=b; // a=a%b;
printf("(a=a%%b)Value of a now = %d\n", a);
}
Output:
a=10,b=20 Assignment of b=a is 10
a=10,b=20 (a=a+b)Value of a now = 30
a=30,b=20 (a=a-b)Value of a now = 10
a=10,b=20 (a=a*b)Value of a now = 200
a=200,b=20 (a=a/b)Value of a now = 10
a=10,b=20 (a=a%b)Value of a now = 10
Special Operators
Special operators in C programming are operators that are used for unique purposes
and are not usually utilized in standard programming jobs.
Here are a few examples of C special operators:
operator Meaning
38
Example Program:
// Pgm for demonstrating size of operator
#include <stdio.h>
void main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
}
Output:
Size of int=4 bytes
Size of float=4 bytes
Size of double=8 bytes
Size of char=1 byte
39
Unary Operators
1.Unary Minus
The minus operator ( – ) changes the sign of its argument.
A positive number becomes negative, and a negative number becomes positive.
int a = 10;
int b = -a; // b = -10
Unary minus is different from the subtraction operator, as subtraction requires two
operands.
2.Increment
The increment operator ( ++ ) is used to increment the value of the variable by 1.
The increment can be done in two ways:
• Prefix increment
• Postfix increment
prefix increment
In this method, the operator precedes the operand (e.g., ++a).
The value of the operand will be altered before it is used.
40
Example:
int a = 1;
int b = ++a; // b = 2
postfix increment
In this method, the operator follows the operand (e.g., a++).
The value operand will be altered after it is used.
Example:
int a = 1;
int b = a++; // b = 1
int c = a; // c = 2
3.Decrement
The decrement operator ( — ) is used to decrement the value of the variable by 1.
The decrement can be done in two ways:
• Prefix decrement
• Postfix decrement
prefix decrement
In this method, the operator precedes the operand (e.g., – -a).
The value of the operand will be altered before it is used.
Example:
int a = 1;
int b = --a; // b = 0
postfix decrement
In this method, the operator follows the operand (e.g., a- -). The value of the operand
will be altered after it is used.
41
Example:
int a = 1;
int b = a--; // b = 1
int c = a; // c = 0
Example Program:
// Pgm for demonstrating unary operator
#include <stdio.h>
void main()
{
int a = 10,b,c;
//unary minus
b = -a;
printf("b value is: %d\n",b);
//increment
b=++a;
printf("a value after increment: %d\n",b);
//decrement
c=--a;
printf("a value after deccrement: %d\n",c);
}
Output:
b value is: -10
a value after increment: 11
a value after deccrement: 10
42
Binary Operators
Ternary Operators
Output: a is even
43
Operator Precedence
The precedence of operators in C dictates the order in which the operators will be
evolved in an expression.
Operator precedence determines the grouping of terms in an expression and decides
how an expression is evaluated.
Certain operators have higher precedence than others;
for example, the multiplication operator has a higher precedence than the addition
operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a
higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with
the lowest appear at the bottom.
Within an expression, higher precedence operators will be evaluated first.
II. When two or more operators of the same precedence level appear, then
associativity rule applied, i.e, evaluation happens from left to right.
c = a * b * d; //a * b is first evaluated
The priority is listed below
Priority Operator
1 ( ),[ ]
2 ++, --, -
3 *, / , %, + , -
4 << , >>
5 =
44
Topic : 12 Expression
Evaluation Of Expressions
Expressions are evaluated using an assignment statement of the form:
variable = expression;
Types of Expression
Depending upon the position of operand, expression are classified as
1. Infix Expression
2. Prefix Expression
3. Postfix Expression
45
Infix
When the operator lies between the operands then the expression is known as an Infix
expression. Below is the representation:
c=a+b
Here + operator is present inbetween the two operands a and b
Prefix
When the operator lies before the operands then the expression is known as a Prefix
expression. Below is the representation:
c = +ab
Here + operator is present before the two operands a and b
Postfix
When the operator lies after the operands then the expression is known as a Postfix
expression. Below is the representation:
c = ab+
Here + operator is present after the two operands a and b
46
int i;
10 = i; //invalid
This is because i has an address in memory and is a lvalue.
While 10 doesn't have an identifiable memory location and hence is an rvalue.
So assigning the value of i to 10 doesn't make any sense.