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

Module 1 POP Notes

This document provides an introduction to the programming language C. It discusses the basic characteristics of computers including their speed, accuracy, automation, diligence, versatility, and memory. It then covers the different generations of computers from the first generation using vacuum tubes to the current fifth generation using artificial intelligence. The document also classifies computers as supercomputers, mainframes, minicomputers, or microcomputers. Finally, it outlines the basic organization of a computer including input, storage, processing, output, and control.

Uploaded by

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

Module 1 POP Notes

This document provides an introduction to the programming language C. It discusses the basic characteristics of computers including their speed, accuracy, automation, diligence, versatility, and memory. It then covers the different generations of computers from the first generation using vacuum tubes to the current fifth generation using artificial intelligence. The document also classifies computers as supercomputers, mainframes, minicomputers, or microcomputers. Finally, it outlines the basic organization of a computer including input, storage, processing, output, and control.

Uploaded by

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

Principles of Programming using C Module 1

Module 1: Introduction to C
Computer: A computer in simple terms can be defined as an electronic device that is designed to
accept data, perform the required mathematical and logical operations at high speed and output
the result.

Characteristics of computer:

1. Speed:

 Computers can perform millions of operations per second.


 The speed of computers is usually given in nanoseconds and 1 picoseconds (1
nanoseconds=1*10^-9 and 1 Pico seconds=1*10^-12 seconds)

2. Accuracy:

A computer is a very fast, reliable and robust electronic device. It always gives accurate results,
provided the correct data and set of instructions are input to it. Hence in the event of an error,
only the user who has fed the incorrect data/program needs be held responsible. If the input is
wrong, then the output will also be erroneous, In computer terminology, this is known as
garbage-in garbage-out(GIGO).

3. Automation:

Computers are automatable devices that can perform a task without any user intervention. The
user just needs to assign the task to the computer, after which it automatically controls different
devices attached to it and executes the program instruction.

4. Diligence:

Unlike humans, a computer never gets tired of a repetitive task. It can continually work for hours
without creating errors.

5. Versatile:

Versatility is the quality of being flexible .Today, computers are used in our daily life in different
fields. For example they are used as personal computers (PCs)for home use, for business-
oriented tasks ,weather forecasting, space exploration,teaching,railways,banking,medicine, and
so on, indicating that computers can perform different tasks simultaneously.

6. Memory:

similar to humans, computers also have memory .Just the way we cannot store everything in our
memory and need secondary media, such as a notebook to record certain important things,
computers also have internal or primary memory(storage space) a s well as external or secondary
Principles of Programming using C Module 1

memory. While the internal memory of computers is very expensive and limited in size the
secondary storage is cheaper and of bigger capacity.

7. No IQ:

Although the trend today is to make computers intelligent by including artificial intelligence (AI)
in them, they still do not have any decision making abilities of their own. That is, their IQ level is
zero. They need guidance to perform various tasks.

8. Economical:

Using computers also reduce man power requirements and leads to an elegant and efficient way
of performing various tasks. Hence, computers save time, energy, and money. When compared
to other systems, computers can do more work in lesser time.

Computer generations [10M]:

Generation in computer terminology is change in technology a computer is being used. There is


five generation of computers.

First generation of computer (1940-1956): vacuum tubes:

 This generation computer is used thousands of vacuum tubes for circuitry for CPU
(Central processing unit)
 Memory requirement were met by magnetic drums.
 Took lot of space consumed enormous amount of power and generated lot of heat and
also was very expensive.
 These computers used machine language.
 Some computers of this generation were: ENIAC, EDVAC, UNIVAC etc..
 Program input was provided by punch cards and outputs were produced on paper.

Second generation of computer (1956-1963) Transistor:

 Transistors were used that were cheaper, consumed less power, more compact in size,
more reliable and faster.
 Magnetic cores were used as the primary memory and magnetic tape and magnetic disks
as secondary storage devices.
 These computers used assembly language (programmer could specify words and symbols
as instructions instead of 0’s and 1’s).
 Some computers of this generation were: IBM 1620, IBM7094.

Third generation computer (1964-1971) Integrated circuits:


Principles of Programming using C Module 1

 A single IC has many transistors, resistors and capacitors along with the associated
circuitry.
 This development made computers smaller in size, reliable and efficient.
 In this generation remote processing, times sharing multi-programming operating system
were used.
 High level language (FORTRAN, COBOL, PASCAL, BASIC, ALGOL) were used during this
generation.
 Keyboard and monitor were used to interact with user.
 Memory capacity increased substantially by the use of hard disks for secondary memory.
 Some computers of this generation were: IBM360 series, Honeywell-6000 series, and
personal data processor.

Fourth generation computer (1971-1980) Microprocessor:

 Used very large scale integrated (VLSI) circuits having about 5000 transistors and other
circuit elements with their associated circuits on a single chip.
 The microprocessor launched the fourth generation of computer, with thousands of
