Module 1 Pps Updated
Module 1 Pps Updated
Module-1
Introduction to programming
Computer:It is a machine made of electronic devices that enable user to enter data(Input),
process it(CPU) and store it in a computer memory.
The word “computer” is derived from the word “compute”.
The electronic device is known as “Hardware” and the set of instructions is known as
“Software”.
A. Input Devices. B. Central Processing Unit ( CPU ). C. Primary Storage. D. Output Devices.
E. Auxiliary Storage Devices.
Input Devices: The input device is usually a keyboard where programs and data are entered in to
the computer. Other Input Devices : a touch screen , a mouse , a pen , an audio input unit.
2
Central Processing Unit ( CPU ): It is responsible for executing instructions such as arithmetic
calculations , comparisons among data and movement of data inside the system. Today’s
computers may have one or more CPU’s.
Primary Storage: It is also known as main memory . It is a place where the programs and data is
stored temporarily during processing. The Data in primary storage is erased , when we turn off a
personal computer or when we log off from a time-sharing computer ( volatile ).
Output Devices: The output device is usually a monitor or a printer to show output. If the output
is shown on the monitor, it is a soft copy and if the output is printed on the printer, it is a hard
copy.
Auxiliary Storage Devices ( secondary storage devices ): It is used for both input and output. It
is also known as secondary storage. It is a place where the programs and data are stored
permanently. When we turn off the computer the programs and data remain in the secondary
storage, ready for the next time when we need them.
Supercomputers are exceedingly large and highly expensive. It can only fit in large, air-
conditioned spaces.
Supercomputers are used for a range of tasks, such as space exploration, seismic research,
and the testing of nuclear weapons.
Supercomputer Features:
They make use of AI (Artificial intelligence)
They are the fastest and strongest;
They are very costly.
They are enormous in size.
They are employed by companies that manufacture goods.
They process information at a rapid rate.
2. Mainframe Computers
Despite being less efficient than supercomputers, mainframe computers are nevertheless
extremely expensive.
Large corporations and governmental organizations frequently employ mainframe
computers to run everyday operations.
4
They have the ability to store and analyze a lot of data. To maintain information on their
customers, students, and insurance policyholders, banks, colleges, and insurance
companies utilize them.
They may also act as a server in a network environment.
Hundreds of users may be managed simultaneously by them.
3. Minicomputers
Minicomputers are used by small businesses and industries. They go by the term
"Midrange Computers."
These minicomputers frequently have several users, just as mainframe computers. They
are a bit slower than mainframe computers.
For example, the manufacturing department may employ minicomputers to keep an eye
on specific production processes.
Features of Minicomputers:
It is smaller than mainframes or supercomputers in terms of size.
In comparison to a mainframe or supercomputer, it is less costly.
5
Features of Microcomputers:
They are extensively employed for personal usage.
They are smaller and comparably less expensive.
Multi-user functionality is not supported.
It has a limited computational capacity.
They are quite simple to use.
Based on Capacity
According to fundamental operating principles, there are three different kinds of computers.
They are as follows:
6
1. Analogous Computers
Analog computers process analog data. Temperature, pressure, weight, depth, and voltage
are a few examples of this type of data.
These have an infinite range of values and are continuous quantities.
The first computers were analog, and they laid the groundwork for today's digital
computers.
2. Digital Computers
In digital computers, letters, numbers, and other special symbols are represented by
digits.
On-off (ON-OFF) inputs are used by digital computers, and ON-OFF signals are also
generated by them.
An ON is often represented by a 1 and an OFF by a 0, respectively. A digital computer is
capable of processing both numerical and non-numerical data.
In addition to doing fundamental arithmetic operations like addition, subtraction,
multiplication, and division, it can also perform logical operations.
3. Hybrid Computers
Computers that combine digital and analog components are called hybrid computers.
It combines the best features of both types, having the speed of an analog computer with
the memory and precision of a digital computer.
7
Hybrid computers are typically used in specific applications where both forms of data
need to be processed.
As an example, a gas pump contains a processor that converts measurements of fuel flow
into information about quality and cost.
Memory is a storage part in a computer system. It is used to store the data, information, and
programs at the time of processing on the computer. It stores data either temporarily or
permanent. The main use of memory is saving and retrieving data.
Types of Memory:
Generally computer system consists of two types of memory −
Primary Memory or Volatile Memory
It is called the internal memory of the computer. And it is also known as main memory or
Temporary memory. It holds the data and instructions that are presently working on the system
8
or by the CPU. Primary Memory is called volatile memory, because when power is switched off
it loses all data.
RAM (Random Access Memory) − It stands for Random Access Memory. RAM is a read /writes
memory. It is referred as main memory of the computer system. It is a temporary memory. The
information stored in RAM is lost whenever the power supply to the computer is switched off.
Dynamic RAM − Dynamic RAM also known as DRAM, This type of RAM stores information
in a very short time basically, a few milliseconds even though the power supply is ON. The
Dynamic RAM is cheaper and of moderate speed and also they consume less power.
ROM (Read Only Memory) − It stands for Read Only Memory. ROM is a permanent type of
memory. ROM information is not lost when power supply is switched off. The Content of ROM
is inserted by the computer manufacturer and permanently stored at the time of manufacturing.
ROM cannot be overwritten by the computer. It is also called Non- Volatile Memory.
PROM (Programmable Read Only Memory) − It is used to write data once and read many.
Once a chip has been programmed, the recorded information cannot be changed. It is a non-
volatile memory.
EPROM (Erasable Programmable Read Only Memory) − EPROM chip can be programmed
by erasing the information stored earlier in it. Information stored in EPROM exposing the chip
for ultraviolet light.
Floppy Disks
Magnetic Tapes
Pen Drive
Winchester Disk
Optical Disk(CD,DVD)
Computer Software : Software is the collection of Programs ( instructions ) that allow the
hardware to do its job. They are two types of Computer Software.
A. System Software B. Application Software
System Software: System Software consists of programs that manage the hardware resources of
a computer and perform required information processing tasks.
i. Operating System Software: It provides services such as a user interface, files and data
base access and interfaces to communication systems such as Internet protocols. The primary
purpose of this software is to keep the system operating in an efficient manner while
allowing the users access to the system.
ii. System Support Software : System Support Software provides system utilities and other
operating services. Examples of system utilities are sort programs and disk format programs.
Operating services consist of programs that provide performance statistics for the operational
staff and security monitors to protect the system and data.
10
iii. System Development Software: It includes language translators that convert programs in to
machine language for execution , debugging tools to ensure that programs are error - free and
computer -assisted software engineering ( CASE ) systems.
Application Software: It is directly responsible for helping users to solve their problems.
Application software is broken in to two classes.
i. General - Purpose Software ii. Application - Specific Software
i. General Purpose Software: It is purchased from a software developer and can be used for
more than one application. Examples: word processors, database management systems,
computer – aided design systems. They are labeled general purpose because they can solve a
variety of user computing problems.
ii. Application Specific Software: It can be used only for its intended purpose. Example: A
general ledger system used by accountants. They can be used only for the task for which
they were designed.They cannot be used for other generalized tasks.
Input Unit:
The input unit contains the devices required to input data into a computer system.
The devices used to input data into the computer are called input devices.
Computers do not react or operate on their own, the input unit is one of the main
components of the computer.
This unit establishes a link between the user and the computer so that the user commands
and data into the computer.
These devices take data from the users and send it to the computer in a systematic
manner for further processing. Like other electronic machines, the computer system also
accepts only raw data(binary data). Input devices convert the data inputted by the user into
binary form for the computer to understand. Data can be entered or given in the form of text,
alphabets, numbers, symbols, images etc.
The following are some of the commonly used input devices to perform the activities as an input
unit:
Mouse:
The mouse is one of the primary input devices that acts as a point-and-click-device.
It enables users to move the cursor and select specific objects on the computers screen or
monitor.
11
The traditional mouse usually comes with three buttons, one on the left, one on the right
and one on the scrolling wheel.
It can be connected to the computer both by wire and wireless.
Keyboard:
The keyboard is another essential input device that consists of several keys.
By pressing the keys, we can enter or input data like letters, numbers, signs and other
information on the computer. Like a mouse, it can also be wired and wireless.
Microphone:
The CPU is an essential electronic hardware component that controls and processes all
functions in a computer system, including arithmetic and logical operations.
All simple to complex calculations, tasks, or comparisons are performed by the CPU.
It takes raw data from the primary memory, processes it or converts it into meaningful
Information desired by the users and then sends the processed data back to the primary
memory.
The CPU is called the brain of the computer because it controls the operation of all the
parts of the computer.
Despite this, the computer’s processor(CPU) also handles the operation of many others
units within the computer system, such as the control unit, arithmetic logic unit and
memory unit.
The Control Unit(CU) and the Arithmetic Logic Unit(ALU) are commonly called
components of the CPU because they both together make up the CPU.
12
Output Unit:
The output unit contains the devices needed to receive and view information from a
computer system.
The devices used to receive information or results from the computer are called output
devices.
This unit is an essential component of a computer because without it we cannot obtain or
see information or desired results. Like the input unit, the output unit also establishes a
link between the user and the computer.
As we discussed above, the results obtained after processing are stored in the primary
memory before sending to other units.
The output unit retrieves processed data from the computer’s primary memory(main
memory), which is converted into a human-understandable form before being displayed
by the corresponding output device.
The following are some of the commonly used output devices to perform the activities of an
output unit:
Monitor:
A monitor, often called a visual display unit, is one of the primary output devices used on
computers these days.
It is connected to a computer to digitally view the output(such as text, images, videos) on
the screen. The output displayed on the computer’s monitor is called soft copy output.
The monitor is typically made of glass, circuitry, adjustment buttons, power supply, etc
all enclosed within a casing.
Printer:A printer is another essential output device used with computer systems these days.
Unlike a monitor, a printer helps us to make a hard copy of the desired output such as a
document, picture or other information on paper.
The printer takes electronic data from the computer and creates an exact copy of it in
monochrome(black and white) or colors, depending on the type of printer.
Speakers:
Speakers are used to receive audio/sound output from the computer system. They are
optional and they can be connected to the computer only when we need to listen to
something like music, movies, dialogues etc.
13
Computing Environment:
When we want to solve a problem using a computer, the computer makes use of various devices
which work together to solve that problem. There may be a various number of ways to solve a
problem. We use the various number of computer devices arranged in different ways to solve
different problems. The arrangement of computer devices to solve a problem is said to be a
computing environment. The formal definition of the computing environment is as follows.
Def: “Computing Environment is a collection of computers which are used to process and
exchange information to solve various types of computing problems.”
Grid computing is a collection of computers from different locations. All these computers work
for a common problem. A grid can be described as a distributed collection of a large number of
computers working for a single application.
Computer Language:
Generally, we use languages like English, Hindi, etc., to make communication between
two persons. That means when we want to make communication between two persons we
need a language through which persons can express their feelings.
Similarly, when we want to make communication between user and computer or between
two or more computers we need a language through which user can give information to
the computer and vice versa.
When a user wants to give any instruction to the computer the user needs a specific
language and that language is known as a computer language.
The user interacts with the computer using programs and that programs are created using
computer programming languages like C, C++, Java, etc.,
Computer languages are the languages through which the user can communicate with the
computer by writing program instructions.
Every computer programming language contains a set of predefined words and a set of rules
(syntax) that are used to create instructions of a program.
Computer Languages Classification
Over the years, computer languages have been evolved from Low-Level to High-Level
Languages. In the earliest days of computers, only Binary Language was used to write programs.
The computer languages are classified as follows...
17
Advantages
Disadvantages
Middle-level language is a computer language in which the instructions are created using
symbols such as letters, digits and special characters.
Assembly language is an example of middle-level language. In assembly language, we
use predefined words called mnemonics.
Binary code instructions in low-level language are replaced with mnemonics and
operands in middle-level language. But the computer cannot understand mnemonics, so
we use a translator called Assembler to translate mnemonics into binary language.
Assembler is a translator which takes assembly code as input and produces machine code
as output.
That means, the computer cannot understand middle-level language, so it needs to be
translated into a low-level language to make it understandable by the computer.
Assembler is used to translate middle-level language into low-level language.
Advantages
Writing instructions in a middle-level language is easier than writing instructions in a
low-level language.
Middle-level language is more readable compared to low-level language.
Easy to understand, find errors and modify.
Disadvantages
Middle-level language is specific to a particular machine architecture, that means it is
machine-dependent.
Middle-level language needs to be translated into low-level language.
Middle-level language executes slower compared to low-level language.
High-Level Language
A high-level language is a computer language which can be understood by the users. The
high-level language is very similar to human languages and has a set of grammar rules
that are used to make instructions more easily.
19
Every high-level language has a set of predefined words known as Keywords and a set of
rules known as Syntax to create instructions.
The high-level language is easier to understand for the users but the computer can not
understand it.
High-level language needs to be converted into the low-level language to make it
understandable by the computer. We use Compiler or interpreter to convert high-level
language to low-level language.
Languages like COBOL, FORTRAN, BASIC, C, C++, JAVA, etc., are examples of high-
level languages.
All these programming languages use human-understandable language like English to
write program instructions. These instructions are converted to low-level language by the
compiler so that it can be understood by the computer.
Advantages
Writing instructions in a high-level language is easier.
A high-level language is more readable and understandable.
The programs created using high-level language runs on different machines with little
change or no change.
Easy to understand, create programs, find errors and modify.
Disadvantages
High-level language needs to be translated into low-level language.
High-level language executes slower compared to middle and low-level languages.
Understanding Computer Languages
The following figure provides a few key points related to computer languages.
20
Language Translators
These are the programs which are used for converting the programs in one language into
machine language instructions, so that they can be excuted by the computer.
Language Translators
These are the programs which are used for converting the programs in one language into
machine language instructions, so that they can be excuted by the computer.
Language Translators
These are the programs which are used for converting the programs in one language into
machine language instructions, so that they can be executed by the computer.
1)Compiler: It is a program which is used to convert the high level language programs into
machine language.
2) Assembler: It is a program which is used to convert the assembly level language programs
into machine language
3) Interpreter: It is a program, it takes one statement of a high level language program,
translates it into machine language instruction and then immediately executes the resulting
machine language instruction and so on.
Compiler Interpreter
A Compiler is used to compile an entire An interpreter is used to translate each line of
program and an executable program is the program code immediately as it is entered
generated through the object program
The compiled programs run faster The Interpreted programs run slower
Most of the Languages use compiler A very few languages use interpreters.
Computer hardware understands a program only if it is coded in its machine language. It is the
job of the programmer to write and test the program .There are four steps in this process:
The software used to write programs is known as a text editor. A text editor
helps us enter, change and store character data. Once we write the program in the text editor we
save it using
Compiling Programs
The code in a source file stored on the disk must be translated into machine
language. This is the job of the compiler. The Compiler is a computer program that translates the
source code written in a high-level language into the corresponding object code of the
low-level language.This translation process is called compilation. The entire high level
program is converted into the executable machine code file. The Compiler which executes C
programs is called as C Compiler. Example Turbo C, Borland C, GC etc.,
The Preprocessor
The Translator
The Preprocessor reads the source code and prepares it for the translator. While preparing the
code, it scans for special instructions known as preprocessor commands. These commands tell
the preprocessor to look for special code libraries. The result of preprocessing is called
the translation unit.
After the preprocessor has prepared the code for compilation, the translator does the
actual work of converting the program into machine language. The translator reads the
translation unit and writes the resulting object module to a file that can then be combined with
other precompiled units to form the final program. An object module is the code in the machine
language.
Linking Programs
The Linker assembles all functions, the program‘s functions and system‘s functions
into one executable program.
Executing Programs
To execute a program we use an operating system command, such as run,
to load the program into primary memory and execute it. Getting the program into memory is
the function of an operating system program known as the loader. It locates the
executable program andreads it into memory. When everything is loaded the program
takes control and it begin execution.
23
Algorithms
An algorithm is basically a procedure of steps that we exactly follow to solve a particular task or
problem. We can say that it is a set of rules that we need to follow while developing a program code
during problem-solving.
Features of an algorithm
The features of an algorithm are as follows:
Clarity
The algorithm should be very clear and unambiguous in its meaning. It should be
simple so that one can understand it easily.
Well-defined inputs
A program may require to take input from the user. Therefore, the algorithm
should clearly define the inputs.
Well-defined outputs
The algorithm should clearly specify the output that the program will produce.
Finiteness
The algorithm should have a termination point. This means that the algorithm
should not be such that it runs infinite times or end up in loops.
Feasibility
The algorithm should be such that we can implement it easily. Hence, it should be
simple and practical to implement.
Language independent
It should be in simple English language. Since the code implementation should
result in the same output no matter which programming language we use while writing the code.
What is problem-solving?
A2. We can define problem-solving as a process of understanding the problem, finding solution for
the problem, and finally implementing the solution to it.
Example of Algorithm
Problem 1: Find the area of a Circle of radius r.
Inputs to the algorithm:
Radius ‘r’ of the Circle.
Expected output:
Area of the Circle
Algorithm:
Step1: Read\input the Radius r of the Circle
Step2: Area PI*r*r // calculation of area
Step3: Print Area
Problem2: Write an algorithm to read two numbers and find their sum.
Inputs to the algorithm:
First num1.
Second num2.
Expected output:
Sum of the two numbers.
Algorithm:
Step1: Start
Step2: Read\input the first num1.
Step3: Read\input the second num2.
Step4: Sum num1+num2 // calculation of sum
Step5: Print Sum
Step6: End
25
Flowcharts
It is basically a diagrammatic representation of an algorithm. Furthermore, it uses various symbols
and arrows to describe the beginning, ending, and flow of the program. Moreover, the programmers
use it to depicting the flow of data and instructions while problem-solving.
Flowchart Symbols
Here is a chart for some of the common symbols used in drawing flowcharts.
Flowchart for an algorithm which gets two numbers and prints sum of their value
28
Part-2
Introduction to C language
C is a general-purpose high level language that was originally developed by Dennis
Ritchie for the Unix operating system. It was first implemented on the Digital
Equipment Corporation PDP-11 computer in 1972.
The Unix operating system and virtually all Unix applications are written in the C language.
C has now become a widely used professional language for various reasons.
Easy tolearn
Structuredlanguage
It produces efficientprograms.
It can handle low-levelactivities.
It can be compiled on a variety ofcomputers.
Facts about C
C was invented to write an operating system called UNIX.
C is a successor of B language which was introduced around1970
The language was formalized in 1988 by the American National
Standard Institute (ANSI).
By 1973 UNIX OS almost totally written inC.
Today C is the most widely used System Programming Language.
Why to use C?
C was initially used for system development work, in particular the programs that make-
up the operating system. C was adopted as a system development language because it
produces code that runs nearly as fast as code written in assembly language. Some
examples of the use of C might be:
History of C language:
The founder of the ‘C’ language, Dennis Ritchie is known as “Father of C” and also “Father
of UNIX”.
The following are the language before ‘c’ & various versions of ‘c’.
1. CPL (Common Programming Language)
The CPL was invented by Martin Richards at the University of Cambridge in the early of
1960s.
3. B Language
B language is derived from BCPL. It was introduced in the year of 1969 by Ken
Thompson and Dennis Ritchie at Bell Laboratory, USA. The B language is similar to BCPL.
4. C Language
C language is derived from the B language. It was introduced in the year of 1972 by Dennis
Ritchie at Bell Laboratory, USA. The C language was mainly developed to create an operating
system called UNIX. The name C is given based on the previous language B and BCPL. Ninety
percent of the UNIX operating system code is written in C language. During the 1970s, the C
language became a very popular programming language. Many universities and organizations
began creating their version of C language for their respective projects. So, C language has got
many variants at that time. Later it was standardized.
5. ANSI C (C89)
In the year of 1983, the ANSI (American National Standards Institute) formed a committee to
frame standard specifications for the C language. In the year of 1989, this committee introduced
a standard version of C with the name "ANSI C" with standard library files. The ANSI C is also
called as C89 in short form.
30
6. C90
In the year of 1990, the ANSI C was got ISO (International Organization for Standardization)
standardization with the inclusion of a few new features like new library files, new processor
commands. And it was also added with keywords const, volatile and signed, etc... ISO
standardized ANSI C as ISO/IEC 9899:1990. This version is called as C90 in short form.
7. C99
In the year of 1995, many new features were added to the C90 to create a new version of it. This
new version of C was got ISO standardization in the year of 1999 with the name ISO/IEC
9899:1999. In the short form, it is called as C99. Later C99 became the official standard version
of C.
The Preprocessor
The Preprocessor accepts source code as input and is responsible for
Removing comments
Interpreting special preprocessor directives denoted by #.
Forexample
#include -- includes contents of a named file. Files usually called header
files.e.g
o #include <math.h> -- standard library mathsfile.
o #include <stdio.h> -- standard library I/Ofile
#define -- defines a symbolic name or constant. Macrosubstitution.
o #define MAX_ARRAY_SIZE100
32
C Compiler
The C compiler translates source to assembly code. The source code is received from the
preprocessor.
Assembler
The assembler creates object code. On a UNIX system you may see files with a .o
suffix (.OBJ on MSDOS) to indicate object code files.
Link Editor
If a source file references library functions or functions defined in other source files the
link editor combines these functions (with main()) to create an executable file.
C TOKENS
C tokens are the basic buildings blocks in C language which are constructed together to write a C
program.
Each and every smallest individual unit in a C program is known as C tokens.
C tokens are of six types. They are
Keywords (eg: int,while),
Identifiers (eg: main,total),
Constants (eg: 10,20),
Strings (eg: ―total‖,―hello‖),
C KEYWORDS
C keywords are the words that convey a special meaning to the c compiler. The keywords
cannot be used as variable names.
The list of C keywords is given below:
volatile while
C IDENTIFIERS
Identifiers are used as the general terminology for the names of variables, functions and
arrays. These are user defined names consisting of arbitrarily long sequence of letters and
digits with either a letter or the underscore(_) as a first character.
There are certain rules that should be followed while naming c identifiers:
They must begin with a letter or underscore (_).
They must consist of only letters, digits, or underscore. No other special character is
allowed. It should not be a keyword.
It must not contain whitespace.
It should be up to 31 characters long as only first 31 characters are significant.
A C constant refers to the data items that do not change their value during the program
execution. Several types of C constants that are allowed in C are:
Integer Constants
Integer constants are whole numbers without any fractional part. 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 three types of integer constants:
Decimal Integer Constants
Integer constants consisting of sequence of digits from the set 0 through 7 starting with 0 is
said to be octal integer constants.
Hexadecimal integer constants are integer constants having sequence of digits preceded by
0x or 0X. They may also include alphabets from A to F representing numbers 10 to 15.
Example of valid hexadecimal integer constants: 0xD, 0X8d, 0X, 0xbD
It should be noted that, octal and hexadecimal integer constants are rarely used in programming.
Real Constants
The numbers having fractional parts are called real or floating point constants. These may
be represented in one of the two forms called fractional form or the exponent form and may
also have either + or – sign preceding it.
Example of valid real constants in fractional form or decimal notation :
0.05, -0.905, 562.05, 0.015
Representing a real constant in exponent form
mantissa e exponent
The mantissa must be either an integer or a real number expressed in decimal notation.
The letter e separating the mantissa and the exponent can also be written in uppercase
i.e. E a nd, the exponent must be an integer.
Examples of valid real constants in exponent form are: 252E85, 0.15E-10,-3e+8
Character Constants
A character constant contains one single character enclosed within single quotes.
Examples of valid character constants: ‘a‘ ,’Z‘,’5‘
It should be noted that character constants have numerical values known as ASCII
values, for example, the value of ‘A‘ is 65 which is its ASCII value.
String Constants: A string constant is a sequence of characters enclosed in double quote, the
characters may be letters, numbers, special characters and blank space etc .
C allows us to have certain non graphic characters in character constants. Non graphic
characters are those characters that cannot be typed directly from keyboard, for example, tabs,
carriage return, etc.
These non graphic characters can be represented by using escape sequences represented by a
backslash() followed by one or more characters.
NOTE: An escape sequence consumes only one byte of space as it represents a single character.
Special Symbols
The following special symbols are used in C having some special meaning and thus, cannot
be used for some other purpose.
[] () {} , ; : * … = #
Braces{}: These opening and ending curly braces marks the start and end of a block of
code containing more than one executable statement.
Parentheses(): These special symbols are used to indicate function calls and function
parameters.
Brackets[]: Opening and closing brackets are used as array element reference. These
indicate single and multidimensional subscripts.
VARIABLES
A variable is nothing but a name given to a storage area that our programs can
manipulate.
Each variable in C has a specific type, which determines the size and layout of
the variable's memory;
The range of values that can be stored within that memory; and the set of
operations that can be applied to the variable.
The name of a variable can be composed of letters, digits, and the underscore
character.
It must begin with either a letter or an underscore.
Upper and lowercase letters are distinct because C is case-sensitive.
The following basic variable types −
Type Description
C programming language also allows defining various other types of variables like
Enumeration, Pointer, Array, Structure, Union, etc.
Variable Definition in C
A variable definition tells the compiler where and how much storage to create
for the variable. A variable definition specifies a data type and contains a list of one or
more variables of that type as follows−
type variable_list;
Here, type must be a valid C data type including char, w_char, int, float, double,
bool, or any user-defined object; and variable_list may consist of one or more identifier
names separated by commas. Some valid declarations are shown here −
int i, j, k;
char c, ch;
float f,
salary;
double d;
The line int i, j, k; declares and defines the variables i, j, and k; which instruct the
compiler to create variables named i, j and k of type int.
Variables can be initialized (assigned an initial value) in their declaration. The
initializer consists of an equal sign followed by a constant expression as follows−
For definition without an initializer: variables with static storage duration are implicitly
initialized with NULL (all bytes have the value 0); the initial value of all other variables
are undefined.
Variable Declaration in C
A variable declaration provides assurance to the compiler that there exists a
variable with the given type and name so that the compiler can proceed for
further compilation without requiring the complete detail about the variable.
A variable definition has its meaning at the time of compilation only; the
compiler needs actual variable definition at the time of linking the program. A
variable declaration is useful when multiple files are used.
Data types
Data types in the c programming language are used to specify what kind of value can be
stored in a variable.
The memory size and type of the value of a variable are determined by the variable data
type.
In a c program, each variable or constant or array must have a data type and this data type
specifies how much memory is to be allocated and what type of values are to be stored in
that variable or constant or array.
Data types are classified as follows...
Integers are whole numbers without a fractional part. In C, the keyword int is used to represent
integer quantity. They generally require 2 bytes of memory for their storage. It is a signed data
type, i.e. it can be used to store both positive as well as negative values. The range of values that
variables of this type can hold lies between -32768 to +32767.
float
double
The keyword "float" is used to represent floating-point data type and "double" to represent
double data type in c. Both float and double are similar but they differ in the number of decimal
places. The float value contains 6 decimal places whereas double value contains 15 or 19
decimal places.
C programming treats all the devices as files. So devices such as the display are addressed in the
same way as files and the following three files are automatically opened when a program executes to
provide access to the keyboard and screen.
Standard file File pointer Device
Standard input Stdin Keyboard
Standard output Stdout Screen
Standard error Stderr Your screen
Input / Output functions are classified into two types
42
Formated I/O Functions : Formatted I/O functions operates on various types of data.
printf() : output data or result of an operation can be displayed from the computer to a standard
output device using the library function printf(). This function is used to print any combination of
data.
Ex : printf(“%d”,3977);
printf() statement another syntax :
Syntax : printf(“fomating string”);
Formating string : It prints all the character given in doublequotes (“ “) except formatting specifier.
Ex:
printf(“ hello “);-> hello
printf(“a”); -> a
scanf() : input data can be entered into the computer using the standard input „C‟ library function
called scanf(). This function is used to enter any combination of input.
Syntax : scanf(“control string “,&var1, &var2,----, &varn);
The scanf() function is used to read information from the standard input device (keyboard).
Ex : scanf(“ %d “,&a);
Each variable name (argument) must be preceded by an ampersand (&). The (&) symbol gives the
meaning “address of “the variable.
Unformatted I/O functions:
a) Character I/O b) String I/O
a) character I/O:
b)String I/O:
1. gets(): Used for accepting any string from the standard input(stdin)
2. puts(): Used to display a string or character array Eg:puts()
3. cgets():read a string from the console eg; cgets(char *st)
4. cputs():display the string to the console eg; cputs(char *st)
43