Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Introduction To C Programming-Unit1-Part1

Uploaded by

sanikagangapur57
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Introduction To C Programming-Unit1-Part1

Uploaded by

sanikagangapur57
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Introduction to C

programming
ESU104E
Dr. Kavitha Devi CS
2

What is C?

 C is a general-purpose programming language


created by Dennis Ritchie at the Bell Laboratories
in 1972.

 It is a very popular language, despite being old.


The main reason for its popularity is because it is a
fundamental language in the field of computer
science.

 C is strongly associated with UNIX, as it was


developed to write the UNIX operating system.

 Its features were derived from an earlier language


B, hence called as C language.
3

Why Learn C?
 It is one of the most popular programming
languages in the world

 If you know C, you will have no problem in


learning other popular programming languages
such as Java, Python, C++, C#, etc, as the syntax is
similar

 C is very fast, compared to other programming


languages, like Java and Python

 C is very versatile; it can be used in both


applications and technologies
4

Difference between C and C++


 C++ was developed as an extension of C,
and both languages have almost the
same syntax.

 The main difference between C and C++


is that C++ support classes and objects,
while C does not.
5
6
7
8
9

Program outcomes (POs)-12


Graduate Attributes

1. Engineering knowledge
2. Problem analysis
3. Design/development of
solutions
4. Conduct investigations
of complex problems
5. Modern tool usage
10

Unit I: Introduction to Programming


1.1:Introduction to computer software
When we talk about computer, we actually
mean the following two parts:
1. Computer Hardware
2. Computer software

1. Computer Hardware : Which performs all the


computation and calculation work that
computers are known for.

2. Computer software : which instructs the


hardware what to do and how to do it.
11

Fig :6.1 illustrates these parts

If we think of a computer as a living being, then the


hardware would be the body that performs actions
such as seeing through eyes, lifting objects, and
filling the lungs with air.

The software would be the intelligence brain that’s


helps in interpreting the images that are seen
through the eyes, Instructing the arms how to lift
objects and instructing the body to fill the lungs with
air.
12

 But the computer is hardware digital machine, it


can understand only two basic values On and
Off.
 The on and off concept forms the basics of
binary language.
 Hence the computer software was developed to
convert binary language into a form that tells the
computer hardware what to do.
 Computer hardware cannot think and make
decisions on its own.
 Hence, it cannot be used to analyze the given set
of data and find the solution on its own.
 So the hardware needs a software
𝒂 𝒔𝒆𝒕 𝒐𝒇p𝒓𝒐𝒈𝒓𝒂𝒎𝒔 to instruct what has to be
done.
13

 A program is a set of instructions that are arranged


in a sequence to guide a computer to find a solution
for a given problem.
 The process of writing a program is called
programming.
 Computer software is written by programmers using
a programming language.
 The programmer writes a set of instructions
p𝑟𝑜𝑔𝑟𝑎𝑚 using a specific programming language.
 Such programs are known as Source Code.
 Another computer program is called as Compiler is
used on source code, used to transform the
instructions into a language that a computer can
understand.
14

 The result is an executable computer program,


another name for software.

The examples of computer software include the


following:
1. Driver software

2. Educational software

3. Media players and media development software

4. Productivity software

5. Operating systems software

6. Computer games
15

1 Driver software : which allows a computer to


interact with hardware devices such as printers,
scanners and video cards.

2. Educational software : which includes


programs and games that help in teaching and
providing drills to help memorize facts.

3. Media players and media development


software : which are specifically designed to
play and/or edit digital media files such as music
and videos
16

4. Productivity software : it is an older term used to


denote any program that allows the user to be more
productive in a business sense. examples : word
processors, database management utilities and
presentation software

5. Operating systems software : helps in coordinating


system resources and allows execution of other
programs. Some Examples of operating systems are
Windows, Mac OS X and Linux.

6. Computer games : widely used as a form of


entertainment software that has many games.
17

1.2 : Classification of Computer Software


Computer software is broadly classified into 2
groups
1. Application software
2. System software

1. Application software : is designed for users to


