Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views22 pages

Notes CCP U1 N

The document provides an overview of programming environments, digital computers, and their components, including input, storage, processing, and output units. It discusses the role of operating systems as intermediaries between hardware and software, types of operating systems, and the differences between Linux and Windows. Additionally, it covers the history and features of the C programming language, highlighting its significance in computer technology.

Uploaded by

thepsyforge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views22 pages

Notes CCP U1 N

The document provides an overview of programming environments, digital computers, and their components, including input, storage, processing, and output units. It discusses the role of operating systems as intermediaries between hardware and software, types of operating systems, and the differences between Linux and Windows. Additionally, it covers the history and features of the C programming language, highlighting its significance in computer technology.

Uploaded by

thepsyforge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Notes: Programming for problem solving(BCS-101/201)

Unit#01
Introduction to Programming Environment:
o Prog. Envt. is the envt, in which programs are created & tested.
o It is much closer to O.S. than the design of a programming language.
o Prog. Envt. consists of a set of support tools & a command language (instruction Set) for
invoking the tools.
o Each support tool is another program that may be used by programmer as an aid during
>1 stages of program-creation.
Tools example : - Editors, debuggers, verifiers, compilers, test data generators & pretty printers etc.

Introduction to Digital Computer:


Ist electronic digital computer developed in-1940. It was used mainly for numerical
computations.

Digital Computer System

Software Hardware
(instruction, data, Application) (electronic components, electromech. Devices)
Digital Computer:
A computer is an electronic machine like any other machine which takes inputs (raw data) and performs
some processing giving out processed data (information).
A digital computer is a digital system that performs various computational tasks. The word
digital implies  the info in computer is shown by variables that take a limited no. of discrete values.
Then these values are processed internally by components and discrete states are maintained. In this case
the discrete elements are digits. So from this place we got the term- “Digital Computer.”
These machines (m/c) cooperate efficiently if only ‘2’ states are used. Hence- Binary values are
being popular now.
Bit: Digital computers use the binary no. system, which has two digits : 0 & 1.
A binary digit ≡ a ‘bit’.
So now info is represented in digital computers in group of bits.
By this group of bits we can not only represent binary nos. but also other discrete symbols (decimal
digits/letter/alphabet) can be shown with the help of many coding techniques. Furthermore development
of symbols & sets of Instructions (program) are also possible for doing many computations with the
proper arrangement of bits. (Coding methods)
Binary numbers use a base 2 system with 2 digits  0 & 1. Decimal equivalent of Binary no. can be
found by expanding Binary no. into a power series with a base of 2. For example  101 as: 1 x 22 + 0 x
21 + 1 x 20 = 4 + 0 + 1 = 5
Block Diagram Of Digital computer

Anurag Tewari, CS, KIET 1


1. Input Unit: Its task is -of entering data and programs in to the computer system. Therefore, the
input unit takes data from user to the computer in an organized manner for processing.

2. Storage Unit: The process of saving data and instructions permanently is task of storage unit. Data
has to be fed into the system before the actual processing starts. It is because the processing speed of
Central Processing Unit (CPU) is so fast that the data has to be provided to CPU with the same speed.
Therefore the data is first stored in the storage unit for faster access and processing. This storage unit or
the primary storage of the computer system is designed to do the above functionality. It provides space
for storing data and instructions.

The storage unit performs the following major functions:


• All data and instructions are stored here before and after processing.
• Intermediate results of processing are also stored here.

3. Processing Unit: The task of performing operations like arithmetic and logical operations is called
processing. The Central Processing Unit (CPU) takes data and instructions from the storage unit and
makes all sorts of calculations based on the instructions. It(result) is then sent back to the storage unit.

4. Output Unit: Its task is -the process of producing results from the data for getting useful
information. Similarly the output produced by the computer after processing must also be kept
somewhere inside the computer before being given to you in human readable form. Again the output is
also stored inside the computer for further processing or display.

5. Control unit: The manner how instructions are executed and the above operations are performed.
Controlling of all operations like input, processing and output are performed by control unit. It takes
care of step by step processing of all operations inside the computer.