integrated circuits built onto a single silicon chip.
 Computers became more powerful, compact, reliable and affordable.
 As a result, it gave rise to personal computer revolution, laptops and smart phones
offering gigabytes of memory compared to few megabytes.
 In this generation, time sharing, real time networks, distributed operating system were
used the high level languages like C,C++,DBASE etc..Were used in this generation
 Fourth generation computers also saw the development of graphical user interface, the
mouse, and handled devices.

Fifth generation computer(1980-till date)Artificial intelligence:

 With ULS (ultra large scale integration) technology, microprocessor chips having ten
million electronic components were manufactured.
 This generation is based on parallel processing hardware and AI.
 AI is an emerging branch in computer science which interprets the means and method of
making computers think like human beings.
 All the high-level languages like C and C++,java ,.Net etc..are used in this generation .
 Neural networks and experts systems have to be developed.

Classification of computer:

Computers are broadly classified into 4 types based on speed, amount of data that they can hold
and price.
Principles of Programming using C Module 1

1. Super computer:
 The super computer is the fastest, most powerful and most expensive computer.
 Supercomputers were first developed in the 1980’s to process large amounts of data and
to solve complex scientific problems.
 A single supercomputer can support thousands of users at the same time.
 Computers are mainly used for weather forcasting, nuclear energy research, aircraft
design, automotive design, online banking, controlling industrial units, etc.

2. Main frame computers:

 Mainframe is very large in size and is an expensive computer capable of supporting


hundreds or thousands of users simultaneously.
 The processor speed is measured in MIPS (million instructions per seconds).
 Used for online transactions.
 Capability to handle large amount of data makes mainframe suitable for use in
government, banks and financial institutions.
 There are basically two types of terminals that can be used with mainframe systems are:

1. Dumb terminal:
Principles of Programming using C Module 1

Dumb terminals consist of only a monitor and a keyboard (or mouse).They do not have their own
CPU, memory and use the mainframe systems CPU and storage devices.

2. Intelligent terminals:

Intelligent terminals have their own processor and thus can perform some processing
operations..They do not have their own storage space.

3. Mini computer:

 It is multi-user computer system, capable of supporting hundreds of users simultaneously.


 Minicomputers are smaller, cheaper and slower than mainframes. They are called
minicomputers because they were the smallest computer of their times. Also know as
midrange computers, the capabilities of minicomputers fall between mainframe and PCs.
 The first minicomputer was introduced by digital equipment corporation (DEC) in the
mid 1960.
 The minicomputers are used in small organization or a department of a large one.

4. Micro computers:

 Microcomputers commonly known as PCs are very small and cheap.


 The microcomputer or PC is introduced by Apple and endorsed by IBM. This is a single-
user machine powered by a single-chip microprocessor.
 They are very powerful machines havinggigabytes of memory.
 They are both used in standalone mode and in a network.
 A microcomputer takes the form of desktop, notebook (laptop) or a notebook (smaller
laptop). PCs today are powered by 3 types of OS – windows (7, 8 or 10), Mac OS X
(Apple) and Linux.
 They are used for engineering and scientific applications and for software development.
 PCs can be classified into the following categories.
o Desktop
o Laptops
o Workstations

Basic organization of computer:

A computer is an electronic device that basically performs five major operations:

 Accepting data or instruction


 Storing data
 Processing data
Principles of Programming using C Module 1

 Displaying data
 Controlling and coordinating all operations inside a computer.

Input:

 This is the process of entering data instructions into the computer system.
 The data and instructions can be entered by using different input devices such as
keyboard, mouse, scanner, trackball, etc...
 Note that the computers understand binary language, which consist of only two symbols
(0 and 1), so it is the responsibility of the input devices to convert the input data into
binary codes.

Storage:

Storage is the process of saving data and instructions permanently in the computer so that they
can be used for processing.

A computer has two types of storage areas:

1. Primary storage:
 Primary storage also known as the main memory is the storage area that is directly
accessible by the CPU at very high speed.
 Drawback of main memory is that it is volatile in nature that is as soon as the
computer is switched off, the information stored gets erased.
 RAM and ROM are the examples of primary storage

RAM:

 It stands for random access memory.


 It is a temporary memory that means it is used to store information that is used
immediately.
 Once the computer is turned off the data will be deleted. With the help of RAM
computers can perform multiple tasks like loading applications, browsing the web tasks
like loading applications, browsing the web, editing a spreadsheet etc…
 There are 2 types:

1.S-RAM (static RAM): Volatile, stores until system is on

2.DRAM(Dynamic RAM): Store binary bits in the form of electrical charges.


Principles of Programming using C Module 1

ROM:

 It stands for read only memory the data stored in these devices are non-volatile i.e. once
the data stored in the memory cannot be modified or deleted.
 The memory form which will only read but cannot write.
 The information is stored permanently.
