Popc Module 01 Notes With Question Bank
Popc Module 01 Notes With Question Bank
MODULE 01
INRODUCTION TO C
1.1 Introduction
Computers have become the basic necessity of any organization with serious
objectives. They have made great inroads in everyone‘s everyday life and thinking. They are
used in engineering simulations, teaching, satellite control weather forecasting, etc. Literally,
there is no field left without the use of computers. Types of computers, characteristics, details
about software, input and output devices and role of computers in network is dealt in this
particular topic.
Engine made by Babbage. A programming language (Ada) has been named after
her. In 1847-49, Babbage designed his second version of Difference Engine but he could not
complete it. The same machine was conceived in 1991 by the Science Museum in Kensington,
England, and it worked!
In 185, George Bool developed Boolean Logic, which is a system for symbolic and
logical reasoning and the basis for computer design. In 1890, computations for the US Census
were carried out by Herman Hollerith‘s punched card machine. Hollerith started Tabulating
Company, which eventually became IBM.
Before the 20th century, all the machines were mechanical. With the advent of
vacuum tubes, the electronics era of computers started. In 1944, Harvard Mark I, the first large
scale, automatic, general purpose, electromechanical calculator driven by a paper tape
containing the instructions was constructed. In the second version of this machine, Grace
Hopper, found the first computer bug, a bug beaten to death in the jaws of a relay, which she
glued into the logbook. The logbook is now in the National Museum of American History of
the Smithsonian.
The first electronic computer, ENIAC (Electronic Numerical Integrator And
Computer) was built in 1946. It was programmed through rewiring between the various
components. After this, many machines like EDVAC, IBM 701, 704 were built and some of
them were commercially successful at that time because they used transistors in place of
vacuum tubes thereby increasing the reliability and performance.
Functional Elements
A typical digital computer system has four basic functional elements: (1) input-
output equipment,
(2) main memory, (3) control unit, and (4) arithmetic logic unit. Any of a
number of devices is used to enter data and program instructions into a computer and to gain
access to the results of the processing operation. Common input devices include keyboards and
optical scanners; output devices include printers and monitors. The information received by a
computer from its input unit is stored in the main memory or, if not for immediate use, in an
auxiliary storage device. The control unit selects and calls up instructions from the memory in
appropriate sequence and relays the proper commands to the appropriate unit. It also
synchronizes the varied operating speeds of the input and output devices to that of the
arithmetic logic unit (ALU) so as to ensure the proper movement of data through the entire
computer system. The ALU performs the arithmetic and logic algorithms selected to process
the incoming data at extremely high speeds—in many cases in nanoseconds (billionths of a
second). The main memory, control unit and ALU, together make up the Central Processing
Unit (CPU) of most digital computer systems, while the input-output devices and auxiliary
storage units constitute peripheral equipment.
Analog Computers
An analog computer is a computer which is used to process analog data. Analog
computers store data in a continuous form of physical quantities and perform calculations
with the help of measures. It is quite different from the digital computer, which makes use of
symbolic numbers to represent results. Analog computers are excellent for situations which
require data to be measured directly without converting into numerals or codes. Analog
computers, although available and used in industrial and scientific applications like control
systems and aircraft, have been largely replaced by digital computers due to the wide range of
complexities involved.
Analog computers were the earliest computer machines developed and were among
the most complicated machines for analog computation and process control. Analog data is not
discrete, but rather is of a continuous nature. Examples of such data are pressure, temperature,
voltage, speed and weight. An analog computer makes use of continuous values and not
discrete values. Because of this, processes with an analog computer cannot be repeated for
exact equivalent results. Unlike digital computers, analog computers are immune to
quantization noise. Some of the common computing elements found in analog computers are
function generators, integrators, comparators and multipliers. Depending on the application,
other specialized components can also be used. Programming on an analog computer involves
Hybrid Computer
A hybrid is a combination of digital and analog computers. It combines the best
features of both types of computers, i.e., it has the speed of analog computer and the memory
and accuracy of digital computer. Hybrid computers are used mainly in specialized applications
where both kinds of data need to be processed. Therefore, they help the user to process both
continuous and discrete data. For example, a petrol pump contains a processor that converts
fuel flow measurements into quantity and price values. In hospital Intensive Care Unit (ICU),
an analog device is used which measures patient‘s blood pressure and temperature, etc., which
are then converted and displayed in the form of digits. Hybrid computers, for example, are used
for scientific calculations, in defense and radar systems.
Applications of Supercomputer
1. Used to forecast the weather and global climates
2. Used in military research and defence systems
3. In automobile, aircraft and space craft designing
4. In encrypting and decoding sensitive intelligence information
5. Used in seismography, plasma and nuclear research
6. Protein folding analysis
7. Study of DNA structure and gene engineering
8. Digital film rendering
Mainframe Computer
Although Mainframes are not as powerful as
supercomputers, but certainly they are quite expensive nonetheless,
and many large firms and government organizations uses
mainframes to run their business operations. The Mainframe
computers can be accommodated in large air- conditioned rooms
because of its size. Supercomputers are the fastest computers with
large data storage capacity, Mainframes can also process and store
large amount of data. Banks educational institutions and insurance
companies use mainframe computers to store data about their
customers, students and insurance policy holders.
Minicomputer
Minicomputers are used by small businesses and firms. Minicomputers are also
called as ―Mid- range Computers‖. These are small machines and can be accommodated on a
disk with not as processing and data storage capabilities as supercomputers and mainframes.
These computers are not designed for a single user. Individual departments of a large company
or organizations use Minicomputers for specific purposes. For example, a production
department can use Minicomputersfor monitoring certain production process.
Popular Minicomputers
� K-202
Microcomputer
Desktop computers, laptops, personal digital assistants (PDAs), tablets and
smartphones are all types of microcomputers. The microcomputers are widely used and the
fastest growing computers. These computers are the cheapest among the other three types of
computers. The Microcomputers are specially designed for general usage like entertainment,
education and work purposes. Well-known manufacturers of Microcomputers are Dell, Apple,
Samsung, Sony and Toshiba.
Desktop computers, Gaming consoles, Sound and Navigation system of a car,
Netbooks, Notebooks, PDAs, Tablet PCs, Smartphones, Calculators are all type of
Microcomputer.
CPU
Memory
Unit
Input Output
Unit Control Unit
Unit
Arithmeticand
Logic Unit
Input device accepts the coded information as source program i.e. high level
language. This is either stored in the memory or immediately used by the
processor to perform the desired operations.
Finally the results are sent to the outside world through output device. All of
these actions are coordinated by the control unit.
Memory unit: - Its function into store programs and data. It is basically to two
types 1. Primary memory 2. Secondary memory 1. Primary memory: - Is the
one exclusively associated with the processor and operates at the electronics
speeds programs must be stored in this memory while they are being
executed.
Number of bits in each word is called word length of the computer. Programs
must reside in the memory during execution.
Arithmetic logic unit (ALU):- Most of the computer operators are executed in
ALU of the processor like addition, subtraction, division, multiplication, etc.
the operands are brought into the ALU from memory and stored in high speed
storage elements called register.
The control and the ALU are may times faster than other devices connected
to a computer system.
Output unit:- These actually are the counterparts of input unit. Its basic
function is to send the processed results to the outside world.
Overview of C
INTRODUCTION
A program is a set of instructions for a computer to perform a specified task. C is
a high level language.
Programs written in C are easy to create, read and understand. C is a general
purpose programming
A language which was developed by Dennis Ritchie at Bell Lab in 1972.
Preprocessor directives
Comment lines
Global Declarations
void main()
{
declaration section
statements
input statement
operational statements
output ststements
}
Preprocessor Directives
• The preprocessor accepts the source program and prepare the
source program for compilation.
• The preprocessor-statements start with symbol #.
• The normal preprocessor used in all programs is include.
• The #include directive instructs the preprocessor to include the
specified file-contents in the beginning of the program.
• For ex:
#include<stdio.h>
Global Declarations
Variables which are declared globally can be used by or
accessible from any function present
in a program.
main()
Function is a set of statements which performs a specific task.
• Every C program should have a function called as main().
• This the first function to be executed always.
• The statements enclosed within left and right brace is called
body of the function. The main() function is divided into 2 parts:
1) Declaration Section
• The variables that are used within the function main() should be
declared in the declaration-section only.
• The variables declared inside a function are
called local-variables. Ex: int a,b,c;
2) Executable Section
• This contains the instructions given to the computer to perform a
specific task.
• The task may be to
→ display a message
→ read data
• Comments are portions of the code ignored by the compiler. The
comments allow the user to make simple notes in the source-code.
// this is an example for single line comment
/* this is an example
for multiple line
comment */
Example:
// C program to add two numbers
#include<stdio.h>
void main()
{
int a, b, c;
In the same way, C is a language used to communicate with computer. In other words,
C is used to instruct computer to perform particular task.
CHARACTER SET
Character-set refers to the set of alphabets, letters and some special characters that are
valid in C language.
C TOKENS
A token is a smallest element of a C program.
One or more characters are grouped in sequence to form meaningful words. These
meaningful words are called tokens.
KEYWORDS
A token which have predefined meaning in C language is called a keyword.
• Keywords are tokens which are used for their intended purpose only.
• Keywords are the instructions given to the Compiler.
• Each keyword has fixed meaning and that cannot be changed by
user. Hence, they are also called reserved-words.
• There are 32 keywords . int, float, char, double, void, case,
switch, if, else, elseif, return, auto, register, static, const, for, while, do-while,
default etc.
IDENTIFIERS
1. num
Valid identifier
2. a_one
valid identifier as first character begins with letter
3. 2sum
Invalid Identifier because first character begins with digit
4. $sum
Invalid because no extra symbols are allowed other than under score
5. for
Invalid as for is a keyword
6. area length
Invalid because no spaces are allowed
Header Files
They have an extension '.h'. They contain the C function declarations and
macro definitions that are shared between various source files.
2. At times the programmer may want to change or add the subroutines and reflect those
changes in all the programs. For doing this, he will have to only change the source file
for the subroutines, recompile the source code and then recompile and re-link the
program.This tells us that including a header file will make it easier at all levels of the
program. If we need to modify anything then changes are made only in the subroutines
after which all the changes will be reflected.
Object files
They are the files that are generated by the compiler as the source code file is processed.
These files generally contain the binary code of the function definitions.
The object file is used by the linker for producing an executable file for combining the
object files together. It has a '.o' extension.
Executable file
Various object files are linked by the linker for producing a binary file which will be executed
directly.They have an '.exe' extension.
Compilers in C
A compiler that supports the source programming language reads the files, analyzes
the code, and translates it into a format suitable for the target platform.
Compilers that translate source code to machine code target specific operating
systems and computer architectures.
This type of output is sometimes referred to as object code (which is not related
to object-oriented programming).
For example, a compiler might output machine code for the Linux x64 platform or
Linux ARM 64-bit platform
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
Example:
#include<stdio.h>
void main()
{
printf(―Welcome to C \n‖);
printf (―Hello\tWorld\n‖);
}
Output:
Welcome to C
Hello World
Data types
C supports 5 primary data types:
1) int
• An int is a keyword which is used to define integers.
• Using int keyword, the programmer can inform the compiler that
the data associated with this keyword should be treated as integer.
• C supports 3 different sizes of integer:
→ short int
→ int
→ long int
2) float
• A float is a keyword which is used to define floating point numbers.
3) double
• A double is a keyword used to define long floating point numbers.
4) char
• A char is a keyword which is used to define single character.
5) void
• void is an empty data type. Since no value is associated with this
data type, it does not occupy any space in the memory.
• This is normally used in functions to indicate that the function does not
return any value.
VARIABLES
A variable is an identifier whose value can be changed during execution of the program.
In other words, a variable is a name given to a memory-location where the
data can be stored.
• Using the variable-name, the data can be
→ stored in a memory-location and
→ accessed or manipulated
Examples:
Valid variables:
A11
Aaa
A_5
abcd
principle_amount,
sum_of_digits
Invalid
variables:
3fact //violates rule 1
sum= sum-of-digits 62$ //violates rule 3
for int if //violates rule 2
Declaration of Variable
• The declaration tells the complier
→ what is the name of the variable used
→ what type of date is held by the variable
Datatype v1,v2..
int a,b;
char ch1, ch2;
CONSTANTS
A constant is an identifier whose value remains fixed throughout the execution of the
program.
• The constants cannot be modified in the program.
• For example:
1, 3.14512,‘a‘
b. Decimal constants (0 1 2 3 4 5 6 7 8 9)
Combination of digits from 0-9 is called decimal constant.
For ex: 0, -9, 22
c. Octal constants (0 1 2 3 4 5 6 7)
Combination of digits with 0-7 with prefix 0 is called Octal
constant.
For ex: 021, 077, 033
d. Hexadecimal constants (0 1 2 3 4
5 6 7 8 9 A B C D E F)
Combination of digits
with 0-9 and then followed with A-F.
3. Character Constant
• A symbol enclosed within a pair of single quotes(' ‗) is called a
character constant.
• Each character is associated with a unique value called an ASCII
(American Standard Code for Information Interchange) code.
• For ex:
'9', 'a', '\n'
4. String Constant
• A sequence of characters enclosed within a pair of double quotes(―)
is called a string constant.
• The string always ends with NULL (denoted by \0) character.
• For ex:
"9" "a" "hello" "\n‖
INPUT/OUTPUT STATEMENTS
stdin: This file is used to receive the input (usually is keyborad file, but can
also take input from the disk file).
stdout: This file is used to send or direct the output (usually is a monitor
file, but can also send the output to a disk file or any other device).
stderr: This file is used to display or store error messages.
Input and Output statement are used to read and write the data in C programming. These
are embedded in stdio.h (standard Input/Output header file).
Input means to provide the program with some data to be used in the program and
Output means to display data on screen or write the data to a printer or a file.C
programming language provides many built-in functions to read any given input and to
display data on screen when there is a need to output the result.
There are mainly two of Input/Output functions are used for this purpose. These
are discussed as:
getchar()
putchar()
gets()
puts()
getch()
getche()
getchar()
This function is an Input function. It is used for reading a single character from the
keyboard. It is a buffered function. Buffered functions get the input from the keyboard and
store it in the memory buffer temporally until you press the Enter key.
putchar()
A simple program is written as below, which will read a single character using
getchar() function and display inputted data using putchar() function:
/*Program illustrate the use of getchar() and putchar() functions*/
#include <stdio.h>
main()
{
char n;
n = getchar();
putchar(n);
}
gets()
This function is an input function. It is used to read a string from the keyboard. It is also a
buffered function. It will read a string when you type the string from the keyboard and
press the Enter key from the keyboard. It will mark null character (‗\0‘) in the memory at
the end of the string when you press the enter key. The general syntax is as:
gets(v);
puts()
This is an output function. It is used to display a string inputted by gets() function. It is also
used to display a text (message) on the screen for program simplicity. This function
appends a newline (―\n‖) character to the output.
or
puts("text line");
getch()
This is also an input function. This is used to read a single character from the keyboard like
getchar() function. But getchar() function is a buffered is function, getchar() function is a
non-buffered function. The character data read by this function is directly assigned to a
variable rather it goes to the memory buffer, the character data is directly assigned to a
variable without the need to press the Enter key.
Another use of this function is to maintain the output on the screen till you have
not press the Enter Key. The general syntax is as:
v = getch();
getche()
All are same as getch(0 function execpt it is an echoed function. It means when
you type the character data from the keyboard it will visible on the screen. The
general syntax is as:
v = getche();
Formatted I/O functions which refers to an Input or Ouput data that has been
arranged in a particular format. There are mainly two formatted I/O functions
discussed as follows:
scanf()
printf()
scanf()
The scanf() function is an input function. It used to read the mixed type of data
from keyboard. You can read integer, float and character data by using its
control codes or format codes. The general syntax is as:
scanf("control strings",arg1,arg2,..............argn);
or
scanf("control strings",&v1,&v2,&v3,................&vn);
%s To read a string
%[^] To read string of words which are not from the defined range
Example Program:
/*Program to illustrate the use of formatted code by using the formatted
scanf() function */
#include <stdio.h>
main()
{
char n,name[20];
int abc;
float xyz;
printf("Enter the single character, name, integer data and real value");
scanf("\n%c%s%d%f", &n,name,&abc,&xyz);
getch();
}
printf()
This ia an output function. It is used to display a text message and to display the
mixed type (int, float, char) of data on screen. The general syntax is as:
printf("control strings",&v1,&v2,&v3,................&vn);
or
printf("Message line or text line");
The control strings use some printf() format codes or format specifiers or
conversion characters. These all are discussed in the below table as:
%s To read a string
Example Program:
/*Below the program which show the use of printf() function*/
#include <stdio.h>
main()
{
int a;
float b;
char c;
printf("Enter the mixed type of data");
scanf("%d",%f,%c",&a,&b,&c);
getch();
}
QUESTION BANK
1. Define Computer. Explain the organization of basic
computer model with a neat diagram.
2. Explain the generation of computers
3. List and explain the types of computers
4. List and explain any 4 input and output devices.
5. With a suitable example, Explain the basic structure of C
program
6. What are the basic data types available in C.
7. Define the variable, Explain the rules to declare a variable
with an example.
8. Write a c program
i. Find the area of circle
ii. Area of rectangle
iii. Area of square
iv. Area of parallelogram
v. Area of Triagle
vi. Volume of a sphere
vii. Newtons law of motion