FUNCTIONAL UNITS

In order to carry out the operations mentioned in the previous section the computer allocates the task
between its various functional units. The computer system is divided into three separate units for its
operation. They are

1) Arithmetical logical unit 2) control unit. 3) Central processing unit.

 Arithmetical Logical Unit (ALU):After you enter data through the input device it is stored in
the primary storage unit. The actual processing of the data and instruction are performed by
Arithmetic Logical Unit. The major operations performed by the ALU are addition, subtraction,
multiplication, division, logic and comparison. Data is transferred to ALU from storage unit
when required. After processing the output is returned back to storage unit for further processing
or getting stored.
Control Unit (CU)
 The next component of computer is the Control Unit, which acts like the supervisor seeing that
things are done in proper fashion.
 Control Unit is responsible for coordinating various operations using time signal.
 The control unit determines the sequence in which computer programs and instructions are
executed. Things like processing of programs stored in the main memory, interpretation of the
instructions and issuing of signals for other units of the computer to execute them.
 It also acts as a switch board operator when several users access the computer simultaneously.
Thereby it coordinates the activities of computer’s peripheral equipment as they perform
the input and output.

Central Processing Unit (CPU) The ALU and the CU of a computer system are jointly known as the
central processing unit. You may call CPU as the brain of any computer system.
Anurag Tewari, CS, KIET 2
Computer H/w : It can be divided into 3 major parts
1. CPU
2. Memory (RAM)
3. I/O Processor
CPU has 
o Arithmetical & logical unit for data manipulation.
o No. of registers for data-storage
o Control circuits to fetch & execute Instructions.
Memory  It contains storage for instructions & data. It is the primary memory, called as Random
Access memory (RAM).
CPU can access RAM at random & retrieve the binary information within a fixed interval of
time.
I/O Processor: It has electronic ckts. for communicating & controlling the transfer of
information b/w the computer & outside world.
I/p devices – Key board, mouse.
O/p devices – Monitor, printer.
Basic Hardware Components

Types of Software

Program: A sequence of instructions for the computer is called a program. The data that are
manipulated by the program create the database.

1. System software-It is the software used to manage and control the hardware components and
which allow interaction between the hardware and the other types of software. The most obvious
type of system software is operating system and device drivers.
Utility software-It is software such as anti-virus software, firewalls, disk defragmenters and so
on.
which helps to maintain and protect the computer system but does not directly interface with the
hardware.

Anurag Tewari, CS, KIET 3


2. Applications software (also known as 'apps') are designed to allow the user of the system to
complete a specific task(application). They include programs such as web browsers, MS office,
games, media players and so on.

Memory Hierarchy

Speed Memory Description

Registers are embedded inside the CPU. They have very less capacity but
fastest speed. CPU uses some specific registers like PC (program counter), IR
Fastest Register (instruction register), MAR (memory address register), MDR (memory data
register), Ax(Accumulator register) and some GPR (general purpose
registers).

Cache memory is memory actually embedded inside the CPU. Cache memory
is very fast, typically taking only once cycle to access, but since it is
Faster Cache embedded directly into the CPU there is a limit to how big it can be. In fact,
there are several sub-levels of cache memory (termed L1, L2, L3) all with
slightly increasing speeds.

All instructions and storage addresses for the processor must come from
Slower RAM. Although RAM is very fast, there is still some significant time taken
than RAM for the CPU to access it (this time is latency). RAM is stored in separate,
Cache dedicated chips attached to the motherboard, meaning it is much larger than
cache memory.

We are all familiar with software arriving on a floppy disk or CDROM, and
saving our files to the hard disk. We are also familiar with the long time a
Slowest Disk program can take to load from the hard disk -- having physical mechanisms
such as spinning disks and moving heads means disks are the slowest form of
storage. But they are also by far the largest form of storage.

Anurag Tewari, CS, KIET 4