There are 3 types
1. PROM (programmable ROM) :Data cannot be altered
2. EPROM(Erasable Programmable ROM): Possible to erase the information
3. EEPROM (electrically Erasable ROM): non-volatile memory used in computers,
usually integrated in microcontroller

1. Secondary storage:
Also known as auxiliary memory this is just the opposite of primary memory.
It basically overcomes all the drawbacks of the primary storage area.
It is cheaper, non-volatile and used to permanently store data and programs of those jobs
that are not being currently executed by the CPU.

Difference between primary and secondary storage devices:

Processing:

 The process of performing operations on the data as per the instructions specified by the
user(programs) is called processing.
 Data and instructions are taken from the primary memory and transferred to the
arithmetic and logical unit, which performs all sorts of calculations.
Principles of Programming using C Module 1

 The intermediate results of processing may be stored in the main memory as they be
required again.
 When processing completes the final result is then transferred to the main memory
.Hence the data may move from main memory to the ALU multiple times before the
processing is over.

Output:

 Output is the processing of giving the result of data processing to the outside world.
 The results are given through output devices such as monitor, printer etc...
 The output devices therefore convert the results available in binary codes into a human-
readable language before displaying it to the user.

Control:

The control unit is the central nervous system of the entire computer system. It manages and
controls all the components of the computer system.

CPU
Memory
unit/storage unit

Control unit Output unit


Input unit

