A Reference Book On C Programming
A Reference Book On C Programming
A
REFERENCE
BOOK
ON
C - PROGRAMMING
(EG 442 CT)
By:
Er. SAMEER SHRESTHA
Kathmandu, Nepal
A
REFERENCE
BOOK
ON
C - PROGRAMMING
(EG 442 CT)
By:
Er. SAMEER SHRESTHA
Department of Electronics and Computer
Kathmandu Engineering College
Kathmandu, Nepal
We would like to express our sincere gratitude to Mr. Hirendra Man Pradhan,
Chairman, Kathmandu Engineering College for inspiring us to write the manual “A
Reference Book on C Programming” that is required for the students of the Electronics
and Computer Engineering.
We would like to thank Er. Ramesh Shrestha, Kathmandu Engineering College for
his necessary advice and encouragement to us while preparing this manual.
We would also like to express our thankfulness to Er. Paras Pant, Head of
Department, Kathmandu Engineering College providing us with all the necessary
books and manuals as needed. Many thanks to friends and family for inspiring and
helping us in times of need.
We would like to thank all the good people who helped us directly and indirectly in
making this manual.
May, 2006
I
TABLE OF CONTENTS
Acknowledgement I
1. Introduction to Computers 1
3. Introduction to C 29
6. Functions 72
7.1. Arrays 99
7.2. Pointers 112
7.3. String and String Handling Functions 137
8. Structures 173
10. Appendix
References
A Reference Book on C Programming
Chapter 1.0
1. Introductions to Computers
Computer is a programmable machine capable of performing computations and making
logical decisions.
The development of computing dates back from the early Sumerian civilization (4000-
1200 BC) when people started to keep records of transactions on clay tables. The actual
computing using a machine started around 3000 BC when Babylons invented the abacus
and Chinese people started using them around 1200 BC.
In 1854 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.
1
A Reference Book on C Programming
In 1961, Integrated Circuit (IC) were commercially available and since then computers
use ICs instead of individual transistors or other components. IBM 360 and DEC PDP 8
were the most popular machines during that time. The invention of microprocessor
further reduced the size and increased the performance of the computers. Altair 8800 was
the first easily available microcomputer for which Bill Gates co-wrote BASIC.
1981 was the year of IBM when it launched the PC. Since then, there had been significant
development in the field of microelectronics and microprocessor. Today, we run a 2GHz
processor on our desk with 256MB of memory (640KB was considered more than
sufficient once upon a time) and more than 40G of storage.
By 1948, the invention of the transistor greatly changed the computer's development. The
transistor replaced the large, cumbersome vacuum tube in televisions, radios and
computers. As a result, the size of electronic machinery has been shrinking ever since.
The transistor was at work in the computer by 1956. Coupled with early advances in
magnetic-core memory, transistors led to second generation computers that were smaller,
faster, more reliable and more energy-efficient than their predecessors.
Throughout the early 1960's, there were a number of commercially successful second-
generation computers used in business, universities, and government. These second
generation computers contained transistors in place of vacuum tubes. They also contained
all the components we associate with the modern day computer: printers, tape storage,
disk storage, memory, operating systems, and stored programs. One important example
was the IBM 1401. High-level languages such as COBOL (Common Business-Oriented
Language) and FORTRAN (Formula Translator) came into common use during this time,
and have expanded to the current day. These languages replaced cryptic binary machine
code with words, sentences, and mathematical formulas, making it much easier to
2
A Reference Book on C Programming
program a computer. New types of careers (programmer, analyst, and computer systems
expert) and the entire software industry began with second-generation computers.
Though transistors were clearly an improvement over the vacuum tube, they still
generated a great deal of heat, which damaged the computer's sensitive internal parts. The
Integrated Circuits (IC) were invented and used to fit even more components on a single
chip. As a result, computers became ever smaller as more components were squeezed
onto the chip. Another third-generation development included the use of an operating
system that allowed machines to run many different programs at once with a central
program that monitored and coordinated the computer's memory and other resources.
Such condensed power allowed everyday people to harness a computer's power. They
were no longer developed exclusively for large business or government contracts. By the
mid-1970's, computer manufacturers sought to bring computers to general consumers.
These minicomputers came complete with user-friendly software packages that offered
even non-technical users an array of applications, most popularly word processing and
spreadsheet programs. Pioneers in this field were Commodore, Radio Shack and Apple
Computers.
In 1981, IBM introduced its personal computer (PC) for use in the home, office and
schools. The 1980's saw an expansion in computer use in all three arenas as clones of the
IBM PC made the personal computer even more affordable. The number of personal
computers in use more than doubled from 2 million in 1981 to 5.5 million in 1982. Ten
years later, 65 million PCs were being used. Computers continued their trend toward a
smaller size, working their way down from desktop to laptop computers (which could fit
inside a briefcase) to palmtop (that could fit inside a pocket).
As computers became more widespread in the workplace, new ways to harness their
potential developed. As smaller computers became more powerful, they could be linked
3
A Reference Book on C Programming
Many advances in the science of computer design and technology are coming together to
enable the creation of fifth-generation computers. Two such engineering advances are
parallel processing, which replaces von Neumann's single central processing unit design
with a system harnessing the power of many CPUs to work as one. Another advance is
superconductor technology, which allows the flow of electricity with little or no
resistance, greatly improving the speed of information flow. Computers today have some
attributes of fifth generation computers. For example, expert systems assist doctors in
making diagnoses by applying the problem-solving steps a doctor might use in assessing
a patient's needs. It will take several more years of development before expert systems
are in widespread use.
The computer consists of two major components - hardware and software. The physical
components – from nuts and bolts to tiny chips to the processor and connecting wires,
which we can touch, is called the hardware. The instructions (also called programs) and
data (processed or unprocessed) are the software. Without software to drive the hardware,
a computer is just an empty shell.
4
A Reference Book on C Programming
The major parts of a computer system are shown in figure 1. It can be seen that a
computer mainly consists of three parts – input device, processing unit and output device.
The function of input device is to provide the necessary data and instructions to the
processing unit. The processing unit then operates on the data with the instructions that
user has given to it through the input device. The result of the processing is then
presented to the user through output device. Besides these three major parts, there are two
other parts inside the computer used for storing data and instructions. These are called
mass storage device and memory. The mass storage device (also called the secondary
memory) usually stores the data and programs. These devices could be made of magnetic
or optical media. The hard disk drive (usually magnetic) is used to store the program
(software) and data (processed or unprocessed). The floppy (also a magnetic media) can
store only a small amount of data. This is a portable media. Data can be erased and
rewritten on hard drive and floppy. The other media CD-ROM (Compact Disk Read Only
Memory) is an optical media and generally non-rewritable. But, writable and rewritable
are also available.
The data, temporary results of processing and currently running programs are stored on
the primary memory known as RAM (Random Access Memory) inside the computers.
A computer that can reside (in terms of dimension) on user’s desktop is called a
microcomputer. But this definition is rather vague because computers having smaller
dimension (not in power) are also available. Microcomputers include PC's, laptops,
notebooks, and hand-held computers.
5
A Reference Book on C Programming
storage capacity. A mainframe is a larger type of computer and is typically 10-100 times
faster than the micro. These computers require a controlled environment both for
temperature and humidity. Super computer is the most powerful computer that can
process at speeds of several hundred or even thousands of millions of instructions
(mflops) per second.
Computers have evolved over the years from machinery that literally filled rooms to the
desktop and laptop computers of today. The desktop computer debuted to mass audiences
in 1980 as an 8088 computer that processed information at a rate of 5-8 MHz. Over the
next fourteen years, computers progressed from the 80286 at 8-16 MHz to the 80386 to a
high of 32 MHz speed. Since the mid 1990's, the progression has been much more rapid
moving from a 80486 at a top capacity of 100 MHz to the first Pentium 60 MHz to the
Pentium IV of today that processes above 1000 MHz.
The microprocessor contains the CPU (Central Processing Unit), which is made up of
three components – the control unit supervises everything that is going on inside the
computer, the arithmetic/logic unit (ALU) performs the mathematical and comparison
(logical) operations, and main memory (RAM) to store the programs and data.
6
A Reference Book on C Programming
The processor
CONTROL UNIT
Interprets the stores instructions in sequence
Issues commands to all elements of the computer
OUTPUT
INPUT Information
Data and - The result of
Instructions processing
MAIN MEMORY
(MAIN STORAGE)
Holds data, instructions and results of processing
SECONDARY/AUXIAILARY STORAGE
(BACKING STORAGE)
INPUT UNIT
This unit consists of the devices used to tale information and data that are external to the
computer and put them into memory unit or ALU. The input unit is used to enter data and
program into the memory unit and ALU prior to the starting of the computer and also
during the execution of the program. Most systems have a keyboard and one or more
additional devices specially designed for interactive input. These include a mouse,
tracker ball, joystick, digitizer, scanner, touch panel etc.
7
A Reference Book on C Programming
The heart of any computer system is the processor unit or Central Processing Unit or
Central Processing Unit, which consists of
Primary storage unit
Arithmetic-logic unit and
Control unit
The CPU lays in side a box called main machine or chasis. It is connected with the timing
chips (clock generators), ROM BIOS (Read Only Memory Basic Input Output System),
floppy drives, hard disk drivers (fixed disks), expansion slots, HDD (Hard Disk Drive)
control cards, FDD (Floppy Disk Drive) control cards, etc. They are located inside the
main machine. The architecture of Central processing unit and a computer system is
shown block diagram.
The primary storage (also called the main memory) section is used for four purposes, out
which three are related data processing.
Data are fed into an input storage area where they are held until ready to be processed.
A working storage space that is like a sheet of scratch paper is used to hold the data being
processed and the intermediate results of such processing.
An output storage area holds the finished results of the processing operations until they
can e released.
In addition to these data-related purposes, the primary storage section is also contains a
program storage area that holds the processing instructions.
Semiconductor memory RAMs and ROMs are used as main memory. The main memory
is a fast memory but expensive.
The arithmetic-logic and control sections together make up the Central Processing Unit
(CPU). All calculations are performed and all comparisons (decisions) are made in the
arithmetic-logic section of the CPU. Once data are fed into primary storage from input
devices, they are held and transferred, when needed, to the arithmetic –logic section,
where the processing takes place. No processing occurs in primary storage. Intermediate
results generated in the arithmetic –logic unit are temporarily placed in a designated
working storage area until needed at a later time. Data may thus move many times before
the processing is finished. Once completed, the final results are released to an output
device.
8
A Reference Book on C Programming
Logic unit is a section of CPU and it has the basic function of comparing two digits
(binary digits) on the basis of “on” of “off” state representing true and false conditions.
The comparison is related to logical operations such as and, or and not. The general
operation is based on Boolean algebra.
OUTPUT UNIT
This unit consists of the devices used to transfer data and information from the computer
to the ″outside world″. The output devices are directed by the control unit and can receive
the data from memory or the ALU, which is then put into appropriate form for external
use. Example of common output devices are CRT (Cathode Ray Tube) screen and
printers. Other output devices are LEDs ( Light Emitting Diodes), LCDs ( Liquid Crystal
Display), indicator lights, disk or tape units and digital to analog converters (DAC).
As the computer executes its program, it usually has results or control signals that it must
present to the external world. For example, a large computer system might have a line
printer as an output device. Here, the computer sends out signals to print out the results
on paper. A computer mighty displays its results on indicator lights or LED displays.
SECONDARY/AUXILIARY STORAGE
Secondary or auxiliary storage stores operation system, data files, compilers, assemblers,
application programs etc. The CPU does not read information (residing in the secondary
memory) directly from the secondary storage. The programs and data residing in the
secondary storage, if needed by CPU are first transferred from the secondary storage to
the primary memory. Then the CPU reads from the primary memory. The results are also
stored in the secondary storage. The secondary storage is the mass storage. It is slow but
cheap. Some of the examples of the secondary storage are Hard Disk, CD−ROM, DVDs,
Pen Drives etc.
9
A Reference Book on C Programming
Examples of popular OS are DOS (Disk Operating System), Windows, Unix, Linux,
OS/2, and Solaris etc. Some of these OSes are specific to a particular computer system –
e.g. Solaris runs on Sun SPARC machines.
Word processing software allows you to process and manipulate test. Examples are Corel
WordPerfect and Microsoft Word. Spreadsheets, very simply put, gives you 4 when you
input 2+2. Examples are Quattro Pro and Excel. Databases are most useful for organizing
data. For example, database software can keep track of your friend’s name; address,
telephone numbers and you can add or remove whenever you want. Some examples are
Paradox and Access. Internet browsers give us access to the world. When this software is
installed and you are connected to an ISP (Internet Service Provider), you have access to
worldwide information. Common examples are Netscape and Explorer. There are other
utility programs that check if the units inside your machine are functioning properly as
they should. Examples are the disk scanner that checks if the surface of your disks are
damaged, anti-virus that checks if your computer has been infected with some virus (a
malicious program that may just annoy you or even delete all the content of your hard
drive) and clean if found etc.
10
A Reference Book on C Programming
11
A Reference Book on C Programming
considered high-level because they are closer to human languages and further from
machine languages. Every high level language has its own set of rules to represent
instructions. This rule is called syntax. The main advantage of high-level languages over
low-level languages is that they are easier to read, write, and maintain.
The first high-level programming languages were designed in the 1950s. Now there are
dozens of different languages, such as Ada, Algol, BASIC, COBOL, C, C++ ,
FORTRAN, LISP, Pascal, and Prolog etc.
Like assembly language programs, programs written in a high-level language also need to
be translated into machine language. This can be done in two ways – by a compiler or
interpreter.
Compiler
A compiler is a program that translates program (called source code) written in some
high level language into object code. The compiler derives its name from the way it
works, looking at the entire piece of source code and collecting and reorganizing the
instructions. A compiler translates high-level instructions directly into machine language
and this process is called compiling.
Interpreter
An interpreter translates high-level instructions into an intermediate form, which it then
executes. Interpreter analyzes and executes each line of source code in succession,
without looking at the entire program; the advantage of interpreters is that they can
execute a program immediately. Compilers require some time before an executable
program emerges. However, programs produced by compilers run much faster than the
same programs executed by an interpreter.
Compiled programs generally run faster than interpreted programs. The advantage of an
interpreter, however, is that it does not need to get through the compilation stage during
which machine instructions are generated. This process can be time-consuming if the
program is long. The interpreter, on the other hand, can immediately execute high-level
programs. For this reason, interpreters are sometimes used during the development of a
program, when a programmer wants to add small sections at a time and test them quickly.
Because compilers translate source code into object code, which is unique for each type
of computer, many compilers are available for the same language. For example, there is a
C compiler for PCs and another for Unix computers.
Compile
Compiling is a process of transforming a program written in a high-level programming
language from source code into object code. Programmers write programs in a form
called source code. Source code must go through several steps before it becomes an
executable program. The first step is to pass the source code through a compiler, which
12
A Reference Book on C Programming
translates the high-level language instructions in the source code follow its syntax rules.
If not it will inform the programmer where rules have been violated.
The final step in producing an executable program –after the compiler has produced
object code - is to pass the object code through a linker. The linker combines molecules
(different program segments) and gives real values to all symbolic addresses (memory
locations), thereby producing machine code.
Source code
Program written in a particular programming language in its original form is called
source code (program). The word source differentiates code from various other forms
that it can have (for example, object code and executable code). To execute the program,
however, the programmer must translate it into machine language. The compiler
translates the source code into a form called object code. Source code is the only format
that s readable by humans. When we purchase programs, we usually receive them in their
machine-language format. This means that we can execute them directly, but cannot read
or modify them.
Object code
Object code is the code produced by a compiler. Object code is often the same as or
similar to a computer’s machine language. The final step in producing an executable
program is to transform the object code into machine languages, if it is not already in this
form. A program called linker does this job.
Liveware
Liveware refers to all personnel who deals with the computer and its management
Example:
• Hardware Maintenance Engineer
• Software Engineer
• System Analyst
• Programmer
• Data Operator
• Computer Technicians
Firmware
This is the term given to in-built programs of the computer, which give instructions to the
computer. It is placed within computer systems at the time of manufacturing. ROM can
be taken as firmware, which is hardware-containing software to control the computer
software. Firmware is a hardware and a software in one.
13
A Reference Book on C Programming
Chapter 2.0
1. Problem Analysis
2. Algorithm Development and Flowcharting
3. Coding
4. Compilation and Execution
5. Debugging and Testing
6. Program Documentation
At this stage you not only want to determine the input and output requirements but also to
get sense of how much everything will cost.
In order to avoid having the right solution to the wrong problem, we need to be sure we
know what the problem is actually is. Making a clear statement of the problem depends
of course, on its size and complexity. If the problem is small and does not involve other
systems, then we can probably state the problem easily and proceed immediately to the
second step, “Program Design”. However, many problems interact with an already
existing information system or would require a series of programs and so require very
complete analysis, meaning, and careful coordination of people, procedures and
programs.
14
A Reference Book on C Programming
Now that you have determined the outputs, you need to define the input and data. To do
this, you list the inputs required and the source of the data. For example, in a payroll
program, inputs could be employee timesheets and the source of the in put could be either
the employees themselves or their supervisors. You need to be sure that the source of the
data is consistent so that the date will be available in the future when you need it.
Now you have to determine the processing requirements for converting the input data to
output. If the proposed program is to replace or to supplement an existing one, you will
want to make a particularly careful evaluation and identification of the present processing
procedures, noting the logic used and any improvements that could be made. If the
proposed system is not designed to replace and existing system, you would be well
advised to look over another system in use that addresses a similar problem.
Now you need to see if what you have accomplished so far is enough to make a new
program feasible. If the program is intended to replace an existing system, you need to
determine id the potential improvements outweigh the costs and possible problems.
Before concluding the program analysis stage, it is best to write up a document, stating
the results of this first phase. This document should contain statements on the program’s
objectives, output specifications, input requirements, processing requirements and
feasibility. In other words, the documents should describe everything you have done so
far.
You know you have a problem and have identified it in the program analysis stage. Now
you need to plan a solution to meet the objectives you have specified. This second phase
is called the program design stage-it consists of designing a solution.
Algorithms
An algorithm is a verbal or say written form of the program. It can be defined as ordered
description of instructions to be carried out in order to solve the given task.
15
A Reference Book on C Programming
For instance to prepare a tea one can follow the following steps.
1. Start.
2. Fetch water and tealeaves along with sugar and milk.
3. Boil the water.
4. Put tealeaves and sugar in boiled water.
5. Mix with milk.
6. Serve the tea.
7. Stop.
1. Finiteness: Every algorithm should lead to a session of task that terminates after a
finite no of steps.
2. Definiteness: Each step must be precisely defined. Actions should be
ambiguously specified for each case.
3. Inputs: Any algorithm can have zero or more inputs. Inputs may be given
initially or as the algorithm runs.
4. Outputs: Any algorithm may result in one or more outputs. Quantities that have
specified relation to inputs.
5. Effectiveness: All operations must be sufficiently basic so as to be implemented
with even paper and pencil.
For example: Read two numbers form user and display the resulting sum.
1. Start.
2. Read two numbers and solve in variables, say A and B.
3. Store the sum of A and B in C.
4. Display the value in C.
5. Stop.
Flowchart
One of the most widely used devices for designing programs is the flowchart, which
graphically represents the logic needed to solve a programming problem.
A programming flowchart represents the detailed sequence of steps, needed to solve the
problem. Program flowcharts are frequently used to visualize the logic and steps in
processing. In other words it’s a diagrammatic representation of algorithm.
16
A Reference Book on C Programming
Diagrams Purpose
Start / Stop
Processing
Decision Making
Input / Outputs
Connector
Flow Lines
For examples: Read two numbers form user and display the resulting sum.
Start
Read a,b
c=a+b
Display
Stop
17
A Reference Book on C Programming
Start
Input
A, B, C
Y N
Is A>B
Is A>C Is B>C
Y N N Y
Stop
2.3. Coding
Writing the program is called coding. In this step, you use the logic you develop in the
program design stage to actually write the program. The coding can be done in any kind
of languages (i.e. low level or high level languages).
As in other areas of life there are many ways to get the same places, in programming also
there are various ways to get things done. Here are some of the qualities of a good
program:
• It should be easily readable and understandable by people other than the original
programmer. This is accomplished by including comments within the program.
• It should be reliable, able to work under all reasonable conditions and always get
the correct output.
18
A Reference Book on C Programming
Unlike the compiled languages. No object code is stored and there is no compilation. This
means that in a program where one statement is executed several times (such as reading
and employ’s payroll record), that statement is converted to machine language each time
it is executed. The most frequently used interpreted language is BASIC. Compiler
languages are better than interpreted languages as they can be executed faster and more
efficiently once the object code has been obtained. On the other hand interpreted
languages do not need to create object code and so are usually easier to develop- that is to
code and test.
The object of the compiler is to translate a program written in a high level programming
language from source code to object code. Programmers write programs in a form called
source code. Source code must go through several steps before it becomes an executable
program.
19
A Reference Book on C Programming
Compiler
Runtime Executable
library Linker Program
The first step is to pass the source code through a compiler, which translate the high level
language instructions into object code.
Linking
Linking is another step following compilation. Linking is necessary for several reasons.
The program may need to be combined with various library routines. As we noted, these
are functions that perform tasks such as input/output. Library routines are stored in files
with the .lib extension. We may not want to compile all of our program at the same time.
Larger C programs commonly consist of several separate files, some of which may
already be compiled. The linker combines these files into a single executable file. The
compiler generates an intermediate kind of file called an object file. The linker then links
all the necessary object files together to produce a final, executable program.
20
A Reference Book on C Programming
Compiler
1. Syntax error
Any violation of rules of the language results in syntax error. The compiler can detect and
isolate such errors. When syntax errors are present, the compilation fails and is
terminated after listing the errors and the line numbers in the source program, where the
errors have occurred. Such error occurs when a computer language compiler cannot
understand a command entered by the user. It is easy to correct the syntax error.
21
A Reference Book on C Programming
Errors such as mismatch of data types or referencing an out of range array element go
undetected by the compiler. A program with these mistakes will run but produce the
erroneous result. Isolating a run time error is usually a difficult task.
These errors are encountered after error free compilation, at the time of execution of the
program. Some typical examples are:
• Data incorrect or in wrong order
• Incorrect file name
• Infinite loops
• Divide check errors – these errors appear when a quantity is divided by
zero.
• Correct outputs only for selected data.
3. Logical errors
These errors are related to the logic of the program execution. Such actions as taking the
wrong path, failure to consider a particular condition and incorrect order of evaluation of
statements belong to this category. Logical errors do not show up as compiler- generated
error messages. Rather, they cause incorrect results. These errors are primarily due to a
poor understanding of the problem, incorrect translation of the algorithm and a lack of
clarity of the hierarchy (precedence) of operators.
But through the oversight, while coding, the formula is written as:
4. Latent errors
It is a ‘hidden’ error that shows up only when a particular set of data is used.
Example:
ratio = (x + y)/(p - q)
22
A Reference Book on C Programming
Debugging
After the program has been developed there may be different types of errors involved
which have to be corrected for the program to run successfully, this is called debugging.
Debugging is the process of isolating and correcting the errors.
Different methods are available for finding the location of execution errors and logical
errors within a program. Such methods are generally referred to as debugging
techniques. There are different types of debugging tools
1. Error Isolation
2. Single stepping (Trace into and step over)
3. Watches
4. Break Points
5. Tracing
1. Error Isolation
Error isolation is useful for locating an error resulting in a diagnostic message. If the
general location of the error is not known, it can be frequently be found by temporarily
deleting a portion of the program and then rerunning the program to see if the error
disappears. The temporary deletion is accomplished by surrounding the instructions with
markers (/* and */), causing the enclosed instructions to become comments. If the error
message then disappears, the deleted portion of the program contains the source of the
error.
2. Single Stepping
3. Watches
In Turbo C++, watch values can be defined by selecting Add Watch from the Debug
menu and then specifying one or more variables or expressions in the resulting dialog
23
A Reference Book on C Programming
box. The watch values will then be displayed within a separate window as the program
executes.
4. Break Points
To set a breakpoint in Turbo C++, select Add Breakpoint from the Debug menu, and
ten provide the requested information in the resulting dialog box. Or, select a particular
line within the program and designate it a breakpoint by pressing function key F5. The
breakpoint may later be disabled by again pressing F5. (Function key F5 is called a
“toggle” in this context, since it turns the breakpoint on or off by successively pressing
the key.)
5. Tracing
Tracing involves the use of printf statement to display the values assigned to certain key
variables, or to display the values that are calculated internally at various locations within
the program.
Testing
Testing is the process of reviewing and executing a program with the intent of detecting
errors, which may belong to any of the four kinds of errors discussed above.
24
A Reference Book on C Programming
Algorithm:
1. Start
2. Input length, l
3. Input breadth, b
4. Calculate Area A = l*b
5. Print Area, A
6. Stop (End)
Flowchart:
Start
Input l
Input b
A=l*b
Print Area, A
Stop
Fig: Flowchart
25
A Reference Book on C Programming
2. Write an algorithm and draw a flowchart to find the even and odd number.
Algorithm:
1. Start
2. Input number n
3. Rem = n mod 2
4. If Rem == 0
Print the number is even
Else
Print the number is odd
5. Stop
Flowchart:
Start
Input a number n
Divide n by 2 (Rem = n % 2)
Is
Rem
Stop
26
A Reference Book on C Programming
3. Write an algorithm and draw a flow chart to find the smallest of three different
numbers.
Algorithm:
1. Start
2. Input A, B, C
3. Is A<B ?
If yes goto step 4
If no goto step 5
4. Is A<C ?
If yes print A goto step 6
If no print C goto step 6
5. Is B<C ?
If yes print B goto step 6
If no print C goto step 6
6. Stop
Flowchart:
Start
Read A, B, C
T F
A<B
T F T F
A<C B<C
Stop
27
A Reference Book on C Programming
4. Write an algorithm and draw a flow chart to calculate the factorial of a number.
Algorithm:
1. Start
2. Input number N
3. Assign Fact = 1
4. Assign n = 1
5. Calculate Fact = Fact * n
6. Is N = n?
If yes goto step 8
7. Print Fact
8. Stop
Flowchart:
Start
Read N
Fact =1
Assign n=1
Fact =Fact * n
No
Is N =
n? Fact =1
Yes
Print Fact
Stop
28
A Reference Book On C Programming
Chapter 3.0
3. Introduction to C
C is a structured, high-level and hardware independent general purpose programming
language. Many popular software tools are written in C. C has strongly influenced many
other languages such as java, perl, etc. C is a very clear and concise language that forces
the user to understand fundamental aspects of computer programming.
C was developed by Ritchie in 1972 from two previous programming languages, BCPL
and B at the Bell Labs. It was used to develop modern operating systems such as UNIX.
By late 1970's C had evolved to “traditional C” the one developed by Ritchie. Many
slight variations of C existed, and were incompatible. A committee was formed to create
an “unambiguous, machine-independent” definition. The standard “ANSI C” was created
in 1989 by ANSI (American National Standards Institute), and was updated in 1999.
The types are important because we can store our data only in the suitable data types. The
fundamental types in C and their typical storage size (memory requirement) are listed in
table 3.1.
The maximum and minimum limits of int and char data types are defined in C file called
LIMITS.H. The size of mantissa and exponent for float and double types are defined in
FLOAT.H.
Qualifiers modify the storage limits of the above data types. There are four type
qualifiers that modify the storage limits of the above-mentioned types. These are short,
long, signed and unsigned. Some examples of types with qualifiers are listed in table 3.2.
30
A Reference Book On C Programming
All the values mentioned in table 3.1 and 3.2 are implementation dependent. For exact
values, refer to the LIMITS.H and FLOAT.H of your C compiler.
The numbers are stored in memory as combinations of 1’s and 0’s (binary numbers) as
shown in figure 3.1. In this figure, representation of an int number is shown.
0/1 0 1 1 1 0 1 0 1 1 0 1 0 1 1 0
15(±) 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
215 214 213 212 211 210 29 28 27 26 25 24 23 22 21 20
Here, if the number is signed int (or int only), the lower 15 bits represent the number and
16th bit represent the sign. If there is 1 in the 16th bit, the number is negative else the
number is positive. If the number is unsigned int, all 16 bits represent the number. Since,
the system is binary, the positional values of each bit in decimal are also shown in the
figure.
± 1.234567 ± 12
Sign of mantissa Mantissa Sign of exponent Exponent
A character type is usually enclosed in single quotation marks as ‘A’, ‘1’ etc and can be
treated as an integer number given by the ASCII code of the corresponding character.
Therefore, ‘A’ and 65 are equivalent because 65 is the ASCII code for character ‘A’.
In addition to these fundamental types, C has some more data types - void and enum,
which will be discussed in later chapters.
31
A Reference Book On C Programming
Expressions represent data items in a program. These could be single data items such as
integer; character etc or many data items interconnected by one or more operators. Some
examples are a + b, d = a / b.
The data items that are acted upon by different operators are also called operands. Some
operators require only one operand whereas most of them require two operands. The
operator that requires only one operand is called unary operator. The operator that
requires two operands is called binary operator. Examples of unary operator is unary
minus used to represent a negative number such as -a, -5 etc. Other examples of unary
operator in C are ++, -- etc. The examples of binary operator are the well-known
arithmetic operators +, -, *, / etc.
In C, there are different types of operators. Some of them are briefly discussed in this
section.
Arithmetic Operators
32
A Reference Book On C Programming
All the operators shown in table 3.3 are binary operators because they require two
operands. The % operator is valid only for integers. Other operators can be used for any
numeric data types. If there are different data types in an arithmetic expression,
specifically, floating point and integer, the result is floating point if the data item on the
right hand side is floating point and integer if it’s integer. For example, if a and c are
integers and b is float, then the result of c = a + b will be integer because c is integer. If a
is integer and b and c are floating point then the result of c = a + b will be floating point
because c is now float type.
The associativity of the arithmetic operators is left to right (L→R). It means when we
compute c = a / b, the first number (left) a is divided by second number (right) b and the
result is assigned to c. Similar is the case for other four operators as well. When two or
more operators are present in the same expression, there is an order by which the
expression is evaluated. This is called operator precedence. For example, if x = a + b / c
* d, then first b is divided by c and the result is then multiplied by d. After that the result
is added to a and assigned to x. If you have confusion, you can enclose the parts of
expression inside parentheses. An equivalent of last example would be x = a + (b / c) * d.
In case of expressions with parentheses, the expressions within the parentheses are
evaluated first.
The precedence and associativity of the arithmetic operators are shown in table 3.4.
33
A Reference Book On C Programming
Usage:
1. Integer Arithmetic
When two operands such as x and y are declared integers, an arithmetic operation
performed on these integers is called integer arithmetic. It always yields an integer value.
Eg:
Let x any y be declared by statement:
int x = 16, y = 5;
then
x + y = 21
x – y = 11
x * y = 80
x/y=3 The result is truncated, the decimal part is discarded.
x%y=1 The result is the remainder of the integer division and the sign of
the result is always the sign of the first operand
Eg:
6/8=0
-6 / -8 = 0
-6 / 8 = 0 or -1 The result is machine dependent
Eg:
Let a and b be declared by the statement:
float a = 14.0, b = 4.0;
Then
p = a/b = 3.500000
q = b/a = 0.285714
r = a + b = 18.000000
In mixed mode arithmetic, if either of the operands is real, the resultant value is always a
real value. For example, 35 / 5.0 = 7.0. Here, since 5.0 is a double constant, 35 is
converted to a double and the result is also a double.
34
A Reference Book On C Programming
Unary Operators
The unary operators act on single operands. The most common unary operator is unary
minus (-) that appears before a data item to make it negative. There are two other unary
operators called increment (++) and decrement (--) operators. The increment operator
causes its operand to increase by one, whereas the decrement operator causes its operand
to decrease by one. For example, if a = 5 then ++a increases the value of a to 6, whereas
--a decreases the value of a to 4.
The increment and decrement operators can be used in two ways – before the operand
(++a or --a, pre-increment or pre-decrement) and after the operand (a++ or a--, post-
increment or post-decrement). When the operator is used before the operand, then the
operand will be altered in value before it is used for its intended purpose. If the operator
follows the operand, the value of operand is altered only after the operand has been used
for its intended purpose. For example: take a = 1and the result of different increment
operations are shown below.
In the above example, in the first part (first three lines), we have displayed the original
value, then pre-increment the data item and display it (so we get 2) and the value after
increment is still 2. In the second part, last three lines; first, we displayed the original
value, then post increment it and display (we get 1 because the number is first displayed
then only increased) and finally we get the result as 2 because it was increased in the
previous statement after display. The decrement operator works in the same way.
Another unary operator is sizeof operator. This operator returns the size of its operand in
byte. This operator always precedes its operand. For example, if a is an integer type then
the result of sizeof (a) will be 2, which is same as sizeof (int).
Another unary operator is called the cast operator. The cast converts one type of data
item to another. For example if a is float and we want only the integer part of a, we can
use cast operator as: (int) a. The result of this can also be assigned to another data item
such as by c = (int) a. Remember, while casting always put the operator in parentheses
before the operand. Therefore, the syntax is: (type) operand.
Another unary operator is ! (the not operator). This operator takes its operand as true (not
equal to zero) or false (equal to zero) usually in the form of an arithmetic or conditional
35
A Reference Book On C Programming
expression and converts it into the opposite. For example, if we have an expression ! b,
the result of this will be false if b is true and vice versa.
Unary operators have a higher precedence than arithmetic operators. The associativity of
unary operators is right to left.
The relational operators identify the relation between its operands. Logical operators
make decision of true or false from its operands. There are four relational operators.
These are shown in table 3.5.
OperatorOperation Example
< Less than a<b
<= Less than or equal a <= b
> Greater than a>b
>= Greater than or a >= b
equal
== Equal to a == b
!= Not equal to a != b
Suppose there are two operands a and b. The first operator (<) compares a with b. If a is
less than b then the result is true else the result is false. The second operator (<=)
compares a with b. If a is less than b or equal to b then the result is true else the result is
false. The third operator (>) produces true if a is greater than b whereas the fourth
operator (>=) produces true if a is greater than or equal to b.
In addition to these four operators, there are two other relational operators, which are
more commonly identified as equality operators. They are also listed in table 3.5. The
first of these two (==) produce true if and only if a and b are equal. The second (!=)
produce true if and only if a and b are not equal.
The precedence of these operators is lower than that of unary and arithmetic operators.
These all have left to right associativity. The four relational operators have the same
precedence. Precedence of equality operators is beneath the other four relational
operators.
These six operators form logical expressions that represent conditions, which can be
either true or false. The result will be integer type having one of the two possible values 1
(for true) and 0 (for false).
In addition to these relational and equality operators, C has logical operators. The logical
operators act upon the logical operands (true or false) usually obtained as the result of
36
A Reference Book On C Programming
relational or equality operators. The effect of these two operators is to combine the effect
of more than one relational operator into more complex logical expression, the result of
which can only be true or false.
These logical operators are logical OR (||) and logical AND (&&). These are binary
operators. The result of logical OR operator is true if either or both of its two operands
are true. The result of logical AND operator is true only when both of its operands are
true.
The logical operator (!) is equivalent to boolean operation NOT, it has only one operand,
located at its right, and the only thing that it does is to invert the value of it, producing
false if its operand is true and true if its operand is false. It is like to say that it
returns the opposite result of evaluating its operand. For example:
!(5 == 5)
returns false because the expression at its right (5 == 5)
would be true.
!(6 <= 4) returns true because (6 <= 4) would be false.
!true returns false.
!false returns true.
Logic operators && and || are used when evaluating two expressions to obtain a single
result. They correspond with boolean logic operations AND and OR respectively. The
result of them depends on the relation between its two operands:
First Second
result result
Operand Operand a && b a || b
a b
true true true true
true false false true
false true false true
false false false false
For example:
( (5 == 5) && (3 > 6) ) returns false ( true && false ).
( (5 == 5) || (3 > 6)) returns true ( true || false ).
Logical operators have the precedence beneath the equality operators. Out of these two,
AND operator has higher precedence than OR operator. The associativity of both are left
to right.
37
A Reference Book On C Programming
Assignment Operators
The assignment operators are used to make assignment expressions. Most of the
computations in C are carried using arithmetic operators along with assignment
expression. Assignment expressions assign the value of one data item or the result of
arithmetic, conditional or other expression to another data item. The mostly used
assignment operator is the = operator. Remember, this is different from the == equality
operator. An example of assignment operator is b = c + d, where c and d are added and
assigned to b. Thus, it follows that the associativity of assignment operator is right to left
that is the value on the left of = is assigned to the data item on the right of =. The
precedence of assignment operator is the lowest (just above the comma operator, which is
has the lowest precedence).
There are some additional assignment operators. They are +=, -=, =, /= and %=. There are
others as well but we will not consider them here. These are shorthand assignment of the
expressions such as expression1 = expression1 + expression2, which is written in the
form of above assignment expression as expression1 +=expression2. Similarly, other
operators -, *, / and % follow. The precedence and associativity of these assignment
operators are also same as those of = assignment operator.
The table 3.6 shows the associativity of the above mentioned operators in the order of
increasing precedence.
assigns the integer value 5 to variable a. The part at the left of the = operator is known as
lvalue (left value) and the right one as rvalue (right value). lvalue must always be a
variable whereas the right side can be either a constant, a variable, the result of an
operation or any combination of them.
38
A Reference Book On C Programming
It is necessary to emphasize that the assignation operation always takes place from right
to left and never at the inverse.
a = b;
assigns to variable a (lvalue) the value that contains variable b (rvalue) independently of
the value that was stored in a at that moment. Consider also that we are only assigning
the value of b to a and that a later change of b would not affect the new value of a.
For example, if we take this code
it will give us as result that the value contained in a is 4 and the one contained in b is 7.
The final modification of b has not affected a, although before we have declared a = b;
(right-to-left rule).
A property that has the assignation operation in C over other programming languages is
that an assignation operation can be used as rvalue (or part of an rvalue) for another
assignation.
For example:
a = 2 + (b = 5);
is equivalent to:
b = 5;
a = 2 + b;
that means: first assign 5 to variable b and then assign to a the value 2 plus the result of
the previous assignation of b (that is 5), remaining a with a final value of 7. Thus, the
following expression is also valid in C:
a = b = c = 5;
Eg: z = a * b / c;
Here * and / enjoy the same priority. But a * b would be performed first, because
* operator enjoys Left to Right associativity. The result obtained would then be divided
by c.
39
A Reference Book On C Programming
Eg: z = a * b + c / d
Here * and / enjoys same priority. However, in this case the compiler is free to perform *
or / operation in any order if the compiler can guarantee a consistent result.
Eg: z = 3 / 2 *5
This is a very simple program that displays a message “C Programming is Easy!”. The
program consists of main function which another function printf is accessed. This printf
function is used to display the message on the screen. The line 1 of the program is a
comment that explains in brief, what the purpose of the program. Similarly, line 7 of the
program is also a comment that indicated the program is completed. The comments can
appear anywhere in the program and are ignored by the compiler when compiling. It is
used to inform the programmer the function of the different program elements used in
making a big program. It is good practice to provide comments even in smaller programs
that will help to identify the program elements later for other programmers of the original
programmer. These comments can be multi-line.
Line 2 of the program includes a file named stdio.h. this inclusion of the file is needed
because we are going to use a function named printf whose prototype is defined in the
file named stdio.h. This type of files with .h extension are called header files because
40
A Reference Book On C Programming
they form the head of the program. The syntax for including a header file is #include
<header filename>. The symbol (#) is a part of the standard syntax, which indicates
include as a preprocessor directive to include the defined file to the program before
compiling. More on this in later chapters. If we call any other function, we have to
include appropriate header file containing the definition of the functions we are going to
use.
Line 3 starts the program by calling the main function. There are no arguments passed to
the function (empty parentheses).
Line 4 has just one symbol, the opening curly brace. This indicates the body of the main
function starts from here. Anything written inside this opening brace and its closing
counterpart in line 6 are the body of the main function.
Line 5 calls function printf whose prototype is defined in the included header file stdio.h.
There is one argument passed to this function – the message “C Programming is Easy!”
This function writes the argument passed to it on the display. Notice, there is a
semiciolon after the end of this line. This semicolon indicates that the statement is
finished. This program will not compile if this semicolon is missed out.
The program (body of main) is closed in line 6 and the program finishes after a comment
on line 7.
C Programming is Easy!
41
A Reference Book On C Programming
Here is another program that adds two numbers (integers) and displays the result on the
screen.
The program starts with a comment in line 1. Line 2, 3 and 4 are same as in program 1. In
line 5, an integer data item named a is declared and its value is defined to be 5. In line 6,
two integer data items are declared but their values are not defined in the declarations.
The memory address assigned to them may contain some garbage value left out by the
previously run program. In line 7, the data item b is assigned a value 10. In line 8, the
content of a and b are summed up and assigned to c. In line 10, the main body is closed
and a comment put on line 12.
In line 9, we are displaying and integer data type. To inform printf ( ) function that we
are going to display an integer data, we provide a format specifier “%d” to it along with
the message “The sum is: ”. After the message and format specifier, we provide the data
item’s name whose content is to be displayed separating the message by a comma. More
on format specifiers in chapter 4. The result of the above program is :
After understanding, all these etiquettes, we define some frequently used terminologies in
programming and few rules of C for them.
Identifier
An identifier is the name given to various program elements or data items such as
variables, constants function names, structures etc. there are different rules for giving
identifiers to data items.
42
A Reference Book On C Programming
• The typical length of identifiers is 31, but 8 are guaranteed across other
implementations.
• Identifiers cannot be reserved word, called keywords of C.
• It is case sensitive. Uppercase and lowercase letters are different.
• It cannot contain spaces.
Some valid identifiers are I, k, roll_no, name_of_student, b52_b16 etc. Identifiers such as
1j, AT&T, roll no, etc are invalid.
Variable
A data item whose value can be changed anywhere inside a program is called a variable.
While giving an identifier to a variable, it is wise to choose names that are related to the
purpose of the variable. For example, if we want to name a variable that stores roll
number of a student, it is better to user roll_no rather than rn.
Syntax:
variable_type variable_list;
Constant
A constant is a data item whose value remains unchanged through out the life of the
program. An example of a constant is the value PI. Everywhere its value is 3.14159… It’s
a good programming practice to use constants whenever fixed numbers are to be used in
a program. This will ease to debug as well as change the value for that constant. A
constant is usually, identified with capital letters. There are two ways to use a constant.
One way is to declare it with const keyword. The other way is to use a preprocessor
directive #DEFINE (also called symbolic constant).
#DEFINE PI 3.14159
const float PI = 3.14159;
43
A Reference Book On C Programming
We can define our own names for constants that you use quite often without having to
resource to variables, simply by using the #define preprocessor directive. This is its
format:
For example:
#define PI 3.14159265
#define NEWLINE '\n'
#define WIDTH 100
They define three new constants. Once they are declared, we are able to use them in the
rest of the code as any if they were any other constant, for example:
circle = 2 * PI * r;
In fact the only thing that the compiler does when it finds #define directives is to replace
literally any occurrence of the them (in the previous example, PI, NEWLINE or WIDTH) by
the code to which they have been defined (3.14159265, '\n' and 100, respectively). For
this reason, #define constants are considered macro constants.
The #define directive is not a code instruction; it is a directive for the preprocessor,
reason why it assumes the whole line as the directive and does not require a semicolon
(;) at the end of it. If we include a semicolon character (;) at the end, it will also be
added when the preprocessor will substitute any occurrence of the defined constant
within the body of the program.
With the const prefix we can declare constants with a specific type exactly as we would
do with a variable:
In case that the type was not specified (as in the last example) the compiler assumes that
it is type int.
44
A Reference Book On C Programming
1. Numeric Constant
It is divided into two parts:
a. Integer Constant
It supports only whole number. It may be decimal number (0 to 9) or octal
number (0 to 7) or hexadecimal number (0 to F). Eg:
2. Character Constant
It may be single letter, number, any sign or special symbol given within single
quotes.
Eg: ‘a’ , ‘?’, ‘\b’, ‘\x1B’ , (symbol of ESC in ASCII code) etc.
3. String Constant
It is group of symbols inclosed by double quote (“ ”).
Eg: “it is group of symbols \n ” , “Kathmandu” , “Nepal” etc.
Escape Sequences:
45
A Reference Book On C Programming
Lowercase = uppercase + 32
Uppercase = lowercase – 32
Keywords
Keywords are the reserved words for the programming language, which have special and
predefined meaning to its compiler. These keywords are the part of the language, used to
identify data types, qualifiers and some statements. A statement causes the computer to
carry out some action. C language has 32 keywords, which you have to avoid when
giving names to variables and constants or function names etc.
The keywords are listed in table 3.8
46
A Reference Book On C Programming
Delimiters
A delimiter is a nothing but a symbol that has a syntactic meaning and significance in C
language. They do not specify any operation to yield a value. They are shown in table:
47
A Reference Book On C Programming
Chapter 4.0
Example:
#include<stdio.h>
void main ()
{
char c;
printf(“Enter one character:”);
c = getchar();
printf(“The character you type is : ”);
putchar(c);
}
Output:
Enter one character: a
The character you type is : a
48
A Reference Book On C Programming
Example:
#include<stdio.h>
void main()
{
char s[40];
printf(“Type a string (Less than 40 characters):”);
gets(s);
printf(“The string you typed is :”);
puts(s);
}
Output:
Type a string (Less than 40 characters): C Programming
The string you typed is: C Programming
#include<stdio.h>
#include<conio.h>
void main(void)
{
int a=12345;
float b=1224.2365;
char p='a';
char name[30]="Kathmandu Engineering College";
clrscr();
printf("%d\n\n",a);
printf("%2d\n\n",a);
printf("%40d\n\n",a);
printf("%040d\n\n",a);
printf("%-40d\n\n",a);
printf("%f\n",b);
printf("%9.4f\n",b);
printf("%10.2f\n",b);
printf("%-10.1f\n",b);
printf("%10.2e\n",b);
printf("%10.2e\n",-b);
printf("%10c\n",p);
printf("%s\n",name);
printf("%10s\n",name);
printf("%20.10s\n",name);
printf("%.5s\n",name);
getch();
}
49
A Reference Book On C Programming
12345
12345
12345
0000000000000000000000000000000000012345
12345
1224.236450
1224.2365
1224.24
1224.2
1.22e+03
-1.22e+03
a
Kathmandu Engineering College2aæÖD
Kathmandu Engineering College2aæÖD
Kathmandu
Kathm
50
A Reference Book On C Programming
Chapter 5.0
51
A Reference Book On C Programming
A repetitive structure helps to execute group of instructions more than once. The
instructions to be executed more than once are placed inside the repetitive structure and
executed. If the number of execution is specified, the repetition should be stopped after
the repetition count reached that number and normal flow of the program resumes. If the
number of repetition is not known in priori, the group of instructions are executed
repeatedly until some condition is satisfied. In this case, a selective structure is used to
continue or terminate the repetitive structure. This is also called looping. An example of
the repetitive structure is shown below. The program finds the Greatest Common Divisor
of two numbers entered by the user.
main(){
1. Start the program.
int M, N, R, temp;
2. Read two numbers.
scanf("%d%d",&M,&N);
3. Assign M and N the value of larger and
if (N>M) {
smaller of the two inputs, respectively.
temp=N; N=M; M=temp;
4. Divide M by N, and call the remainder R.
}
5. If R is not 0, then
R = M % N;
assign M the value of N,
while (R>0){
assign N the value of R, and return to step 4.
M=N; N=R; R = M % N;
Otherwise,
}
the answer is N.
printf("%d",N);
6. End the program.
}
In C, there are various methods that can control the flow of logic in a program. The flow
controlling condition is derived from one or more of the relational and logical operators
described in chapter 3.
if (expression) statement(s)
In this format, if the result (value of expression) is non-zero that is true, then the
statement(s) will be executed otherwise it will be skipped and the program continues. If
the statement(s) consists of more than one lines of code, they should be enclosed in {}
and such statement is called compound statement, which is treated as a single statement
by the compiler. Remember, the expression must always be enclosed in parentheses. If
the expression is longer, extra parentheses can be used to follow the precedence rules and
for clarity of the expression.
52
A Reference Book On C Programming
Flowchart:
if (r>0) {
if (a>0) printf("%d",a); /* if a > 0 display a m = n;
*/ n = r;
printf("\nEnd"); } /*compound statement consisting of 2 lines of
code */
53
A Reference Book On C Programming
if (expression)
statement1
else
statement2
In this format, if expression is true (value other than zero) then statement1 (a single line
of code or a compound statement with multiple lines of code, which in turn, can contain
other control statements) will be executed and the normal flow continues, otherwise
(expression results to zero), statement2 (a single line of code or a compound statement
with multiple lines of code) will be executed and the normal program flow continues.
Flowchart:
54
A Reference Book On C Programming
55
A Reference Book On C Programming
Flowchart:
Nested if statements
The if statement can be placed inside another if or if else statement. The compound
statement resulting from such structure is called nested if. In fact, the above mentioned if
else if format is also a nested if else structure where if statements are nested in the else
clause of each if statement except the last one.
56
A Reference Book On C Programming
We can put complete if else statements within the if or else clause of preceding if
statement. The only thing required is some care so that the else clause would not be
misplaced and belong to another if clause other than the intended one. One example of
this is shown below.
if (expression1) In this example, if expression1 is true and expression2 is also true, then
if (expression2) statement1 will be executed. If expression1 is true and expression2 is false,
statement1 statement2 will be executed. But, if expression1 is false, neither statement1
else nor statement2 will be executed. This is because, the else clause belongs to
statement2 the second if statement (i.e. if (expression2)).
The other example below shows how the else clause will be associated with the first if
statement (i.e. if(expression1)).
if (expression1)
In this example, if expression1 is true and expression2 is also true, then
{
statement1 will be executed. If expression1 is true and expression2 is false, there
if
are no instructions to be executed within this control statement and control is
(expression2)
transferred out of this structure. But, if expression1 is false, then statement2 will
statement1
be executed. This is because, the braces ({}) after the first if statement and
}
statement1 cause the else clause to be associated with the first if statement (i.e. if
else
(expression1)).
statement2
An example of nested if else statements are shown below. The program finds the largest
of three numbers.
if (a>b)
if (a>c) /* a>b and a>c so a is the largest */
l = a;
else /* a>b but a<c so c is the largest */
l=c;
else
if (b>c) /* b<a and b>c so b is the largest */
l=b;
else /* b<a and b<c so c is the largest */
l=c;
57
A Reference Book On C Programming
58
A Reference Book On C Programming
Flowchart:
switch ( expression ){
In switch statement, the result of expression should be an integer value
case value1:
(or a character because character can be treated a small integer). The
statement1;
selection will be made on the basis of the result of this expression. If the
break;
expression results in any one of the values labeled as value1 (case
case value2:
value1:) upto valueN, the corresponding statement (e.g. statement1 if
statement2;
expression equals value1) gets executed and the control is transferred
break;
out of the switch block. For switch statement to work, case labels or case
...
prefixes must be unique. case is the required keyword and every label
case valueN:
must be followed by a colon (:). The value of case label must be a
statementN;
integer or character constant not a variable.
break;
If expression does not match any of the case labels, statements after the
default:
default label (if provided) gets executed. In this example, statement
defaultactions;
defaultactions will be executed in such case.
}
59
A Reference Book On C Programming
The break statement is needed to terminate the switch after execution of one choice.
Otherwise the next case would get evaluated. The default clause is optional, and is
executed if the cases are not met. The right brace at the end signifies the end of the case
selections. The order of the 'case' statements is unimportant and the default clause may
occur anywhere (convention places it last). The break statement at the last case is also
optional. But, it's a good programming practice to put a break in default as well. If you
later add some cases after default, you don't have to worry about the break statement. An
example of switch statement is shown below. The program performs one of five
arithmetic operations with two numbers and one operator entered by the user. If the
operator is not one of the five arithmetic operators, the program will display some error
message.
Conditional operator ? :
The ternary operator ? : (operator that take three operands) is a more efficient form for
expressing simple if statements. It has the following form
expression1 ? expression2 : expression3
This simply states, if expression1 is true, then expression2 else expression3! An example
of the use of conditional operator is:
c = (a > b) ? a : b ;
60
A Reference Book On C Programming
The above statement checks if a is greater than b. If it's so, set c = a else set c = b. This
one line of code will assign c the greater of a and b. The code achieved with if else would
be if (a>b) c = a; else c= b;
Y N
Test
Expre
ssion
Expression 1 Expression 2
Exit
In C, different methods can be used to perform repeated task. The process is called
looping. (This will be the most frequently used term to refer repetitive structure in this
section)
The expression1 is used to initialize a control parameter that will control the looping.
This is called index variable. The expression2 defines the continuity condition of the for
loop. The loop will continue to repeat till this condition satisfies. If the condition never
satisfies, the loop will execute forever. This expression usually checks for certain value
of the index variable. The expression3 is usually used to change the index variable by
increasing or decreasing in value such that the condition specified in expression2 may
eventually become false and the loop terminates. Typically, the expression1 is an
assignment expression, expression2 is a logical expression and expression3 is an
61
A Reference Book On C Programming
Here's how a for loop works: On entry to the for statement, the first expression
(expression1) is executed. After this, the next operation is to test whether expression2
evaluates to true or false. If it's false, the loop will terminate and if it is true, the loop
body will be executed. After the loop body is executed, expression3 is evaluated. After
that, again expression2 is executed to test whether after the execution of expression3, the
value of expression2 becomes false. If it becomes false, the loop will be terminated. If it's
still true, the loop body will be executed once more and the process repeats. The
flowchart of for loop is here:
62
A Reference Book On C Programming
An example of for loop to calculate the sum of first hundred numbers is shown below.
sum =0;
for (i=1; i<=100; i++)
sum += i;
printf("Sum of 1 to 100 is %d",sum);
printf("\nEnd");
Since, the test of looping condition is carried out before the execution of the loop body, in
some for loops, the loop body may not execute even once. All three expressions
mentioned above can be omitted from the for statement and we can construct an infinite
loop. But, by providing some method to change a variable inside the loop after each
execution and checking its value for certain condition, we can terminate the loop. But,
this is seldom done in practice. So, a statement like for (;;) loopbody is a valid code, but
the condition to exit the loop must be placed inside the loop body.
The for loop is an entry controlled loop because, the condition for looping is tested before
the loop body executes. Hence, there could be no looping at all. The features of for loop
lends it well suited for looping requirements where the number of loops that should be
performed is known in priori.
while (condition)
loopbody
In this statement, the loopbody will be executed till the condition is true. Once the
condition is out of bound (false), the loop terminates. Somewhere within the body of the
while loop a statement must alter the value of the condition to allow the loop to finish.
The variable upon which the while is dependant is initialized prior to the while statement.
Like for loop, while loop also tests the condition before entering the loop. If the condition
fails in the first step, the loop will terminate. Hence, in while loop also, there may be no
execution of the loop body and it is also an entry controlled loop. The flowchart of while
loop is here:
63
A Reference Book On C Programming
In some cases, the number of looping is known before the loop is executed. But this may
not be true in some cases. For example, when reading characters from a file or keyboard,
there may be unknown number of characters to be read. In such cases, the loop can be
constructed in such a way that when a special character is encountered, the loop will
terminate (such as end of line - "enter key" or end of file - EOF). Thus, the structure of
while loop makes it suitable for applications where the number of repetitions is not
known in advance. An example of such situation is shown below. In this program,
numbers are read from keyboard and added till the user enters zero. After that the sum
and average of the numbers entered is displayed.
64
A Reference Book On C Programming
The third looping method is the do while loop. This method is an extension of while
loop. The main distinction between the while loop and do while loop is that the former is
an entry controlled loop whereas the later is exit controlled loop. This means that in do
while loop, the condition for looping is done at the end of each pass of the loop. Thus, the
body of the do while always executes at least once. The general format of do while loop
is:
do statement
while (condition);
The statement inside the loop body is almost always a compound statement that will
perform some task and change the index variable. The condition is checked after
executing the loop body and if the condition is true the loop is executed again, otherwise,
the loop is terminated. It is, thus, a requirement that the variable used to test the condition
must be changed within the loop body or the loop is executed only once or it will never
stop. The flowchart of do-while loop is here:
65
A Reference Book On C Programming
Notice that there should be ; (semicolon) at the end of the while clause. If it is missed, the
program will not compile. An example of do-while loop is shown below. The program
computes the greatest common divisor of two numbers M and N.
main(){
int M, N, R, temp;
scanf ("%d %d",&M,&N);
if (N>M) {
temp=N;
N=M;
M=temp;
}
do {
R = M % N;
if (R>0){
M=N;
N=R;
}
}while (R>0);
printf ("%d is the answer", N);
printf("\n");
}
66
A Reference Book On C Programming
Nested loops
Like if statements, loops can also be nested that is one loop placed inside another. The
level of nesting can be more than one depending upon the requirement. The compound
statement resulting from such structure is called a nested loop.
We can put other control statements like if else statements, switch statements or any other
code inside the loops and nested loops. The one thing required is some care so that there
will be always a condition that will transfer the control of the program out of all the
nested loops. Otherwise, the computer will never come out of the loop and hang. One
example of nested loop is shown below. In this program, one for loop is nested within
another for loop and the program prints out the multiplication table from 1 to 10. It is not
necessary that the nested loops must be of same type. A for loop can be nested within a
while loop or do while loop and so on.
In this program, the outer for loop has i as index variable and
another for loop having index variable j is nested within it. The
int i, j; range of index variables in both loops are 1 to 10. The outer
for (i=1; i<=10; i++) { loop body repeats for 10 times and the inner loop body repeats
for (j=1; j<=10; j++) 10 times for each repetition of the outer loop. The loop body of
printf ("%d\t", i*j); inner loop is printf ("%d\t",i*j); and the loop body of outer
printf("\n"); loop is the inner loop and the statement printf("\n");. Since
} these are two separate statements that make one compound
printf("\n"); statement for outer loop, they are enclosed in {}.
The statement printf ("%d\t",i*j); executes 100 times and the
statement printf("\n"); executes 10 times.
The break statement, when used with switch statement, causes the transfer of control out
of the entire switch block to the first statement following the switch block. This is already
illustrated in the switch statement.
The break statement, when used with a loop (for, while or do while), causes the transfer
of control out of the loop to the first statement following the loop. When used with nested
loops, the loop inside which the break statement is contained, is terminated not the entire
nested loop. This is useful to terminate a loop when an error occurs or certain condition is
met and further repetition is not required.
67
A Reference Book On C Programming
Flowchart:
An example of use of break in for loop is shown below. The program finds whether a
number is a prime number or not.
clrscr();
printf ("Please enter an integer:");
scanf("%d",&num);
rem=1;
for (div=2; div<=num/2; div++){
rem = num % div;
if (rem==0){
/* If the number is divisible by 2, it's confirm
printf("\nThe number is not prime");
that the number not prime and there is no need
break;
of dividing number any more, so display the
}
message and terminate the loop */
}
if(rem!=0)
printf("The number is prime");
printf("\nEnd");
getch();
68
A Reference Book On C Programming
The continue statement is used to bypass (skip) the remaining statements in a loop. Note,
the break statement terminates the loop, whereas, continue statement does not. It causes
control to pass to the end of the loop, at which point the loop continuation condition is re-
evaluated. The syntax of continue statement is:
continue;
Flowchart:
69
A Reference Book On C Programming
An example of continue statement is shown below. This program counts the numbers
divisible by 3 or 5 till user enters numbers less than 1000. If user enters number greater
than 1000, the program will terminate with display of the count. If the user enters a
negative number or zero, program displays an error message and asks for another
number. The negative number or zero is not checked. Continue statement is used to skip
this check.
cnt=0;
do {
printf("\n\aPlease enter a number (>1000 to terminate):");
scanf("%d",&a);
if (a<=0) {
printf("\n\aPlease enter a positive number");
continue;
}
if (a%3==0 || a%5==0)
cnt++;
} while(a<1000);
printf("\n\aYou entered %d numbers divisible by 3 or 5",cnt);
printf("\n\nEnd");
goto label;
...
label: statement
The goto causes a jump to the statement which is labeled label. The label is an identifier
used to label the target statement to which the control will be transferred. If more than
one labels are used in a function, then they must be unique. The goto statement can be
used for branching, jumping to the end of the loop or out of the loop (not into the loop)
etc. But, the structured programming method requires that the program must written in
orderly and sequential manner. The use of goto violates this general rule and if the same
things can be done without using goto, why use it then?
Though, most of the programming languages provide this statement, most of the
programmers do not prefer to use it. But, in some cases, use of goto statement may result
in an efficient and easy coding. For example, when the control is to be transferred
completely out of the center of a highly nested loop.
70
A Reference Book On C Programming
71
A Reference Book On C Programming
Chapter 6.0
6.0 Functions
6.1 Introduction
C program is a collection of functions. Every program starts with main( ) function, from
which, other functions can be invoked (called or accessed).
A function is a block of code that performs one or more specific tasks within the source
program. The function may or may not return result to the caller (calling function), but
performs its intended action.
There are mainly two types of functions in C - Library functions and User-defined
functions. Library functions are the functions provided by the vendor with the C
compiler. You just need to include proper header files in your program to use them.
(Click here for a list of some frequently used functions, their header file and their
purpose). User-defined functions are the functions created by programmer for his/her
own requirement. This will be the subject matter of this chapter.
The use of user-defined functions allows the programmer to break up a large program
into a number of smaller program segments. The characteristics of such segments or
functions are:
The use of user-defined functions has many advantages. Some of them are:
In this chapter, we will see that a function performs its intended action whenever it is
accessed (invoked or called). After performing its action, it returns the control to the
point from where it was called. Function can be called many times from different parts of
a program. We will also see that the function is provided with required data items (known
as parameters or arguments) for processing and may or may not return the explicit
result or some indication that the process has been completed successfully. In the later
part of the chapter we will see that a function can call itself and perform the operation.
72
A Reference Book On C Programming
Function definition
Function definition specifies the name of the function, the types and parameters
(arguments) it expects to receive, and its return data type. The function definition also
includes the function body with declarations of the function's local variables and the
statements that determine what the function does. Function definitions differ from
function declarations in that they supply function bodies - the code that makes up the
function.
The first line of the function definition contains the name of the function, the data types
and parameters (arguments) it expects to receive, and return data type of the function.
The general format for the first line of a function definition is:
where the return_data_type is the data type of the value (result) that the function will
return to the calling program when it finished its task, function_name is the identifier that
gives the function a unique name. As this is an identifier, all the rules of an identifier
applies to the function name also. The argument_list is the arguments (parameters) if any
that are passed to the function to process. In the later part, we will see that we can pass
the parameters by value or by reference (?).
A function may or may not return any value to the calling function. It can return only one
value whether it is an integer, float, char or any other data type. If a function does not
return a value, the return type is simply assigned to be a void type, which means that the
function does not return any value to the calling program. If the return type is not
explicitly mentioned, most of the compilers assume that to be an integer type.
The name of the function is mandatory. That means it must be provided. The first letter of
the function name must be an alphabet (or your compiler will generate an error message -
declaration syntax error). The name must be unique in the program. That is, same name
cannot be given to two different functions.
The argument list (also called parameters, formal arguments or formal parameters) is
used to pass the necessary data to be processed in the function. These must be enclosed in
parentheses. There could be any number of parameters including none. Even if there are
no parameters, the parentheses must be provided. The argument list starts with the data
type and the name of the argument for each of them. The types of arguments can be
different. If there are no arguments required for a program, the keyword void can be used
inside the parentheses. This is not mandatory, but considered a good programming
practice because some versions of C may require this keyword in such cases. This is same
in case of return type as well.
73
A Reference Book On C Programming
So, to be more precise, the first line of the function body can generally be written as:
return_data_type function_name (type1 arg1, type2 arg2, type3 arg3 ... typeN argN)
The function body
The second part of the function definition is the body of the function. This portion of that
function definition contains the declarations of its local variables and executable
statements. The body of a function defines the operations that function will perform. It
may perform some operations (processing) on the passed arguments, or perform some
entirely new things and return or may not return the result or any other value. The body
can contain compound statements, control statements like if else, looping statements like
for, while or combinations of them and call different functions.
The result of the function or any value can be returned from the function by using return
statement. The syntax for return statement is:
return [(expression)];
Here, the expression is optional. For functions that do not return any result to the calling
program (void functions) we cannot provide expression. For other cases, we have to
provide the expression which is evaluated at the end of the function and the result is
returned to the calling function. The return statement can return only one value to the
calling function!.
A function with return type of void cannot return any value, so in this case, there would
be no expression. That means, if the expression is omitted, the control is transferred to the
calling program without returning any value. If the expression is present, the result of the
expression should match the return data type of the function. There can be any number of
return statements inside a function, but only one will be effective depending upon the
condition that which one is evaluated inside the function body when it completes its task.
If the return statement is not present in a function, the control still transfers to the calling
function but without any value or a garbage. When there is no return statement, control is
transferred back to the calling program when the closing brace of the function is reached.
It is called "falling off the end". An example of function without return statement is
shown below.
74
A Reference Book On C Programming
When there is one return statement at the end of the function, it statement is executed and
the control is transferred back to the calling program returning the value of expression
within that return statement. A function that computes the factorial of a number is shown
below.
As already mentioned, there could be more than one return statement, but only one will
be executed because when a return statement is executed, the expression is evaluated if
any and the control is transferred immediately back to the calling program. This is more
likely when different values must be returned for different conditions within the function.
An example of such a program is given below, which calculates absolute value of a
number.
If the expression results in a different data type than the return type of the function
automatic conversion of result is carried out to match the return type of the function. But
it is recommend to keep track of proper data types. Here's a simple program that returns
float type but the data to be returned is int. The type is automatically converted to float
while returning the result. The function returns square of number is it is negative and
cube otherwise.
75
A Reference Book On C Programming
Moreover, even a function returns a value, it is not necessary for the calling program to
use it. We have been doing this when we call printf function to print a message such as
printf ("Hello, there");. Similarly, we discard what ever key the user presses, returned by
getch(); at the end of every program. (Exercise: find out what value the printf function
returns by changing different arguments).
Like every variable, every function should be known to compiler before it can be used.
For library functions, this declaration is normally carried out by including the appropriate
header file at the start of the program using #include preprocessor directive. A
preprocessor directive tells the compiler to perform certain things (preprocess) before
compiling the program. Some examples are such as including the header files, defining
symbolic constants, conditional compilation for different operating system platforms. A
header file therefore, contains the function prototypes of the library functions.
There are two ways to declare a user-defined function. The first method is the bottom-up
approach where all functions are defined before calling them in the function main, that is,
by placing all function definitions before the function main. The next way is to declare all
functions before function main, and put their function definition after main. This is the
top-down approach followed by most of the programmers and that's what we are also
going to follow.
A function declaration precedes the function definition and specifies the name, return
type, storage class, and other attributes of a function. To be a prototype, the function
declaration must also establish types and identifiers for the function’s arguments. eg:
The prototype has the same form as the function definition, except that it is terminated by
a semicolon immediately following the closing parenthesis and therefore has no body.
The general syntax of function declaration is:
The return type could be any data type including void (for non result-returning functions).
The parameter type list is a comma separated list of data types of the arguments or
parameters. The name of parameters are not necessary because at this time the compiler
only needs to know their types. This is called the function declaration. But it will not be
76
A Reference Book On C Programming
wrong to provide them, which, actually helps the programmer to understand the purpose
of the arguments. This is often referred to as the function prototype. For simplicity, we
will consider both as same. Keyword void can be used in parameter list if there are no
arguments to pass.
The return type of the function must be same in function prototype and function
definition. If there is any mismatch, the compiler will complain. Similarly, the number of
arguments in the argument list must be same in both prototype and function definition.
Compiler will complain if there is a mismatch. In traditional C, it is not necessary (not
allowed) to mention the argument types in declaration. But to ensure correct result in
ANSI C, it must be mentioned. Eg. if the argument passed to a factorial function is a float
and the type is not mentioned in prototype, the returned result will not be correct. This
also indicates that automatic type coercion is carried out by the compiler if we try to
compute the factorial of a float number. That is, the compiler will convert the float
argument to int before calling the factorial function. One limitation of the arguments is
that they can not be initialized at the function definition.
• They establish the return type for functions that return types other than int.
Although functions that return int values do not require prototypes, prototypes are
recommended.
• The parameter list is used for checking the correspondence of arguments in the
function call with the parameters in the function definition.
77
A Reference Book On C Programming
Parts of a function
#include<stdio.h>
Some Examples
78
A Reference Book On C Programming
void calc_factorial(int);
void main()
{
int number;
clrscr();
printf(“Enter a number:”);
scanf(“%d”,&number);
calc_factorial(number);
getch();
}
void calc_factorial(int n)
{
int i, factorial =1;
for(i=1;i<=n;i++)
factorial=factorial*i;
printf(“The factorial of %d is %d”,n,factorial);
}
79
A Reference Book On C Programming
Function invocation
C program is a collection of functions and functions are accessed whenever required. The
program starts from the execution of function main from where other functions are
accessed or invoked. When a function is accessed, the control is transferred to the called
function and when the called function completes its job program control is transferred
back to the calling function and the program continues.
A function is invoked (called or accessed) by its name and required parameters within a
set of parentheses. The arguments must match in number (necessary) and type as defined
in the function definition.
Note:
void main( )
{
int i = 5;
printf (“Address of i = %u”,&i); //65522
printf(“\n Value of i = %d”,i); //5
printf(“\n Value of i = %d”,*(&i)); //5
}
When a function is called, the arguments are passed to the called function from the
calling function by value. It means that, each argument is evaluated (if necessary) and
their copy is provided to the called function. This form of passing the formal parameters
is known as call-by-value because only the values of arguments are passed from calling
function to called function and any change of the arguments in the called function does
not affect the value of data items in the calling function. An example is shown below.
80
A Reference Book On C Programming
void modify(int a)
{
a = a * 2;
printf(“\n a= %d(After being modified)”,a);
}
Output:
a = 5 (Before Calling the Function)
a = 10 (After being modified)
a = 5 (After Calling the Function)
Passing argument by value has pros and cons. On the pro side, it allows a single-valued
actual argument to be written as an expression rather than being restricted to a single
variable. In addition, if the actual argument is expressed simply as a single variable, it
protects the value of this variable from alterations within the called function. This is the
only way if we have to pass a constant value (such as 1, 'a'', 120 etc) to the function. The
81
A Reference Book On C Programming
con side is that, it does not allow information to be passed back to the calling function via
arguments. The net effect is the one way transfer of data.
Call by reference
In the previous section, it was mentioned that whenever variables are passed to a function
as arguments, the values are copied to the corresponding function parameters and the
variables inside the calling function are not changed.
In certain cases, it may be useful to have the called function change the variables inside
the calling function. If the arguments are passed in such a way as to change the variables
in the calling function, the function invocation is known as call-by-reference. In C, this
is achieved by passing the address of the variable as an argument to the function rather
than passing the copy of the variable. When the address of the original variable is
accessible to the called function, it can place any value (depending upon the requirement)
to that address. As the same address is used in calling program as well, the content of the
original variable will be changed. An example of swapping the content of two variables
are shown below.
In the above program, swap function takes addresses of variable a and b from function
main and exchanges the content of those address with the the help of a temporary
variable. Note that, &a and &b are used to pass their addresses. (& is called the address
of operator). Similarly, in swap function, *m and *n are used to access the content of
addresses of a and b respectively.
As with call by value, in call by reference also, all the eight steps take place. The only
difference is that instead of passing arguments by value, their address (reference) are
passed.
Passing arguments by reference also has some pros and cons. The major advantage is that
the content of original variables can be changed from the called function. That means,
more than one results can be passed to the calling function from the called function
82
A Reference Book On C Programming
through these references. But, this should be done with proper care. Otherwise unwanted
values may be written to the reference and the original data is lost.
We already know that a variable is characterized by its data type - the type of data it can
store; examples are integer (int), floating point (float), character (char) etc. But
there is another type of characterization of a variable - the storage class.
The data type defines what type of data a variable can hold. On the other hand, storage
class of a variable defines the permanence (life time - how long the variable reside in
memory) and the scope (the portion of the program over which it is recognized).
The storage class is specified with one of four keywords - auto, extern, static and
register. Out of these four, we will briefly discuss the first three. For fourth, see
Programming with C (Gottfried) p. 424 onwards. The three keywords - auto, extern and
static are used with automatic (local), external (global) and static variables respectively.
The storage class associated with a variable is sometimes automatically specified by the
location where the variable is declared. Sometimes, it must be explicitly specified.
Variables declared within a function are automatic by default. It means, their scope (life-
time) is restricted to that function. Automatic variables with same name in different
functions are different from each other (contain separate values and have separate
memory address). Thus, automatic variables are the most common variables. An
automatic variable is also called a local variable because it is local to that function only.
Since the location of the variable declaration determines the storage class, the keyword
auto is seldom used to declare an automatic variable (in fact it is not necessary). The
variables declared within a block (eg a compound statement or a loop) are also automatic
by default. So, scope of such variables is restricted within the block where they are
declared.
Memory space for automatic variables are reserved when the control is transferred to the
function or block where they are declared. The content of an automatic variable is lost as
soon as the control is out of function or block where it is defined.
An automatic variable is not initialized by the compiler. The programmer must initialize
or assign proper expression to it before it can be used. Otherwise, it will contain some
garbage value.
Following are two sample programs using automatic (local) variables. The first example
uses a function to use automatic variable. The second example has automatic (local)
variables in a compound statement and a loop.
83
A Reference Book On C Programming
#include<stdio.h>
int fun (int num1, int num2); #include<stdio.h>
void main(){
void main() { int i;
auto int a =10; /* local (automatic) to printf ("Number Square Cube");
main*/ for (i=1; i<= 10; i++){
int result; /* local (automatic) to main*/ auto int b; /* local inside for loop not
result = fun (20, 30); accessible outside*/
printf("\n%d", result); int a = i * i; /* local inside for loop */
printf("\n%d", a); b = a * i;
} printf("\n %d\t%d\t%d", i, a, b);
}
int fun (int num1, int num2) { printf("\n%d", i); /* i is accessible here */
int a; /* local to fun, different from a of printf("\n%d %d", a, b);
main /* a and b are not accessible here
not accessible from other function so this line gives compilation error
*/ undefined symbol a in function main
a = num1 + num2; undefined symbol b in function main
/* num1 and num2 are also automatic */ so remove this line before compiling */
return a; }
}
What will happen if same variable is declared twice inside the same function? One is
declared at the start of the function and another inside a block in the same function. An
example is shown below.
Here's a brief explanation. Variable x is first declared in the function level. So this
variable is accessible anywhere within this function (somefunction). In the middle of
84
A Reference Book On C Programming
void function1( )
{
int test_variable = 10;
printf(“%d\n”,test_variable);
}
void function2( )
{
int test_variable = 100;
function1( );
printf(“%d\n”,test_variable);
}
Output:
10
100
1000
85
A Reference Book On C Programming
Since their scope is the whole program, global variables are accessible from any function.
So their content can be changed from any function and the new value is accessible from
other functions as well. This can be a convenient means of transferring data across
functions. Because they are accessible from anywhere, appropriate values can be set
before calling a function. In side that called function their values can be changed as
required. Thus, use of global variables eliminate the need of passing values as arguments
from one function to other. This may be particularly useful if the number of arguments to
be passed are large. But, this may not always be a good idea. This is because as they are
accessible from anywhere, their values might have been changed with mistakes and the
program may not work as desired.
The life time of a global variable is throughout the life of the program. Once created,
global variables remain in memory till the program ends.
A global variable is declared at the top of the program before writing any function. Since
the storage class (local or global) of a variable is characterized by the location where it is
declared, a global variable declared at the top of a program file does not require extern
keyword. In fact, most of the compilers do not permit to use this keyword.
Global variables are initialized to zero if they are not initialized at the time of declaration.
If another variable is declared local in a function with the same name as the global
variable, the global variable is shaded by the local variable within that function, that is,
global variable is not accessible inside that function. All other functions have access to
the global variable. Any changes made to the variable inside that function will affect only
the local variable.
An example of a program using global variable and local variable is given below.
void main() {
printf("\nFrom main a = %d", a); /* global */ /* 20 */
integ ();
otherfun (); /* 21 */
printf("\nFrom main new value of a = %d", a); /* global */
}
void integ () {
printf("\nFrom integ a = %d", a++); /* global a accessible from /* 20 */
here */
return;
}
86
A Reference Book On C Programming
void otherfun () {
int a; /* local a different from global a, global a not
accessible*/
a =1000; /* 1001 */
printf("\nFrom other function a = %d",++a);
return;
}
void function1(void)
{
a = a + 10;
printf(“a = %d\n”,a);
}
void function2(void)
{
a = a + 10;
printf(“a = %d\n”,a);
}
Output:
a = 20
a = 30
a = 30
87
A Reference Book On C Programming
Static Variables
A static variable is amalgam of a local and a global variable. The scope of a static
variable is same as local variable. It means that a static variable is accessible only within
the defining function or block. The life time of a static variable is same as that of a global
variable. Once created (initialized on the first call to function), a static variable remains in
memory until the program ends. It means that, a static variable declared within a function
retains its value even the function is not executing currently and when the function is
reentered, its old value is maintained. In other words, the value of the variable on leaving
the function remains intact. On the next call to the function the the static variable has
the same value as on leaving. Moreover, like a global variable, if a static variable is not
initialized at the time of declaration, it is automatically initialized to zero.
void fun();
/* output */
void main() {
fun();
/* This is call no. 1
fun();
*/
fun();
/* This is call no. 2
}
*/
/* This is call no. 3
void fun() {
*/
static int a=1; /* static variable
a */
printf("\n This is call no. %d",
a);
a++;
return;
}
What is recursion?
Recursion is a process by which a function calls itself repeatedly until some specified
conditions has been satisfied. Recursive functions are very useful in computations in
which each action can be stated in terms of previous result.
88
A Reference Book On C Programming
There are two major requirements while making a recursive function. The first important
thing to remember when creating a recursive function is to give an end-condition. We
don't want the function to keep calling itself forever, do we? Somehow, it should know
when to stop. The other thing is as already mentioned above the problem should be
convertible to a recursive algorithm.
A very popular and simple example is the factorial of a number. Factorial of a number,
say n, is equal to the product of all integers from 1 to n. Factorial of n is denoted by n! =
1x2x3...x n. Eg: 10! = 1x2x3x4x5x6x7x8x9x10
The simplest program to calculate factorial of a number is a loop with a product variable.
Instead, it is possible to give a recursive definition for Factorial as follows:
There are many ways of providing the stopping condition. One of the simplest is by
means of an if statement, as above. In the above example, the recursion stops when n
reaches 1. In each instance of the function, the value of n keeps decreasing. So it
ultimately reaches 1 and ends.
The recursive function calls are not executed immediately. They are placed on a stack (a
portion of memory working just like a pile of dishes where every new dish is placed on
the top and popped from top whenever required) until the condition that terminates the
recursion is encountered. The function calls are then executed in reverse order as they are
popped off the stack. So the actual process of getting the factorial of a number would be
as shown below.
89
A Reference Book On C Programming
First put function calls into the stack After stopping condition, evaluation
in this order proceeds this order
n! = n x (n-1)! 1! = 1
(n-1)! = (n-1) x (n-2)! 2! = 2 x 1! = 2
(n-2)! = (n-2) x (n-3)! 3! = 3 x 2! = 6
(n-3)! = (n-3) x (n-4)! 4! = 4 x 3! = 24
... ...
2! = 2 x 1 (n-1)! = (n-1) x (n-2)!
1! = 1 n! = n x (n-1)!
Similarly, any recursive function heavily utilizes the stack memory. Some other examples
are getting Fibonacci numbers, magic squares, reversing a string etc.
Fibonacci series:
0 , 1, 1, 2, 3, 5, …………
#include<stdio.h>
#include<conio.h>
90
A Reference Book On C Programming
#include<stdio.h>
#include<conio.h>
void main(void)
{
char *string; //char string[10];
clrscr();
printf("Enter String:");
scanf("%s",string); // gets(string);
printf("The length of the string is %d",length(string));
getch();
}
I
int length(char str[])
{
static int i;
if(*(str+i)=='\0') //if(str[i]=='\0')
return i;
else
{
i=i+1;
length(str);
}
}
91
A Reference Book On C Programming
92
A Reference Book On C Programming
93
A Reference Book On C Programming
Storage Class:
The life time of the variable is categorized by its storage class. The storage class of a
variable indicates the allocation of storage space to the variable.
Scope of variable:
The scope of the variable determines over what part of a variable is actually available
for use.
Longetivity:
It refers to the period during which a variable retains a given value during execution of
a program (alive).
Extent of variables:
The period time during which memory is associated with a variable is called the extent
of the variable.
94
A Reference Book On C Programming
main( )
{
register int i;
for(i = 1; i<=10; i++)
printf(“\n%d”,i);
}
95
A Reference Book On C Programming
Categories of functions
1. Library functions
2. User defined functions
1. Library functions- These functions have already been written, compiled and placed in
libraries. printf , scanf , getch etc are examples of library functions.
2. User defined functions- These are function which the programmer creates and uses in
a C program .
1. Functions with arguments and return values -The above described add function is
in this category. Which is taking two arguments a and b form the calling function (main)
and it will return the sum to the calling function.
2. Functions with arguments and no return values - The above example can be written
in the following form where the called function takes the arguments(a and b) from the
calling function but do not return the sum(s) the calling function. The add function
displays the result itself. So return type is void and the return statement is not required in
the called function.
96
A Reference Book On C Programming
#include<stdio.h>
#include<conio.h>
void add(int, float);
void main()
{
int a;
float b;
printf("Enter values of a and b:");
scanf("%d%f",&a,&b);
add(a,b);
getch();
}
void add(int p, float q)
{ float s;
s=p+q;
printf("\nSum=%f",s);
}
3. Functions with no arguments and no return values- Above example can also be
written in the following way. Study the following example and find the differences.( void
return type, no arguments, no return statement, function add reads the value of a and b
itself and displays the result(s) from the function body, main function only calls the add
function all the other activities are done inside the called function body.)
#include<stdio.h>
#include<conio.h>
void add();
void main()
{ add();
getch();
}
void add()
{ int a;
float b,s;
printf("Enter values of a and b:");
scanf("%d%f",&a,&b);
s=a+b;
printf("\nThe sum=%f",s);
}
97
A Reference Book On C Programming
4. Functions with no arguments and return values- Study the following example and
find the differences.
#include<stdio.h>
#include<conio.h>
float add();
void main()
{ float x;
x=add();
printf("The sum is %f",x);
getch();
}
float add()
{ int a;
float b;
printf("Enter values of a and b:");
scanf("%d%f",&a,&b);
return a*a + b*b; // you can write a expression in the return statement.
}
A program having more then one user defined functions.
#include<stdio.h>
#include<conio.h>
float add(float, float);
float sub(float,float);
float mul(float,float);
float div(float,float);
void main()
{
float a,b,sum,dif,product,quotient;
char ch,flag;
clrscr();
do{
printf("Enter any operator(+,-,*,/):");
scanf(" %c",&ch);
printf("Enter values of a and b:");
scanf("%f%f",&a,&b);
switch(ch)
{
case '+':
sum=add(a,b);
printf("sum=%f",sum);
break;
case '-':
dif=sub(a,b);
printf("Difference=%f",dif);
break;
98
A Reference Book On C Programming
case '*':
product=mul(a,b);
printf("Product=%f",product);
break;
case '/':
quotient=div(a,b);
printf("Quotient=%f",quotient);
break;
default:
printf("Enter any above shown characters");
break;
}
printf("\n\nEnter e for exit\nany character for continue\t");
scanf(" %c",&flag);
}while(flag!='e');
getch();
}
float add(float p, float q)
{
return p+q;
}
float sub(float p, float q)
{
return p-q;
}
float mul(float p, float q)
{
return p*q ;
}
float div(float p, float q)
{
return p/q;
}
99
A Reference Book On C Programming
Chapter 7.0
Arrays:
• An array is defined as a set or collection of data items of the same type.
• The data item grouped in an array can be simple types like integer, float or
character
• It is also called subscripted variable
Types of Arrays:
• One dimensional array
• Two dimensional array
• Multi dimensional array
Global declaration:
int number[50]; //global declaration
main( )
{
………
}
Local decleration:
main( )
{
int number[20]; //local declaration
}
99
A Reference Book On C Programming
Local initialization:
main( )
{
int num[ ]= {10,20,35,40,45};//local initialization
}
• If the number of initializes is less than the number of elements in the array
the remaining elements are set to zero
• It is an error to specify more initializes than the no. of elements in the array
• It is not necessary to specify the size of an array especially in the case , if
initializes are provided for the array
num 2 4 3 12 5 45 5 6
100
A Reference Book On C Programming
Output:
Enter element 0: 80
Enter element 1: 90
Enter element 2: 70
Enter element 3: 80
Enter element 4: 75
80.000000 90.000000 70.000000 80.000000 75.000000
The initialized array is:
23 19 20 21
101
A Reference Book On C Programming
#include<stdio.h>
#include<conio.h>
void main( )
{
float marks[5],sum=0,avg;
int i;
clrscr( );
for(i=0;i<5;i++)
{
printf(“Enter element %d:”,i);
scanf(“%f”,&marks[i]);
sum=sum+marks[i];
}
avg=sum/i; // because latest value of i is 5
printf(“Average=%f”,avg);
getch( );
}
Output:
Enter element 0: 1
Enter element 1: 2
Enter element 2: 3
Enter element 3: 4
Enter element 4: 5
Average = 3.0000000
102
A Reference Book On C Programming
}
}
printf(“\n The highest element is %f”,max);
}
Syntax:
data_type array_name[row_size][column_size];
Global initialization:
int marks[3][4] = {{2,3,5,4},{2,5,3,4},{4,3,3,2}};
int marks[][4] = {{2,3,5,4},{1,2,3,4},{4,3,3,2}};
int values[3][4]={1,2,3,4,5,6,7,8,9,10,11,12} ;
main ( )
{
…………
}
Local initialization:
main( )
{
int marks[3][4]={
{2,3,5,4},
{2,5,3,4},
{4,3,3,2}
};
int values[3][4]={1,2,3,4,5,6,7,8,9,10,11,12} ;
}
103
A Reference Book On C Programming
We need the concept of rows and column as in matrix. So we use two loops to
access the elements- one for row and one for column
int values[3][4]={1,2,3,4,5,6,7,8,9,10,11,12} ;
104
A Reference Book On C Programming
Examples:
1. Program to illustrate the input and output the elements of a two dimensional array.
void main( )
{
int i,j,num[3][3];
clrscr( );
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“Enter num[%d][%d]:”i,j);
scanf(“%d”,&num[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d”,num[i][j]);
}
printf(“\n”);
}
getch ( );
}
Output:
Enter num[0][0]: 1
Enter num[0][1]: 2
Enter num[0][2]: 3
Enter num[1][0]: 4
Enter num[1][1]: 5
Enter num[1][2]: 6
Enter num[2][0]: 7
Enter num[2][1]: 8
Enter num[2][2]: 9
105
A Reference Book On C Programming
In C, the name of the array represents the address of its first element
By passing the array name, we are passing the address of the array to the
called function
The array in the called function refers to the same array stored in the
memory. Hence any changes in the array in the called function will be reflected in
the original array
In C, arrays are automatically passed by reference to a function
The name of an array is the beginning address of where the array data is
stored in memory also known as “pointer constant”
Eg: marks[0] – array element
marks=&mark[0] -- here the array marks holds the address of element
mark[0]
Program to illustrate passing one dimensional array to the function (sorting of array in
ascending order – selection sort)
#include<stdio.h>
#include<conio.h>
void selectionsort(int[],int);
void main( )
{
int marks[5],temp,i,j;
for(i=0;i<5;i++)
{
printf(“Enter element %d:”,i);
scanf(“%d”,&marks[i]);
}
selectionsort(marks,5);
getch( );
}
106
A Reference Book On C Programming
}
}
}
printf(“Sorted array is :”);
for(i=0;i<5;i++)
{
printf(“%d”,marks[i]);
}
}
Output:
Enter element 0:90
Enter element 1:78
Enter element 2:7
Enter element 3:56
Enter element 4:45
Sorted array is: 7 45 56 78 90
Some rules:
1. the function must be called by passing only the array name
2. the size of the second dimension must be specified
3. The prototype declaration should be similar to the function
header/decelerator
Example:
Program to demonstrate addition of two 2-dimensional array using function
#include<stdio.h>
#include<conio.h>
voidreadarray(int a[][10],int row, int col);
void sum(int a[][10],b[][10],int s[][10],int r1, int c1, int r2, int c2);
void main( )
{
int r1, c1,r2, c2;
int a[10][10], b[10][10], s[10][10];
clrscr( );
107
A Reference Book On C Programming
108
A Reference Book On C Programming
This program reads two 2-dimensional arrays, passes them to a function to multiply
multiplies them and displays the result on the screen from the main function.
#include<stdio.h>
#include<conio.h>
void mul2darray(int[][10],int[][10],int [][10],int,int,int);
void main()
{ int b,i,j,num1[10][10],r1,c1,k, num2[10][10],r2,c2,num3[10][10];
clrscr();
printf("Enter the maximum size of row and column of array num1:");
scanf("%d%d",&r1,&c1);
printf("Enter the maximum size of row and column of array num2:");
scanf("%d%d",&r2,&c2);
if(c1==r2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("Enter num1[%d][%d]:",i,j);
scanf("%d",&num1[i][j]);
}
}
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("Enter num2[%d][%d]:",i,j);
scanf("%d",&num2[i][j]);
}
}
mul2darray(num1,num2,num3,r1,c1,c2);
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d ",num3[i][j]);
}
printf("\n");
}
}
else
{
printf("Array size mismatch.");
}
getch();
109
A Reference Book On C Programming
}
void mul2darray(int num1[][10],int num2[][10],int num3[][10],int r1,int c1,int c2)
{
int i,j,k;
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{ //*(*(num3+i)+j)=0;
num3[i][j]=0;
for(k=0;k<c1;k++)
{
num3[i][j]=num3[i][j]+num1[i][k]*num2[k][j];
//*(*(num3+i)+j)=*(*(num3+i)+j)+(*(*(num1+i)+k))*(*(*(num2+k)+j));
}
//printf("\t%d",num3[i][j]);
}
printf("\n");
}
}output
Enter the maximum size of row and column of array num1:3 3
Enter the maximum size of row and column of array num2:3 3
Enter num1[0][0]:0
Enter num1[0][1]:2
Enter num1[0][2]:3
Enter num1[1][0]:5
Enter num1[1][1]:0
Enter num1[1][2]:7
Enter num1[2][0]:1
Enter num1[2][1]:0
Enter num1[2][2]:1
Enter num2[0][0]:2
Enter num2[0][1]:0
Enter num2[0][2]:5
Enter num2[1][0]:3
Enter num2[1][1]:0
Enter num2[1][2]:0
Enter num2[2][0]:5
Enter num2[2][1]:3
Enter num2[2][2]:1
21 9 3
45 21 32
7 3 6
110
A Reference Book On C Programming
C. Multidimensional arrays
• C allows arrays of three or more dimensions.
• The exact limit is determined by the compiler
data_type array_name[s1][s2][s3][s4]…….s[m]
int num[3][10][4][5];
111
A Reference Book On C Programming
Pointers:
112
A Reference Book On C Programming
0
1
2
65535
Concept of variable:
Pointer variable:
Representation of a variable:
Eg: int number = 50;
number variable
50 value
1000 Address
Representation of a variable
113
A Reference Book On C Programming
number 50 1000
number_ptr 1000
1020
Deference operator(*):
Concept of pointers:
Pointers
114
A Reference Book On C Programming
Pointer Decleration:
• Since pointer variables contain addresses that belong to a separate data type,
they must be declared as pointers before we use them
• When a pointer variable is declared , the variable name must be preceded by
an asterisk (*), this identifies the fact that the variable is a pointer
• The data type of the pointer is the data type of the variable to which it is
pointing
Syntax:
data_type *pointer_variable_name;
eg: int *p;
- declares the variable p as a pointer variable that points to an integer data
type
char *character;
float *marks;
Pointer initialization:
int number;
int *number_ptr;
number_ptr = &number;
Pointer expressions:
If p1 and p2 are properly declared and initialized pointers they can be used
in expression as:
p = *p1 * *p2;
sum = sum + *p2;
*p1 = *p1 + 40;
115
A Reference Book On C Programming
Example:
#include<stdio.h>
void main ( )
{
int *p , *q; //decleration of pointer variables
int a, b;
clrscr ( );
p = &a; //use of reference operator
q = &b;
printf(“Address of a = %u\n”,&a);
printf(“Address of b = %u\n,&b”);
printf(“value of p = %u\n”,p);
printf(“value of q = %u\n”,q);
printf(“”Enter value of a and b:”);
scanf(“%d%d”,&a,&b);
printf(“the value pointed by p is %d\n”,*p);
printf(“the value pointed by q is %d\n”,*q);
printf(“a+b = %d\n”,a+b);
printf(“*p+*q = %d”,*p+*q);
getch( );
}
Output:
Address of a = 65524
Address of b = 65522
value of p = 65524
value of q = 65522
Enter value of a and b: 45 67
the value pointed by p is 45
the value pointed by q is 67
a+b = 112
*p+*q = 112
Pointer Arithmetic:
116
A Reference Book On C Programming
• If p is pointing to the address 1000, then after operation p=p+1; the value of
p will be 1002 not 1001.
• When we increment a pointer, its value is incremented by the length of the
data type that it points to, this length is called the scale factor
117
A Reference Book On C Programming
118
A Reference Book On C Programming
num 2 4 3 12 5 45 5 6
• We know that on mentioning the name of the array we get its base address
num[0]=2
&num[0]=4002 (base address)
*(&num[0])=2
num=4002 (base address)
• By saying *num we would be able to refer to the zeroth element of the array
i.e 2
*num = *(num+0) = 2
Similarly,
*(num+1) => refers to the 1st element of the array,i.e 4
• In fact Compiler understands,
num[i] is same as *(num+i)
• Following notations are same:
num[i]
*(num+i)
*(i+num)
i[num]
• If you want to calculate the address of
num[4] = base address+(index*scale factor of int)
=4002+(4*2) = 4010
119
A Reference Book On C Programming
*p = *num = num[0] = 2
*(p+1) = *(num+1) = 4
int s[4][2]={
{1234, 56},
{1235, 33},
{1236, 80},
{1237, 78}
};
120
A Reference Book On C Programming
In general,
s[i][j] = *(s[i]+j) = *(*(s+i)+j)
Example:
//pointer notation to access array elements
#include<stdio.h>
#include<conio.h>
void main( )
{
int age[10],i;
clrscr( );
printf(“Enter numbers:”);
for(i=0;i<n;i++)
{
scanf(“%d”,age+i);
}
for(i=0;i<n;i++)
{
printf(“%d”,*(age+i));
}
getch( );
}
121
A Reference Book On C Programming
p2 p1 Variable - c
• Here, the pointer variable p2 contains the address of the pointer variable p1,
which points to the location that contains the desired value. This is known as
multiple declarations
• A variable that is a pointer to a pointer must be declared using additional
indirection operator symbols in front of the name
• General format :
data_type **pointer_variable;
eg: int **p2;
Example:
#include<stdio.h>
void main( )
{
int x, *p1, **p2;
x = 100;
p1 = &x;
p2 = &p1;
printf(“%d”,**p2);
}
Output:
100
Array of Pointers:
• Eg: int *p[10]; => which means that p is an array of 10 pointers which
points to integer data type
122
A Reference Book On C Programming
void pointer
Null pointer
A null pointer is a regular pointer of any pointer type which has a special
value that indicates that it is not pointing to any valid reference or memory address.
int * p;
p = 0;
Do not confuse null pointers with void pointers. A null pointer is a value that any
pointer may take to represent that it is pointing to "nowhere", while a void pointer
is a special type of pointer that can point to somewhere without a specific type. One
refers to the value stored in the pointer itself and the other to the type of data it
points to.
123
A Reference Book On C Programming
Disadvantage:
124
A Reference Book On C Programming
Permanent
C Program Storage area
Instructions
• malloc ( )
• calloc( )
• realloc( )
• free( )
1. malloc ( ):
• a block of memory of specified size can be allocated using the function malloc
• malloc function reserves a block of memory of specified size and return a
pointer of type void a pointer to anything
• General form:
o ptr = (cast-type*)malloc(byte-size)
where ptr is a pointer of cast-type
125
A Reference Book On C Programming
• The data type of the pointer returned from malloc must agree with the type
of the data to be stored
• If the space in the heap is not sufficient to satisfy the request, the allocation
can fail, and returns a NULL pointer
Eg: ptr=(int *)malloc(10*sizeof(int));
2. calloc ( ):
• used for storing derived data types such as array and structures
• malloc allocates single block of memory but calloc allocates multiple blocks
of memory each of same size and sets all bytes to zero
• General form:
ptr=(cast-type*)calloc(n,sizeof(element-size));
126
A Reference Book On C Programming
0 0 0 0 0 0 0 0 0
3. free( ):
where ptr is a pointer to a memory block which has already been created by
malloc or calloc
4. realloc ( ):
• Here a new memory space of size newsize is allocated to the pointer variable
ptr and returns a pointer to the first byte of the new memory block
• The newsize may be larger or smaller then the size
• The new memory block may or may not begin at the same place as the old
one but the old data will remain intact
127
A Reference Book On C Programming
Review
Function Task
malloc Allocated requested size of bytes and returns a pointer to the first byte of the
allocated space
calloc Allocates space for an array of elements, initializes them to zero and then
returns a pointer to the memory.
free frees the previously allocated space.
realloc Modifies the size of previously allocated space.
Usage example
/* allocation succeeded */
Examples
Example 1: This program asks the required size of array to the user and displays
the addresses of allocated blocks.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main(void)
{
int n,i;
float *address;
clrscr();
printf("Enter number of elements:");
scanf("%d",&n);
address=(float*)calloc(n,sizeof(int));
if(address==NULL)
{ printf("Memory can not allocated.");
exit();
}
for(i=0;i<n;i++)
128
A Reference Book On C Programming
{
printf("\nAddress of %d block %d ",i,(address+i));
}
free(address);
getch();
}
Output
Enter number of elements:6
Address of 0 block 2558
Address of 1 block 2562
Address of 2 block 2566
Address of 3 block 2570
Address of 4 block 2574
Address of 5 block 2578
Example 2: This program reads array elements and displays them with their
corresponding storage locations. Sometimes memory can not be allocated due to
lack of requested amount of free memory. We must handle this situation. So, we
should check whether the required memory is allocated or not in every program. If
the memory can be allocated, the memory allocation function returns NULL pointer
to the pointer variable. Study the following example, and understand how the above
discussed is handled.
#include<stdio.h>
#include<conio.h>
void main(void)
{
int n,i;
float *address;
clrscr();
printf("Enter number of elements:");
scanf("%d",&n);
if((address=(float*)malloc(n*sizeof(int)))==NULL)
{
printf("Memory can not allocated.");
exit();
}
printf("Enter array elements:");
for(i=0;i<n;i++)
{ scanf("%f",(address+i));
}
for(i=0;i<n;i++)
{ printf("\nElement at %u is %f ",(address+i),*(address+i));
}
free(address);
getch();
}
129
A Reference Book On C Programming
Output
Enter number of elements:6
Enter array elements:1 3 7 33 45 78 90
Element at 2574 is 1.000000
Element at 2578 is 3.000000
Element at 2582 is 7.000000
Element at 2586 is 33.000000
Element at 2590 is 45.000000
Element at 2594 is 78.000000
Example 3: This program sort a dynamic array using concept of function and
pointer.
#include<stdio.h>
#include<conio.h>
void sort(float*,int);
void main(void)
{
int n,i;
float *marks;
clrscr();
printf("Enter number of elements:");
scanf("%d",&n);
if((marks=(float*)calloc(n,sizeof(int)))==NULL)
exit()
130
A Reference Book On C Programming
*(m+j+1)=*(m+j);
*(m+j)=temp;
}
}
}
}
Output
Enter number of elements:5
Enter array elements:123 456 111 111 23
Element at 2574 is 456.000000
Element at 2578 is 123.000000
Element at 2582 is 111.000000
Element at 2586 is 111.000000
Element at 2590 is 23.000000
#include<stdio.h>
#include<conio.h>
void findminmax(int *,int *,int *,int);
void main()
{
int largest,smallest,n,*p,i;
clrscr();
printf("Enter array size:");
scanf("%d",&n);
p=(int*)calloc(n,sizeof(int));]
if(p==NULL)
exit();
for(i=0;i<n;i++)
{
printf("Enter array element [%d]:",i+1);
scanf("%d",(p+i));
}
findminmax(p,&largest,&smallest,n);
printf("Largest=%d\nSmallest=%d",largest,smallest);
free(p);
getch();
}
131
A Reference Book On C Programming
int i;
*l=*(array+0);
*s=*(array+0);
for(i=1;i<n;i++)
{
if(*l<*(array+i))
*l=*(array+i);
if(*s>*(array+i))
*s=*(array+i);
}
}
Output
Enter array size:5
Enter array element [1]:12345
Enter array element [2]:23
Enter array element [3]:354
Enter array element [4]:56
Enter array element [5]:87
Largest=12345
Smallest=23
132
A Reference Book On C Programming
for(j=0;j<cols;j++)
{ printf("\nEnter element[%d][%d]",i,j);
//scanf("%f",&matrix[i][j]);
scanf("%f",(*(matrix+i)+j));
}
}
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{ printf("%f",*(*(matrix+i)+j));
}
}
free(matrix);
getch();
}
void linkfloat()
{ float x=0,*y;
y=&x;
x=*y;
}
133
A Reference Book On C Programming
for(j=0;j<cols;j++)
{
printf("\nEnter element[%d][%d]",i,j);
//scanf("%f",&matrix[i][j]);
scanf("%f",(*(matrix+i)+j));
}
}
power(matrix,rows,cols);
free(matrix);
getch();
}
void power(float **mat,int ros,int cos)
{ int i,j;
for(i=0;i<ros;i++)
{
for(j=0;j<cos;j++)
{
printf("%f",mat[i][j]*2);
//printf("%f",*(*(mat+i)+j)*2);
}
}
}
void linkfloat()
{ float x=0,*y;
y=&x;
x=*y;
}
Output
Enter maximum rows:3
Enter maximum columns:3
Enter element[0][0]1 Enter element[0][1]2
Enter element[0][2]3 Enter element[1][0]4
Enter element[1][1]5 Enter element[1][2]6
Enter element[2][0]7 Enter element[2][1]8
Enter element[2][2]9
2.0000004.0000006.0000008.00000010.00000012.00000014.00000016.00000018.00000
0
Example 7: A program to use functions to read, process, and display the sum of
two dimensional dynamic arrays.
#include<stdio.h>
#include<conio.h>
void display(float**,int,int);
void readmatrix(float**,int,int);
void processmatrix(float**,float**,int,int);
void main(void)
134
A Reference Book On C Programming
{ int rows1,cols1,i,j,rows2,cols2;
float **matrix1,**matrix2;
clrscr();
printf("Enter maximum rows of matrix1:");
scanf("%d",&rows1);
printf("Enter maximum colunmns of matrix1:");
scanf("%d",&cols1);
printf("Enter maximum rows of matrix2:");
scanf("%d",&rows2);
printf("Enter maximum colunmns of matrix2:");
scanf("%d",&cols2);
//allocating memory for the matrix1, matrix2.
/* first allocating a single dimensional array of n float pointers.*/
matrix1=(float**)calloc(sizeof(float*),rows1);
matrix2=(float**)calloc(sizeof(float*),rows2);
/*Now for each row,allocating a single dimensional array of floats.*/
for(i=0;i<rows1;i++)
{
matrix1[i]=(float*)calloc(cols1,sizeof(float));
}
for(i=0;i<rows2;i++)
{
matrix2[i]=(float*)calloc(cols2,sizeof(float));
}
readmatrix(matrix1,rows1,cols1);
readmatrix(matrix2,rows2,cols2);
display(matrix1,rows1,cols1);
printf("\n");
display(matrix2,rows1,cols1);
processmatrix(matrix1,matrix2,rows1,rows2);
printf("\n");
display(matrix1,rows1,cols1);
free(matrix1);
free(matrix2);
getch();
}
void display(float **mat, int r1,int c1)
{
int i,j;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{ printf("%f ",*(*(mat+i)+j));
}
}
}
135
A Reference Book On C Programming
136
A Reference Book On C Programming
1. Introduction
In C, a string is stored as a null-terminated char array. This means that after the last truly
usable char there is a null, which is represented in C by '\0'. The subscripts used for the
array start with zero (0). Any group of characters (except double quote sign) defined
double quotation marks is a constant string.
Example- “We are very laborious students."
Characters strings are used to build meaningful and readable programs. The operations
performed on character strings are:
-Reading and writing strings.
-Combining strings together.
-Copying one string toe another.
-Comparing strings for equality.
-Extracting a portion of a string.
Example- The following line declares a char array called str. C provides fourteen
consecutive bytes of memory. Only the first fourteen bytes are usable for character
storage, because one must be used for the string-terminating null.
char str[15];
Characters H e L l o , w o r l d ! '\0'
ASCII 72 100 107 107 111 44 32 119 111 114 107 100 33 0
Subscripts 0 1 2 3 4 5 6 7 8 9 10 11 12 13
137
A Reference Book On C Programming
C also permits to initialize the character array without specifying the numbers of
elements as-
char name[ ]="kathmandu city";
char name[ ]={'k','a','t','h','m','a','n','d','u',' ','c','i',t','y','\0'};
4. Character I/O
getchar( ) function reads a character from the standard input device, while putchar( )
writes a character to the standard output device.
Example 1: A program shows a single character input/output.
#include<stdio.h>
void main()
{
char ch;
printf("Enter a character:");
ch=getchar();
printf("The entered character is:");
putchar(ch);
}
The scanf function can use with % s format specification to read a string of characters.
The syntax is as
char name[15];
scanf("%s",name);
The problem with the scanf function is that it terminates its input on first white space it
finds.
Note that unlike other variable's scanf calls, in case of character arrays, the ampersand(&)
is not required before the variable name. The scanf function automatically terminates the
string that is read with a null character. If we want to n numbers of words then we need
to read them n times.
Example 2: This program reads and displays a single word using scanf function.
#include<stdio.h>
void main()
{
char namestr[10];
138
A Reference Book On C Programming
clrscr();
printf("Enter name:");
scanf("%s",namestr);
printf("Name: %s",namestr);
getch();
}
Example 3: This example reads a string character by character until enter key is
pressed(new line character(\n)) and inserts the null character at the end. In stead of
new line other character can be used. This is a technique to read strings having
white space characters.
#include<stdio.h>
#include<conio.h>
void main()
{ char namestr[100],ch;
int i;
clrscr();
printf("Enter any string:");
i=0;
do
{ ch=getchar();
namestr[i]=ch;
i++;
}while(ch!='\n');
i=i-1;
namestr[i]='\0';
printf("String: %s",namestr);
getch();
}
139
A Reference Book On C Programming
Output
Kathmandu Engineering College
Kath
Kath
Kat
Kathmandu Engineering College
140
A Reference Book On C Programming
Output
k
ka
kat
kath
kathm
kathma
kathman
kathmand
kathmandu
Example 6: This example shows to get the equivalent integer value of a character
digit and the equivalent integer value of string of character digits.
#include<stdio.h>
#include<conio.h>
void main()
{ char ch[5];
char character;
int n,x; clrscr();
141
A Reference Book On C Programming
8. String I/O
a. gets- This function accepts the name of a string as a parameter, and fills the string with
characters that are input from the keyboard, till a new character is encountered( i.e., till
we press the enter key.) At the end the function gets appends an null character and
returns. The new line character is not added to the string.
b. puts- puts displays the string stored in the string variable.
Most of the following examples are done using gtes and puts function for inputting and
outputting strings.
142
A Reference Book On C Programming
length=strlen(text1);
printf("Length of given string=%d",length);
getch();
}
The same task is done without using strlen() which is illustrated in the following
example.
Example 8: Finding length of a given string.
#include<stdio.h>
#include<conio.h>
void main(void)
{
char text[100]; int i=0,length=0;
clrscr();
printf("Enter a string:");
gets(text);
while(text[i]!='\0')
{
length++;
i++;
}
printf("\nLength of given string is %d",length);
printf("\nThe latest value of i is also length(%d)",i);
getch();
}
Output
Enter a string:my name
Length of given string is 7
The latest value of i is also length(7)
b. strcat()
This concatenates(joins) two strings together. its general form is:
strcat(string1,string2);
string1 and string2 are character arrays. Which are given as argument to the function.
When strcat is executed ,string2 is appended to string1. It does so by removing the null
character at the end of string1 and placing string2 from there. The string2 remains
unchanged. Which is illustrated in the following example.
Example 9: Illustration of strcat().
#include<stdio.h>
#include<conio.h>
void main()
{
char text1[100];
char text2[50];
clrscr();
printf("Enter first string1:");
gets(text1);
printf("Enter second string2:");
143
A Reference Book On C Programming
gets(text2);
strcat(text1,text2);
puts(text1);
getch();
}
The same task is done without using the library function which is illustrated in the following
example
Example 10: Concatenating two strings.
#include<stdio.h>
#include<conio.h>
void main(void)
{
char text1[100];
char text2[100];
char text3[200];
int i=0,j;
clrscr();
printf("Enter string1:");
gets(text1);
printf("Enter string2:");
gets(text2);
while(text1[i]!='\0')
{
text3[i]=text1[i];
i++;
}
j=0;
while(text2[j]!='\0')
{
text3[i+j]=text2[j];
j++;
}
text3[i+j]='\0';
puts(text3);
getch();
}
Output
Enter string1:Kathmandu Engineering
Enter string2:College.
Kathmandu EngineeringCollege.
c. strcpy()
This function copies one string to another. It takes the form
strcpy(deststring,sourcestring);
144
A Reference Book On C Programming
deststring and sourcestring are two strings as argument to the function. Which may be
character array or string constant. When this function is executed, it will assign the
content of string variable sourcrsting to the string variable deststring.
The same task is done without using the strcpy which is illustrated in the following
example
Example 12: Copying one string to another.
#include<stdio.h>
#include<conio.h>
void main()
{
char text1[50];
char text2[50]; int i=0;
clrscr();
printf("Enter first string1:");
gets(text1);
printf("Enter second string2:");
gets(text2);
while(text2[i]!='\0')
{
text1[i]=text2[i];
i++;
}
text1[i]='\0';
printf("%s",text2);
getch();
}
Output
145
A Reference Book On C Programming
d.strcmp()
The strcmp function compares two strings. The general form of the string is
strcmp(string1,string2);
Where string1 and string2 are arguments which may by character arrays or string
constants. When this function is executed it returns 0 if both strings are equal. If the
strings are not equal then, it returns the numeric difference between first nonmatching
characters in the strings.
Example 13: Illustration of strcmp().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char text1[100];
char text2[50];int c;
clrscr();
printf("Enter first string1:");
gets(text1);
printf("Enter second string2:");
gets(text2);
c=strcmp(text1,text2);
if(c==0)
printf("Strings are equal.");
else
printf("Strings are not equal.");
getch();
}
The same task is done without using the strcpy which is illustrated in the following
example
Example 14: Comparing two strings.
#include<stdio.h>
#include<conio.h>
void main()
{
char text1[50];
char text2[50]; int i=0;
//clrscr();
printf("Enter first string1:");
gets(text1);
printf("Enter second string2:");
gets(text2);
while(text1[i]==text2[i] && text1[i]!='\0'&&text2[i]!='\0')
146
A Reference Book On C Programming
{
i++;
}
if(text1[i]=='\0'&& text2[i]=='\0')
printf("Strings are equal.\n");
else
printf("Strings are not equal.");
getch();
}
Output
Run 1:
Enter first string1: Kathmadnu
Enter second string2: Engineering
Strings are not equal.
Run 2:
Enter first string1: College
Enter second string2: College
Strings are equal.
j o h n s h ‘\0’
s m i t h ‘\0’
t r u d y ‘\0’
b o b ‘\0’
s h e r r y ‘\0’
The following example shows how to input, output, and access elements of arrays of
characters
Example 15: Sorting of strings in ascending order.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char namelist[5][40],temp[40]; int pass,j;
clrscr();
147
A Reference Book On C Programming
printf("Enter names:");
for(j=0;j<5;j++)
gets(namelist[j]);
for(pass=0;pass<5-1;pass++)
{
for(j=0;j<5-pass-1;j++)
{
if(strcmp(namelist[j],namelist[j+1])>0)
{
strcpy(temp,namelist[j]);
strcpy(namelist[j],namelist[j+1]);
strcpy(namelist[j+1],temp);
}
}
}
printf(Sorted name list is:);
for(j=0;j<5;j++)
puts(namelist[j]);
getch();
}
Example16:
#include<stdio.h>
void main()
{
char *text;
printf("Enter a string:");
scanf("%[^+");
printf("%s",text);
}
Output
Enter a string:Kathmandu Engineering College
148
A Reference Book On C Programming
is located at kalimati.+
Kathmandu Engineering College
is located at kalimati
Where text is a pointer to a character and assigns the address of first character as the
initial value.
All above discussed examples can be done using pointer notation.
One important use of pointers is in handling of a table of strings. Suppose the following
array of strings:
char namelist[3][20];
This says that namelist is a table containing three names, each with a maximum length of
20 characters(including null character). The total storage requirements for the namelist
table are 60 bytes. We know that rarely the individual strings will be of equal lengths.
Therefore, instead of making each row a fixed number of characters, we can make it
pointer to a string of varying length.
example:
char *namelist[3]={"John Smith", "Sherry", "Dave"};
declares namelist to be an array of three pointers to characters, each pointer pointing to a
particular name as shown below:
namelist[0] John Smith
namelist[2] Sherry
namelist[3] Dave
This declaration allocates only 25 bytes, sufficient to hold all characters as shown below.
J o h n S m i t h ‘/0’
S h e r r y '\0'
D a v e '\0'
Following example shows sorting of arrays of string using pointer. In the previous example of
sorting we copied the string from one string to another string but in the following example we
will swap the pointer. This technique will eliminate:
-complicated storage management.
-high overheads of moving lines.
149
A Reference Book On C Programming
{
if(strcmp(namelist[j],namelist[j+1])>0)
{
temp=namelist[j];
namelist[j]=namelist[j+1];
namelist[j+1]=temp;
}
}
}
printf("Sorted name list is:\n");
for(i=0;i<5;i++)
{
puts(namelist[i]);
}
getch();
}
In the above example the number of rows are fixed. But we can make array of strings(2D
array of characters) dynamic using dynamic memory allocation. Which is illustrated in
the following example.
2d dynamic array of characters.
#include<stdio.h>
#include<alloc.h>
void main()
{
char **namelist;int n,i,length;
clrscr();
printf("Enter numbers of items:");
scanf("%d",&n);
fflush(stdin);
namelist=(char**)calloc(n,sizeof(char*));
if(namelist==NULL)
{
exit();
}
else
{
for(i=0;i<n;i++)
{
printf("Enter length of name %d:",i+1);
scanf("%d",&length);
fflush(stdin);
namelist[i]=(char*)calloc(length,sizeof(char));
printf("Enter string %d",i+1);
gets(namelist[i]);
}
}
150
A Reference Book On C Programming
for(i=0;i<n;i++)
{
puts(namelist[i]);
}
getch();
}
6.b Write a program that takes a string and pass to a function. The function takes
the string and converts all of its lower case characters to upper case characters if the
first character is in lower case and vice versa. Display the string before conversion
and after conversion in the calling function. Your function must convert the original
string not a copy of it.(2062,baishakh)
#include<stdio.h>
#include<conio.h>
void stringconvert(char[]);
void main()
{
char anystring[100];
clrscr();
printf("Enter a string:");
gets(anystring);
printf("String before conversion : ");
puts(anystring);
stringconvert(anystring);
printf("\nstring after conversion : ");
puts(anystring);
getch();
}
void stringconvert(char anystr[])
{
int i;
if(anystr[0]>='A'&&anystr[0]<='Z')
{ i=1;
while(anystr[i]!='\0')
{
if(anystr[i]>='a'&&anystr[i]<='z')
{
anystr[i]=anystr[i]-32;
i++;
}
else
{
i++;
continue;
}
}
151
A Reference Book On C Programming
}
else if(anystr[0]>='a'&&anystr[0]<='z')
{ i=1;
while(anystr[i]!='\0')
{
if(anystr[i]>='A'&&anystr[i]<='Z')
{
anystr[i]=anystr[i]+32;
i++;
}
else
{
i++;
continue;
}
}
}
else
printf("\nString is started wothout a upper case or a lower case.");
}
152
A Reference Book On C Programming
}
Question-Write a program to insert a string in to another string in the location
specified by the user. Read the strings in the main program and pass them to
function along with the inserting position and return the resulting
string.(2062/poush)
#include<stdio.h>
#include<conio.h>
void insertstring(char[],char[],int);
void main()
{
char mainstring[100],stringtobeinserted[20];
int n; clrscr();
printf("Enter mainstring:");
gets(mainstring);
153
A Reference Book On C Programming
printf("Enter stringtobeinserted:");
gets(stringtobeinserted);
printf("Enter position to insert a string:");
scanf("%d",&n);
insertstring(mainstring,stringtobeinserted,n);
printf("The new string is:");
puts(mainstring);
getch();
}
void insertstring(char mstr[],char str[],int p)
{
int i=p-1,j=0;
char temp[100];
while(mstr[i]!='\0')
{
temp[j]=mstr[i];
i++;j++;
}
temp[j]='\0';j=0;i=p-1;
while(str[j]!='\0')
{
mstr[i]=str[j];
i++;j++;
}
j=0;
while(temp[j]!='\0')
{
mstr[i]=temp[j];
i++;j++;
}
mstr[i]='\0';
}
char string[100];
printf("Enter any sting:");
154
A Reference Book On C Programming
gets(string);
rev(string,strlen(string));
}
void rev(char s[],int l)
{
putchar(s[l]);
if(l>=0)
{
rev(s,l-1);
}
}
Q. Write a program that prompts the user for two strings assemble them in to a
single string and then print the string reversed. Check whether the assembled string
is palindrome or not.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(void)
{
char first[30];
char second[40];
char assembled[100];
char reversed[100];
int i=0,j=0,k=0;
clrscr();
printf("Enter first string:");
gets(first);
printf("Enter second string:");
gets(second);
while(first[i]!='\0')
{
assembled[i]=first[i];
i++;
}
assembled[i]=' ';i++;
while(second[j]!='\0')
{
assembled[i]=second[j];
j++;i++;
}
assembled[i]='\0';
puts(assembled);
printf("The reversed string is:");
for(j=i-1;j>=0;j--)
{
155
A Reference Book On C Programming
reversed[k]=assembled[j];
k++;
}
reversed[k]='\0';
printf("\n");
puts(reversed);
if(strcmp(assembled,reversed)==0)
printf("The assembled string is palindrome");
else
printf("The assembled string is not palindrome");
getch();
}
Check whether a string entered by user is a palindrome or not and display a proper
message.
[Hint: A palindrome word spells same back and forth e g : madam, noon].
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(void)
{
clrscr();
char astring[100];
int i,length,j,flag=1;
printf("Enter a string:");
scanf("%[^\n]s",astring);
length=strlen(astring);
for(i=0,j=length;i<length/2;i++,j--)
{
if(astring[i]!=astring[j-1])
{
flag=0;
break;
}
}
if(flag==1)
printf("\n%s is a palindrome",astring);
else
printf("\n%s is not a palindrome",astring);
getch();
}
156
A Reference Book On C Programming
#include<string.h>
void main(void)
{
clrscr();
char astring[100],ch;
int i=0,charactercount=0;
printf("%c",ch);
printf("Enter a string:");
gets(astring);
printf("Enter a character to check its frequecy:");
scanf("%c",&ch);
do
{
if(astring[i]==ch)
charactercount++;
i++;
}while(astring[i]!='\0');
printf("\nThe number of %c is %d",ch,charactercount);
getch();
}
157
A Reference Book On C Programming
#include<conio.h>
#include<string.h>
void main(void)
{
char string[200],tempword[50],word[50];int i=0,length=0,count=0;
clrscr();
printf("Enter a string:");
scanf("%[^\n]",string);
while(string[i]!='\0')
{ if(string[i]==' ')
{
tempword[count]='\0';
if(length<count)
{
length=count;
strcpy(word,tempword);
}
count=0;
}
else
{
tempword[count]=string[i];
count++;
}
i++;
}
printf("The longest word is :%s and its length is %d.",word,length);
getch();
}
OUTPUT
Enter a string:
kathmandu engineering college is located at kalimati .
The longest word is :engineering and its length is 11.
158
A Reference Book On C Programming
if(ch>=65&&ch<=90||ch>=97&&ch<=122)
printf("The character is alphabet");
else if(ch>=48&&ch<=57)
printf("\nThe character is digit");
else if(ch>=0&&ch<=32||ch==127)
printf("\nThe character is control character");
else
printf("\nSpecial character");
getch();
}
A program to read a string from users and converting the string to lowercase if the
first character is in lower case and vice versa. 059(p)/4/b, (059/©/2/b-similar), 060/
(P)/5/b)
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
char name[200];
printf("Enter a sting:");
gets(name);
if(65<=name[0]&&name[0]<=90)
{ while(name[i]!='\0')
{
if(97<=name[i]&&name[i]<=122)
{name[i]=name[i]-32;i++;}
else
{ i++;continue;}
}
puts(name);
}
else if(97<=name[0]&&name[0]<=122)
{
while(name[i]!='\0')
{
if(65<=name[i]&&name[i]<=90)
{name[i]=name[i]+32;i++;}
else
{i++;continue;}
}
puts(name);
}
else
printf("\nStart string from any alphabetic character.");
159
A Reference Book On C Programming
getch();
}
A program a program to read a line of characters. Now pass this line to a function
that finds the number of vowels, consonants, digits, white spaces (space and tab
character) and other characters in the line .Your function must return these
numbers to the calling function at once(use pointers). In the calling function, display
those numbers. In this problem count[i] are *(count+i) are used interchangeably.
similarly &count[i] and (count+i) are that is the relation between pointer and array.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void charactercount(char[],int*);
void main(void)
{
clrscr();
static char string[200];
int counts[5]={0,0,0,0,0};
printf("Enter a string:");
gets(string);
charactercount(string,counts);
printf("\nNumber of vowels =%d",counts[0]);
printf("\nNumber of consonants =%d",*(counts+1));
printf("\nNumber of digits =%d",counts[2]);
printf("\nNumber of white space =%d",*(counts+3));
printf("\nNumber of other characters=%d",*(counts+4));
getch();
}
void charactercount(char str[],int *count)
{
int i=0;
while(str[i]!='\0')
{
if(isalpha(str[i])>0)
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
*(count+0)=*(count+0)+1;
else
count[1]++;
}
else if(isdigit(str[i])>0)
(*(count+2))++;
else if(isspace(str[i])>0)
count[3]=count[3]+1;
else
(*(count+4))++;
160
A Reference Book On C Programming
i++;
}
}
Character testing:
161
A Reference Book On C Programming
int hcf(int,int);
void main(void)
{
int x,y;
clrscr();
printf("Enter two numbers x and y:");
scanf("%d%d",&x,&y);
printf("The HCF of %d , %d is:%d",x,y,hcf(x,y));
getch();
}
162
A Reference Book On C Programming
int calpower(int,int);
void main()
{
int x,y;
clrscr();
printf("Enter the value of x and y:");
scanf("%d%d",&x,&y);
printf("The reqd value is %d",calpower(x,y));
getch();
if(b==0)
return 1;
else
return a*calpower(a,b-1);
163
A Reference Book On C Programming
clrscr();
printf("Enter String:");
scanf("%s",string); // gets(string);
reverse(string,strlen(string));
getch();
}
void main()
{
char string[100]; //char *string;
clrscr();
printf("Enter string:");
gets(string);
printf("The length of string
is:%d\n",string_length(string));
printf("The reverse of the string is:\n");
string_rev(string);
getch();
}
int string_length(char str[])
{
int i=0,length=0;
while (str[i]!='\0')
{
length=length+1;
i++;
}
return length;
164
A Reference Book On C Programming
len=string_length(str);
while(len>=0)
{
putchar(str[len]);
len=len-1;
}
}
void main()
{
//char string[100];
char *string;
clrscr();
printf("Enter string:");
gets(string);
printf("The length of string
is:%d",string_length(string));
getch();
}
int i=0,length=0;
while (*(str+i)!='\0')
{
length=length+1;
i++;
}
return length;
165
A Reference Book On C Programming
void main()
{
char string1[100]; //char *string;
char string2[100];
clrscr();
printf("Enter string1:");
gets(string1);
printf("Enter string1:");
gets(string2);
string_comp(string1, string2);
string_concat(string1, string2);
string_copy(string1,string2);
getch();
}
int i=0;
while (str1[i]==str2[i] && str1[i]!='\0' &&
str2[i]!='\0')
{
i++;
}
166
A Reference Book On C Programming
int i=0,j=0;
char str3[100];
while(str1[i]!='\0')
{
str3[i]=str1[i];
i++;
}
while(str2[j]!='\0')
{
str3[i+j]=str2[j];
j++;
}
puts(str3);
int i=0;
while(str2[i]!='\0')
{
str1[i]=str2[i];
i++;
}
str1[i]='\0';
puts(str2);
}
void main()
{
167
A Reference Book On C Programming
for(int i=0;i<10;i++)
{
scanf("%d",a+i); //p+i
sum=sum+(*(a+i)); //*(p+i)
}
printf("Sum = %d",sum);
getch();
}
void main()
{
int a[10]; //int *a;
//int *p;p=a;
clrscr();
printf("Enter 10 elements of array:");
for(int i=0;i<10;i++)
{
scanf("%d",a+i); //p+i
}
printf("The maximun value is:%d\n",max(a));
printf("The minimum value is:%d",min(a));
getch();
}
168
A Reference Book On C Programming
void main()
{
int *p, *q, *sum;
int n,i;
clrscr();
printf("Enter the size for array:");
scanf("%d",&n);
p=(int *)calloc(n,sizeof(int));
q=(int *)calloc(n,sizeof(int));
sum=(int *)calloc(n,sizeof(int));
for(i=0;i<n;i++)
{
printf("Enter element for %d of first array:",i);
scanf("%d",p+i);
}
for(i=0;i<n;i++)
{
printf("Enter element for %d of second
array:",i);
scanf("%d",q+i);
}
for(i=0;i<n;i++)
{
169
A Reference Book On C Programming
*(sum+i)=*(p+i)+*(q+i);
printf("\t%d",*(sum+i));
}
free(p);
free(q);
free(sum);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
int i;
int r1,c1,r2,c2;
int **matrix1,**matrix2;
clrscr();
printf("Enter row for matrix 1:");
scanf("%d",&r1);
printf("Enter column for matrix 1:");
scanf("%d",&c1);
printf("Enter row for matrix 2:");
scanf("%d",&r2);
printf("Enter column for matrix 2:");
scanf("%d",&c2);
170
A Reference Book On C Programming
matrix1[i]=(int*)calloc(c1,sizeof(int));
}
for(i=0;i<r2;i++)
{
matrix2[i]=(int*)calloc(c2,sizeof(int));
}
free(matrix1);
free(matrix2);
getch();
}
}
printf("\n");
}
}
171
A Reference Book On C Programming
{
for(j=0;j<c1;j++)
{
printf("Enter element[%d][%d]:",i,j);
scanf("%d",(*(mat+i)+j));
//scanf("%d",&mat[i][j]);
}
}
}
*(*(mat1+i)+j)=*(*(mat1+i)+j)+*(*(mat2+i)+j);
}
}
}
172
A Reference Book On C Programming
Chapter 8.0
8.0. Structures
8.1. Introduction
An array is a data structure that can store more than on data items under a single name
but the limitation is that all the data items must be of same type. Data items with different
types cannot be stored in a single array of one type. If we need different types of data to
be stored in a single unit, we use structures. Thus a single structure might contain integer
elements, floating-point elements, character elements and so on. For this reason, array is
often referred to as a homogeneous data structure but structure is a non-homogeneous
data structure.
This chapter is fully dedicated to structures. We will see how structures are defined, how
their individual variables are accessed and processed, how structures are passed to and
returned from functions etc. In addition, we will see how structure can have arrays as its
element and how array of structures can be used.
The individual variables declared inside a structure are called the members of that
structure. A member of a structure can be of any data type including another structure,
array etc. The individual members of structure are accessed using a special operator
called member access operator. The symbol of member access operator is . (dot).
Defining a structure
struct [tag] {
member 1;
member 2;
member 3;
...
member n;
};
173
A Reference Book On C Programming
In the above declaration, struct is the required keyword used to define a structure. Tag is
a name that identifies the structure of this type. For simplicity, we can consider tag as the
name of the structure. Member 1, member 2, ... member n enclosed within { } are the
individual member variables of structure of type tag. Notice the semicolon after the
closing }. The member variables can be of any type - integer, float, double, char, array of
any type, another structure, pointers etc. The requirement is that every member must have
different names. Moreover, we cannot initialize the value for member variables within the
structure definition. We cannot assign the storage class of member variables. This is
because, we are just defining the template for the structure and no memory space is
reserved at this step. When we declare a variable of this structure memory space is
reserved.
After the structure is defined, we can declare the structure-type variables in similar way
to other variables. This can be done while defining the structure as well. So, there are
many ways to declare and define structure and structure type variables. Three cases are
listed below.
struct student {
struct student { struct {
char name[50];
char name[50]; char name[50];
char roll[13];
char roll[13]; char roll[13];
int marks;
int marks; int marks;
char remarks[100];
char remarks[100]; char remarks[100];
};
} st1, st2; } s1, s2, s3;
struct student s1, s2;
In the first example, a structure of type student (this is the tag) is defined with four
member variables - name, roll, marks and remarks. The type of member variables are also
defined in the structure. Altogether, the structure has a size of 50 + 13 + 100 + sizeof (int)
bytes. s1 and s2 are defined as the variables of type struct student. This is a typical
variable declaration for a structure type. Once we define the structure out of any function,
we can declare variables of structure type anywhere and use them.
In the second example, same structure as in first example is defined but the variable
declaration is done differently. The variables are declared within the framework of
structure itself. This is another typical structure-type variable declaration. Here st1 and
st2 are variables of type struct student. We can define more variables of type struct
student whenever we require.
In the third example, we have omitted the tag (name of the structure) but still have the
same members as in first and second examples. Also, we have defined s1, s2 and s3 are
variables of type of this structure. But we don't have the name of the structure. So, with
this definition, we cannot declare new variable of this structure type because we don't
have the name of the structure. If we need more variables, we just put them after s3. This
method of defining a structure is rarely used.
174
A Reference Book On C Programming
First and second method are mostly used. The rule of variable declaration for structure
type also follows the general rule of variable declaration. The standard syntax format is :
A Variable of structure type can also be initialized during its declaration. An example is
shown below.
struct account {
int accNo;
char accType;
char accName[50];
float curBalance;
};
In the above example, a structure of a bank account is defined having accNo (account
number), accType (account type), accName (account owners name) and curBalance
(current balance). Two variables client1 and client2 are defined and initialized the values.
The values are assigned as shown below.
client1: client2:
accNo = 11001 accNo = 11002
accType = S accType = F
accName = Some Body1 accName = Some Body2
curBalance = 500000 curBalance = 100000
The individual members of a structure variable are accessed using the member access
operator "." (also called the dot operator). The syntax is
structure-variable.member-name
175
A Reference Book On C Programming
For the structures defined in previous examples, we can access different member
variables by using the member access operator as shown below.
The member access operator (.) is a member of highest precedence group and its
associativity is left to right.
A complete program, which uses a structure type variable to store data for a student, read
the data and display it on the screen, is given below.
void main(){
struct student st; /* variable declaration */
printf("Enter Name:");
fflush(stdin);
gets(st.name); /* read data for members */
printf("Enter Roll No:");
fflush(stdin);
gets(st.roll);
printf("Enter Mark:");
scanf(" %d",&st.marks);
printf("Enter Remarks:");
fflush(stdin);
gets(st.remarks);
puts("You entered:");
printf("%s\n%s\n%d\n%s",st.name, st.roll, st.marks, st.remarks);
}
Structure members can be processed in the same manner as ordinary variables of same
data type. These members can be used as a part of an expression, passed to and returned
from functions.
176
A Reference Book On C Programming
A whole structure data can be assigned to another structure of same type at once. We
don't need to copy member by member. This is handled by the compiler itself. We cannot
compare to structure variables as a whole. We have to compare them member by
member.
Using typedef
Most programmers use typedef keyword to conveniently define and use a new data type
(user-defined type). Therefore, structures are almost always defined along with typedef
keyword so that subsequent variable declarations are easier. An example of the use of
typedef is shown below.
Structure Pointer
Pointer to a structure is one of the very frequently used data item in C. Declaration of a
structure pointer is very similar declaration of pointers to primitive data types. The
pointer variable is preceded by the * symbol and the data type is written before it. The
general syntax is
struct tag *strptr;
Here, tag is the structure name, struct the is keyword and strptr is a pointer variable that
can point to a variable of type struct tag.
177
A Reference Book On C Programming
In the above expression the parenthesis is necessary because of the precedence rules of C.
Otherwise, the expression would be equivalent to *(strptr.member-name) which means,
the result of expression is the value pointed by member-name member of strptr variable
of type struct tag.
An example is shown below that uses a structure and structure pointer to perform the
same operation as given in the example above.
struct student{
char name[40];
char roll[15];
int marks;
char remarks[50];
};
void main(){
struct student st, *pst = &st;
/*st is a structure variable and pst is a pointer that points to st*/
printf("Enter Name:");
fflush(stdin);
gets(st.name);
printf("Enter Roll No:");
fflush(stdin);
gets(st.roll);
printf("Enter Mark:");
scanf(" %d",&st.marks);
printf("Enter Remarks:");
fflush(stdin);
gets(st.remarks);
puts("You entered:"); /*we use -> operator to access the members
pointed by pst*/
printf("%s\n%s\n%d\n%s",pst->name, pst->roll, pst->marks, pst-
>remarks);
}
178
A Reference Book On C Programming
Comparing the two examples, we can conclude that following are equivalent
st.name pst->name
st.marks pst->marks
(*pst).name pst->name
An array is a data structure that can store a number of similar data items as a single
entity. We have already seen how arrays of primitive data types can be used to handle
number of similar data as single units. In this section we will see how arrays of structures
can be used to handle more than one structure variable at a time.
Here, struct is the keyword, tag is the name of the structure, str-variable is the name of
the array and no-of-elements in the size of the array.
An example of a structure array is given below. In this example, structure having four
member variables are created and an array containing 48 student type elements is defined.
Every element has its own set of four members and they can be accessed and processed
individually.
struct student {
char name[50];
char roll[13];
int marks;
char remarks[100];
};
struct student students[48];
A particular array element can be accessed by its subscript and members of that elements
can be accessed using the member access operator. For example, students[0] is the first
element in the array and students[1].name is the name member of second element of the
array and so on.
A complete example using a structure array is shown below. This program uses an array
of 48 element array, reads data and displays. If we have not used structure we would have
179
A Reference Book On C Programming
four arrays for name, roll, marks and remarks and track and process every element of all
four arrays.
struct student{
char name[40];
char roll[15];
int marks;
char remarks[50];
};
void main(){
struct student st[48]; /* array of 48 elements of type student */
int i;
for(i=0; i<3;i++){ /* read data for all members of each element*/
printf("Enter Name:");
fflush(stdin);
gets(st[i].name);
printf("Enter Roll No:");
fflush(stdin);
gets(st[i].roll);
printf("Enter Mark:");
scanf(" %d",&st[i].marks);
printf("Enter Remarks:");
fflush(stdin);
gets(st[i].remarks);
}
printf("You entered:");
for(i=0; i<3; i++) /*display all members of each element*/
printf("\n%s\n%s\n%d\n%s", st[i].name, st[i].roll, st[i].marks, st[i].remarks);
}
In such cases, the member of the inner structure can be accessed through the outer
structure. This is shown in the example below.
An example is shown below. This program defines a structure named person and within
this person, bday is another structure as a member variable.
180
A Reference Book On C Programming
struct person{
char name[40];
char address[15];
struct date bday; /* structure as member in another structure */
};
void main(){
struct person per; /* member of bday can be accessed through per.bday.year and so
on */
printf("Enter Name:"); /* read data*/
fflush(stdin);
gets(per.name);
printf("Enter Address:");
fflush(stdin);
gets(per.address);
printf("Enter Year of Birth:");
scanf(" %d", &per.bday.year);
printf("Enter Month of Birth:");
scanf(" %d", &per.bday.month);
printf("Enter Day of Birth:");
scanf(" %d", &per.bday.day);
printf("Hello, %s", per.name); /* display data*/
printf(" your address is %s and you were born on %d-%d-%d", per.address,
per.bday.year, per.bday.month, per.bday.day);
}
A structure can contain an array as its member. The member is defined as an ordinary
array of any data type. The elements of the member array is accessed in similar manner
as an ordinary array element. We use subscript after the member name. An example is
shown below. This program has a array of integer numbers as a member in struct student.
181
A Reference Book On C Programming
struct student {
char name[40];
char roll[15];
int marks[7]; /*array as a member to store 7 marks */
};
void main(){
struct student st;
int i, total=0;
printf("\nEnter Name:");
gets(st.name);
printf("Enter Roll No:");
fflush(stdin);
gets(st.roll);
puts("Enter marks for 7 subjects");
for(i=0; i<7; i++) /* read marks for seven subjects */
scanf(" %d",&st.marks[i]);
for(i=0; i<7; i++)
total+=st.marks[i]; /* add marks to get total */
puts("You entered following data:");
printf("\nName: %s",st.name);
printf("\nRoll No: %s\nMarks: ",st.roll);
for(i=0; i<7; i++) /*display seven marks and total */
printf("%d\t", st.marks[i]);
printf("\nTotal: %d\nAverage %.2f", total, total/7.0);
}
In this example we have seen how the elements of an array member is accessed and
processed in the program and finally displayed the result.
There are several ways to pass structure-type data to or from a function. Structure
members can be transferred individually, or entire structure can be transferred. Individual
members can be passed to a function as arguments in the function call, and a single
member can be returned to the calling function via return statement. To do so, each
structure member is treated as an ordinary variable.
182
A Reference Book On C Programming
183
A Reference Book On C Programming
#include<stdio.h>
#include<conio.h>
184
A Reference Book On C Programming
for(int i=0;i<n;i++)
printf("NAME:%s\nADDRESS:%s\nROLL NO:%d\n\n",s[i].name, s[i].address,
s[i].roll);
getch();
}
185
A Reference Book On C Programming
Chapter 9.0
Introduction
In all the programs written in previous chapters, we have seen that the data entered by the
user is stored in computer memory till the program is running. As soon as the program is
finished, the data are lost and to process the same data, user has to re-enter them.
Similarly, all the data processed by those programs were just displayed in screen and
destroyed when the program is finished.
In really useful programs (or applications), it is necessary that we store the data entered
by the user and the result of the processing done by the program. To store such data, we
use data files or simply files. Sometimes, we store the input data, sometimes the
processed output, or sometimes both of them. Whatever be the requirement, we use files
to store data. These files are saved in the auxiliary memory device such as hard disk or
floppy disk.
In C, there are a lot of library functions available for creating and processing files. There
are two types of files, namely, stream-oriented and system-oriented. The stream-oriented
files are also divided into two types - text files and unformatted data files. We will cover
stream-oriented text files in this chapter.
The data files help the users to keep records of input data, processed result or output data
for future use. Once created, the content of data files can be printed, re-opened and re-
processed. New data can be added or unnecessary data deleted (removed) from the stored
data file. When the data file becomes larger, required data can be searched, unnecessary
data removed and so on.
When working with data file, the first step is to establish a buffer area. A buffer area (a
portion of computer memory) temporarily stores information while being transferred
between the memory and the actual file. This buffer area allows the information to be
accessed faster because data are temporarily stored in this memory.
The buffer area is established by using a pointer to built-in structure FILE. This is done
by writing
FILE *fileptr;
Here FILE (note it is in upper case) is a built-in structure data type (defined in stdio.h)
and fileptr is a pointer to a FILE structure. The FILE establishes a buffer area and
different properties (including the starting address of that buffer area) are pointed by
fileptr variable. This variable is often referred to as a stream pointer or simply a stream.
186
A Reference Book On C Programming
A data file must be opened before before it can be created(!) or processed. Opening a file
means to associate a buffer area with that file so that we can read from or write to that
file. This also specifies how the data file will be utilized, that means what operations (like
reading, writing, adding etc) can be performed on the opened data files. We cannot open
as many files as we wish. Typically, the number of files that can be opened
simultaneously, is limited to 20.
A library function is provided to open a data file. The function is called fopen and its
general syntax is
Here, fileptr is a pointer to data type FILE. The function opens the file specified by the
string file-name. The naming convention of the file is governed by the operating system.
File-type (also called file mode) defines what operations can be performed on the opened
file. The table below shows the possible values for file-type and their meaning (possible
file operations). This argument is also a string type.
File-type Meaning
"r" Open an existing file only for reading.
Open a new file for writing only. If the file already exists, it is
"w"
destroyed and a new file is created in its place.
Open an existing file for appending (adding more data at the end of
"a"
the file). A new file is created if the file does not exist.
"r+" Open an existing file for both reading and writing.
Open a new file for both reading and writing. If the file already
"w+"
exists, it is destroyed and a new file is created in its place.
Open an existing file for both reading and appending. A new file is
"a+"
created if the file does not exist.
The function fopen returns a pointer to the beginning of the buffer area associated with
the file whose name is supplied as an argument to the function. If a buffer area could not
be allocated, it returns NULL. This NULL value can be used to inform the user about the
problem.
When all the operations associated to a opened file are complete, the file must be closed.
This is done by using the fclose function. This function takes the fileptr as an argument
and closes the file associated with that pointer. Before closing the file, it updates the file
with the content of buffer area if the file is not up-to-date. The syntax for fclose is
fclose (fileptr);
Typically, many compilers automatically close the file at the end of the program if not
closed explicitly by the programmer.
187
A Reference Book On C Programming
We can write a program to create a data file (sometimes a data file is created using a text
editor also). A file must be created before it can be processed. The first step in creating a
data file is already mentioned in the preceding section. After opening the file, we can
directly write information entered by the user or write data obtained after processing
some input by the user. Whatever be the case, there are several library functions to write
data to a file. This can be done character by character, a whole string or a complete data
structure like an array or a structure variable. This time we will consider only character
and string writing.
188
A Reference Book On C Programming
An example is given below that reads some information from the user and writes to a data
file. This program uses three different functions to write to a file. The program will
generate an error message if the file could not be opened.
void main(){
FILE *fileptr; /* required variables */
char name[40];
int roll;
char program[12];
char group;
189
A Reference Book On C Programming
Otherwise, we may lose the required information. Table below shows some of the
functions to read data from an existing file.
Here's an example that reads and displays the content of the previously written
file. The program does this in two ways, character by character until the end of file (a
integer value of -1 to indicate the end of file) is reached and line by line till the end is
reached.
/* display the file line by line*/
/* display the file character by character */
void main(){
void main(){
FILE *fileptr;
FILE *fileptr;
char line[81];
char ch;
fileptr = fopen ("test.txt","r"); /* open for
fileptr = fopen ("test.txt","r"); /* open for
reading*/
reading*/
if (fileptr==NULL) {
if (fileptr==NULL) {
printf ("ERROR opening the file. Press
printf ("ERROR opening the file. Press
any key to continue...");
any key to continue...");
getch();
getch();
exit(1);
exit(1);
}
}
/* read 80 characters at a time until end
/* read character by character untill eond
of file is reached */
of file is reached */
while ((fgets (line, 80,
while ((ch = fgetc (fileptr))!=EOF) /*
fileptr))!=NULL)/* when all the lines are
EOF indicated end of file */
read, line will be NULL */
putchar (ch);
printf ("%s",line);
/* close the file */
/* close the file */
fclose (fileptr);
fclose (fileptr);
printf("\n\nEnd of data");
printf("\n\nEnd of data");
}
}
190
A Reference Book On C Programming
Here is another program that takes two filenames entered by the user and copies the
content of the first file to second file if the first file exists.
void main(){
FILE *srcfile, *trgfile;
char ch;
char sfile[13], tfile[13]; /* file names*/
printf("Enter the source file name:");
scanf(" %s", sfile);
printf("Enter the target file name:");
scanf(" %s", tfile);
srcfile = fopen (sfile, "r"); /* open source for reading*/
if (srcfile==NULL) {
printf ("File %s does not exist. Press any key to continue...", sfile);
getch();
exit(1);
}
trgfile = fopen (tfile, "w"); /* open destination for writing*/
if (trgfile==NULL) {
printf ("File %s cannot be created. Press any key to continue...", tfile);
getch();
exit(1);
}
/* read character by character from srcfile and write to trgfile until end of file in
srcfile is reached */
while ((ch=fgetc (srcfile))!=EOF)
fputc (ch, trgfile);
/* close both files */
fclose (srcfile);
fclose (trgfile);
printf("\n\n1 file copied.");
}
191
A Reference Book On C Programming
Here is a program that reads the formatted data from a file using fscanf function. The file
is written using the first example program of this chapter. The formats for reading are
same as format used in writing and all separate data are written in new line.
Function fscanf reads data from each line.
192
A Reference Book On C Programming
Example:
#include<stdio.h>
struct employee
{
char name[100];
int salary;
float age;
};
void main()
{
struct employee emp, e;
FILE *filepointer;
char filename[50];
int decision = 1;
printf(“Enter filename:”);
scanf(“%s”,filename);
if((filepointer=fopen(filename, “wb”)= =NULL)
exit();
while(decision)
{
printf(“Enter name salary and age of employees:”);
scanf(“%s%d%f”,emp.name,&emp.salary,&emp.age);
fwrite(&emp,sizeof(emp),1,filepointer);
printf(“Enter \n0 to exit: \n 1 to continue”);
scanf(“%d”,&decision);
}
fclose(filepointer);
if((filepointer=fopen(filename, “rb”))= =NULL)
exit();
while(fread(&e,sizeof(e),1,filepointer)= =1)
printf(“Name : %s\nSalary : %d\n age : %f\n”,e.name,e.salary,e.age);
fclose(filepointer);
getch( );
}
This example reads the structure members from the standard input device, writes them to
the file using function fwrite. It takes four arguments: the first is the address of the
structure to be written to the disk. The second is the size of the structure in bytes, the
third is the number of such structures that we want to write at a time and the fourth is the
pointer to the file where we want to write. Then, the written contents is read back using
function fread, which takes the similar four arguments as in fwrite: address of structure,
193
A Reference Book On C Programming
size, number of structures to be read at a time and file pointer. It returns the number of
records it read. In the above example, it returns 1. After reaching at the end of file, fread
cannot read anything; it returns 0, which signal to stop reading. Function fread causes the
data read from the disk and to be placed in the structure variable e. We can use the
structure variable emp. Note that, in this example file is opened in binary mode. After
reading from file to memory, the structure members are displayed on the screen using
function printf.
194
A Reference Book On C Programming
Computer Programming I
(EG 442 CT)
Lecture: 3 Year: 1
Practical: 3 Part: A
Course Objective:
To develop a working knowledge of computer methods, systems and
languages. Emphasis on developing programming skills using C language.
3. Introduction to C 3 hours
3.1 Data Types in C
3.2 Arithmetic Expressions in C
3.3 Structure of a C program
195
A Reference Book On C Programming
6. Functions 5 hours
6.1 Introduction
6.2 Function Definition and return statement
6.3 Function Prototypes
6.4 Function Invocation, Call by value & Call by reference
6.5 Concept of Local, Global, Automatic and static variables
6.6 Recursive Functions
8. Structures 5 hours
8.1 Introduction
8.2 Declaring and defining Structures
8.3 Arrays of Structures
8.4 Structures within Structures
8.5 Structures containing Arrays
Reference Books:
196
A Reference Book On C Programming
Practice Questions
1. Write a program that takes two strings - string1 and string2. Add a function that
takes those two strings and compare their corresponding characters until end of
one string is reached. When a differing character is found, the function should
return the difference of ASCII code of the differing characters of string1 and
string2. If no differing characters are found the function should return 0. Now test
your function by providing two strings and calling the function.
2. Write a program that allocates memory space for n integer numbers. Take n from
the user. Write a function to read and store n numbers in the reserved space. Write
another function that finds the prime numbers among the stored numbers and
display them.
4. Write a program that reads numbers until the user enters zero. Your program
should find out the largest positive number and the smallest negative number
entered by the user, calculate sum of these two numbers and display it.
5. Write a program that takes a string and pass to a function. The function takes the
string and converts all of its upper case characters to lower case and vice versa. In
the main program display the string before conversion and after conversion. Your
function must convert the original string not a copy of it.
6. Write a program that allocates memory space for n integer numbers. Take n from
the user. Write a function to read and store n numbers in the reserved space. Write
another function that finds the largest and smallest numbers stored in the reserved
space and display them.
8. Write a program that reads numbers until the user enters zero. Your program
should calculate and display the average of positive and negative numbers
separately excluding the last zero.
197
A Reference Book On C Programming
10. Write a program that allocates memory space for n integer numbers. Write a
function that reads and stores n number in the reserved space. Write another
function that finds the largest and smallest number stored and display their sum.
11. Write a program that reads n elements of a one-dimensional array. Write one
function that takes the array, and counts the prime numbers in that array and
return the count. In the main program, display the count.
12. Write a program that reads numbers until the user enters zero. Count and sum all
the numbers which are exactly divisible by four but not by eight. Display the sum
and count.
13. Write a program that allocates memory space for n integer numbers. Write a
function that reads and stores n number in the reserved space. Write another
function that finds the largest positive and smallest negative number stored and
display their sum.
14. Write a program that reads data for a MxN order matrix. Write a function that
takes the matrix and generates its transpose. Write another function that takes the
transpose matrix and display it.
15. Write a program that reads numbers until the user enters zero. If a number entered
is prime display it.
16. Write a program that reads n elements of a one-dimensional array. Write one
function that takes the array, and finds the smallest positive number and largest
negative numbers in that array and return them to the main function and in the
main display them.
17. Write a program that reads numbers until user enters zero. Your program must
sum and count positive and negative numbers separately and displays them.
18. Write a program that reads n elements of a one-dimensional array. Write one
function that takes the array, finds and returns both the largest and smallest
numbers in the array. In the main program, display the largest and smallest
number obtained from the function.
19. Write a program that finds the sum of a series 1x2 + 2x3 + 3x4 + 4x5 + … up to n
terms. Read n from the user.
20. Write a program that reads n elements of a one-dimensional array. Write one
function that takes the array, and counts the prime numbers in that array and
return the count. In the main program, display the count.
198
A Reference Book On C Programming
21. Write a program that reads numbers until the user enters zero. Find and display
the smallest negative number and largest positive number among them.
22. Write a program that reads n elements of a one-dimensional array. Write one
function that takes the array, and returns the count and sum of the elements that
are divisible by seven. In the main program, display the count and sum.
23. Write a program that reads numbers until the user enters zero. Count and sum all
the numbers which are exactly divisible by five but not by four. Display the sum
and count.
24. Write a program to calculate and display the interest for a bank account. The
conditions for interest rates are as follows:
a. Rate = 15% if P >= 1,000,000
b. Rate = 12 % if P > 100,000 or N >= 2
c. Rate = 10 % if P < 20,000 or N < 2
d. Rate = 8% otherwise
25. Write a program that allocates memory space for n integer numbers. Write a
function that reads the n numbers and stores them in the reserved memory and
find and return the largest number. In the main program, display that number.
26. Write a program that takes a string from the user and passes it to a function. The
function finds and returns the number of words in the string. In the main program,
display the number of words.
27. Write a function that finds and returns the number of digits in an integer number.
In the main program read a number, pass it to the function, and display the result.
28. Write a program that reads n elements of a one-dimensional array. Write one
function that takes the array and converts each element to its absolute value if the
element is negative and square value if the element is positive. In the main
program, display the new content of the array.
29. Write a program that allocates memory space for n integer numbers. Write a
function to read n numbers, store them in the reserved memory space, and find
and return the average of them. In the main program, display all the numbers and
the average.
30. Write a program to take a string form the user and convert all lower case
characters into upper case and display. Your program must have a function that
takes the string and converts the characters.
31. Write a function that takes an integer number and return the number reversed. In
the main program read a number, pass it to the function, and display the result. (if
n =123 return 321)
199
A Reference Book On C Programming
32. Write a program that reads n elements of a one-dimensional array. Write one
function that takes the array, finds and returns the average, the largest and the
smallest element of the array. In the main program display them.
33. Write a computer program to evaluate the following series, where the value of x
and n can be given by a user in main function. Create two functions. One for
evaluating xn and other for calculating the factorial of n. Both functions should
return values to the main functions. You should display the individual terms and
total sum of the given series.
34. Write a computer program to read two matrices A and B in the main function,
check the condition for multiplication, if the condition is not satisfied display a
message for that, if the condition is satisfied multiply them and store the result in
the third matrix named C. Now create a function display( ).Pass the matrix to the
display( ) function and display the product matrix.
35. Write a program that reads two one dimensional arrays of order m and n
respectively and merge the contain of first and second array to find third array of
order (m +n ) and display it.( Use dynamic memory allocation.)
37. Write a program to take names as string, marks for seven subjects as integer array
and total percentage of a student as float as input & print them back using
structure.
40. Write a program to find product of two matrices. Make interactive output.
42. WAP to find the sum of user given numbers until user enters a zero.
43. Write a program to store roll no, name, faculty & batch of 10 students using
structures and print only the name of all students who joined in the year 2000.
200
A Reference Book On C Programming
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
45. Write a program that takes two strings – string 1 and string 2. Add a function
comstr( ) that takes those two strings and compare them.
46. Write a program that takes a string from the user and pass it to a function strfunc(
). The function finds and returns the number of words in the string in the main
program. Display the number of words.
47. Write a program to sort the given array. Take inputs from user.
48. Write a program to display highest user given number from an array, the number
of elements in array is input by the user.
49. Create function count( ) to count the vowels in entered string using switch
statement and display result in main( ).
50. Write a program that reads numbers until the user enters zero. Your program
should calculate and display the average of positive and negative numbers
separately.
51. Write a program that allocates memory space for n integer numbers. Write a
function that reads and stores n numbers in the reserved space. Write another
function that finds the largest positive and smallest negative number stored and
display their sum.
52. Write a program that reads data for an M x N order matrix. Write a function that
takes the matrix and generates its transpose. Write another function that takes the
transpose matrix and display.
201
A Reference Book On C Programming
55. Write a program to generate the Fibonacci series up to nth term using recursive
function.
56. Write a program that reads two one dimensional arrays of order m and n
respectively and merge the contain of first and second array to find third array of
order (m+ n) and display it.( Use dynamic memory allocation.)
57. Write a program to take names, mark and percentage of a student as input & print
them back using structure.
62. WAP to find the sum of user given numbers until user enters a zero.
63. Write a program to store roll no, name, faculty & batch of 10 students using
structures and write a function to print the name of all students who joined in the
year 2000.
64. Write a program that takes two strings – string 1 and string 2. Add a function that
takes those two strings and compare them.
65. Write a program to input a sentence and display the number of words in the
sentence.
66. WAP to sort the given array. Take inputs from user.
67. Write a program to display highest user given number from an array, the number
of elements in array is input by the user.
68. Write a program for calculator operation with +, /, *, - using switch statement and
function for each case.
69. Write a program that allocates memory apace for n integer numbers. Write a
function that reads and stores n numbers in the reserved space. Write another
function that finds the largest positive and smallest negative number stored and
display their sum.
202
A Reference Book On C Programming
70. Write a program that reads data for an M x N order matrix. Write a function that
takes the matrix and generates its transpose. Write another function that takes the
transpose matrix and display.
71. Write a computer program to copy the content of one text file named first.txt to
another file second.txt. Then, read the content of second file and display on the
screen. The name of both files must be input by the user.
73. Write a computer program to read two matrices A and B in the main function,
check the condition for multiplication, if the condition is not satisfied display a
message for that, if the condition is satisfied multiply them and store the result in
the third matrix named C. Now create a function display().Pass the matrix to the
display() function and display the product matrix.
74. Write a computer program to evaluate the following series. Where the value of x
and n can be given by a user in main function. Create two functions. One for
evaluating xn and other for calculating the factorial of n. Both functions should
return values to the main functions. You should display the individual terms and
total sum of the given series.
x/1!-x3/3!+x5/5!-x7/7!…………………..
75. Write a computer program to allocate the memory space as required by the user
for three arrays. User enters the numbers for two arrays and the program sums the
corresponding array elements and stores them in the third array.
76. Write a computer program to create an integer type array of size n. Read the array
elements from user. Create a function minmax(). Pass the array to the function
using pointer notation. Find the largest and smallest element in the side the
function. Display the smallest element from minmax() function and return the
largest element to main function and display it.
77. Write a computer program that takes a string from a user pass it to function
named wordscount(). Calculate the numbers of words in the string as well as
number of a’s in the string. Return the numbers of words in the main function and
display. Display the number of a’s in the wordcount() function.
78. Write a program that takes a string from the user and converts the string into
lower case if the first character is lowercase or convert the string in to uppercase
if the first character is uppercase. (Hint use islower() or isupper() functions to
detect the characters in the given string and use toupper() or tolower() functions
to convert the characters.)
203
A Reference Book On C Programming
References
1. Gottfried, Byron S., Programming with C, Second Edition, Tata McGraw-Hill
Edition, 2003.