Operating System
Introduction to Operating System (O.S.)
An O.S. acts as an intermediary between the user of a computer S/W & the computer H/w.
“The purpose of an O.S. is to provide an environment in which a user can execute programs in a
convenient & efficient manner.”
O.S. is a S/w which controls or operates the H/w of computer.
H/w of computer : CPU, memory, I/O devices Etc. (touch & feel)
S/w of computer : Packages for different applications.
: Compliers of different Languages
: Others, Information/data (e-mail, …)
So O.S. is just like a government, it does nothing (important) useful functions by itself. It only provides
an environment within which other programs can do useful work.

An operating system, or "OS," is a system software that communicates with the hardware and allows
other programs to run. It has the fundamental files your computer needs to boot up and function. Every
desktop computer, tablet, and smart phone includes an operating system that provides basic functionality
for the device.
e.g MS DOS, Win 95 ,win 2000,win xp, win 8, Unix, Linux, Android etc.
Important functions of an operating system:
 Memory Management
 Process Management
 Device Management
 File Management
 Security Mgt
 Control over system performance
 Job accounting
 Error detecting aids
 Coordination between other software and users

Anurag Tewari, CS, KIET 5


Types of operating systems:

1. Batch OS:The users of batch operating system do not interact with the computer directly. Each
user prepares his job on an off-line device like punch cards and submits it to the computer
operator. Main problems are - Lack of interaction between the user and job, CPU is often idle.

2. Real-time OS: A real-time operating system is a multitasking operating system that aims at
executing real-time applications. Real-time operating systems often use specialized scheduling
algorithms so that we can perform multiple tasks simultaneously. The main objective of real-time
operating systems is their quick and predictable response to events. They have an event-driven or
time-sharing design. Ex- VxWorks, MARS, RTLinux

3. Multi-user OS: A multi-user operating system allows multiple users to access a computer
system at the same time. Time-sharing systems and Internet servers can be classified as multi-
user systems. Single-user operating systems have only one user but may allow multiple programs
to run at the same time.Ex-UNIX,Main-Frame,Supercomputer.

4. Distributed OS: A distributed operating system manages a group of independent computers and
makes them appear to be a single computer. The development of networked computers that could
be linked and communicate with each other gave rise to distributed computing. Distributed
computations are carried out on more than one machine. When computers in a group work in
cooperation, they make a distributed system.Ex-NFS-Andrew,Amoeba

Difference between Linux and Windows OS:

S. no. Point Linux Windows


1 Open-Source & Proprietary & Licensed
Type
FreeWare:
Linus Torvalds(1991), MS-Win3.1(1992)
Inventor(year)
2
3 Application Less More
4 Cost Freely available Costly
High Security:It never Low security:
5 Security
creates Registry Keys
boot from either primary Must boot from primary
6 Booting
or logical partition. partition.
File names-case File names- not case
7. Case sensitivity
sensitive sensitive
More Reliable: has Less Reliable
8. Reliability
better backup concept
Less Popular More Popular(Due to Its
9. Popularity
better GUI support)

Anurag Tewari, CS, KIET 6


Linux basic commands
1. ls :- This command is used to list all the files in the current working
directory . Eg :- $ls
2. rm :- This command is used to remove the file from the directory.
Eg :- $rm filename
3. rmdir :- This command is used remove directory . But it should not contain
any file.Then only we can remove the directory
Anurag Tewari, CS, KIET 7
eg :-$rmdir directory name
4. cat :- This command is used to display the content of file.
Eg :- $cat>filename (i. e.,)used to write the content in to the file.
eg :- $cat file .(i.e.,) used to display the content in the file
5. CD :- This command is used to change the current working directory.
Eg :- $cd
6. mv :- This command is also called rename command.it is used to move the
content from the source file to the desigination file.
Eg :- $mv sourcefile desgination file
7. who :- This command is used to display the users who are currently logged
on. Eg :- $who
Programming language
A programming language is a vocabulary and set of grammatical rules for instructing a
computer or computing device to perform specific tasks. The term programming
language usually refers to high-level languages, such as BASIC, C, C++, COBOL, Java,
FORTRAN, Ada, and Pascal.

A Brief History of C:
 C is a general-purpose language which has been closely associated with the UNIX operating
system for which it was developed - since the system and most of the programs that run it are
written in C.
 Many of the important ideas of C originated from the language BCPL, developed by Martin