solve a particular problem. Ex : spread sheets,
database system, desktop publishing software,
program development software, games, & web
browsers. Simply it can be defined as, application
software represents programs that allow users to
do something to run the hardware.
18

2. System software :
 provides a general programming environment
in which programmers can create specific
application to suit their needs.
 This environment provides new functions that
are not available at the hardware level and
performs tasks related to application software.
 It acts as an interface between the hardware
and application software.
 Fig 6.2 illustrates the relationship between
application software and system software.
 And Table 6.1 lists the difference between
application software and system software.
19
20
21

1.2.1 : System Software


As we know that system software is designed to
operate computer hardware. Some of the most
widely used system software as follows :
1. Computer BIOS and Device Drivers
2. Operating System
3. Utility Software
4. Translators :Compilers, Assemblers, Interpreter
5. Linker
6. Loader
7. Debugger
22

1.Computer BIOS and Device Drivers


 Basic Input/Output System BIOS and device
derivers provide basic functionality to operate
and control the hardware connected to or built
into the computer.

 BIOS is a built in of the computer and is the first


code run by the computer when it is switched on.

 The key role of BIOS is to load and start the OS.

 When the computer starts, the first function that


BIOS performs is to initialize and identify system
devices such as the video display card,
keyboard, mouse, hard disk, CD/DVD drive and
other hardware. Hence BIOS chip runs a series of
test called POST – Power On Self Test, to ensure
that the system devices are working correctly.
23

The BIOS screen is shown in Fig 6.3, This menu enables the
user to configure hardware
Set the system clock
Enable/Disable system components
24

In summary, BIOS performs the following


functions;
 Initializes system hardware
 Initializes system registers
 Initializes power management system
 Tests RAM
 Tests all the serial and parallel ports
 Initializes CD/DVD disk derive and hard
disk controllers
 Displays system summary information
25

2. Operating System 𝑶𝑺
 The primary goal of OS is to make the computer system or
cell phone convenient and efficient to use.

 The OS offers generic services to support user applications.

 The primary consideration is always convenience.

 Users should find it easy to work on it.

 For examples- we use icons, which give us an idea about


which application they launch

Hence we can tell that it is the human- computer interface that


helps to identify and launch an application.
 This interface hides a lot of details of the instructions that
performs all these tasks
 OS ensures that system resources such as CPU, memory, I/O
devices and so on are utilized efficiently.
26

3. Utility Software
Utility software is used to analyze, configure, optimize and
maintain the computer system. Utility programs may be
requested by application program during their execution for
multiple purposes. Examples
1. Disk defragmenters
2. Disk checkers
3. Disk cleaners
4. Disk space analyzers
5. Disk partitions
6. Backup utilities
7. Disk compression
8. File managers
9. System profilers
10. Anti virus utilities
11. Data compression utilities
12. Cryptographic utilities
13. Launcher applications
14. Registry cleaners
15. Network utilities
16. Command line interface 𝑪𝑳𝑰 and graphical user interface 𝑮𝑼𝑰
27
1. Disk defragmenters – are used to detect computer files whose contents are broken
across several locations on the hard disk, and its fragments can be moved to one
location in order to increase efficiency.

2. Disk checkers – are used to scan the contents of a hard disk to find files that are either
corrupt in some way, or not correctly saved and eliminate/repair them in order to
make the hard derive operate more efficiently.

3. Disk cleaners – helps the user to decide what to delete when their hard disk is full.

4. Disk space analyzers – are used for visualizing disk space usage by obtaining the size
of all folders and files in the folder or drive.

5. Disk partitions – are used to divide an individual derive into multiple logical derives,
each with its own file system.

6. Backup utilities – are used to make a copy of all information stored on a disk. In case
a disk failure occurs, backup utilities are used to restore the entire disk. Even if a file gets
deleted accidently, the backup utility can be used to restore the deleted files

7. Disk compression – are used to enhance the capacity of the disk by compressing the
contents of the disk.

8. File managers – are used to provide routine data management tasks performance
conveniently like deleting, renaming, cataloguing, moving, copying, merging etc.
28

