Unit 1.1 C Notes by Prof. Shahid Masood
Unit 1.1 C Notes by Prof. Shahid Masood
CABSMJ-1001
Problem Solving Using C
Unit - I
CABSMJ1001 [2024-25]
Text Book
Concept of Algorithm
and
Flowchart
Algorithm toCABSMJ1001 [2024-25]
add two numbers
1. write "Enter two numbers"
Algorithm 2. read a, b
3. sum a + b
4. write "Sum = ",sum
is a finite sequence of instructions 5. end
for solving a problem or accomplishing a task.
It transforms input into desired output.
Examples:
1. Algorithm to add two numbers
2. Algorithm to find sum and average of n numbers
3. Algorithm to check whether a given integer is odd or even.
Algorithm:
is independent of any prog. language/machine/compiler
can be translated into a computer program using any
programming language
can be implemented on any machine.
CABSMJ1001 [2024-25]
Characteristics of Algorithm
1. Definiteness
2. Effectiveness
3. Finiteness
4. Input
5. Output
CABSMJ1001 [2024-25]
1. Definiteness
clear and accurate
1. add 10 or 20 to total
X
2. divide 10 by 0
2. Effectiveness
3. Finiteness
4. Input
5. Output
Algorithm
Can be expressed in 3 ways:
2. Pseudocode
is a structured way of describing algorithms – using a
combination of:
programming language constructs and
informal English sentences.
Algorithm Note:
1. write "Enter three numbers" 1. Each step has a
2. read a, b, c sequence number.
Comments
3. compute sum and average 2. Comments can be
4. sum a + b + c included
5. avg sum/3.0 compute average 3. Operations:
6. output result +, -, *, /, ^, %
7. write "Sum=",sum, " Average=",avg
<, >, =, <=, >=, !=
8. end
and, or, not
4. No particular agreement
on the syntax used
CABSMJ1001 [2024-25]
Algorithm Output
1. sum 0 Enter number-1
2. count 1 10
3. if count <= 5 then Enter number-2
4. write "Enter number-",count 20
5. read n Enter number-3
6. sum sum + n 30
7. count count + 1 Enter number-4
8. go to 3 40
9. end if Enter number-5
10. write "Sum of 5 numbers = ",sum 50
11. end Sum of 5 numbers = 150
CABSMJ1001 [2024-25]
3. Flowchart
is a graphical (diagrammatical)
representation of an algorithm.
Input or Output
Assignment or Processing
F T Decision
Cond?
Direction of flow
CABSMJ1001 [2024-25]
Document or Report
comment
Dotted line should extend to the symbol it
references
CABSMJ1001 [2024-25]
How to
use
on-page
connector
Advantages of Flowchart
4. Helpful in debugging
start
stop
CABSMJ1001 [2024-25]
start
n=1 Output
1
F 2
is n<=10 ?
3
4
T
5
write n
6
7
n=n+1 8
9
10
stop
CABSMJ1001 [2024-25]
stop
CABSMJ1001 [2024-25]
Computer Program,
System S/w and
Application S/w ?
CABSMJ1001 [2024-25]
System Software
It is a set of programs that is designed:
to control & manage the operations of a computer's
hardware, and
to provide a platform for running other softwares.
Application Software
It is a set of programs used to accomplish specific tasks for
which they are designed for.
It simplifies an operation and helps users get their tasks done
effortlessly.
Examples:
Word-processing software (create and print documents)
Spreadsheet software (numeric data-analysis tool)
Real-time online communication s/w (Skype, Google Meet, Zoom)
Database s/w (manage a collection of related data, e.g. MS Access, Oracle)
Graphics s/w (create drawings, edit pictures e.g. MS Paint, Photoshop)
Multimedia s/w (like VLC Media player, Windows Media Player etc).
Programming
Languages
CABSMJ1001 [2024-25]
Programming Language
A programming language:
is a computer language used to write programs to be
executed by the computer to perform a specified task.
has a set of rules (syntax) and structure for writing the
code (program instructions) in that prog. language.
1. Machine Language
Machine Language
It is extremely difficult for programmers to
understand machine language instructions.
Example of machine language instructions:
To add the two numbers stored at memory
locations 1471 and 2041, and storing the result at 3456:
Machine language instructions
( opcode + operand ) Operation to be performed
Clear & store into Accumulator
001000000000001100111001 register - the number stored at 1471
Add to the contents of Accumulator
001100000000010000100001 - the number stored at 2041
Store the contents of Accumulator
011000000000011100101110 - at memory location 3456
CABSMJ1001 [2024-25]
Symbolic name Memory location
2. Assembly Language FRST 0000001100111001
SCND 0000010000100001
RSLT 0000011100101110
Since machine lang. uses binary numbers which are extremely
difficult for humans to decipher, hence assembly language is
developed to make programming a bit easier for humans.
In assembly language programming:
Mnemonic codes are used to specify opcodes (operations
to be performed). Opcodes usually consist of three letters
e.g. ADD, SUB, MUL, DIV, MOV, END etc.
Symbolic names are used to specify operands (memory
locations) e.g. FRST, SCND, RSLT, A, B, C etc.
Mnemonic codes & symbolic names are easier to remember &
use in writing programs.
It is machine dependent, so precise knowledge of architecture
of the computer is required to write the programs.
CABSMJ1001 [2024-25]
Assembly Language
Example:
Assembly language instructions to add two
numbers FRST and SCND stored in the memory
and store the result in RSLT.
Assembly
language Operation Equivalent machine code
instruction
Clear & store FRST into
CLA FRST Accumulator 001000000000001100111001
Add SCND to the
ADD SCND contents of Accumulator 001100000000010000100001
Store the contents of
STA RSLT Accumulator in RSLT 011000000000011100101110
Assembler
3. High-Level Languages
are designed to overcome the following limitations of low-
level prog. languages:
machine dependent
knowledge of hardware of the machine is required
difficult to program.
High-Level Languages
HLLs like FORTRAN, COBOL, C, C++, C#, Java etc. use compilers.
CABSMJ1001 [2024-25]
Interpreter
Working of
Interpreter
An interpreter:
reads a statement from the source code, converts it into
machine code and executes it,
then it takes the next statement in sequence and repeats
the above process until an error is encountered or all the
statements are executed.
If an error occurs, the interpreter stops execution and reports
the error.
Overview
Of C
Programming
language
CABSMJ1001 [2024-25]
broadly applicable across a
C Programming Language variety of application domains
Important Features of C
Feature Description
Structured C language allows to break a large code
programming into different parts using functions and it
language allows the use of control statements like if-
else, switch-case, while, do-while, for loop.
Above features make the program easier to
understand and also make debugging,
testing and maintenance of the program easier.
Free-form language C programming language allows:
to begin writing the statements
anywhere on a line.
to have any number of blank spaces
between symbols.
We also don't need to start a statement on
a new line.
CABSMJ1001 [2024-25]
C Program
A C program is a sequence of instructions:
written using the syntax of C prog. language
to perform a specified task by the computer.
Saving and
Executing
a C program
CABSMJ1001 [2024-25]
Overview of
Compilation & Linking
Process
and
Execution Process of C
Program
CABSMJ1001 [2024-25]
C Compilation System:
converts the source code into executable code
that can be directly executed by the system.
C Compilation System:
consists of 4 components: a preprocessor, a compiler, an
assembler, a linker and
performs following 4 steps (to convert C source code into
executable code):
1. Preprocessing,
2. Compiling,
3. Assembling and
4. Linking.
CABSMJ1001 [2024-25]
2. Compiler
Compiler:
translates the expanded code (preprocessed source code)
into assembly language code.
3. Assembler
It translates the assembly language code into object code
(machine language code) and generates an object code file.
Name of the object code file is similar to that of the source
file, i.e. if source file name is "hello.c":
then object code file would be "hello.obj" in Windows and
"hello.o" in Unix.
CABSMJ1001 [2024-25]
Executable Executed
Execution Process of code Loader
by the CPU
a C Program hello.exe
Loader ?
Loader is a system program (component of OS) which loads
the executable file from the disk into the primary memory
(RAM) for execution.
CABSMJ1001 [2024-25]
C Character Set
and
C Tokens
CABSMJ1001 [2024-25]
C Character Set
The C character set contains all the characters that we can use
for the source program text.
CABSMJ1001 [2024-25]
C Character Set
Following characters can be used to form words, numbers and
expressions in C programs:
C Tokens
In a C program, an atomic unit (smallest indivisible unit) is
known as a C token.
They are the most basic elements in a program recognized by
the C compiler. It may be a single character or a group of
characters.
The compiler cannot breakdown the token any further.
Examples: main
{
(
float
int
sum
"Computer Science"
CABSMJ1001 [2024-25]
Types of C Tokens
C programs are written using C tokens and the syntax of the
language.
C has six types of tokens:
Types of C tokens Examples
1. Keywords float, int, double, while, for, switch, case
2. Identifiers rollno, name, age, amount, sum, total
3. Constants 3.14, 2024
4. Strings "New Delhi", "Monday"
5. Special symbols [, ], {, }, (, )
6. Operators +, -, *, /, %
CABSMJ1001 [2024-25]
1. C Keywords
C Keywords are predefined tokens, also called reserved words.
They have special meaning to the C compiler.
They can be used for their intended action only; can’t be used
for any other purpose.
There are 32 keywords in C:
auto double int struct break
else long switch case enum
register typedef char extern return
union const float short unsigned
continue for signed void default
goto sizeof volatile do if
static while
CABSMJ1001 [2024-25]
2. Identifiers
refer to the names given to entities, such as variables, pointers
arrays, structures, unions, functions etc.
are user-defined names consisting of a sequence of letters
(lowercase & uppercase), digits and underscore ( _ ) character.
Rules for identifiers
Must consist of only letters, digits or underscore.
Must begin with an alphabet or underscore.
Only first 31 characters are significant.
Must not contain a white space. Some valid identifiers are:
It can’t be a keyword. Average, average
Are case sensitive. height1,
total_height
Note: Uppercase & lowercase letters class_strength
are treated as different characters.
CABSMJ1001 [2024-25]
Contd...
CABSMJ1001 [2024-25]
5. Backslash These constants are used in output functions,
character e.g. '\n' stands for newline character.
constants
Each constant represents a single character
(or Escape (although they consist of two characters):
sequences)
'\a' audible alert (beep sound)
'\b' backspace
'\f' form feed
'\n' new line
'\r' carriage return
'\t' horizontal tab
'\v' vertical tab
'\'' apostrophe (single quote)
'\"' double quote
'\\' backslash
'\?' question mark
'\0' null
CABSMJ1001 [2024-25]
Variables in C
Which of the following variable names are valid and which are
invalid?
Variable name Valid/invalid
first_tag Valid
char Invalid
price$ Invalid
group one Invalid
avg_number1 Valid
int_type Valid
(area) Invalid
25th Invalid