Richards. The influence of BCPL on C proceeded indirectly through the language B, which was

Anurag Tewari, CS, KIET 8


written by Ken Thompson in 1970 at Bell Labs, for the first UNIX system on a DEC PDP-
7. BCPL and B are "type less" languages whereas C provides a variety of data types.
 In 1972 Dennis Ritchie at Bell Labs written C and in 1978 the publication of The C
Programming Language by Kernighan & Ritchie caused a revolution in the computing world.
 In 1983, the American National Standards Institute (ANSI) established a committee to provide a
modern, comprehensive definition of C. The resulting definition, the ANSI standard, or "ANSI
C", was completed late 1988.

Features of C Programming Language :


C Programming is widely used in Computer Technology, We can say that C is inspiration for
development of other languages. We can use C for different purposes. Below are some of the
Features of C Programming –
1 . Low Level Features :
C Programming provides low level features that are generally provided by the Lower level
languages. C is Closely Related to Lower level Language such as “Assembly Language“.
It is easier to write assembly language codes in C programming.
2 . Portability :
C Programs are portable i.e they can be run on any Compiler with Little or no Modification.
Compiler and Preprocessor make it Possible for C Program to run it on Different PC.
3 . Powerful
Provides Wide variety of ‘Data Types’ and limited no. of keywords.
Provides Wide variety of ‘Functions’
Provides useful Control & Loop Control Statements
4 . Bit Manipulation
C Programs can be manipulated using bits. We can perform different operations at bit level. We
can manage memory representation at bit level. [Eg. We can use Structure to manage Memory at
Bit Level].It provides wide verity of bit manipulation Operators. We have bitwise operators to
manage Data at bit level.
5 . High Level Features :
It is more User friendly as compare to Previous languages. Previous languages such as
BCPL,Pascal and other programming languages never provide such great features to manage
data.
Previous languages have their pros and cons but C Programming collected all useful features of
previous languages thus C become more effective language.
6 . Modular Programming:
Modular programming is a software design technique that increases the extent to which software
is composed of separate parts, called modules.
C Program Consist of Different Modules that are integrated together to form complete program

Anurag Tewari, CS, KIET 9


7 . Efficient Use of Pointers:
Pointers has direct access to memory.
C Supports efficient use of pointer .
8 . More Efficient & Fast:
During execution it gives output in a correct and timely way.
Application of C Programming: Application of C Programming are listed below –
1. C language is used for creating computer applications.
2. Used in writing Embedded software.
3. Firmware for various electronics, industrial and communications products which use micro-
controllers.
4. It is also used in developing verification software, test code, simulators etc. for various
applications and hardware products.
5. For Creating Compilers of different Languages which can take input from other language and
convert it into lower level machine dependent language.
6. C is used to implement different Operating System Operations.
7. UNIX kernel is completely developed in C Language.
Different parts of C program.
1. Pre-processor
2. Header file
3. Function
4. Variables
5. Expressions
6. Comments
All these are essential parts of a C language program.

Programming Languages classification-chart


Examples
Algorithmic--- Fortran, COBOL,C
Procedural Object-oriented--C++, JAVA,C#
Scripting ------ VB, PERL, JavaScript
Functional ---- LISP, ML
High Level non-procedural
Logical ------- PROLOG
Numerical ----- MatLab
Problem-oriented Symbolic ------ Mathematica
Publishing ---- Latex

Programming Langs

Low level Assembly Language (Machine dependent)

C Program: Generally three basic phases occurs when we execute any C prog:
• Preprocessing
• Assembling & Compiling

Anurag Tewari, CS, KIET 10


• Linking & Loading

Compile, link and execute stages of hello.c in OS(using GCC)

Assembler: A computer will not understand any program written in a language, other than its machine
language. The programs written in other languages must be translated into the machine language. Such
translation is performed with the help of software. A program which translates an assembly language
program into a machine language program is called an assembler. If an assembler which runs on a
computer and produces the machine codes for the same computer then it is called self assembler or
resident assembler. If an assembler that runs on a computer and produces the machine codes for other
computer then it is called Cross Assembler.
Assemblers are further divided into two types: One Pass Assembler and Two Pass
Assembler. One pass assembler is the assembler which assigns the memory addresses to the variables
and translates the source code into machine code in the first pass simultaneously. A Two Pass
Assembler is the assembler which reads the source code twice. In the first pass, it reads all the variables