9. System Profilers – are used to provide detailed information of both software


installed and hardware connected to the computer.

10. Antivirus – utilities are used to scan the computer for viruses.

11. Data Compression – utilities are used to compress files to smaller size.

12. Cryptographic Utilities – are used to encrypt and decrypt files.

13. Launcher Applications – are used as convenient access point for


application software.

14. Registry Cleaners – are used to clean and optimize the windows registry by
deleting old registry keys that are no longer in use.

15. Network Utilities – are used to analyze the computer’s network connectivity,
configure network settings and check data transfer or log events.

16. Command line interface 𝑪𝑳𝑰 and graphical user interface 𝑮𝑼𝑰 - are
used to interface the OS with other software.
29

4. Translators :
Translators are computer programs used to translate a code written in one
programming language to a code in another language that computer can
understand.
Types :
1. Compilers,
2. Interpreter
3. Assembler.

1. Compilers : is a special type of program that transforms source code written in


a higher programming language into lower machine language consisting of
just two digits 0’s and 1’s. The resultant code is known as object code.

If the source code contains errors, then the compiler will not be able do its task. The
errors will limit it in understanding a program called as Syntax Error.

Syntax errors are similar to spelling mistakes or tying mistakes.

Another type of error is the logical error, which occurs when a program dies not
function accurately and its much harder to identify and correct.
30

2. Interpreter : is similar to compiler

Compiler- Translates all of a program's source code into


machine code before the program runs. This allows for faster
execution because no translation is required during
runtime. Compilers are ideal for high-performance applications.
Interpreter-Translates code line-by-line as the program
runs. Interpreters are well-suited for rapid development,
debugging.

3. Assembler : convert the code written in assembly language


into machine language.

An assembly language is a type of low-level


programming language that is intended to communicate
directly with a computer’s hardware. Unlike machine language,
which consists of binary and hexadecimal characters, assembly
languages are designed to be readable by humans.
31

#include <stdio.h>
int main()
{ int x=3;
int y=6;
int sum;
sum = x+y;
printf("integer1=%d", x, "\n");
printf("integer2=%d", y, "\n");
printf("integer3=%d", sum, "\n" );
return 0;
}
32

#include <stdio.h>
int main()
{
int x=3;
int y=6;
int sum, Diff, Mul, Div ;
Sum = x+y;
Diff = x-y;
Mul = x*y;
Div= x/y;
printf("num1=%d", x, "\n");
printf(" num2=%d", y, "\n");
printf(" sum=%d, Diff=%d, Mul=%d, Div=%d", sum, Diff, Mul, Div, "\n" );
return 0;
}
33

5. Linker – also called as link editor and binder, is


a program that combines object modules to
form an executable program.
object modules-
Generally, in case of large programs, the
programmers prefer to break the code into
smaller modules, as this simplifies the
programming task. And eventually , when the
source code of all the modules has been
converted into object code, these modules
need to be put together, this job is done by the
linker.
Usually, the compiler automatically invokes the
linker as the last step in compiling a program.
34

6. Loader – is a special type of program that is a


part of OS and it copies the programs from
storage device to main memory, where they can
be executed.

7. Debugger – Debugging is one of the important


and necessary step in software development
process.
This will identify the errors 𝑏𝑢𝑔𝑠 and remove them
as early as possible is very important.
Usually in real time applications there will be
thousands of lines of code so possibility of errors
are common which can be removed and
corrected using debugging tools called as
debugger.
35

A debugger is a program that runs other programs allowing users to have


some percentage of control over their programs. so that they can
examine them when things go wrong.

The debugger helps the programmer to discover the following things-


1. Which statement or expression was being executed when the error
occurred?
2. If an error occurred during the execution of a function, what
parameters were passed to it while it was called?
3. What is the value of variables at different lines in the program?
4. What is the result of evaluating an expression?
5. What is the sequence of statements actually executed in the
program?

 Debuggers show the position of the error in the program.

 Many debuggers allow programmers to run programs in step-by-step


mode.

 They also allow them to stop on specific point at which they can
examine the value of certain variables.
36

1.2.2 : Application Software