ALU (arithmetic
and logical unit

Input and output devices:

An input device is used to fees data and instructions into the computer. In the absence of an input
device, a computer would have only been a display device.
Principles of Programming using C Module 1

Input devices

Keyboard Pointing devices Handheld devices Optical devices Audio/video devices

Mouse Stylus Barcode


reader

Track ball Touch


Scanner

Track pad
Joystick
OCR

OMR

MICR

Keyboard:

 The keyboard is the main input device for computers. Computer keyboards look very
similar to the keyboards of typewriters with some additional keys.
 Most keyboards have between 80 and 110 keys.
 The layout of a keyboard is known as QWERTY for its first six letters.

Types of keys:
Principles of Programming using C Module 1

Pointing devices:

 Pointing input devices enable the users to easily control the movement of the pointer to
select items on a display screen to select commands menu, to draw graph, etc...
 Some examples of pointing devices include mouse, track ball, light pen joystick and
touch pad

Mouse:

 The mouse is an input device that was invented by Douglas Engelbart in 1963.
 It is the key input device used in a graphical user interface
 It can be used to handle the pointer easily on the screen to perform various functions such
as opening a program or file.
 The mouse has two buttons and a scroll wheel. It can be held in the hand and easily
moved without lifting, along a hard flat surface to move the cursor to the desired
location-up, down, left, right.
 Once the mouse is placed at appropriate postion,the user may perform the following
operation:
Principles of Programming using C Module 1

o Point: Placing the mouse pointer over the word or the object on the screen by
moving the mouse on the desk is termed as pointing.
o Click: pressing either the left or the right button of the mouse is known as
clicking.
o Drag: Pointing to a desired location while pressing the left button.

The most popular Mouse types are:

1. Mechanical mouse:
 This type of mouse has a rubber or metal ball at its bottom and an electronic circuit
containing sensors.
 When the mouse is moved over a flat surface, the sensor detects the direction of
movement of the ball.
 The electronic circuit translates the movement into signals and feed it as input to the
computer.

2. Optical mouse:

 The optical mouse is more advanced than the mechanical mouse.


 It contain ball inside. The movement of the mouse is detected using laser technology,
by using optical sensors.

3. Cordless mouse:

 A cordless or wireless mouse is not connected to the computer. The movement of the
mouse is detected using radio waves or infrared light.
Principles of Programming using C Module 1

Track ball:

 A trackball is a pointing device that is used to control the position of the cursor on the
screen. It is usually used in notebook computers, where it is placed on the keyboard.

Fig: Trackball used in mouse

Fig: Trackball on keyboard


Principles of Programming using C Module 1

Touchpad:

 A touchpad (track pad) is a small, flat, rectangular stationary pointing device with a
sensitive surface of 1.5-2 square inches.
 The user has to slide his or her fingertips across the surface of the pad to point to a
specific object on the screen.
 The surface translates the motion and position of the users fingers to relative position on
the screen.
 There are also button around the edge of the pad that work like mouse buttons.
 Touch pad are widely used in laptops and are in built on the laptop keyboard.

Handheld device:

Joystick:

A joystick is a cursor control device widely used in computer games and computer aided design
(CAD)/computer aided manufacturing (CAM) applications.
Principles of Programming using C Module 1

Stylus:

 A stylus is a pen-shaped input device used to enter information or write on the touch
screen of a handheld device.
 It is a small stick that can also be used to draw lines on a surface as input into a device
choose an option from a menu, move the cursor to another location on the screen.

Touch screen:

 A touch screen is a display screen that can identify the occurrence and position of a touch
inside the display region.
 The user can touch the screen either by using a finger or a stylus.
 The touch screen facilitates the users to interact with what is displayed on the screen in a
straightforward manner, rather than in an indirect way by using a mouse or a touchpad.
Principles of Programming using C Module 1

Optical devices:

 Optical devices also known as data-scanning devices use light as a source of input for
detecting or recognizing different objects such as characters, marks, codes and images.
 The optical device converts these objects into digital data and sends it to the computer for
further processing

Barcode reader:

 A barcode reader is a handheld input device that ais used to capture and read information
stored in a barcode
 It consists of a scanner a decoder and a cable used to connect the reader to a computer.
 The function of the barcode reader is to capture and translate the barcode into numerical
and/or alphabets.
 It is connected to computer for further processing of the captured information.
Principles of Programming using C Module 1

Image scanner:

A scanner is a device that captures images, printed text and handwriting from different sources
such as photographic prints, posters and magazines and converts them into digital images for
editing and displaying on computer.

Optical character recognition:


Principles of Programming using C Module 1

Optical character recognition is the process of converting printed materials into text or word
processing files that can be easily edited and stored.

The steps involved in OCR include:

1. Scanning the text character by character


2. Analyzing the scanned image to translate the character images into character codes.

Optical mark reader:

 Optical mark recognition is the process of electrically extracting data from marked fields
such as checkboxes and fill in fields on printed forms.
 The optical mark reader is fed with an OMR sheet that has pen or pencil marks is pre-
defined positions to indicate each selected response marks and space and stores the
interpreted data in a computer for storage analysis and reporting
Principles of Programming using C Module 1

Magnetic ink character reader:

 Magnetic ink character reader is used tto verify the legitimacy of paper documents
especially bank checks.
 It consists of magnetic ink printed characters that can be recognized by high speed
magnetic recognition devices.

Audiovisual input devices:

Audio devices:
Principles of Programming using C Module 1

 Audio devices are used to either capture or create sound


 They enable computers to accept music, speech or sound effects for recording and /or
editing.
 Microphones and CD players are examples of two widely used audio input devices.
 A microphone feeds audio input to the compter.however the audio input must be
converted into digital data before being stored in the computer.

Video input devices:

 Video input devices are used to capture video from the outside world into the computer.
 Digital camera and webcam are the popular examples of video input device.
 A digital camera is a handheld and easily portable device used to capture images or
videos.
 The digital cameras digitalize images or video and store them on a memory card.
 Web cameras too capture videos that can be transferred via the internet in real-time.
 Web cameras are widely used for video conferencing

Digital camera
Web came
Principles of Programming using C Module 1

Output devices:

Output devices

Soft copy devices Hard copy devices

Monitor Projector Speaker Printer Plotter

Monitor: The monitor is an integral part of computer which displays both text and
graphics. The performance is measured in terms of image quality, resolution, energy
consumption.

CRT (Cathode Ray Tube) Monitors:


 Cathode ray tube is a specialized vacuum tube in which images are produced when
an electron beam strikes a phosphorescent surface.
 The smaller the pixel, the better image clarity or resolution.
 A finite number of characters can be displayed on screen at once.
 Most screens are capable of displaying 80 characters of data horizontally and 25
lines vertically.
 It is large in size and it consumes very high power.
 They usually have resolution of 640*840 pixels. They are large, heavy, energy
efficient and produce lot of heat.
Principles of Programming using C Module 1

LCD (Liquid Crystal Display) Monitors:


 Flat panel display is a thin screen display found on all portable computers and is the
new standard for desktop computers.
 Unlike monitors, flat panel displays use liquid-crystal display(LCD) or light-
emitting diode(LED)technology to make them much lighter and thinner compared
to a traditional monitor.
 The image is formed by applying voltage on crystals.
 The backlight is provided by fluorescent light. They consume less power, generate
less heat and have increased life span.

Printer: Printer is an output device that prints any data, report, document, picture, diagram
etc. Printers are classified into:
Principles of Programming using C Module 1

 Impact Printers: It produces the hardcopy of output. The impact


printers are old, noisy.But, still dot-matrix printer is in usage.

1. Dot-Matrix Printer: The print head of the dot-matrix printer has either 9 or 24
pins. When the pins fire against the ribbon, an impression is created on the paper.
The speed is 300 cps and doesn’t produce high quality output. It can produce
multiple copies.

2. Daisy-wheel Printer: It employs a wheel with separate characters distributed along


its outer edge. The wheels can be changed to obtain different set of fonts.

3. Line Printer: For heavy printing, the line printer is used. It uses a print chain
containing the characters. Hammers strike the paper to print and it rotates at the
speed of 1200 lpm. It is also noisy and is of low-quality.

 Non-Impact Printers: They are fast, quiet and of high resolution. The most
commonly used non- impact printers are laser and ink-jet printers. The thermal printer is
not discussed here because it uses heat to print on high-sensitive paper.

1. Laser Printer: It works like a photocopier and uses toner i.e. black magnetic
powder. The image is created in the form of dots and passed from drums on to the
paper. It has built-in RAM which acts as buffer and RAM to store fonts. The resolution
varies from 300 dpi to 1200 dpi and the speed is about 20 ppm.

2. Ink-jet Printer: These are affordable printers. It sprays tiny drops of ink at high
pressure as it moves along the paper. The separate cartridges are available for
different colors. The resolution is about 300 dpi, can print1 to 6 pages/min.
Principles of Programming using C Module 1

Printer:
Printers

Impact printer Non impact printer

Dot matrix Daisy wheel Line Inkjet Laser Thermal

Plotters: The plotter can make drawings. It uses one or more automated pens. The
commands are taken from special file called vector graphic files. Depending on type of
plotter either paper or pen moves. It can handle large paper sizes and it is used for
creative drawings like buildings and machines. They are slow and expensive.

Speakers: The speaker is an output device which produces the voice or sound waves.
Principles of Programming using C Module 1

Projector: The projector is the by-pass device for the monitor where it produces larger
images or pictures

Applications of computers:

Internet:

 The internet is a network of networks that connects computers all over the world.
 It gives users access to an enormous amount of information, much more than available in
any library.
 Government:
Principles of Programming using C Module 1

 Computes are used in government organization to keep records on legislative, internal


revenue service records etc..

Music:

 All computers today have musical instrument digital interface facility, which links
musical instruments to a PC, thereby enabling the computers to generate a variety of
sounds.

Movies:

Computers are used to create sets, special effects, animations, cartoons, imaginary characters,
videos and commercials.

Travel and tourism:

Computers are used to prepare tickets, monitor the trains or airplanes route and guide the plane
to a safe landing.

Business and industry:

In business and industry, computers are used and mainly for data processing, which includes
tasks such as word processing, analyzing data entering records, pay roll processing, personnel
record keeping and inventory management.

Hospitals:

Hospitals use computers to records every information about patients from the time of their
admission till their exit.

Weather forecasting:

When computers are fed with mathematical equations along with data about air pressure,
temperature, humidity, and other values, the solution of these equations given an accurate
prediction of the weather of a particular area.

Online banking:

Industry and engineering:

Introduction to C programming:

 C is a general purpose, structured programming language. It bridges the gap between


machine language and conventional high level languages. This flexibility allows c
language to be used for system programming as well as for applications programming.
Principles of Programming using C Module 1

 C is a powerful language that provides fast program execution. Its power and fast
program execution come from its ability to access low commands similar to assembly
language but with high level syntax.

History of C language:

 The c programming language was developed by Dennis Ritchie at AT&T bell


laboratories in the early 1970s.He was also known as father of c language.
 Like many other modern language C is derived from ALGOL.ALGOL was not accepted
widely in the United States but it was widely used in Europe.
 In 1967 Martin Richard developed a language called BCPL.
 BCPL was basically a type-less language which facilitated the user with direct access of
memory.
 1970 ken Thompson developed a language called B. B was used to develop the first
version of UNIX
 C was developed by Dennis Ritchie in 1972 that took concept from ALGOL, BCPL and
B.
 In addition to the concepts of these languages, c also supports the concept of data types.
 C was documented and popularized in the book the c programming language by Brain W
Kernighan and Dennis Ritchie in 1978.This book was so popular that the language came
to be known as “k & RC”.
 In 1983 the American national standards institute started working on defining the
standard for C. In December 1989 and came to be known as ANSI C.
 In 1990 the international standard organization adopted the ANSI standard. This version
of C came to be known as C89.
 In 1995 some minor changes were made to C89 the new modified version was known as
C95.
 In 1990 C++ and java became popular programming languages among the users so the
standardization committee of C felt that a few features of C++/java if added to C would
enhance its usefulness. So in 1999 when some significant changes were made to C95, the
modified version came to be known as C99.
Principles of Programming using C Module 1

Features of C language:

Simple robust:

C is a general purpose, structured programming language. It has the simplicity of a high level
language as well as the power of low level language. This aspect of c makes it suitable for
writing both application programs and system programs. Hence it is an excellent, efficient and
general purpose language for most of the application such as mathematical, scientific, business
and system software applications.

Small in size and power full:


Principles of Programming using C Module 1

C languages consist of 32 English words known as keywords. The power c lies in a rich set of
built in library functions and variety of operation.

Modularity:

C programs are modular in nature it supports compartmentalization of code and data. This
capability makes it very easy for c-programs to share sections of code. We can divide c programs
to several modules that will combine in a single module to build the final program.

Extensibility:

The c language is extensible since it allows the users to add their own library function to the
library function .section of code or modules can be stored in libraries for re-use them in future.

Portability:

C program written on one computer or operating system can be compiled and run on another
computer or operating system with little or no modification.

Efficient and fast:

The compilation and execution of c programs are faster than modern programming languages.
This is due to its variety of data type and powerful operators.

Efficient Use of pointers:

C supports efficient use of pointer. Pointers have direct access to memory.

Bit manipulation:

It provides wide variety of bitwise operators to manage data at bit level. We can perform
different operations at bit level

STRUCTURE OF ‘C’ PROGRAM

 C program consists of different sections. The structure of C program is as shown below.

 Documentation Section: It consists of a set of comment line giving the name of the
program, theauthor and other details. Compiler ignores these comments. Comment is a
short description of the problem to be solved which is written at beginning of each
program. There are two formats:

a) Line comments //Single line comment: it uses two slashes(//)