Anurag Tewari, CS, KIET 11


and assigns them memory addresses. In the second pass, it reads the source code and translates the code
into object code.
Compiler: It is a program which translates a high level language program into a machine language
program. A compiler is more intelligent than an assembler. It checks all kinds of limits, ranges, errors
etc. But its program run time is more and occupies a larger part of the memory. It has slow speed.
Because a compiler goes through the entire program and then translates the entire program into machine
codes. If a compiler runs on a computer and produces the machine codes for the same computer then it is
known as a self compiler or resident compiler. On the other hand, if a compiler runs on a computer and
produces the machine codes for other computer then it is known as a cross compiler.
Interpreter: An interpreter is a program which translates statements of a program into machine code. It
translates only one statement of the program at a time. It reads only one statement of program, translates
it and executes it. Then it reads the next statement of the program again translates it and executes it. In
this way it proceeds further till all the statements are translated and executed. On the other hand, a
compiler goes through the entire program and then translates the entire program into machine codes. A
compiler is 5 to 25 times faster than an interpreter.
By the compiler, the machine codes are saved permanently for future reference. On the
other hand, the machine codes produced by interpreter are not saved. An interpreter is a small program
as compared to compiler. It occupies less memory space, so it can be used in a smaller system which has
limited memory space.
Linker: In high level languages, some built in header files or libraries are predefined and these contain
basic functions which are essential for executing the program. Predefined(system defined) functions are
linked to the libraries by a program called Linker. If linker does not find a library of a function then it
informs to compiler and then compiler generates an error. The compiler automatically invokes the linker
as the last step in compiling a program.
Not built in libraries, it also links the user defined functions to the user defined libraries.
Usually a longer program is divided into smaller subprograms called modules. And these modules must
be combined to execute the program. The process of combining the modules is done by the linker.
Loader: Loader is a program that loads machine codes of a program into the system memory (RAM)
from Hard Disk. In Computing, a loader is the part of an Operating System that is responsible for
loading programs. It is one of the essential stages in the process of starting a program. Because it places
programs into memory and prepares them for execution. Loading a program involves reading the
contents of executable file into memory. Once loading is complete, the operating system starts the
program by passing control to the loaded program code. All operating systems that support program
loading have loaders. In many operating systems the loader is permanently resident in memory.

Difference between Compiler and Interpreter:

Interpreter Compiler
Scans the entire program and translates it as a whole
Translates program one statement at a time.
into machine code.
It takes large amount of time to analyze the source
It takes less amount of time to analyze the source
code but the overall execution time is comparatively
code but the overall execution time is slower.
faster.
No intermediate object code is generated, hence Generates intermediate object code which further
are memory efficient. requires linking, hence requires more memory.
Continues translating the program until the first It generates the error message only after scanning
error is met, in which case it stops. Hence the whole program. Hence debugging is
debugging is easy. comparatively hard.
Ex-Programming language like Python, Ruby use Ex-Programming language like C, C++ use
interpreters. compilers.

Anurag Tewari, CS, KIET 12


The First – C Program
// this is the first program in c
/* in this first program in c we will print hello world on
the monitor screen(console) as output */
#include <stdio.h>
int main( )
{
printf(“hello world\n”);
}
Symbol: ‘//’ this is used to write comment(single line)
‘/* */’ this is used to write comments(multiple lines)
# : Preprocessor Directive
include – It is a keyword which tells the compiler to include information about the standard i/p – o/p,
files.
<stdio.h> - It is a header file which contains all (functions, keywords,
library function) information’s related to standard i/p – (keyboard, mouse,
joystick…) & std. o/p – (monitors, printers…)
main()– It is a function defined by system, from this pt.(where main is written) Program starts
executing.
Every program must have a main somewhere.
main(), will usually call other functions to help perform its job, either
user defined functions or system defined functions (library functions).
{ } – statements of main are enclosed in curly braces.
printf (“ ”):- It is a library function defined in <stdio.h> header file to
print the seq. of characters within “ ”.
\n – represents the new line character.
Writing a program:-
Objective: A program to find the area & perimeter of a rectangle whose sides are p & q.
area = pxq
Perimeter = 2*(p + q)
Formulae above can be written in C-
area = p * q => multiplication operation
o it is called – statement.
o p, q & area – variable names.
o * is multiplication operator.
o = is assignment operator.
Semantics/Meaning of the above statements –
“Read the contents of the memory box whose label (name) is p and multiply it by the contents of another memory
box named q. Store the result of multiplication in a memory box named – area.
P q area
4 6 24
*
4014 7096 operation Memory box6768

