Programming in ANSI C 6th Edition E. Balagurusamy - Quickly download the ebook to never miss important content
Programming in ANSI C 6th Edition E. Balagurusamy - Quickly download the ebook to never miss important content
Programming in C E. Balagurusamy
https://ebookmass.com/product/programming-in-c-e-balagurusamy/
https://ebookmass.com/product/computer-programming-e-balagurusamy/
https://ebookmass.com/product/programming-for-problem-solving-e-
balagurusamy/
https://ebookmass.com/product/problem-solving-and-python-programming-
e-balagurusamy/
Data Structures E. Balagurusamy
https://ebookmass.com/product/data-structures-e-balagurusamy/
https://ebookmass.com/product/c-programming-language-c-programming-
lang-_p2-2nd-edition-ebook-pdf/
https://ebookmass.com/product/c-programming-8th-ed-edition-d-s/
https://ebookmass.com/product/c-programming-for-dummies-2nd-edition-
dan-gookin/
https://ebookmass.com/product/c-programming-absolute-beginners-
guide-3rd-edition-perry/
PROGRAMMING IN
ANSI C
— Sixth Edition —
ABOUT THE AUTHOR
E Balagurusamy, is presently the Chairman of EBG Foundation, Coimbatore. In the past he has also
held the positions of member, Union Public Service Commission, New Delhi and Vice-Chancellor, Anna
Fundamentals of Computers
Computing Fundamentals and C Programming
Programming in C#, 3/e
Programming in Java, 4/e
Object-Oriented Programming with C++, 5/e
Programming in BASIC, 3/e
Numerical Methods
Reliability Engineering
A recipient of numerous honors and awards, he has been listed in the Directory of Who’s Who of
PROGRAMMING IN
ANSI C
— Sixth Edition —
E Balagurusamy
Chairman
EBG Foundation
Coimbatore
McGraw-Hill Offices
New Delhi New York St Louis San Francisco Auckland Bogotá Caracas
Kuala Lumpur Lisbon London Madrid Mexico City Milan Montreal
San Juan Santiago Singapore Sydney Tokyo Toronto
Tata McGraw-Hill
Published by the Tata McGraw Hill Education Private Limited,
7 West Patel Nagar, New Delhi 110 008.
Copyright © 2012, 2011, 2007, 2004, 2002, 1992, 1982 by Tata McGraw Hill Education Private Limited.
No part of this publication may be reproduced or distributed in any form or by any means, electronic,
mechanical, photocopying, recording, or otherwise or stored in a database or retrieval system without
the prior written permission of the publishers. The program listings (if any) may be entered, stored and
executed in a computer system, but they may not be reproduced for publication.
Information contained in this work has been obtained by Tata McGraw-Hill, from sources believed to be
reliable. However, neither Tata McGraw-Hill nor its authors guarantee the accuracy or completeness of
any information published herein, and neither Tata McGraw-Hill nor its authors shall be responsible for
any errors, omissions, or damages arising out of use of this information. This work is published with the
understanding that Tata McGraw-Hill and its authors are supplying information but are not attempting
to render engineering or other professional services. If such services are required, the assistance of an
appropriate professional should be sought.
Typeset at Tej Composers, WZ-391, Madipur, New Delhi 110063, and printed at
Cover Printer:
CONTENTS
1 Overview of C 1
1.1 History of C 1
1.2 Importance of C 3
3
6
7
9
10
12
13
14
1.11 Unix System 14
1.12 MS-D System 17
Review Questions 18
Programming Exercises 20
45
Review Questions 48
Programming Exercises 50
131
135
Review Questions 143
Programming Exercises 147
7 Arrays 192
7.1 Introduction 192
194
195
197
203
207
7.7 Multi-Dimensional Arrays 215
7.8 Dynamic Arrays 216
7.9 More about Arrays 217
Review Questions 230
Programming Exercises 233
11 Pointers 357
11.1 Introduction 357
357
360
361
11.5 Initialization of Pointer Variables 362
363
11.7 Chain of Pointers 366
11.8 Pointer Expressions 366
11.9 Pointer Increments and Scale Factor 368
11.10 Pointers and Arrays 369
372
11.12 Array of Pointers 374
375
378
11.15 Pointers to Functions 379
11.16 Pointers and Structures 382
384
Review Questions 391
Programming Exercises 394
428
429
431
435
438
440
Review Questions 448
Programming Exercises 450
Bibliography 555
Index 556
Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
C
New to this Edition
pleasurable activity!
and their execution. Chapter 2 discusses how to declare the constants, variables and data types.
Chapter 3 Chapter 4 details
Chapter 5
Chapter 6
Chapters 7 and 8
covered in Chapter 8. are on functions, structures and unions. Pointers, perhaps
Chapter 11 in the most user-friendly manner.
Chapters 12 and 13 Chapter 14
Chapter 15
xii Preface to the Sixth Edition
and educational.
areas.
Numerous chapter-end questions and exercises provide ample opportunities to the readers to
review the concepts learned and to practice their applications.
questions
Separate appendix dedicated to latest compiler C99 features
Web Supplement
http://www.mhhe.com/balagurusamy/ansic6 which
E Balagurusamy
Publisher’s Note
[email protected]
Please report any piracy spotted by you as well!
1 OVERVIEW OF C
Key Terms
printf I Program
1.1 HISTORY OF C
‘C’ seems a strange name for a programming language. But this strange sounding language is one of
the most popular computer languages today because it is a structured, high-level, machine independent
language. It allows software developers to develop programs without worrying about the hardware
platforms where they will be implemented.
The root of all modern languages is ALGOL, introduced in the early 1960s. ALGOL was the
widely used in Europe. ALGOL gave the concept of structured programming to the computer science
In 1967, Martin Richards developed a language called BCPL (Basic Combined Programming
Language) primarily for writing system software. In 1970, Ken Thompson created a language using
system at Bell Laboratories. Both BCPL and B were “typeless” system programming languages.
many concepts from these languages and added the concept of data types and other powerful features.
superhighway.
For many years, C was used mainly in academic environments, but eventually with the release of
support among computer professionals. Today, C is running under a variety of operating system and
hardware platforms.
traditional C”. The language became more
‘The C Programming Language’
the programming community. The rapid growth of C led to the development of different versions of the
language that were similar but often incompatible. This posed a serious problem for system developers.
2 Programming in ANSI C
Java modelled on C
All popular computer languages are dynamic in nature. They continue to improve their power and
the usefulness of the language. The result was the 1999 standard for C. This version is usually referred
to as C99. The history and development of C is illustrated in Fig. 1.1
Although C99 is an improved version, still many commonly available compilers do not support all of
the new features incorporated in C99. We, therefore, discuss all the new features added by C99 in an
1.2 IMPORTANCE OF C
whose rich set of built-in functions and operators can be used to write any complex program. The C
compiler combines the capabilities of an assembly language with the features of a high-level language
operating system.
Another important feature of C is its ability to extend itself. A C program is basically a collection of
functions that are supported by the C library. We can continuously add our own functions to C library.
main( )
{
/*…………printing begins………………*/
printf(“I see, I remember”);
/*………………printing ends…………………*/
}
program is main and the execution begins at this line. The main( ) is a special function used by the
C system to tell the computer where the program starts. Every program must have exactly one main
function. If we use more than one main
beginning of the program.
The empty pair of parentheses immediately following main indicates that the function main has no
arguments (or parameters). The concept of arguments will be discussed in detail later when we discuss
functions (in Chapter 9).
The opening brace “{ main and the closing
end of the program. All the statements between these two braces form the function body. The function
In this case, the function body contains three statements out of which only the printf line is an
executable statement. The lines beginning with /* and ending with */ comment lines. These
are used in a program to enhance its readability and understanding. Comment lines are not executable
statements and therefore anything between /* and *
/* = = = =/* = = = = */ = = = = */
is not valid and therefore results in an error.
use them liberally in our programs. They help the programmers and other users in understanding the
various functions and operations of a program and serve as an aid to debugging and testing. We shall
see the use of comment lines more in the examples that follow.
printf( ) function, the only executable statement of the program.
printf(“I see, I remember”);
printf means that it is a function that
I see,
I remember!
This can be achieved by adding another printf function as shown below:
printf(”I see, \n”);
printf(“I remember !”);
The information contained between the parentheses is called the argument of the function. This
printf function is “I see, \n” and the second is “I remember !”. These arguments are
simply strings of characters to be printed out.
Overview of C 5
a typewriter. After printing the character comma (,) the presence of the newline character \n causes the
and I.
It is also possible to produce two or more lines of output by one printf statement with the use of
newline character at appropriate places. For example, the statement
printf(“I see,\n I remember !”);
will output
I see,
I remember !
while the statement
printf( “I\n.. see,\n… … … I\n… … … remember !”);
will print out
I
.. see,
… … … I
… … … remember !
#include <stdio.h>
a distinction between uppercase and lowercase letters. For example, printf and PRINTF are not the
REMEMBER”.
The above example that printed I see, I remember is one of the simplest programs. Figure 1.3
highlights the general format of such simple programs. All C programs need a main function.
Program statements
End of program
beginning to give information such as name of the program, author, date, etc. Comment characters are
also used in other lines to indicate line numbers.
The words number and amount are variable names that are used to store numeric data. The
numeric data may be either in integer form or in real form. In C, all variables should be declared to tell
the compiler what the variable names are and what type of data they hold. The variables must be
declared before they are used. In lines 5 and 6, the declarations
int number;
float amount;
tell the compiler that number is an integer (int) and amount (
The words such as int and are called the keywords and cannot be used as variable names. A
assigning a data value to it. This is done in lines 8 and 10. In line-8,
an integer value 100 is assigned to the integer variable number and in line-10, the result of addition of
amount. The statements
number = 100;
amount = 30.75 + 75.35;
are called the assignment statements. Every assignment statement must have a semicolon at the end.
The next statement is an output statement that prints the value of number. The print statement
printf(“%d\n”, number);
In the program, the variable value represents the value of money at the end of the year while amount
represents the value of money at the start of the year. The statement
amount = value ;
current year as the value at start of the next year.
8 Programming in ANSI C
Let us consider the new features introduced in this program. The second and third lines begin with
instructions. A symbolic constant for use in the program.
Whenever a symbolic name is encountered, the compiler substitutes the value associated with the
0 5000.00
1 5550.00
2 6160.50
3 6838.15
4 7590.35
5 8425.29
6 9352.07
7 10380.00
8 11522.69
9 12790.00
10 14197.11
Author: Various
Language: Finnish
J. A.
Runollisuudella, niinkuin ylipään kirjallisuudella, on velvollisuutena
yksinkertaisimmilla esimerkeillä elämästä ja luonnosta näyttää niiden
loisto- ja varjopuolia. Se on silloin, kuten matematikikin, puhtainta ja
sanommepa ylevintä, kun voi osoittaa suurimpia poikkeamattomia
totuuksia kaikkein vähäisimmillä ja jokapäiväisimmillä keinoilla.
Silloin se kohtaa kaiun jokaisen rinnassa, kun koskettaa aivan
likeisimpiä ja tutuimpia kieliä elämämme soittokoneessa. Jos se
poikkeaa tältä uralta ja joko ylenee Olympoon muka jumalallista
kanteletta soittamaan taikka muuten menee teeskentelyyn, tuntuu
se meistä vieraalta. Se ei enää viehätä eikä miellytä meitä, me
emme voi seurata sen ääntä todellisuudessa ja joskus vain voimme
hämärän kuvitusvoiman avulla saada siitä jotakin kuvaa eteemme.
Näin ollen on jo runollisuus menettänyt lukijalle kaiken arvonsa, se
jääpi unohduksiin ja kuolee kuin äitinsä rinnoilta temmattu imevä
lapsi. Tämmöiseen ilmassa häälyvään tilaan on runollisuus useinkin
noussut, ja onpa välistä voinut ajaksi huikaista lukijoitaan, mutta
esteetillisemmästi sivistyneiden arvostelua se ei ole koskaan voinut
kestää, se on itse siitä huiennut. — Päin vastoin on taas semmoisten
runomusten laita, jotka, niinkuin mainitsimme, ovat sopusoinnussa
meissä itsessämme vallitsevien aatteiden ja olojen kanssa, ja siten,
itse juuri kohoamatta erittäin ylös, nostavat vastustamattomalla
voimalla meidät ja aatteemme korkeuksiin. Semmoiset runomukset
ovat pysyneet ijäti uusina ja ihailtuina, ovatpa saavuttaneet
klassillisuuden nimen. Ne ovat kuin itsestään syntyneitä, ei
kenenkään tekemiä, niistä voi sanoa, että ne ovat syttyneet
"jumalallisesta kipunasta", joka on runoilijan henkeä lämmittänyt.
Tämmöisten runomusten joukkoon on luettu maailman suurimpain
yksityisten runoilijain (Runebergin y.m.) teokset ynnä myöskin
muutamat kansanrunot. Viimemainituista Kalevalamme tuskin
tarvinnee siirtyä muiden kuin Kreikkalaisten kansanrunojen edestä.
Tämäkin esiintuo kaikkein runollisimmat ajatukset ja ihanimmat
kuvat tavallisimmilla sanoilla, jokapäiväisimmät tapaukset näyttää se
semmoisena kuin ne esiintyvät ja me ne olemme nähneet, tuomalla
siinä kuitenkin esiin jonkin puolen, jota me emme ole huomanneet ja
joka juuri sen vuoksi, että se ilmaantuu ikäänkuin uutena-
vanhanatuttuna, viehättää meitä. Pienimmissäkin seikoissa löytää se
jonkun kohdan, jota kannattaa ihailla, samalla kun se taas juhlalliset
kohtaukset elämässä ja ylentävät ilmiöt luonnossa selittää sillä
arvollisuudella kuin asia vaatii, ottaen molempia selittääkseen
vertauksilla, jotka johtuvat asiasta ja likeltä kuuluvat siihen.
Selma Anttila.
Kirjailijataipaleeltani.
Selma Anttila.
Kaarlo Atra.
*****
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookmass.com