Application software is a type of computer software that employs
the capabilities of a computer directly to perform a user defined
task.

An analogy for this is the relationship of an electric light bulb (an


application) to an electric power generation plant (a system)
i.e. the power plant generates electricity, which is not of real use
by itself until it is used through an application such as the electric
light, which performs a service that actually benefits the user.

The typical examples of software applications are word


processors, spreadsheets, media players, education software,
CAD, CAM, data communication software etc. Multiple
applications bundled together as a package are called as
application suit.
37

3. Programming Languages
 A programming language is a language specifically designed to
express computations that can be performed using the computer.

 Programming languages are used to create programs that control


the behavior of a system, to express algorithms, or as a mode of
human communication.

 Usually, these programming languages have a vocabulary of syntax


and semantics for instructing a computer to perform specific tasks.

 The term programming language usually refers to high level


languages, such as BASIC C, C++, COBOL, FORTRAN, Ada and
Pascal, to name few.

 Each of these languages has a unique set of keywords and special


syntax for organizing program instructions.
38

 While high level languages are easy for humans to read and
understand but the computer can only understand the machine
language consisting of only numbers i.e. zeros and ones.

 Different type of central processing unit (CPU) has its own unique
machine language.

 In between the machine language and high level language there is
another type of language known as assembly language, they are
much easier to program.

 However, irrespective of language used by the programmer, the


programs that are written using any programming language has to
be converted into the machine language so that the computer can
understand it.

 There are two ways to do this i.e. compiling or interpreting the


program.
39

The best language depends on the following factors-


1. The type of computer on which the program has to be
executed

2. The type of program

3. The expertise of the programmer

For Example, FORTRAN is particularly a good language for


processing numerical data, but it does not lead itself very well
to organizing large programs

Pascal can be used for writing well structured and readable


programs, but it is not flexible as the C programming
language.

C++ goes one step ahead of C by incorporating powerful


object oriented features, but it is complex and difficult to
learn.
40

4. Generations of Programming Languages


 We now know that the programming languages are the
primary tools for creating a software.
 Today, hundreds of programming languages are there in the
market and each claiming to be the best.
 However in 1940’s, when computers were introduced, there
was only one language i.e machine language.
 The concept of generations of programming languages
(levels) is closely connected to the advances in technology
that brought about the different generations of computers.
 The 4 generations of programming languages include
1GL – Machine language
2GL – Assembly language
3GL – High level language
4GL – Very high level language
5GL - AI (Artificial Intelligence) research
41

4.1- First generation- Machine language


The first stored program computer systems was
performed in machine language as shown in the
figure below.
42

 This is the lowest level of programming language and also the only language
that computers understand.

 All the commands and data values are expressed using 1’s and 0’s it
corresponds to ‘on’ and ‘off ‘ of electrical states in computer

 Here all instructions, memory locations, numbers and characters are


represented in strings of 1’s and 0’s.

 Although this language programs are typically displayed with the binary
numbers represented in octal (base 8) or hexadecimal (base 16 ) and these
programs are not easy for humans to read, write and debug.

 The main advantage of machine language is that the code can be executed
very quickly and efficiently, since it is directly executed by CPU.

 And more difficult to learn & far more difficult to correct if errors occur.

 Lastly the code written in machine language is not portable and transferable to
a different computer. Because it needs to be completely rewritten since the
machine language for one computer could be significantly different from that
of another.
43

4.2- Second generation- Assembly language


 The second generation programming languages (2GL)
include assembly language.

 They are symbolic programming languages that use symbolic


notation to represent machine language instructions.

 This is closely connected to machine language hence is also


called as low-level languages.

 Nearly all computer systems have an assembly language


available for use.

 It was developed in mid 1950s, it uses symbolic codes called


as mnemonic codes, which are easy to remember
abbreviations rather than numbers Ex- ADD for addition, CMP
for compare, MUL for multiply
44

 Assembly language programs consists of a series of individual


statements to instruct the computer what to do.

 Basically, an assembly language statement consists of a label, an