Similarly the perimeter


2
P q perimeter
4 6 10
* 20
+

Anurag Tewari, CS, KIET 13


So the program will be –
#include<stdio.h>
main()
{
int p,q, area, perimeter;
p=4;
q=6;
area=p*q;
perimeter=(p+q)*2;
printf(“area=%d\n”,area);
printf(“perimeter=%d\n”, perimeter);
}
Explanation of program:
int p,q, area, perimeter  declaration statement which tells the compiler that p, q, area & perimeter are the
variable names of integer type (no decimal values).
P = 4  Assignment statement. a value 4 is assigned to variable name p.
area = p*q  arithmetic statement.
Output statement:
The printf function is used to output information (both data from variables and text) to standard output.
 It is A C library function in the <stdio.h> library.
 Takes a format string and parameters for output.
As:printf(format string, arg1, arg2, …);
e.g. printf("The result is %d and %d\n", a, b);
The format string contains:
Literal text: is printed as itis without variation
Escaped sequences: special characters preceeded by \ eg.=>\n used as new line character
Conversion specifiers: % followed by a single character
 Indicates (usually) that a variable is to be printed at this location in the output stream.
 The variables to be printed must appear in the parameters to printf following the format string,
in the order that they appear in the format string.
 It returns integer value calculated by counting no. of characters printed on console.

Syntax1:
printf("user defined message");
O/P:user defined message
Syntax2:
printf("Format specifers",value1,value2,..);
O/P:values of variables as per specifiers type

Example of printf function


int a=10;
double d=13.4;
printf("%f%d",d,a);
In printf() format string can have
%f – for printing real values.
%d – for printing integer values.
%c – for printing character values.
%i – for printing integer values.
%p- for printing address values.

So In the statement  printf(“area=%d\n”,area), the format string is  “area=%d\n” & variable is area.
%d interprets the variable area occurring after the comma in the printf statement as an integer
Anurag Tewari, CS, KIET 14
& displays its value.
\nnew line advancement.
Input Statement:
To make the program generalized we have to add user interaction in it during program execution.
It means user can give different i/ps & observe the o/p accordingly.
Input unit is keyboard & o/p unit is monitor.
The input statement in C uses-scanf() as its library function.
 The scanf function is the input equivalent of printf
 It is a C library function in the <stdio.h> library
 Takes a format string and parameters, much like printf
 The format string specifiers are nearly the same as those used in printf

Examples:
scanf ("%d", &x); /* reads a decimal integer */
scanf ("%f", &rate); /* reads a floating point value */
The ampersand (&) is used to get the “address” of the variable
All the C function parameters are “passed by value”.
If we used scanf("%d",x) instead, the value of x is passed. As a result, scanf will not know where
to put the number it reads.
It returns integer value calculated by counting no. of characters entered thru keyboard on console.

