CS25C01 Computer Programming: C
Introduction to C: Problem Solving, Problem Analysis Chart,
Developing an Algorithm, Flowchart and Pseudocode,
program structure, Compilation & Execution process,
Interactive and Script mode, Comments, Indentation, Error
messages, Primitive data types, Constants, Variables, Reserved
words, Arithmetic, Relational, Logical, Bitwise, Assignment,
Conditional operators, Input/Output Functions, Built-in
Functions.
Practical: Create Problem Analysis Charts, Flowcharts and
Pseudocode for simple C programs (Minimum three).
R. SUDHA, ASSISTANT PROFESSOR, SRM
TRP ENGINEERING COLLEGE, TRICHY
R. SUDHA, ASSISTANT PROFESSOR, SRM
TRP ENGINEERING COLLEGE, TRICHY
R. SUDHA, ASSISTANT PROFESSOR, SRM
TRP ENGINEERING COLLEGE, TRICHY
Constants
A constants is a quantity that does not change during the
execution of a program. C constants can be divided into
different categories. constant is divided into numeric and
character constants. Integer and real constants lie in
numeric constants while character and string constants
lie into character constant types.
R. SUDHA, ASSISTANT PROFESSOR, SRM
TRP ENGINEERING COLLEGE, TRICHY
Writea progmm to find intemstfor girvenprincipleamount 'p',time T years and rate 'r'%.
R. SUDHA, ASSISTANT PROFESSOR, SRM
TRP ENGINEERING COLLEGE, TRICHY
PROGRAM
Program is the set of instructions which is run by the
computer to perform specific task. The task of developing
program is called programming.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
COMPUTATIONAL PROBLEM SOLVING
Problem solving technique is a set of techniques that helps
in providing logic for solving a problem. Problem solving can
be expressed in the form of
1. Algorithms
2. Flowcharts.
3. Pseudo codes.
4. Programs
R. SUDHA, ASSISTANT PROFESSOR,
SRM
ALGORITHM
Algorithm is defined as a sequence of instructions that
describe a method for solving a problem. In other words it is a
step by step procedure for solving a problem. The steps must
be ordered, unambiguous and finite in number
1.3.1 Properties of Algorithms
Algorithm should be written in simple English
Each and every instruction should be accurate and explicit
Instructions in an algorithm should not be repeated
infinitely.
R. SUDHA, ASSISTANT PROFESSOR, SRM
Algorithm should conTRcP ElNuGdINeEERaINfGtCeOLrLEaGE,fTiRnICHitY e number
of steps.
Should have an end point
Derived results should be obtained only after the algorithm
terminates.
Qualities of a good algorithm
Time – To execute a program if the lesser time is required then it is
the better one.
Memory – To execute a program if the lesser memory is required
then it is the better.
Accuracy – Multiple algorithms may provide suitable or correct
solutions to a given problem, some of these may provide more
accurate results than others, and such algorithms may be suitable.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Algorithm Example:
Write an algorithm to print, “Welcome to python world”
• Step 1: Start
• Step 2: Print “Welcome to python world”
• Step 3: Stop
• Step 1: Start Step 2: get a,b
• Step 3: calculate c=a+b Step 4: Print c
• Step 5: Stop
R. SUDHA, ASSISTANT PROFESSOR,
SRM
FLOW CHART
The solution of any problem in picture form is
called flowchart. It is the one of the most
important technique to depict an
R. SUDHA, ASSISTANT PROFESSOR, SRM
TRP ENGINEERING COLLEGE,
Flow Chart
• Example: Write an algorithm to check whether the person
is eligible to vote? Step 1: Start
• Step 2: Get age
• Step 3: if age >= 18 print “Eligible to vote” Step 4: else print
“Not eligible to vote”
• Step 6: Stop
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Example: Algorithm for addition of two numbers using function
Main function()
• Step 1: Start
• Step 2: Call the function add()
• Step 3: Stop
sub function add()
• Step 1: Function start
• Step 2: Get a, b Values
• Step 3: add c=a+b
• Step 4: Print c Step 5: Return
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Advantages of flowchart:
1. Easy Communication: - Flowcharts are better way
of communicating the logic of a system to all
involved.
2. Effective analysis: - With the help of flowchart, problem
can be analyzed in more effective way.
3. Efficient Coding: - The flowcharts act as a guide or
blueprint during the systems analysis and program
development phase.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
4. Efficient Program Maintenance: - The maintenance of
operating program becomes easy with the help of flowchart.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Advantages of flowchart:
• It helps the programmer to put efforts more efficiently on
that part.
• Documentation: - Program flowcharts serve as a
good program documentation, which is needed for
various purposes.
• Debugging: - The flowchart helps in debugging process.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Disadvantages of flow chart:
• Complex logic: - Sometimes, the program logic is quite
complicated. In that case, flowchart becomes complex
and uncoordinated.
• Alterations and Modifications: - If alterations are required
the flowchart may require re-drawing completely.
• Reproduction: - As the flowchart symbols cannot be typed,
reproduction of flowchart becomes a problem.
• Cost: For large application the time and cost of
flowchart drawing becomes costly.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
PSEUDO CODE
consists of short, readable and English languages used by an
algorithm.
does not include details like variable declaration, subroutines.
It is easier for the programmer or non programmer to
understand the general working of the program, because it is
not based on any programming language.
It gives us the sketch of the program before actual coding.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
PSEUDO CODE
It is not a machine readable
Pseudo code can’t be compiled and executed.
There is no standard syntax for pseudo code.
Guidelines for writing pseudo code
• Write one statement per line
• Capitalize initial keyword
• Indent to hierarchy
• End multiline structure
• Keep statements language independent
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Syntax for if else:
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Advantages:
Pseudo is independent of any language; it can be used by most
programmers.
It is easy to translate pseudo code into a programming
language.
It can be easily modified as compared to flowchart.
Converting a pseudo code to programming language is very
easy as compared with converting a flowchart to
programming language.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Disadvantages
It does not provide visual representation of the program’s
logic.
There are no accepted standards for writing pseudo codes.
It cannot be compiled nor executed.
For a beginner, it is more difficult to follow the logic or write
pseudo code as compared to flowchart.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
COMPARE: ALGORITHM,
FLOWCHART AND PSEUDO CODE
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Compilation Process in C
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Interactive Mode:
• Refers to environments or tools that allow for a more
immediate and step-by-step execution of C code, primarily
for learning, debugging, or rapid prototyping
Script Mode:
•compiled language, meaning source code is translated
into machine-executable code before execution, unlike
scripting languages which are typically interpreted line by
line
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Indentation:
• Indentation in C programming refers to the use of
whitespace (spaces or tabs) at the beginning of lines of code
to visually organize and structure the program.
• While indentation is not mandatory for the C compiler to
understand the code, it is crucial for human readability
and maintainability
• which statements belong to a particular code block, such as
the body of a function, a loop (e.g., for or while), a
conditional statement (e.g., if, elif, else), or a class definition.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Comments
Single-line Comments
Multi-line Comments (using multiple single-line comments)
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Uses of Comments
• Code Explanation:
To clarify complex logic, algorithms, or the purpose of specific
code sections.
• Documentation:
To describe the functionality of functions, classes, modules, and
methods (especially using docstrings).
• Debugging:
To temporarily disable sections of code during testing or
troubleshooting without deleting them.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Uses of Comments
• Readability:
To make code more understandable for yourself and other
developers who might read or maintain it in the future.
• Collaboration:
To facilitate understanding and communication within a
development team.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Error messages
• Also known as exceptions, provide information about issues
encountered during program execution. These messages
are crucial for debugging and identifying problems in code.
• Common Types of Errors:
• SyntaxError: Occurs when the interpreter encounters
invalid syntax (e.g., missing colons and semicolon, unclosed
parentheses)
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Error messages
• Linked Error: Library files missing
• Runtime Errors: These errors occur while the program
is running.
1. Segmentation Fault: Often caused by attempting to
access memory outside of allocated bounds or
dereferencing
a NULL pointer.
2. File Not Found: When a program attempts to open a file
that does not exist.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
3. Division by Zero: Attempting to divide a number by zero.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Variables
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Reserved words
• There are 32 keywords in ANSI C
• DataTypes: int, float, char, double, void, short, long, signed,
u nsigned
• Control
Flow: if, else, switch, case, default, for, while, do, break, contin
ue, goto
• Storage Class Specifiers: auto, extern, register, static
• Qualifiers: const, volatile
• Other: enum, struct, union, sizeof, typedef, return
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Primary Data Types
categorized into five types:
1) Integer type (int)
2) Floating point type (float)
3) Double precision floating point
type (double)
4) Character type (char)
5) void type (void)
The words in parentheses indicate the
keyword used in C to indicate the data
type.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Built-in functions
Type Conversion:
• int(): Converts a value to an integer.
• float(): Converts a value to a floating-point number.
• str(): Converts a value to a string.
• list(), tuple(), dict(), set(): Convert or create
respective collection types.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Built-in functions
• Mathematical Operations:
• abs(): Returns the absolute value of a number.
• sum(): Returns the sum of all items in an iterable.
• min(), max(): Return the minimum and maximum values
from an iterable.
• round(): Rounds a number to a specified number of decimal
places.
• pow(): Returns the result of a number raised to a power.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Built-in functions
Iterables and Iterators:
• len(): Returns the length (number of items) of an object.
• range(): Generates a sequence of numbers.
• enumerate(): Adds a counter to an iterable.
• sorted(): Returns a new sorted list from an iterable.
• reversed(): Returns a reversed iterator.
• all(), any(): Check if all or any elements in an iterable are true.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Arithmetic operators and
expressions
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Arithmetic operators and
expressions
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Input/Output Functions
• printf(): This is a formatted output function used to display
data on the console. It can print various data types (integers,
floats, characters, strings) using format specifiers (e.g., %d
for integer, %f for float, %c for character, %s for string).
• scanf(): This is a formatted input function used to read data
from the standard input (usually the keyboard). It also uses
format specifiers to interpret the input and stores the read
values in specified variables (using the address-of operator
&).
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Built-in functions
• Also known as standard library functions or predefined
• Pre-implemented: already written and compiled, ready for
use.
• Part of the Standard Library: They are organized into
various header files (e.g., <stdio.h>, <math.h>, <string.h>,
<stdlib.h>)
• Perform Common Tasks: range of common operations, such
as:Input/Output: printf(), scanf(), fgets(), fputs()
• Mathematical Operations: sqrt(), pow(), sin(), cos(), abs()
• String Manipulation: strlen(), strcpy(), strcat(), strcmp()
• Memory Management: malloc(), calloc(), free()
• Utility Functions: rand(), exit()
R. SUDHA, ASSISTANT PROFESSOR,
SRM