Unit-I PPS
Unit-I PPS
PPS Unit-I
Unit-I Programming for Problem Solving
Syllabus:
Introduction to Programming: Compilers, compiling and executing a program.
Representation of Algorithm, Flowchart/ Pseudocode with examples, Program design
and structure of C programming. Variables, Data types Operators, expressions and
precedence, Expression evaluation, Storage classes, type conversion. I/O: Simple input
and output with scanf and printf, formatted I/O, Introduction to stdin, stdout and stderr.
Conditional Branching: Branching with if, if-else, nested if-else, else-if ladder,
switchcase, goto.
Notes:
Compilers, Compiling and executing a program:
A compiler is a software that translates source code written in a high-level programming
language (like C,C++ or Java) into a lower-level language (such as machine code) that a
computer's central processing unit (CPU) can directly understand and execute.
Compiling means translating the C program into machine understandable format.
Execution of the program is dependent on the environment where we are working.
Step 1: Start
Step 5: Stop
2. Pseudocode Representation
Pseudocode looks like a simplified programming language — it’s structured and close to
code, but not bound by syntax of any real programming language.
Example:
BEGIN
INPUT A, B
2
PPS Unit-I
SUM ← A + B
END
3. Flowchart Representation
A flowchart uses symbols and arrows to visually represent the flow of control in an
algorithm.
Symbol Meaning
➜
⬛ Arrow Flow direction
Step 1: Start
Step 4: Stop
2. Pseudocode Representation
BEGIN
INPUT A, B
IF A > B THEN
ELSE
ENDIF
END
3. Flowchart Representation
Step Description
6. Compilation and
Compile (check for errors) and run the program.
Execution
Structure of a C Program
1. Documentation Section
2. Link Section
3. Definition Section
1. Documentation Section
Author : ABC
Date : 05-Oct-2025
5
PPS Unit-I
Purpose: To add two numbers entered by user */
2. Link Section
#include <stdio.h>
3. Definition Section
#define PI 3.14159
int main() {
int a, b, sum;
sum = a + b;
return 0;
void display(void) {
}
6
PPS Unit-I
Example Program:
#include <stdio.h>
int main()
printf("Hello World");
return 0;
Output
Hello World
The first component is the Header files in a C program. A header file is a file with
extension .h which contains C function declarations and macro definitions to be shared
between several source files. All lines that start with # are processed by a preprocessor
which is a program invoked by the compiler. In the above example, the preprocessor
copies the preprocessed code of stdio.h to our file. The .h files are called header files in
C.
Some of the C Header files:
The next part of a C program is the main() function. It is the entry point of a C program and
the execution typically begins with the first line of the main(). The empty brackets indicate
that the main doesn't take any parameter. The int that was written before the main
indicates the return type of main(). The value returned by the main indicates the status of
program termination.
The body of the main method in the C program refers to statements that are a part of the
main function. It can be anything like manipulations, searching, sorting, printing, etc. A
7
PPS Unit-I
pair of curly brackets define the body of a function. All functions must start and end with
curly brackets.
The comments are used for the documentation of the code or to add notes in your
program that are ignored by the compiler and are not the part of executable program .
The last part of any C function is the return statement. The return statement refers to the
return values from a function. This return statement and return value depend upon the
return type of the function. The return statement in our program returns the value from
main(). The returned value may be used by an operating system to know the termination
status of your program. The value 0 typically means successful termination.
Variables:
A variable in C is a named piece of memory which is used to store data and access it
whenever required. It allows us to use the memory without having to memorize the exact
memory address.
To create a variable in C, we have to specify a name and the type of data it is going to
store in the syntax.
data_type name;
C provides different data types that can store almost all kinds of data. For example, int,
char, float, double, etc.
int num;
char letter;
float decimal;
In C, every variable must be declared before it is used. We can also declare multiple
variables of same data type in a single statement by separating them using comma as
shown:
We can assign any name to a C variable as long as it follows the following rules:
C Variable Initialization
Once the variable is declared, we can store useful values in it. The first value we store is
called initial value and the process is called Initialization. It is done using assignment
operator (=).
int num;
num = 3;
int num = 3;
Note: It is compulsory that the values assigned to the variables should be of the same
data type as specified in the declaration.
Accessing Variables
The data stored inside a C variable can be easily accessed by using the variable's name.
Example:
#include <stdio.h>
int main() {
int num = 3;
printf("%d", num);
return 0;
Output
We can also update the value of a variable with a new value whenever needed by using
the assignment operator =.
9
PPS Unit-I
Example:
#include <stdio.h>
int main() {
int n = 3;
n = 22;
printf("%d", n);
return 0;
Output
22
60
When a variable is declared, the compiler is told that the variable with the given name
and type exists in the program. But no memory is allocated to it yet. Memory is allocated
when the variable is defined.
The size of memory assigned for variables depends on the type of variable. We can check
the size of the variables using sizeof operator.
Example:
#include <stdio.h>
int main() {
return 0;
Output
4 bytes
Scope of Variables in C
10
PPS Unit-I
A variable can be accessed using its name anywhere in a specific region of the program
called its scope. It is the region of the program where the name assigned to the variable
is valid.
Example:
int main() {
// Variable declaration
int num;
return 0;
Constants in C
C also provides some variables whose value cannot be changed. These variables are
called constants and are created simply by prefixing const keyword in variable
declaration.
Syntax:
Data Types in C
Each variable in C has an associated data type. It specifies the type of data that the
variable can store like integer, character, floating, double, etc.
Example:
int number;
The above statement declares a variable with name number that can store
integer values.
C is a statically typed language where each variable's type must be specified at the
declaration and once specified, it cannot be changed.
11
PPS Unit-I
The integer datatype in C is used to store the integer numbers (any number including
positive, negative and zero without decimal part). Octal values, hexadecimal values, and
decimal values can also be stored in int data type in C.
Size: 4 bytes
Format Specifier: %d
Format specifiers are the symbols that are used for printing and scanning values of given
data types.
Example:
int val;
#include <stdio.h>
int main() {
return 0;
Output
var = 22
A variable of given data type can only contains the values of the same type. So, var can
only store numbers, not text or anything else.
12
PPS Unit-I
The integer data type can also be used as:
1. unsigned int: It can store the data values from zero to positive numbers, but it
can’t store negative values
2. short int: It is lesser in size than the int by 2 bytes so can only store values from -
32,768 to 32,767.
3. long int: Larger version of the int datatype so can store values greater than int.
4. unsigned short int: Similar in relationship with short int as unsigned int with int.
Note: The size of an integer data type is compiler dependent. We can use sizeof operator
to check the actual size of any data type. Here, we are discussing the sizes according to
c4-bit compilers.
Character data type allows its variable to store only a single character. The size of the
character is 1 byte. It is the most basic data type in C. It stores a single character and
requires a single byte of memory in almost all compilers.
Size: 1 byte
Format Specifier: %c
Example:
#include <stdio.h>
int main() {
char ch = 'A';
return 0;
Output
ch = A
In C programming, float data type is used to store single precision floating-point values.
These values are decimal and exponential numbers.
Size: 4 bytes
Format Specifier: %f
13
PPS Unit-I
Example:
#include <stdio.h>
int main() {
return 0;
Output
val = 12.450000
The double data type in C is used to store decimal numbers (numbers with floating point
values) with double precision. It can easily accommodate about 16 to 17 digits after or
before a decimal point.
Size: 8 bytes
Example:
#include <stdio.h>
int main() {
return 0;
Output
val = 1.452100
The void data type in C is used to indicate the absence of a value. Variables of void data
type are not allowed. It can only be used for pointers and function return type and
parameters.
Example:
14
PPS Unit-I
void fun(int a, int b){
// function body
where function fun is a void type of function means it doesn't return any value.
The size of the data types in C is dependent on the size of the architecture, so we cannot
define the universal size of the data types. For that, the C language provides the
sizeof() operator to check the size of the data types.
Example
#include <stdio.h>
int main(){
return 0;
Output
Different data types also have different ranges up to which can vary from compiler to
compiler. Below is a list of ranges along with the memory requirement and format
specifiers on the 64-bit GCC compiler.
Format
Data Type Size (bytes) Range Specifier
-2,147,483,648 to
4 %d
int 2,147,483,647
-2,147,483,648 to
4 %ld
long int 2,147,483,647
Note: The long, short, signed and unsigned are datatype modifier that can be used
with some primitive data types to change the size or length of the datatype.
Literals in C
16
PPS Unit-I
In C, literals are constant values assigned to variables. They represent fixed values that
cannot be changed. Literals occupy memory but do not have references like variables.
Often, the terms constants and literals are used interchangeably.
Operators in C
1. Arithmetic Operators
+ Addition a+b 15
- Subtraction a-b 5
* Multiplication a*b 50
% Modulus (remainder) a % b 0
++ Increment by 1 a++ 11
-- Decrement by 1 b-- 4
2. Relational Operators
== Equal to a == b 0
!= Not equal to a != b 1
3. Logical Operators
CC Logical AND a CC b 0
|| Logical Or A || b 1
! Logical NOT !a 0
4. Bitwise Operators
~ Bitwise NOT ~a -7
5. Assignment Operators
= a=5 Assign 5
+= a += 5 a=a+5
-= a -= 5 a=a-5
*= a *= 5 a=a*5
/= a /= 5 a=a/5
%= a %= 5 a=a%5
7. Special Operators
Operator Description
Precedence (Highest
Operators Associativity
→ Lowest)
3 *, /, % Left to Right
4 +, - Left to Right
14 =, +=, -=, *=, /=, %= , <<=, >>=, C=, ^=, ` Right to Left
Expression Evaluation:
Example 1: Arithmetic Expression
int a = 10, b = 5, c = 2;
int result = a + b * c;
printf("%d", result);
Evaluation:
= 10 + 5 * 2
= 10 + 10
= 20
Output: 20
int result = (a + b) * c;
Evaluation:
= (10 + 5) * 2
= 15 * 2
= 30
Output: 30
int x = 4, y = 3, z = 2;
int result = x + y * z / x;
Evaluation Steps:
1. y * z = 3 * 2 = 6
20
PPS Unit-I
2. 6 / x = 6 / 4 = 1 (integer division)
3. x + 1 = 4 + 1 = 5
Output: 5
printf("%d", result);
Evaluation:
(a < b) → 1 (true)
(b < c) → 1 (true)
1 CC 1 → 1
Output: 1
Evaluation:
(a > b) → false
So, max = b = 40
Output: Max = 40
Example 6: Increment/Decrement
int x = 5;
Evaluation:
3. y = 6 + 6 = 12
Final: x = 7, y = 12
* Example of Associativity
˛
C
21
PPS Unit-I
int a = 5, b = 10, c = 15;
Left to Right:
a<b→1
1 < c → 1 (true)
Output: 1
Example : Assignment Operators
int a, b, c;
a = b = c = 10;
Evaluation:
a = (b = (c = 10))
So,
c = 10
b = 10
a = 10
Storage classes:
A storage class in C defines the scope, lifetime, and visibility of a variable or function
that is, where it can be accessed and how long it exists in memory.
Storage Memory
Keyword Default Value Scope Lifetime
Class Location
Till program
External extern Data Segment 0 Global
ends
22
PPS Unit-I
#include <stdio.h>
void test() {
int main() {
test();
return 0;
Output:
a = 10
You can omit auto since it’s the default for local variables.
Suggests that the variable be stored in a CPU register instead of RAM (for faster
access).
#include <stdio.h>
int main() {
register int i;
return 0;
Output:
12345
23
PPS Unit-I
The compiler may ignore register if no registers are available.
#include <stdio.h>
void counter() {
count++;
int main() {
counter();
counter();
counter();
return 0;
Output:
Count = 1
Count = 2
Count = 3
Declares a global variable defined in another file or later in the same file.
24
PPS Unit-I
Used to share variables between multiple files.
File 1: main.c
#include <stdio.h>
int main() {
return 0;
File 2: data.c
x = 50
Default Value 0 0
Type conversion:
Typecasting in C is the process of converting a variable or value from one data type to
another. This can be done either implicitly (automatically by the compiler) or explicitly
(manually by the programmer).
This occurs automatically when the compiler converts a data type to another compatible
data type without any explicit instruction from the programmer. This usually happens
during assignments or when performing operations involving different data types. For
example, assigning an int to a float will implicitly convert the int to float.
#include <stdio.h>
int main() {
int num_int = 10;
25
PPS Unit-I
float num_float;
num_float = num_int; // Implicit conversion from int to ffoat
printf("Implicit conversion: %f\n", num_float);
return 0;
}
#include <stdio.h>
int main() {
int numerator = 15;
int denominator = 2;
float result;
return 0;
}
Key Points:
Data Loss: Explicitly casting a higher-precision data type (like float or double) to
a lower-precision one (like int) can lead to data loss (e.g., truncation of decimal
places).
Input s Output in C
Simple input and output with scanf and printf and formatted I/O:
printf() → Used to display (output) text or values on the screen.
#include <stdio.h>
26
PPS Unit-I
int main() {
int num;
return 0;
Here:
#include <stdio.h>
int main() {
int a, b;
return 0;
#include <stdio.h>
int main() {
int age;
float height;
char grade;
int %d
float %f
double %lf
char %c
Format Specifiers in C
scanf("%d", Ca); → 10
int (decimal) %d
printf("%d", a); → 10
scanf("%c", Cch); → A
char %c
printf("%c", ch); → A
◆ Notes:
For scanf(), always use s before variable name (except for strings).
Example:
#include <stdio.h>
int main() {
int age;
float marks;
char grade;
char name[20];
return 0;
Syntax: %[flags][width][.precision][length]specifier
Flags:
- left-justify
0 zero-pad
Examples:
#include <stdio.h>
int main() {
int age;
return 0;
Explanation:
When you type a value, it’s read from the standard input stream → stdin.
Example:
#include <stdio.h>
int main() {
return 0;
Explanation:
By default, it is also connected to the screen, but it’s a separate stream from
stdout.
This separation helps distinguish normal output from error output (useful in
debugging).
#include <stdio.h>
int main() {
if (file == NULL) {
return 1;
fclose(file);
return 0;
⬛ Explanation:
So even if normal output is redirected to a file, the error message still appears on
the screen.
31
PPS Unit-I
Conditional Branching:
or
Decision Making in C (if , if..else, Nested if, if-else-if )
In C, programs can choose which part of the code to execute based on some condition.
This ability is called decision making and the statements used for it are called
conditional statements. These statements evaluate one or more conditions and make
the decision whether to execute a block of code or not.
1. if in C
A condition is any expression that evaluates to either a true or false (or values convertible
to true or flase).
Example
#include <stdio.h>
int main() {
int i = 10;
// If statement
32
PPS Unit-I
if (i < 18) {
Output
The expression inside () parenthesis is the condition and set of statements inside {}
braces is its body. If the condition is true, only then the body will be executed.
2. if-else in C
The if statement alone tells us that if a condition is true, it will execute a block of
statements and if the condition is false, it won’t. But what if we want to do something else
when the condition is false? Here comes the C else statement. We can use
the else statement with the if statement to execute a block of code when the condition
is false. The if-else statement consists of two blocks, one for false expression and one for
true expression.
Example
#include <stdio.h>
int main() {
int i = 10;
if (i > 18) {
else {
return 0;
Output
The block of code following the else statement is executed as the condition present in
the if statement is false.
33
PPS Unit-I
3. Nested if-else in C
Example
#include <stdio.h>
int main(){
int i = 10;
if (i == 10) {
if (i < 18)
else
else {
if (i == 20) {
if (i < 22)
else
return 0;
Output
4. if-else-if Ladder in C
The if else if statements are used when the user has to decide among multiple options.
The C if statements are executed from the top down. As soon as one of the conditions
controlling the if is true, the statement associated with that if is executed, and the rest of
34
PPS Unit-I
the C else-if ladder is bypassed. If none of the conditions is true, then the final else
statement will be executed. if-else-if ladder is similar to the switch statement.
Example
#include <stdio.h>
int main() {
int i = 20;
if (i == 10)
printf("Not Eligible");
else if (i == 15)
else if (i == 20)
else
return 0;
Output
5. switch Statement in C
The switch case statement is an alternative to the if else if ladder that can be used to
execute the conditional code based on the value of the variable specified in the switch
statement. The switch block consists of cases to be executed based on the value of the
switch variable.
Example
#include <stdio.h>
int main() {
switch (var) {
35
PPS Unit-I
case 15:
break;
case 18:
break;
default:
break;
return 0;
Output
Note: The switch expression should evaluate to either integer or character. It cannot
evaluate any other data type.
6. Conditional Operator in C
The conditional operator is used to add conditional code in our program. It is similar to
the if-else statement. It is also known as the ternary operator as it works on three
operands.
Example:
#include <stdio.h>
int main() {
// using conditional operator to assign the value to var according to the value of flag
return 0;
Output
These statements are used in C for the unconditional flow of control throughout the
functions in a program. They support four types of jump statements:
A) break
This loop control statement is used to terminate the loop. As soon as the break statement
is encountered within a loop, the loop iterations stop there, and control returns from the
loop immediately to the first statement after the loop.
Example
#include <stdio.h>
int main() {
int arr[] = { 1, 2, 3, 4, 5, 6 };
if (arr[i] == key) {
break;
return 0;
Output
B) continue
This loop control statement is just like the break statement. The continue statement is
opposite to that of the break statement, instead of terminating the loop, it forces to
execute the next iteration of the loop. As the name suggests the continue statement
forces the loop to continue or execute the next iteration. When the continue statement is
executed in the loop, the code inside the loop following the continue statement will be
skipped and the next iteration of the loop will begin.
Example:
#include <stdio.h>
int main() {
37
PPS Unit-I
int i;
if (i == 6)
continue;
else
return 0;
Output
1 2 3 4 5 7 8 9 10
C) goto
The goto statement in C also referred to as the unconditional jump statement can be
used to jump from one point to another within a function.
Examples:
#include <stdio.h>
int main() {
int n = 1;
label:
n++;
if (n <= 10)
goto label;
return 0;
Output
1 2 3 4 5 6 7 8 9 10
D) return
The return in C returns the flow of the execution to the function from where it is called.
This statement does not mandatorily need any conditional statements. As soon as the
38
PPS Unit-I
statement is executed, the flow of the program stops immediately and returns the control
from where it was called. The return statement may or may not return anything for a void
function, but for a non-void function, a return value must be returned.
Example:
#include
<stdio.h> int
sum(int a, int b) {
int s1 = a + b;
return s1;
int main()
return 0;
Output
20