operation code called as opcode and one or more operands.

 Labels are used to identify and refer to instructions in the program.

 Opcode is a mnemonic that specifics the operation that has to be


performed such as move, add, subtract or compare

 The operand specifies the register or location in main memory where


the data to be processed is located

 Here also the language will vary from machine to machine, because
it is related to the internal architecture of the computer.

 Hence the code written in assembly language is less portable, as the


code written to be executed on one machine will not run on
machines from a different machine.

 The translator used here is the assembler to convert them to machine


language.
45

The following instructions are the art of assembly language code


to illustrate addition of two numbers

Even today, some programmers still use assembly language to


write arts of applications where speed of execution is critical like
video games, but most programmers today have switched to
3GLs or 4GLs to do the same.
46

4.3- Third generation- High level language


 3GL is a refinement of 2GL and these 2GLs brought logical
structure to software

 3rd generation was introduced to make the languages


more programmer friendly.

 In 3GL there is an great increase in data processing


occurred in 1960s and 1970s

 Here the program statements are not closely related to the


internal characteristics of the computer and therefore
referred as high level language.

 3GLs made programming easier, efficient and less errors.

 High- level languages falls between natural languages and


machine language. Ex’s- FORTRAN and COBAL
47

 Here the programs are written in an English like manner,


making them more convenient to use and giving the
programmer more time to address a client’s problems.

 While some high-level language are specifically designed


for specific purpose.

 Again a translators are required for translating the


instructions.

 3GLs make the users, easier to write and debug a program


and also give a programmer more time to think about its
overall logic.

 Hence the programs written here are portable between


machines. For example, a program written in standard C
can be compiled and executed on any computer that has
a standard C compiler.
48

4.4- Fourth generation- Very High level language


With each generation, programming languages have
become easier to use and more like natural languages.
The characteristics of 4GL are as follows:
1. The code comprising instructions are written in English like
sentences.
2. They are non-procedural, so users concentrate on the
‘what’ instead of ‘how’ aspect of the task.
3. The code is easier to maintain
4. The code enhances the productivity of the
programmers, as they have to type only few lines of
code to get something done. It is said that a
programmer becomes 10times productive when he or
she writes the code using 4GL than 3GL.
5. A typical example of 4GL is the query language that
allows a user to request information from database with
precisely worded English-like sentences,
49

 For example, when working with structured query language (SQL) the
programmer just needs to remember a few rules of syntax and logic,
but it is much easier to learn than COBOL OR C.

 Let us take an example in which a report has to be generated that


displays the total number of students enrolled in each class and in
each semester in the school. The request would look like this…

 Hence, we can see that 4GL is much simpler to learn and work with.
The same code, if written in C language or any other 3GL, would
require multiple lines of code to do the same task.

 4GLs are still evolving, which makes it difficult to define them.

 The only limitation of it is that it does not make efficient use of


machine’s resources.

 However, the benefit of executing a program fast and easy.


50

4.5- Fifth generation Programming language


 5GLs are widely used in Artificial Intelligence (AI) research. Examples:
programming in logic (Prolog), Official production system version 5
(OPS5) and Mercury.

 It contains visual tools to help in developing a program called visual


basic (VB).

 Hence, taking a forward leap from 4GLs, 5GLs are designed to make
the computer solve a given problem without the programmer.

 While with 4GL, the programmer has to write specific code to


perform a task, but with 5GL the programmer only needs to worry
about how to implement a routine or algorithm to solve them.

 Generally, 5GLs were built upon list programming (Lisp), many


originating on the Lisp machine, such as ICAD and frame languages
such as KL-ONE
51

 In 1990s, 5GLs were considered to be the wave of the


future and some predicted that they would replace
all other languages for system development.

 In 1982-93, Japan had invested much research and


money into their 5G computer network of machines.

 Researchers began to observe that, starting from a


set of constraints defining a particular problem,
deriving an efficient algorithm to solve is very difficult.
All these things could not be automated and still
require the insight of a programmer.

 However, today the 5GLs are back as a possible level


of computer language and software vendors across
the globe currently claim that their software meets
the visual programming requirements of the 5GL
concept.

You might also like