Syntax:
scanf("format specifiers",&value1,&value2,. ...);
Example of scanf function list of address of variables
int a;
float b;
scanf("%d%f",&a,&b);
In the above syntax format specifier is a special character in the C language used to specify the
data type of value.so now program will be as follows
*Returned values of printf() and scanf()
In C, printf() returns the number of characters successfully written on the output and scanf() returns number of
items successfully read.
For example, below program prints jhello world 12
main()
{
printf("%d",printf("jhello world"));

}
Irrespective of the string user enters, below program prints 1.
main()
{
char a[50];
Anurag Tewari, CS, KIET 15
printf(" %d", scanf("%s", a));
}
Modulus operator (%) :
It returns the remainder as a result after division operation
4 %3 = 1, -5 % 2 = -1
7% 3 = 1, 5 % -2 = 1, 2 % 3 = 2
Hierarchy of operators:
1st priority  Parenthesized value (value within bracket or paranthesis): a*(b/(c-d))
2nd priority  *, /, %.
3rd priority  +, -
4th priority  =
Direction of solving is from left to right in an expression.
i = 2 * -3 / 4 + 4/4 + 8 -2 + 5%8
Result 
6/4 + 4/4 +8 - 2 + 5/8
1 + 4/4 + 8 - 2 + 5/8
1 + 1 + 8 -2 + 5/8
10 – 2 + 0
8+08
/ *… .............. */  comment
int A = 10
float b =2.2
char c = ‘A’
C Tokens

 The smallest individual unit in a c program is known as token or a lexical unit.


 In C Programming punctuation, individual words, characters etc are called tokens.

C tokens can be classified as follows:

Token Example :

No Token Type Example 1 Example 2


1 Keyword Do While
2 Constants -76 89
3 Identifier or variable Number Sum
Anurag Tewari, CS, KIET 16
No Token Type Example 1 Example 2
4 String “HTF” “PRITe@_”
5 Special Symbol * @
6 Operators ++ /

Basic Building Blocks and Definition :


Token Meaning
Keyword Reserve word used by compiler only
Constant Constants are expressions with a fixed value
The term identifier is usually used for variable
Identifier
names
String Sequence of characters
Special Symbols other than the Alphabets, Digits and
Symbol white-spaces
A symbol that represents a specific mathematical
Operator
or non-mathematical action.

C KEYWORDS

C keywords are the words that convey a special meaning to the c compiler. The keywords cannot be
used as variable names because by doing so, we are trying to assign a new meaning to the keyword
which is not allowed. There are total 32 keywords defined in C

The list of C keywords is given below:

Auto break Case Char const

Continue default Do Double else

Enum extern Float For goto

If Int Long Register return

Short signed Sizeof Static struct

Switch typedef Union Unsigned void

Volatile While

Anurag Tewari, CS, KIET 17


C IDENTIFIERS/ VARIABLES

Identifiers are used as the general terminology for the names of variables, functions and arrays.
These are user defined names consisting of arbitrarily long sequence of letters and digits with
either a letter or the underscore ( _ ) as a first character.

Structured programming
Structured programming, sometimes known as modular programming.

It is a subset of procedural programming that enforces a logical structure on the program being written
to make it more efficient and easier to understand and modify.

It is A technique for organizing and coding computer programs in which a hierarchy of modules is
used, each having a single entry and a single exit point.

In Struct.Prog., control is passed downward through the structure without unconditional branches to
higher levels of the structure.

Three types of control flows are used: sequential, conditional (test), and iterational (repeating).
Structured programming frequently employs a top-down design model, in which developers map out
the overall program structure into separate subsections.

A defined function or set of similar functions is coded in a separate module or sub module, which
means that the code can be loaded in to memory more efficiently and that modules can be reused
in other programs.
After a module has been tested individually, it is then integrated with other modules into the overall
program structure.
Top down approach and Stepwise refinement
With the top-down approach, we start with our top-level program, then divide and further sub-divide it
into many different modules. The division process is known as stepwise refinement.
As we design each module, we will discover what kind of sub modules we will need, and then continue
to program those.
After programming our smaller sub modules, we will group them together into the larger module.

 The key to stepwise refinement is that each module should


end up doing only one task.
 One advantage of the top-down approach is that we see the
full picture at first.
 Therefore, the top-down approach is suitable for larger(big)
problems.

Anurag Tewari, CS, KIET 18


Algorithm: The Fundamental knowledge needed to solve problems using a computer is the notion of
an algorithm.
“An Algorithm is a precise specification of a seq. of instructions
(tells what task is to be executed) to be carried out in order to solve a given problem”.

