C Module-1
C Module-1
Introduction to
C
Programming
Dr. Naveenkumar D T
Associate Professor
Department of civil engineering
SJBIT, Bengaluru
Wisdom of wood cutter
C
2
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
3
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
4
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
5
Dr. Naveenkumar D. T. SJBIT, Bengaluru
Introduction to Computers
Dr. Naveenkumar D T
Associate Professor
Department of civil engineering
SJBIT, Bengaluru
C
7
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
8
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
9
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
10
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
11
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
12
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
13
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
14
Dr. Naveenkumar D. T. SJBIT, Bengaluru
What is computer?
C ❑ An electronic device that is designed to accept data, perform the
required mathematical and logical operations at high speed, and
output the result.
❑ Past: large size, high power consumption and costly for
commercial applications.
❑ Hence, used for computing trajectories for astronomical or
military applications
❑ Present: smaller size, less power and cheaper
15
Dr. Naveenkumar D. T. SJBIT, Bengaluru
Characteristics of Computers
C Speed
Economical Versatile
No IQ Computer Memory
Diligence Accuracy
Automation
16
Dr. Naveenkumar D. T. SJBIT, Bengaluru
❑ Speed: millions of operations/second. The speed usually given in
17 and energy.
19
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
20
Dr. Naveenkumar D. T. SJBIT, Bengaluru
Computer Generations
C
21
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C Vacuum Tubes: The First Generation
❑ Hardware technology manufactured using thousands of
vacuum tubes.
❑ Memory Electromagnetic relay as primary memory and
punched cards to store data and instructions.
❑ Software Technology Programming in machine or
assembly language.
❑ Used for Scientific applications
22
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
23
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
24
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C Highlights
Costly
❑ Difficult to use
tubes.
magnetic tapes and magnetic disks were used to store data and
instructions. These computers had faster and larger memory than
the first generation computers.
27
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
28
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C Highlights
❑ Faster, smaller, cheaper, reliable, and easier to use
than the first generation computers
❑ They consumed 1/I0th the power consumed by first
generation computers
❑ Bulky in size and required a complete room for its
installation
❑ Dissipated less heat than first generation computers
but still required air-conditioned rooms
❑ Costly
29 ❑ Difficult to use
Dr. Naveenkumar D. T. SJBIT, Bengaluru
Integrated Circuits: The Third
C Generation (1964-1975)
❑ Hardware manufactured using integrated chips (ICs). ICs consist of
several components such as transistors, capacitors, and resistors on a
single chip to avoid wired interconnections between components.
These computers used SSI and MSI technology. Minicomputers came
into existence.
❑ Memory Larger magnetic core memory was used as primary
memory; larger capacity magnetic tapes and magnetic disks were used
to store data and instructions.
❑ Software Technology high level programming languages such as
FORTRAN, COBOL, Pascal, and BASIC.
❑ Used for Scientific, commercial, and interactive online applications
31
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
32
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C Highlights
33
Dr. Naveenkumar D. T. SJBIT, Bengaluru
The Microprocessor: The Fourth
C Generation (1975-1989)
❑ Hardware Technology lCs with LSI (Large Scale Integrated) and later with
VLSI technology (Very Large Scale Integration). Microcomputers came into
existence. Use of personal computers became widespread. High Speed computer
networks in the form of LANs, WANs, and MANs started growing. Besides
mainframes, supercomputers were also used.
❑ Memory Semiconductor memory as primary memory, large capacity magnetic
disks as built-in secondary memory. Magnetic tapes and floppy disks as portable
storage devices.
❑ Software Technology Programming was done in high level programming
language such as C and C++. GUI based OS (e.g. Windows) introduced. Had
icons and menus among other features. UNIX also introduced as an open source
operating system. Apple Mac OS and MS DOS were also released during this
period. All these operating systems had multi-processing and multi-
programming capabilities.
34
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
35
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
36
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C ❑ Used for Scientific, commercial, interactive
37
Dr. Naveenkumar D. T. SJBIT, Bengaluru
Artificial Intelligence: The Fifth
C Generation (1989-Present)
❑ Hardware Technology manufactured using ICs with ULSI. The use of
Internet became widespread and very powerful mainframes, desktops,
portable laptops, and smartphones.
❑ Memory Semiconductor memory as primary memory; large capacity
magnetic disks are as built-in secondary memory. Magnetic tapes and floppy
disks were used as portable storage devices, which have now been replaced by
optical disks and USB flash drives.
❑ Software Technology Programming is done in high-level programming
languages such as Java, Python, and C#. Graphical User Interface (GUI)-based
OS such as Windows, Unix, Linux, Ubuntu, and Apple Mac are being used.
❑ Used for Scientific, commercial, interactive online, multimedia (graphics,
audio, video), and network applications
❑ Examples IBM notebooks, Pentium PCs, SUM workstations, IBM SP/2,
Param supercomputer
38
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
39
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C Highlights
❑ Faster, smaller, cheaper, powerful, reliable, and easier to
use than the previous generation computers
❑ Speed of microprocessors and the size of memory are
growing rapidly
❑ High-end features available on mainframe computers in
the fourth generation are now available on the
microprocessors.
❑ Consume less power than computers of prior generations
41
Dr. Naveenkumar D. T. SJBIT, Bengaluru
Supercomputers
C ❑ Among the four - fastest, most powerful, and most expensive
42
Dr. Naveenkumar D. T. SJBIT, Bengaluru
Mainframe computers
C ❑ Are large-scale computers (smaller than supercomputers).
44
Dr. Naveenkumar D. T. SJBIT, Bengaluru
Microcomputers
C ❑ AKA PCs, are very small and cheap.
❑ The first one was designed by IBM in 1981 and was named
IBM-PC.
❑ Later on, many computer hardware companies copied this
design and termed as PC-compatible.
❑ Another type of popular PC is designed by Apple.
47 solve a problem
Dr. Naveenkumar D. T. SJBIT, Bengaluru
Monolithic programming
C ❑ Written using monolithic programming
language such as assembly language and
BASIC
❑ Consist of global data and sequential code.
49
Dr. Naveenkumar D. T. SJBIT, Bengaluru
Procedural programming
C ❑ A program is devided into subroutines that can access global
data. To avoid repetition of code, each subroutine performs a
well defined task.
❑ jump', 'goto', and 'call' instructions, the sequence of
execution of instructions can be altered.
50
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C Advantages
❑ The only goal is to write correct programs
52
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
53
Dr. Naveenkumar D. T. SJBIT, Bengaluru
Advantages
❑ Takes less time as modules or procedures written for one program can
be reused
❑ Each module performs a specific task.
56
Dr. Naveenkumar D. T. SJBIT, Bengaluru
ALGORITHM
C ❑ Defined procedure for performing some calculation.
57 obtained
Dr. Naveenkumar D. T. SJBIT, Bengaluru
Control Structures Used In Algorithms
58
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C Repetition: executing one or more steps for a
number of times. Can be implemented using
constructs such as the while, do-while, and for
loops.
59
Dr. Naveenkumar D. T. SJBIT, Bengaluru
FLOWCHART
C ❑ A graphical or symbolic representation of a process.
❑ Used to design and document complex processes to help the viewers to visualize
the logic of the process to gain a better understanding of the process and find
flaws, bottlenecks, and other less-obvious features within it.
Start and end symbols also known as the terminal symbol is always the first and the
last symbol in a flowchart.
Generic processing step also called an activity is represents instructions like add a to
b, save the result.
Input/ Output symbols are represented as a parallelogram and are used to get input
from users or display the results to them.
A Conditional or decision symbol is used to depict a Yes/No question or True/False
test.
Labeled connectors represented by an identifying label inside a circle are used in
complex or multi-sheet diagrams to substitute for arrows.
60
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C Limitations of using Flowcharts
❑ Drawing flowcharts is a laborious and a time
consuming activity.
❑ Flowchart of a complex program becomes, complex
and clumsy. At times, a little bit of alteration in the
solution may require complete re-drawing of the
flowchart
❑ Essentials of what is done may get lost in the
technical details of how it is done.
❑ There are no well defined standards that limits the
details that must be incorporated in a flowchart
61
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
62
Dr. Naveenkumar D. T. SJBIT, Bengaluru
PSEUDOCODE
C ❑ Structured English that describes algorithms. It facilitates the designers to
focus on the logic of the algorithm without getting bogged down by the
details of language syntax.
❑ Pseudocode is a compact and informal high-level description of an algorithm
that uses the structural conventions of a programming language.
❑ It is meant for human reading rather than machine reading, so it omits the
details that are not essential for humans. Such details include keywords,
variable declarations, system-specific code and subroutines.
❑ It is commonly used in textbooks and scientific publications for documenting
algorithms, and for sketching out the program structure before the actual
coding is done. This would help even the non-programmers to understand
the logic of the designed solution.
❑ There are no standards defined for writing a pseudocode because a
pseudocode is not an executable program. Flowcharts can be considered as a
graphical alternative to pseudocode, but are more spacious on paper.
63
Dr. Naveenkumar D. T. SJBIT, Bengaluru
❑ Example: Write a pseudocode to calculate the weekly wages of an employee. The pay depends
on wages per hour and the number of hours worked. Moreover, if the employee has worked
C for more than 30 hours then he gets twice the wages per hour, for every extra hour that he
has worked.
Read hours worked
Read wages per hour
Set overtime charges to 0
Set overtime hrs to 0
If hours worked > 30 then
Calculate overtime hrs = hours worked – 30
Calculate overtime charges =overtime hrs * (2 * wages per hour)
Set hours worked = hours worked - overtime hrs
ENDIF
Calculate salary = (hours worked * wages per hour) + overtime charges
Display salary
End
64 Variables: hours worked, wages per hour, overtime charges, overtime hrs, salary
❑ Quick language
❑ C is a core language
❑ C is a portable language.
67 ❑ C is an extensible language
Function1()
parts:
{
Statement 1;
Statement 2;
……………
……………
Preprocessor Commands }
Function2()
Statement N;
Functions
Statement 1;
Statement 2;
……………
……………
Statement N;
Variables }
………………….
………………….
FunctionN()
Comments
……………
69
Statement N;
}
#include<stdio.h>
int main()
{
/* My first program in C */
printf("\n Welcome to the world of C ");
return 0;
}
70
Dr. Naveenkumar D. T. SJBIT, Bengaluru
FILES USED IN A C PROGRAM
C
Files in a C
program
71
Dr. Naveenkumar D. T. SJBIT, Bengaluru
Source code file
❑ Contains code that defines the main function and maybe other functions.
Header Files
❑ A file with extension .h
The programmer wants to change, or add, subroutines, and have those changes
be reflected in all other programs.
❑ Some standard header files are available in C, but the programmer may also
create his own user defined header files
72
Dr. Naveenkumar D. T. SJBIT, Bengaluru
Object Files
❑ In the first step, the preprocessor program reads the source file as
❑ Source code lines which begin with the hash symbol are actually
❑ The output of the preprocessor is a text file which does not contain
74
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C
75
Dr. Naveenkumar D. T. SJBIT, Bengaluru
C KEYWORDS
❑ C has a set of 32 reserved words often known as
keywords.
❑ By convention all keywords must be written in
lowercase (small) letters.
Example: for, while, do-while, auto break, case,
char, continue, do, double, else, enum, extern,
float, goto, if, int, long, register, return, short,
signed, sizeof, static, struct, switch, typedef, union,
76 unsigned, void, volatile
Dr. Naveenkumar D. T. SJBIT, Bengaluru
Variables
C ❑ A meaningful name given to the data storage location in
computer memory.
❑ Refer to address of the memory where the data is stored.
Numeric values may also be associated with modifiers like short, long,
signed and unsigned.
By default, C automatically a numeric variable signed..
79
Dr. Naveenkumar D. T. SJBIT, Bengaluru
Constants
C ❑ Are identifiers whose value does not change.
❑ The control string specifies the type and format of the data that has to be obtained from
the keyboard and stored in the memory locations pointed by the arguments arg1, arg2,…,
argn.
The prototype of the control string can be give as: [=%[*][width][modifiers]type=]
❑ * is an optional argument that suppresses assignment of the input field. That is, it
indicates that data should be read from the stream but ignored (not stored in the memory
location).
❑ width is an optional argument that specifies the maximum number of characters to be
read.
❑ modifiers is an optional argument that can be h, l or L for the data pointed by the
corresponding additional arguments. Modifier h is used for short int or unsigned short
int, l is used for long int, unsigned long int or double values. Finally, L is used long double
data values.
83 %[flags][width][.precision][length]specifier
Dr. Naveenkumar D. T. SJBIT, Bengaluru
flags is an optional argument, which specifies output justification
L
specifiers.
Description
When the argument is a short int or unsigned short int.
When the argument is a long int or unsigned long int for integer
When the argument is a long double (used for floating point specifiers)
The number is left-padded with zeroes (0) F For floating point numbers
0
instead of spaces E, e Floating point numbers in exponential format
G, G Floating point numbers in the shorter of e format
o For Octal number.
s For a sequence of (string of) characters
u For Unsigned decimal value
x,X For Hexadecimal value.
85
Dr. Naveenkumar D. T. SJBIT, Bengaluru
ω
C
86
86
Dr. Naveenkumar D. T. SJBIT, Bengaluru