Principles of Programming using C Module 1

Syntax: //

Example:// The program takes the 2 values

b) Block comments /*Multi line comment*/: It is used when comments as several line.

Syntax: (opening token symbol)/* */(closing token symbol)

Example:/*the program takes the 2 values*/

 Link Section: It provides instruction to the compiler to link functions from system
library.

Every c program is made of one or more pre-processor commands.

#include<stdio.h>

 They come in the beginning of the program


 All preprocessor commands start with hash (#) symbol.
 The preprocessor command tells the compiler to include the standard input /output
library.
 Stdio we need library file to print a message to a terminal.
 #include<stdio.h>tells the compiler that we will be using parts of the standard function
library.
 .hheader file
 <>tell the compiler the exact location of the header file.

#include<conio.h>

 Console input output header file.


 It consist of function clrscr(),getch().

#include<math.h>:

 Consist of mathematical functions like pow(),sqrt(),fabs() etc..

 Definition Section: It defines all symbolic constants.


 Global Declaration Section: variable type that is declared outside any function
and is accessible to all functions through the program.
 main( ) Section:
 It is the starting point of the program.
Principles of Programming using C Module 1

 Main is a user defined function.


 Void: void is data type, void has no values and no operations, which
is returned to operating system to indicate end of the program.
 The function main starts with open braces {and terminates with a
close braces}.

Every ‘C’ program has one main ( ) function and it contains two parts:

i. Declaration Part: Here, all the variables used are declared.


ii. Executable Part: Here, it contains at least one executable statement.

Sub-program Section: It contains all the user defined functions that are called by main( )
function.

Documentation section
Link section
Definition section
Global declaration section
main( ) Section
{
Declaration part
Executable part
}
Sub program section
Function 1
Function 2
-
-
Function n
Principles of Programming using C Module 1

Example:

//Addition of two numbers


#include<stdio.h>
void main( )
{
int a, b, sum;
printf(“Enter two numbers\n”);
scanf(“%d%d”, &a, &b);
sum = a + b;
printf(“The sum is %d”, sum);
}

Compiling and executing a C program:

The program development cycle has following four steps:

1. Writing the source code:

The c program is written using vi editor or text editor .Most language compilers have their
built-in editors. The program or source code should be written in the syntax provided by that
language. In c a source code file has extensions.

2. Compiling the source code (compilation):


The second stage a compilation contains the steps preprocessing, compilation and
assembling code. In preprocessing source code is passed to preprocessor. Preprocessor
removes comments, expand macros and do expansion of the included files. The source
code is compiled using compiler. The compiler checks the syntax errors in the source
code. It produce an intermediate compiled output file .obj .
3. Linking the object code (linking):

Linker assembles the entire libraries which are necessary for the program but existing
somewhere else. The system uses object code as input and translates it to executable code.

4. Executing programs(file.exe) :
Principles of Programming using C Module 1

Executable file is ready to run file. Loader loads the executable file into the main/primary
memory and run program.

Writing a Editor source code Compiler object code Linker executable

program using

Compilers:

 A compiler is a program that translate the instruction of high level language (source code)
into machine language (object language)
 A compiler is a program or set of programs that converts source code written in a high
level language to low level language (assembly language or machine language)
 A programming language can have many compilers. For example, GCC,turbo C,Quick C
etc.. are different compilers for C programming language.

Need of computer:

 A computer understands only binary language and executes instructions coded in binary
language. It cannot execute a single instruction given in any other form. Therefore we
must provide instructions to the computer in binary language.
 Means we must write computer programs entirely in binary language and it is not
possible to write the program in binary language.’
 So there was a need of a translator that translates the compiler instructions given in
English language to binary language.

Difference between compiler and interpreter:

Compiler interpreter
Compiler takes entire program as input Interpreter takes single instructions as input
Compiler generates object code Interpreter is not generates object code.
No source code is required in executable file Source code is required in executable file
Programming language like C,C++,C# uses Programming language like python ,ruby uses
compilers interpreters.
Compiler translates entire program at a time Interpreter translates program line by line
Principles of Programming using C Module 1

Character set: A character set include.

 Letter: A-Z,a-z
 Digits: All decimal digit 0-9.
 Special characters: . ; : , $ # ^&-+=
 Whitespace: enter space, horizontal tab.

C tokens:

Tokens: token are the smallest unit of a program which are made up of one or more characters.

There are 5 different types of tokens as follows:

1. keywords:
 Keywords are meant for some special purpose called as reserved words.
 User cannot change the meaning of keywords.

Example:

Auto double int struct break do


Else Long switch goto sizeof if
Case enum register typedef default
Char extern return union volatile
const Float short unsigned while
continue for signed void static

Identifiers:

 Identifiers are the names given to the elements of the program such as variable name ,
name or a function name.
 An identifier is a word consisting of sequence of one or more letters or digits or a special
symbol called as _(underscore)

Rules:

 The rules to write an identifier are as follows:


1. It must contain only alphabets (A to Z, a to z), numbers (0 to 9) and underscore ( _
).
2. It must start with alphabet or underscore but not numeric character.
3. It should not contain any special symbols apart from underscore.
Principles of Programming using C Module 1

4. It should not have any space.


5. Keywords cannot be used as identifiers.
6. Identifiers are case sensitive.
7. Maximum length of an identifier is 31 characters.
 Examples:
Valid Identifiers: integer, minimum, sum_total, row1, _cpps
Invalid Identifiers: float  It is a keyword.
I am  It has space
123_Abc  It is starting with number N1 + n2. It contains
special symbol (+)

Constants:

Constants are the data values that cannot be change during the execution of a program.

Operators:

It is a symbol that indicates the operation to be performed.

Special symbol:

Special symbols have some special meaning in the c programming language for the compiler and
it is used to perform some special function\task in c.

Example: {,}, [,], &,*.

Data types:

 Data types: Data type defines the types of data that is stored in a variable.
 There are 3 types:
i. Primary/ Built-in/ Fundamental data type int , float, double, char, void
ii. Derived data type  array, structure
iii. User defines data type  enum , type def

 Primary/ Built-in/ Fundamental data type: These are the built in data types
available in C. There are5 types. Namely:
1. Integer (int)
2. Floating point (float)
Principles of Programming using C Module 1

3. Character (char)
4. Double precision floating point (double)
5. Void (void)
1. Integer type:
 It is used to store whole numbers.
 The keyword int is used to declare variable of integer type.
 Int type takes 2B or 4B of storage depending on machine size.
 The classes of integer are:

Unsigne Signe
d d
Data type Keyword Size Data type Keyword Size
Short Integer short int 1B Signed short integer signed short int 1B
Integer int 2B Signed integer signed int 2B
Long Integer long int 4B Long integer long int 4B

2. Floating point data type:


 It is used to store decimal numbers.
 The keyword float is used to declare variable of floating point data type.
 Float type takes 4B or 8B of storage depending on machine size.
 It can be expressed in fractional or exponential form.

Data type Keyword Size


Floating point Float 4B
Double Double 8B
Long double long double 10B

3. Double:
 It is used to store double precision floating point numbers.
 The keyword double is used to declare variable of floating point data type.
 Double type takes 8B of storage.
 Double precision is related to accuracy of data.
Principles of Programming using C Module 1

4. Char:
 It is used to store character type of data.
 The keyword char is used to declare variable of character type.
 Char type takes 1B of storage.

5. Void:
 It is a special data type that has no value.
 It doesn’t have size.
 It is used for returning the result of function that returns nothing.

Variable A variable is a name given to memory location whose value changes during
execution.

Types of variables:

1. Numeric variable: numeric variable can be used to store either integer values or floating
point values.

2. Character variable:
Character variable can include any letter from the alphabet or from the ASCII chart and
number from 0-9 that are given within single quotes.

Declaration:

Syntax: datatype variable_list;

Where,
datatype Any built in data type
variable_list Identifiers that specifies variable name.

Example: int a;

Where,
int is the built in data type
a is variable of type int

Rules for declaring/naming variables:


Principles of Programming using C Module 1

1. Name should only consist of alphabets (both upper and lower case ),digits and underscore
sign.
2. First characters should be alphabet or underscore.
3. Name should not be a keyword.
4. Since C is a case sensitive the upper case and lower case considered differently for
example code ,Code<CODE etc..Are different identifiers.
5. As C defined up to 32 significant characters can be used and will be considered
significant by most compilers. If more than 32 are used they will be ignored by the
compiler.
6. No white space is used.

Initialization:
Syntax: datatype variable_name = value;
Where,
datatype  Any built in data type
variable_name  Identifier that specifies variable name.
value The data which will be stored in variable name.

Example: int a = 5;

Where,
int  data type
a  variable name
5 value

NOTE: Refer class notes for example program

Difference between variable and identifiers:

Identifiers Variables
All identifiers are not variables All variables are identifiers
Identifiers may not have any All variables have memory.
memory unless it is a variable
Mentioning the type of an Type of the variable must be
identifier it not needed unless it is defined.
a variable

Constants:
 Constants or a literal refers to fixed values that do not change during the execution of a
program.
Principles of Programming using C Module 1

 Constants are treated just like regular variables except that their values cannot be
modified after their definition.
 C supports several types of constants as shown

Numeric constants:
Numeric constants consist of numeric digits they may or may not have decimal point.
The rules for defining numeric constants are as follows:
1. Numeric constant should have at least one digit.
2. No comma or space is allowed within the numeric constant.
3. Numeric constant can either be positive or negative but default sign is always positive.
4. The value of a constant cannot exceed specified minimum and maximum bounds.
There are two types of numeric constants namely, integer constant floating point constant.
1. Integer constants:
Integer constants are whole numbers without any fractional part or decimal point. It must have at
least one digit and may contain either + or – sign. A number with no sign is assumed to be
positive.
There are 3 types of integer constants:
1. Decimal integer constants (base 10):

 Decimal integer constant consists of a set of digits 0 to 9 preceded by an optional + or –


sign. If the constant contains two or more digits, the first digit must be something other
than 0.
Example: 0, 1, 743, 5233, 9999, 6000
2. Octal integer constants (base 8):

 Octal integer constant is always preceded with 0 and consists of 0 to 7 digits.


Example:
0,01,0743
3. Hexadecimal integer constants (base 16):
 Hexadecimal integer constant is always preceded by 0x or 0X. It consist of digits 0-9 and
letters A-F or a-f. Note that the letters although f represents the quantities 10 through 15
respectively.
Example:
Ox, 0x1, 0x7fff
4. Floating point constant or real constant:
 A floating point constant is a base 10 number that contains either a decimal point or an
exponent or both. It may also have either + or – sign preceding it.
Example: 0.05,-0. 09 , 33. 44

 The interpretation of a floating point constant with an exponent is essentially the same as
scientific notation, except that the base 10 is replaced by the letter E or e.
Principles of Programming using C Module 1

 Thus the number 1.2X 10^-3 would be written as 1.2E-3 or 1.2e-3.

2. Character constants:
A character constant contains one single character enclosed within single quotes (‘’)
Example:
‘a’, ’Z’,’\n’
3. String constants:
String constant are sequence of character enclosed within double quotes (‘””).
Example:
“hello”,”1234”,”abc”,”\n”

Declaring constants:

1. To declare a constant precede the normal variable declaration with const keyword and
assign it a value.
Syntax: const data type constant value
Example: const float Pi=3.142
2. Another way is to use the preprocessor command define.
Syntax: #define const value
Example:
#define PI 3.142

NOTE: Refer class notes for example program

Constants

Numeric constant Character constant String constant

Integer Floating point

Decimal Octal Hexadecimal


Principles of Programming using C Module 1

Files used in C program:

Files used in C

Source file Header file Object file Executable file

Source file:

 The source code file contains the source code of the program.
 The file extension of any C source code file is ‘.c’. This file contains C source code that
define the main function and may be other functions. The main () function is the starting
point of execution when you successfully compile and run the program.

Header file:

 When working with large projects, it is often desirable to separate out certain sub routines
from the main () of the program.
 There also may be a case that the same subroutine has to be used in different programs. In
the latter case one option is to copy the code of the desired subroutine from one program
to another. However copying the code is often tedious as well as error-prone and makes
maintainability more difficult.

Example:

Stdio.h, stdlib.h, conio.h

Object file:

Object files are generated by the compiler as a result of processing the source code file. They
contain compact binary code of the function definition. Linker uses this object file to produce an
executable file (.exe file) by combining the object files together. Object file have a ‘.o’
extension.

Input and output statements/Formatted I/O statements:

Input function: scanf():

We use the scanf() function for getting the formatted inputs or standard inputs so that the printf ()
function can provide the program with numerous options of conversion.
Principles of Programming using C Module 1

Syntax for scanf()


scanf (format_specifier, &data_a, &data_b,……); // Here, & refers to the address operator
The purpose of the scanf() function is to read the characters that we get from the standard input,
convert them according to the string of format specification, and then store the available inputs in
the memory slots that the other arguments represent
Example:

scanf(“%d %c”, &info_a,&info_b);

Output statement: Printf():

We use this function for displaying a single or multiple values in the form of output for the user
end at the console.

Syntax:

printf (format_specifiers, variable, variable, ......... );

Example:

printf(“%d %c”, info_a, info_b);

Program to illustrate input and output statements:

#include <stdio.h>

int main()
{
int i;
float f;
char c;

printf("Enter an integer and a float, then Y or N\n> ");


scanf("%d%f%c", &i, &f, &c);

printf("You entered:\n");
printf("i = %d, f = %f, c = %c\n", i, f, c);

You might also like