Features of a good algorithm:


1. Finiteness: An algorithm must terminate after a finite number of steps and further each
step must be executable in finite amount of time
2. Definiteness (no ambiguity): Each steps of an algorithm must be precisely defined; the
action to be carried out must be rigorously and unambiguously specified for each case.
3. Inputs/ Output: An algorithm has zero or more (but only finite), number of inputs and
(atleast 1)output.
4. Effectiveness(efficient): An algorithm should be effective. This means that each of the
operation to be performed in an algorithm must be sufficiently basic that it can be done
exactly and in a finite length of time.

FLOWCHARTS

 Flowchart is developed for showing the steps involved in a process (algorithm).


 A flowchart is a diagram made up of boxes, diamonds and other shapes, connected by arrows.
 Flowchart combines symbols and flow lines, to show the operation of an algorithm in a figure.
Flocharting Symbols

Anurag Tewari, CS, KIET 19


Example 1:Algo. designed to convert

temper ature
to
in degree Celsius  degree Fahrenheit.
Input: Temp. in degree Celsius. Output: Temp. in degree Fahrenheit.
1. Start
2. Obtain value of temp. in °C.
3. Apply mathematical formula of conversion(Multiply this value by 1.8 & add 32)
4. Assign value obtained of temp. in °F.
5. Display result.
6. Stop.
to
Flowchart: to convert temperature in degree Celsius   degree Fahrenheit.

Start

Read(i/p)
C

F = C x 1.8

F = F + 32

(Display)Write temp
in F

Stop

Example 2: Design Algo. for choosing the largest of 3 given positive integers.
Input : Three integers :– A, B, C. o/p : Largest no. among A, B, C.

Anurag Tewari, CS, KIET 20


Example 3. Design Algo for finding the sum of given N nos. & display the result.
I/p: N nos. entered (one by one) by keyboard. O/p: sum of N elements (entered by keyboard).
1. start
2. obtain the value N(total nos. to be entered)
3. Initialize variable sum & counter to 0.
4. Read the no. as num
5. Add this num to sum and update sum.
6. Increment the value of the counter by one
7. Continue steps 4 to 6 till counter < = N.
8. Print the sum value.
9. Stop
Pseudocode
Pseudocode is an informal high-level description of the operating principle of a computer
program or algorithm. It has following Features :
1. It uses the structural conventions of a normal programming language, but is
intended for human reading rather than machine reading.
2. Pseudocode typically omits details that are essential for machine understanding
of the algorithm, such as variable declarations, system-specific code and
some subroutines.
3. The programming language is augmented with natural language description
details, where convenient, or with compact mathematical notation.

Anurag Tewari, CS, KIET 21


4. The purpose of using pseudocode is that it is easier for people to understand
than conventional programming language code, and that it is an efficient and
environment-independent description of the key principles of an algorithm.
5. It is commonly used in textbooks and scientific publications that are documenting
various algorithms, and also in planning of computer program development, for
sketching out the structure of the program before the actual coding takes place.
Example: pseudo-code to find factorial of a given number:
Read number
Fact = 1
i = 1
WHILE i<=number
Fact=Fact*i
i=i+1
ENDWHILE
WRITE Fact

The way of data processing For Program


Before discussing our program we should understand the way of data processing by computer:
Steps of Data-Processing using Computer:
1. Analysis of the requirement (problem).
2. Designing of method after understanding the- to do task.
3. Expressing the method in a step-wise procedure to design algorithm (in human-language)
4. Expressing the Algorithm in a std. notation using a programming language so a program is
designed to be executed.
5. Input the program (& data) to be executed & save (store) it in the memory of computer.
6. Issue the command to execute the program in computer.
7. The computer interprets/compiles the program saved in memory (RAM). As per the instructions
Written in the program (to need or store or manipulate) the processor of computer performs
accordingly.
8. After executing the program, the result is written to the O/p unit if execution is successful or
error message will be displayed.
9. Then error removal (modify and recompile the program) or further processing continues.
John Von Neumann – a computer Scientist proposed the first Model of Computer – 1945.

Anurag Tewari, CS, KIET 22

You might also like