Free Essay

C Book

In:

Submitted By vamsi
Words 46741
Pages 187
Let Us C
Fifth Edition

Yashavant P. Kanetkar

Dedicated to baba Who couldn’t be here to see this day...

About the Author
Destiny drew Yashavant Kanetkar towards computers when the IT industry was just making a beginning in India. Having completed his education from VJTI Mumbai and IIT Kanpur in Mechanical Engineering he started his training company in Nagpur. Yashavant has a passion for writing and is an author of several books in C, C++, VC++, C#, .NET, DirectX and COM programming. He is a much sought after speaker on various technology subjects and is a regular columnist for Express Computers and Developer 2.0. His current affiliations include being a Director of KICIT, a training company and DCube Software Technologies, a software development company. In recognition to his contribution Microsoft awarded him the prestigious “Best .NET Technical Contributor” award recently. He can be reached at kanetkar@kicit.com.

Acknowledgments
It has been a journey of almost a decade from the stage the book idea of “Let Us C” was conceived up to the release of this Fifth Edition. During this journey I have met so many students, developers, professors, publishers and authors who expressed their opinions about Let Us C. They have been the main motivators in my effort to continuously improve this book. In particular I am indebted to Manish Jain who had a faith in this book idea, believed in my writing ability, whispered the words of encouragement and made helpful suggestions from time to time. The five editions of this book saw several changes and facelifts. During this course people like Ajay Joshi, Amol Tambat, Ajay Daga, Nandita Shastri, Mrunal Khandekar and Rahul Bedge helped in writing programs, spotting bugs, drawing figures and preparing index. I trust that with their collective acumen all the programs would run correctly in all situations. Anup Das, my colleague has a lot of influence on this Fifth Edition. He helped my clarify my thoughts and pointing me in the direction of Windows and Linux. He sincerely wanted this edition to offer “C, in today’s perspective”. I am hopeful that his dream has been realized. I thank Seema, my wife, for her friendship and for her contributions in everything that I do in IT in ways more than she could ever guess. Though she is a Gynecologist by profession she has the uncanny ability to come up with suggestions that make me feel “Oh, why didn’t it occur to me”. And finally my heartfelt gratitude to the countless students who made me look into every nook and cranny of C. I would forever remain indebted to them..

v

Preface to the Fifth Edition
It is mid 2004. World has left behind the DOTCOM bust, 9/11 tragedy, the economic downturn, etc. and moved on. Countless Indians have relentlessly worked for close to two decades to successfully establish “India” as a software brand. At times I take secret pleasure in seeing that a book that I have been part of, has contributed in its own little way in shaping so many budding careers that have made the “India” brand acceptable. Computing and the way people use C for doing it keeps changing as years go by. So overwhelming has been the response to all the previous editions of “Let Us C” that I have now decided that each year I would come up with a new edition of it so that I can keep the readers abreast with the way C is being used at that point in time. There are two phases in every C programmer’s life. In the first phase he is a learner trying to understand the language elements and their nuances. At this stage he wants a simple learning environment that helps him to master the language. In my opinion, even today there isn’t any learning environment that can beat Turbo C/C++ for simplicity. Hence the first fifteen chapters are written keeping this environment in mind, though a majority of these programs in these chapters would work with any C compiler. Armed with the knowledge of language elements the C programmer enters the second phase. Here he wishes to use all that he has learnt to create programs that match the ability of programs that he see in today’s world. I am pointing towards programs in Windows and Linux world. Chapters 16 to 21 are devoted to this. I would like to your attention the fact that if you want to program Windows or Linux you need to have a very good grasp over the programming model used by each of these OS. Windows messaging architecture and Linux signaling mechanism are the cases in point. Once you understand these thoroughly rest is just a vi

matter of time. Chapters 16 to 21 have been written with this motive. In Linux programming the basic hurdle is in choosing the Linux distribution, compiler, editor, shell, libraries, etc. To get a headstart you can follow the choices that I found most reasonable and simple. They have been mentioned in Chapter 20 and Appendix H. Once you are comfortable you can explore other choices. In fourth edition of Let Us C there were chapters on ‘Disk Basics’, ‘VDU Basics’, ‘Graphics’, ‘Mouse Programming’, ‘C and Assembly’. Though I used to like these chapters a lot I had to take a decision to drop them since most of them were DOS-centric and would not be so useful in modern-day programming. Modern counterparts of all of these have been covered in Chapters 16 to 21. However, if you still need the chapters from previous edition they are available at www.kicit.com/books/letusc/fourthedition. Also, all the programs present in the book are available in source code form at www.kicit.com/books/letusc/sourcecode. You are free to download them, improve them, change them, do whatever with them. If you wish to get solutions for the Exercises in the book they are available in another book titled ‘Let Us C Solutions’. ‘Let Us C’ is as much your book as it is mine. So if you feel that I could have done certain job better than what I have, or you have any suggestions about what you would like to see in the next edition, please drop a line to letuscsuggestions@kicit.com. All the best and happy programming!

vii

Contents
1. Getting Started What is C Getting Started with C The C Character Set Constants, Variables and Keywords Types of C Constants Rules for Constructing Integer Constants Rules for Constructing Real Constants Rules for Constructing Character Constants Types of C Variables Rules for Constructing Variable Names C Keywords The First C Program Compilation and Execution Receiving Input C Instructions Type Declaration Instruction Arithmetic Instruction Integer and Float Conversions Type Conversion in Assignments Hierarchy of Operations Associativity of Operators Control Instructions in C Summary Exercise 2. The Decision Control Structure Decisions! Decisions! The if Statement The Real Thing Multiple Statements within if The if-else Statement 1 2 4 5 6 7 8 9 10 11 11 12 13 19 21 23 24 25 29 29 31 34 37 37 38

49
50 51 55 56 58

viii

Nested if-elses Forms of if Use of Logical Operators The else if Clause The ! Operator Hierarchy of Operators Revisited A Word of Caution The Conditional Operators Summary Exercise 3. The Loop Control Structure Loops The while Loop Tips and Traps More Operators The for Loop Nesting of Loops Multiple Initialisations in the for Loop The Odd Loop The break Statement The continue Statement The do-while Loop Summary Exercise 4. The Case Control Structure Decisions Using switch The Tips and Traps switch Versus if-else Ladder The goto Keyword Summary Exercise

61 62 64 66 72 73 73 76 77 78

97
98 99 101 105 107 114 115 116 118 120 121 124 124

135
136 140 144 145 148 149

ix

5. Functions & Pointers What is a Function Why Use Functions Passing Values between Functions Scope Rule of Functions Calling Convention One Dicey Issue Advanced Features of Functions Function Declaration and Prototypes Call by Value and Call by Reference An Introduction to Pointers Pointer Notation Back to Function Calls Conclusions Recursion Recursion and Stack Adding Functions to the Library Summary Exercise 6. Data Types Revisited Integers, long and short Integers, signed and unsigned Chars, signed and unsigned Floats and Doubles A Few More Issues… Storage Classes in C Automatic Storage Class Register Storage Class Static Storage Class External Storage Class Which to Use When Summary Exercise

157
158 165 166 171 172 173 174 175 178 178 179 186 189 189 194 197 201 201

213
214 216 217 219 221 223 224 226 227 230 233 234 235

x

7. The C Preprocessor Features of C Preprocessor Macro Expansion Macros with Arguments Macros versus Functions File Inclusion Conditional Compilation #if and #elif Directives Miscellaneous Directives #undef Directive #pragma Directive Summary Exercise 8. Arrays What are Arrays A Simple Program Using Array More on Arrays Array Initialization Bounds Checking Passing Array Elements to a Function Pointers and Arrays Passing an Entire Array to a Function The Real Thing Two Dimensional Arrays Initializing a 2-Dimensional Array Memory Map of a 2-Dimensional Array Pointers and 2-Dimensional Arrays Pointer to an Array Passing 2-D array to a Function Array of Pointers Three Dimensional Array Summary

241
242 244 248 252 253 255 258 260 260 261 263 264

269
270 272 275 275 276 277 279 286 287 289 290 291 292 295 297 300 302 304

xi

Exercise 9. Puppetting On Strings What are Strings More about Strings Pointers and Strings Standard Library String Functions strlen( ) strcpy( ) strcat( ) strcmp( ) Two-Dimensional Array of Characters Array of Pointers to Strings Limitation of Array of Pointers to Strings Solution Summary Exercise 10. Structures Why Use Structures Declaring a Structure Accessing Structure Elements How Structure Elements are Stored Array of Structures Additional Features of Structures Uses of Structures Summary Exercise 11. Console Input/Output Types of I/O Console I/O Functions Formatted Console I/O Functions

304

327
328 329 334 335 337 339 342 343 344 347 351 352 353 354

363
364 367 370 370 371 374 383 384 384

393
394 395 396

xii

sprintf( ) and sscanf( ) Functions Unformatted Console I/O Functions Summary Exercise 12. File Input/Output Data Organization File Operations Opening a File Reading from a File Trouble in Opening a File Closing the File Counting Characters, Tabs, Spaces, … A File-copy Program Writing to a File File Opening Modes String (line) I/O in Files The Awkward Newline Record I/O in Files Text Files and Binary Files Record I/O Revisited Database Management Low Level Disk I/O A Low Level File-copy Program I/O Under Windows Summary Exercise 13. More Issues In Input/Output Using argc and argv Detecting Errors in Reading/Writing Standard I/O Devices I/O Redirection Redirecting the Output xiii

404 405 409 409

415
416 417 418 420 421 422 422 424 425 426 427 430 430 434 437 441 447 448 453 453 454 465 466 470 472 473 474

Redirecting the Input Both Ways at Once Summary Exercise 14. Operations On Bits Bitwise Operators One’s Complement Operator Right Shift Operator Left Shift Operator Bitwise AND Operator Bitwise OR Operator Bitwise XOR Operator The showbits( ) Function Summary Exercise 15. Miscellaneous Features Enumerated Data Type Uses of Enumerated Data Type Renaming Data Types with typedef Typecasting Bit Fields Pointers to Functions Functions Returning Pointers Functions with Variable Number of Arguments Unions Union of Structures Summary Exercise

476 477 478 478

481
482 484 486 488 493 498 499 500 501 501 505 506 507 510 511 513 515 518 520 524 530 531 531

xiv

16. C Under Windows Which Windows… Integers The Use of typedef Pointers in the 32-bit World Memory Management Device Access DOS Programming Model Windows Programming Model Event Driven Model Windows Programming, a Closer Look The First Windows Program Hungarian Notation Summary Exercise 17. Windows Programming The Role of a Message Box Here Comes the window… More Windows A Real-World Window Creation and Displaying of Window Interaction with Window Reacting to Messages Program Instances Summary Exercise 18. Graphics Under Windows Graphics as of Now Device Independent Drawing

535 536 537 537 539 540 543 543 547 551 552 554 558 558 559

561
562 563 566 567 569 570 572 575 575 576 579 580 580

xv

Hello Windows Drawing Shapes Types of Pens Types of Brushes Code and Resources Freehand Drawing, the Paintbrush Style Capturing the Mouse Device Context, a Closer Look Displaying a Bitmap Animation at Work WM_CREATE and OnCreate( ) WM_TIMER and OnTimer( ) A Few More Points… Windows, the Endless World… Summary Exercise 19. Interaction With Hardware Hardware Interaction Hardware Interaction, DOS Perspective Hardware Interaction, Windows Perspective Communication with Storage Devices The ReadSector( ) Function Accessing Other Storage Devices Communication with Keyboard Dynamic Linking Windows Hooks Caps Locked, Permanently Did You Press It TTwwiiccee…… Mangling Keys KeyLogger Where is This Leading Summary Exercise

582 586 590 592 596 596 600 601 603 607 611 611 612 613 614 615

617
618 619 623 626 631 633 634 635 635 637 643 644 645 646 647 647

xvi

20. C Under Linux What is Linux C Programming Under Linux The ‘Hello Linux’ Program Processes Parent and Child Processes More Processes Zombies and Orphans One Interesting Fact Summary Exercise 21. More Linux Programming Communication using Signals Handling Multiple Signals Registering a Common Handler Blocking Signals Event Driven Programming Where Do You Go From Here Summary Exercise Appendix A – Precedence Table Appendix B – Standard Library Functions Appendix C – Chasing the Bugs Appendix D – Hexadecimal Numbering Appendix E – ASCII Chart Appendix F – Helper.h File Appendix G – Boot Parameters Appendix H – Linux Installation Index

649 650 651 652 653 655 659 660 663 664 664 667 668 671 673 675 678 684 684 685 687 691 701 713 719 725 729 735 739

xvii

1

Getting Started
• What is C • Getting Started with C The C Character Set Constants, Variables and Keywords Types of C Constants Rules for Constructing Integer Constants Rules for Constructing Real Constants Rules for Constructing Character Constants Types of C Variables Rules for Constructing Variable Names C Keywords • The First C Program • Compilation and Execution • Receiving Input • C Instructions Type Declaration Instruction Arithmetic Instruction Integer and Float Conversions Hierarchy of Operations Associativity Of Operators • Control Instruction in C • Summary • Exercise

1

2

Let Us C

efore we can begin to write serious programs in C, it would be interesting to find out what really is C, how it came into existence and how does it compare with other computer languages. In this chapter we would briefly outline these issues. Four important aspects of any language are the way it stores data, the way it operates upon this data, how it accomplishes input and output and how it lets you control the sequence of execution of instructions in a program. We would discuss the first three of these building blocks in this chapter.

B

What is C
C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. In the late seventies C began to replace the more familiar languages of that time like PL/I, ALGOL, etc. No one pushed C. It wasn’t made the ‘official’ Bell Labs language. Thus, without any advertisement C’s reputation spread and its pool of users grew. Ritchie seems to have been rather surprised that so many programmers preferred C to older languages like FORTRAN or PL/I, or the newer ones like Pascal and APL. But, that's what happened. Possibly why C seems so popular is because it is reliable, simple and easy to use. Moreover, in an industry where newer languages, tools and technologies emerge and vanish day in and day out, a language that has survived for more than 3 decades has to be really good. An opinion that is often heard today is – “C has been already superceded by languages like C++, C# and Java, so why bother to

Chapter 1: Getting Started

3

learn C today”. I seriously beg to differ with this opinion. There are several reasons for this: (a) I believe that nobody can learn C++ or Java directly. This is because while learning these languages you have things like classes, objects, inheritance, polymorphism, templates, exception handling, references, etc. do deal with apart from knowing the actual language elements. Learning these complicated concepts when you are not even comfortable with the basic language elements is like putting the cart before the horse. Hence one should first learn all the language elements very thoroughly using C language before migrating to C++, C# or Java. Though this two step learning process may take more time, but at the end of it you will definitely find it worth the trouble. (b) C++, C# or Java make use of a principle called Object Oriented Programming (OOP) to organize the program. This organizing principle has lots of advantages to offer. But even while using this organizing principle you would still need a good hold over the language elements of C and the basic programming skills. (c) Though many C++ and Java based programming tools and frameworks have evolved over the years the importance of C is still unchallenged because knowingly or unknowingly while using these frameworks and tools you would be still required to use the core C language elements—another good reason why one should learn C before C++, C# or Java. (d) Major parts of popular operating systems like Windows, UNIX, Linux is still written in C. This is because even today when it comes to performance (speed of execution) nothing beats C. Moreover, if one is to extend the operating system to work with new devices one needs to write device driver programs. These programs are exclusively written in C.

4

Let Us C

(e) Mobile devices like cellular phones and palmtops are becoming increasingly popular. Also, common consumer devices like microwave oven, washing machines and digital cameras are getting smarter by the day. This smartness comes from a microprocessor, an operating system and a program embedded in this devices. These programs not only have to run fast but also have to work in limited amount of memory. No wonder that such programs are written in C. With these constraints on time and space, C is the language of choice while building such operating systems and programs. (f) You must have seen several professional 3D computer games where the user navigates some object, like say a spaceship and fires bullets at the invaders. The essence of all such games is speed. Needless to say, such games won't become popular if they takes a long time to move the spaceship or to fire a bullet. To match the expectations of the player the game has to react fast to the user inputs. This is where C language scores over other languages. Many popular gaming frameworks have been built using C language. (g) At times one is required to very closely interact with the hardware devices. Since C provides several language elements that make this interaction feasible without compromising the performance it is the preferred choice of the programmer. I hope that these are very convincing reasons why one should adopt C as the first and the very important step in your quest for learning programming languages.

Getting Started with C
Communicating with a computer involves speaking the language the computer understands, which immediately rules out English as the language of communication with computer. However, there is

Chapter 1: Getting Started

5

a close analogy between learning English language and learning C language. The classical method of learning English is to first learn the alphabets used in the language, then learn to combine these alphabets to form words, which in turn are combined to form sentences and sentences are combined to form paragraphs. Learning C is similar and easier. Instead of straight-away learning how to write programs, we must first know what alphabets, numbers and special symbols are used in C, then how using them constants, variables and keywords are constructed, and finally how are these combined to form an instruction. A group of instructions would be combined later on to form a program. This is illustrated in the Figure 1.1.
Steps in learning English language: Alphabets Words Sentences Paragraphs

Steps in learning C: Alphabets Digits Special symbols Constants Variables Keywords

Instructions

Program

Figure 1.1

The C Character Set
A character denotes any alphabet, digit or special symbol used to represent information. Figure 1.2 shows the valid alphabets, numbers and special symbols allowed in C.

6

Let Us C

Alphabets

A, B, ….., Y, Z a, b, ……, y, z

Digits Special symbols

0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ~‘!@# %^&*()_-+=|\{} []:; "',.?/

Figure 1.2

Constants, Variables and Keywords
The alphabets, numbers and special symbols when properly combined form constants, variables and keywords. Let us see what are ‘constants’ and ‘variables’ in C. A constant is an entity that doesn’t change whereas a variable is an entity that may change. In any program we typically do lots of calculations. The results of these calculations are stored in computers memory. Like human memory the computer memory also consists of millions of cells. The calculated values are stored in these memory cells. To make the retrieval and usage of these values easy these memory cells (also called memory locations) are given names. Since the value stored in each location may change the names given to these locations are called variable names. Consider the following example. Here 3 is stored in a memory location and a name x is given to it. Then we are assigning a new value 5 to the same memory location x. This would overwrite the earlier value 3, since a memory location can hold only one value at a time. This is shown in Figure 1.3.

Chapter 1: Getting Started

7

x

3 x=3

x

5 x=5

Figure 1.3 Since the location whose name is x can hold different values at different times x is known as a variable. As against this, 3 or 5 do not change, hence are known as constants.

Types of C Constants
C constants can be divided into two major categories: (a) Primary Constants (b) Secondary Constants These constants are further categorized as shown in Figure 1.4.

8

Let Us C

C Constants

Primary Constants Integer Constant Real Constant Character Constant

Secondary Constants Array Pointer Structure Union Enum, etc.

Figure 1.4 At this stage we would restrict our discussion to only Primary Constants, namely, Integer, Real and Character constants. Let us see the details of each of these constants. For constructing these different types of constants certain rules have been laid down. These rules are as under:

Rules for Constructing Integer Constants
An integer constant must have at least one digit. It must not have a decimal point. It can be either positive or negative. If no sign precedes an integer constant it is assumed to be positive. (e) No commas or blanks are allowed within an integer constant. (f) The allowable range for integer constants is -32768 to 32767.
(a) (b) (c) (d)

Truly speaking the range of an Integer constant depends upon the compiler. For a 16-bit compiler like Turbo C or Turbo C++ the

Chapter 1: Getting Started

9

range is –32768 to 32767. For a 32-bit compiler the range would be even greater. Question like what exactly do you mean by a 16bit or a 32-bit compiler, what range of an Integer constant has to do with the type of compiler and such questions are discussed in detail in Chapter 16. Till that time it would be assumed that we are working with a 16-bit compiler.
Ex.: 426 +782 -8000 -7605

Rules for Constructing Real Constants
Real constants are often called Floating Point constants. The real constants could be written in two forms—Fractional form and Exponential form. Following rules must be observed while constructing real constants expressed in fractional form:
(a) (b) (c) (d) (e)

A real constant must have at least one digit. It must have a decimal point. It could be either positive or negative. Default sign is positive. No commas or blanks are allowed within a real constant.
Ex.: +325.34 426.0 -32.76 -48.5792

The exponential form of representation of real constants is usually used if the value of the constant is either too small or too large. It however doesn’t restrict us in any way from using exponential form of representation for other real constants.

10

Let Us C

In exponential form of representation, the real constant is represented in two parts. The part appearing before ‘e’ is called mantissa, whereas the part following ‘e’ is called exponent. Following rules must be observed while constructing real constants expressed in exponential form:
(a) The mantissa part and the exponential part should be

separated by a letter e.
(b) The mantissa part may have a positive or negative sign. (c) Default sign of mantissa part is positive. (d) The exponent must have at least one digit, which must be a

positive or negative integer. Default sign is positive. (e) Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
Ex.: +3.2e-5 4.1e8 -0.2e+3 -3.2e-5

Rules for Constructing Character Constants
(a) A character constant is a single alphabet, a single digit or a

single special symbol enclosed within single inverted commas. Both the inverted commas should point to the left. For example, ’A’ is a valid character constant whereas ‘A’ is not. (b) The maximum length of a character constant can be 1 character.
Ex.: 'A' 'I' '5' '='

Chapter 1: Getting Started

11

Types of C Variables
As we saw earlier, an entity that may vary during program execution is called a variable. Variable names are names given to locations in memory. These locations can contain integer, real or character constants. In any language, the types of variables that it can support depend on the types of constants that it can handle. This is because a particular type of variable can hold only the same type of constant. For example, an integer variable can hold only an integer constant, a real variable can hold only a real constant and a character variable can hold only a character constant. The rules for constructing different types of constants are different. However, for constructing variable names of all types the same set of rules apply. These rules are given below.

Rules for Constructing Variable Names
(a) A variable name is any combination of 1 to 31 alphabets,

digits or underscores. Some compilers allow variable names whose length could be up to 247 characters. Still, it would be safer to stick to the rule of 31 characters. Do not create unnecessarily long variable names as it adds to your typing effort. (b) The first character in the variable name must be an alphabet or underscore. (c) No commas or blanks are allowed within a variable name. (d) No special symbol other than an underscore (as in gross_sal) can be used in a variable name.
Ex.: si_int m_hra pop_e_89

These rules remain same for all the types of primary and secondary variables. Naturally, the question follows... how is C able to differentiate between these variables? This is a rather simple

12

Let Us C

matter. C compiler is able to distinguish between the variable names by making it compulsory for you to declare the type of any variable name that you wish to use in a program. This type declaration is done at the beginning of the program. Following are the examples of type declaration statements:
Ex.: int si, m_hra ; float bassal ; char code ;

Since, the maximum allowable length of a variable name is 31 characters, an enormous number of variable names can be constructed using the above-mentioned rules. It is a good practice to exploit this enormous choice in naming variables by using meaningful variable names. Thus, if we want to calculate simple interest, it is always advisable to construct meaningful variable names like prin, roi, noy to represent Principle, Rate of interest and Number of years rather than using the variables a, b, c.

C Keywords
Keywords are the words whose meaning has already been explained to the C compiler (or in a broad sense to the computer). The keywords cannot be used as variable names because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the computer. Some C compilers allow you to construct variable names that exactly resemble the keywords. However, it would be safer not to mix up the variable names and the keywords. The keywords are also called ‘Reserved words’. There are only 32 keywords available in C. Figure 1.5 gives a list of these keywords for your ready reference. A detailed discussion of each of these keywords would be taken up in later chapters wherever their use is relevant.

Chapter 1: Getting Started

13

auto break case char const continue default do

double else enum extern float for goto if

int long register return short signed sizeof static

struct switch typedef union unsigned void volatile while

Figure 1.5 Note that compiler vendors (like Microsoft, Borland, etc.) provide their own keywords apart from the ones mentioned above. These include extended keywords like near, far, asm, etc. Though it has been suggested by the ANSI committee that every such compiler specific keyword should be preceded by two underscores (as in __asm ), not every vendor follows this rule.

The First C Program
Armed with the knowledge about the types of variables, constants & keywords the next logical step is to combine them to form instructions. However, instead of this, we would write our first C program now. Once we have done that we would see in detail the instructions that it made use of. Before we begin with our first C program do remember the following rules that are applicable to all C programs:
(a) Each instruction in a C program is written as a separate

statement. Therefore a complete C program would comprise of a series of statements.

14

Let Us C

(b) The statements in a program must appear in the same order in

which we wish them to be executed; unless of course the logic of the problem demands a deliberate ‘jump’ or transfer of control to a statement, which is out of sequence.
(c) Blank spaces may be inserted between two words to improve

the readability of the statement. However, no blank spaces are allowed within a variable, constant or keyword.
(d) All statements are entered in small case letters. (e) C has no specific rules for the position at which a statement is

to be written. That’s why it is often called a free-form language.
(f)

Every C statement must end with a ;. Thus ; acts as a statement terminator.

Let us now write down our first C program. It would simply calculate simple interest for a set of values representing principle, number of years and rate of interest.
/* Calculation of simple interest */ /* Author gekay Date: 25/05/2004 */ main( ) { int p, n ; float r, si ; p = 1000 ; n=3; r = 8.5 ; /* formula for simple interest */ si = p * n * r / 100 ; printf ( "%f" , si ) ;

Chapter 1: Getting Started
}

15

Now a few useful tips about the program... − Comment about the program should be enclosed within /* */. For example, the first two statements in our program are comments. Though comments are not necessary, it is a good practice to begin a program with a comment indicating the purpose of the program, its author and the date on which the program was written. Any number of comments can be written at any place in the program. For example, a comment can be written before the statement, after the statement or within the statement as shown below:
/* formula */ si = p * n * r / 100 ; si = p * n * r / 100 ; /* formula */ si = p * n * r / /* formula */ 100 ;







Sometimes it is not so obvious as to what a particular statement in a program accomplishes. At such times it is worthwhile mentioning the purpose of the statement (or a set of statements) using a comment. For example:
/* formula for simple interest */ si = p * n * r / 100 ;



Often programmers seem to ignore writing of comments. But when a team is building big software well commented code is almost essential for other team members to understand it.

16


Let Us C
Although a lot of comments are probably not necessary in this program, it is usually the case that programmers tend to use too few comments rather than too many. An adequate number of comments can save hours of misery and suffering when you later try to figure out what the program does. The normal language rules do not apply to text written within /* .. */. Thus we can type this text in small case, capital or a combination. This is because the comments are solely given for the understanding of the programmer or the fellow programmers and are completely ignored by the compiler. Comments cannot be nested. For example,
/* Cal of SI /* Author sam date 01/01/2002 */ */





is invalid. − A comment can be split over more than one line, as in,
/* This is a jazzy comment */

Such a comment is often called a multi-line comment. − main( ) is a collective name given to a set of statements. This name has to be main( ), it cannot be anything else. All statements that belong to main( ) are enclosed within a pair of braces { } as shown below. main( ) { statement 1 ; statement 2 ;

Chapter 1: Getting Started statement 3 ; }

17



Technically speaking main( ) is a function. Every function has a pair of parentheses ( ) associated with it. We would discuss functions and their working in great detail in Chapter 5. Any variable used in the program must be declared before using it. For example, int p, n ; float r, si ;





Any C statement always ends with a ; For example, float r, si ; r = 8.5 ;



In the statement, si = p * n * r / 100 ;

* and / are the arithmetic operators. The arithmetic operators available in C are +, -, * and /. C is very rich in operators. There are about 45 operators available in C. Surprisingly there is no operator for exponentiation... a slip, which can be forgiven considering the fact that C has been developed by an individual, not by a committee. − Once the value of si is calculated it needs to be displayed on the screen. Unlike other languages, C does not contain any instruction to display output on the screen. All output to screen is achieved using readymade library functions. One such

18

Let Us C function is printf( ). We have used it display on the screen the value contained in si. The general form of printf( ) function is, printf ( "", ) ; can contain, %f for printing real values %d for printing integer values %c for printing character values

In addition to format specifiers like %f, %d and %c the format string may also contain any other characters. These characters are printed as they are when the printf( ) is executed. Following are some examples of usage of printf( ) function: printf ( "%f", si ) ; printf ( "%d %d %f %f", p, n, r, si ) ; printf ( "Simple interest = Rs. %f", si ) ; printf ( "Prin = %d \nRate = %f", p, r ) ;

The output of the last statement would look like this...
Prin = 1000 Rate = 8.5

What is ‘\n’ doing in this statement? It is called newline and it takes the cursor to the next line. Therefore, you get the output split over two lines. ‘\n’ is one of the several Escape Sequences available in C. These are discussed in detail in Chapter 11. Right now, all that we can say is ‘\n’ comes in

Chapter 1: Getting Started

19

handy when we want to format the output properly on separate lines. printf( ) can not only print values of variables, it can also print the result of an expression. An expression is nothing but a valid combination of constants, variables and operators. Thus, 3, 3 + 2, c and a + b * c – d all are valid expressions. The results of these expressions can be printed as shown below: printf ( "%d %d %d %d", 3, 3 + 2, c, a + b * c – d ) ;

Note that 3 and c also represent valid expressions.

Compilation and Execution
Once you have written the program you need to type it and instruct the machine to execute it. To type your C program you need another program called Editor. Once the program has been typed it needs to be converted to machine language (0s and 1s) before the machine can execute it. To carry out this conversion we need another program called Compiler. Compiler vendors provide an Integrated Development Environment (IDE) which consists of an Editor as well as the Compiler. There are several such IDEs available in the market targeted towards different operating systems. For example, Turbo C, Turbo C++ and Microsoft C are some of the popular compilers that work under MS-DOS; Visual C++ and Borland C++ are the compilers that work under Windows, whereas gcc compiler works under Linux. Note that Turbo C++, Microsoft C++ and Borland C++ software also contain a C compiler bundled with them. If you are a beginner you would be better off using a simple compiler like Turbo C or Turbo C++. Once you have mastered the language elements you can then switch over to more sophisticated compilers like Visual C++ under Windows or gcc under Linux. Most of the

20

Let Us C

programs in this book would work with all the compilers. Wherever there is a deviation I would point it out that time. Assuming that you are using a Turbo C or Turbo C++ compiler here are the steps that you need to follow to compile and execute your first C program… (a) Start the compiler at C> prompt. The compiler (TC.EXE is usually present in C:\TC\BIN directory). (b) Select New from the File menu. (c) Type the program. (d) Save the program using F2 under a proper name (say Program1.c). (e) Use Ctrl + F9 to compile and execute the program. (f) Use Alt + F5 to view the output. Note that on compiling the program its machine language equivalent is stored as an EXE file (Program1.EXE) on the disk. This file is called an executable file. If we copy this file to another machine we can execute it there without being required to recompile it. In fact the other machine need not even have a compiler to be able to execute the file. A word of caution! If you run this program in Turbo C++ compiler, you may get an error — “The function printf should have a prototype”. To get rid of this error, perform the following steps and then recompile the program. (a) Select ‘Options’ menu and then select ‘Compiler | C++ Options’. In the dialog box that pops up, select ‘CPP always’ in the ‘Use C++ Compiler’ options. (b) Again select ‘Options’ menu and then select ‘Environment | Editor’. Make sure that the default extension is ‘C’ rather than ‘CPP’.

Chapter 1: Getting Started

21

Receiving Input
In the program discussed above we assumed the values of p, n and r to be 1000, 3 and 8.5. Every time we run the program we would get the same value for simple interest. If we want to calculate simple interest for some other set of values then we are required to make the relevant change in the program, and again compile and execute it. Thus the program is not general enough to calculate simple interest for any set of values without being required to make a change in the program. Moreover, if you distribute the EXE file of this program to somebody he would not even be able to make changes in the program. Hence it is a good practice to create a program that is general enough to work for any set of values. To make the program general the program itself should ask the user to supply the values of p, n and r through the keyboard during execution. This can be achieved using a function called scanf( ). This function is a counter-part of the printf( ) function. printf( ) outputs the values to the screen whereas scanf( ) receives them from the keyboard. This is illustrated in the program shown below.
/* Calculation of simple interest */ /* Author gekay Date 25/05/2004 */ main( ) { int p, n ; float r, si ; printf ( "Enter values of p, n, r" ) ; scanf ( "%d %d %f", &p, &n, &r ) ; si = p * n * r / 100 ; printf ( "%f" , si ) ; }

22

Let Us C

The first printf( ) outputs the message ‘Enter values of p, n, r’ on the screen. Here we have not used any expression in printf( ) which means that using expressions in printf( ) is optional. Note that the ampersand (&) before the variables in the scanf( ) function is a must. & is an ‘Address of’ operator. It gives the location number used by the variable in memory. When we say &a, we are telling scanf( ) at which memory location should it store the value supplied by the user from the keyboard. The detailed working of the & operator would be taken up in Chapter 5. Note that a blank, a tab or a new line must separate the values supplied to scanf( ). Note that a blank is creating using a spacebar, tab using the Tab key and new line using the Enter key. This is shown below: Ex.: The three values separated by blank
1000 5 15.5

Ex.: The three values separated by tab.
1000 5 1000 5 15.5 15.5

Ex.: The three values separated by newline.

So much for the tips. How about another program to give you a feel of things...
/* Just for fun. Author: Bozo */ main( ) { int num ; printf ( "Enter a number" ) ;

Chapter 1: Getting Started scanf ( "%d", &num ) ; printf ( "Now I am letting you on a secret..." ) ; printf ( "You have just entered the number %d", num ) ; }

23

C Instructions
Now that we have written a few programs let us look at the instructions that we used in these programs. There are basically three types of instructions in C:
(a) Type Declaration Instruction (b) Arithmetic Instruction (c) Control Instruction

The purpose of each of these instructions is given below:
(a) Type declaration instruction



To declare the type of variables used in a C program. To perform arithmetic operations between constants and variables. To control the sequence of execution of various statements in a C program.

(b) Arithmetic instruction



(c) Control instruction



Since, the elementary C programs would usually contain only the type declaration and the arithmetic instructions; we would discuss only these two instructions at this stage. The other types of instructions would be discussed in detail in the subsequent chapters.

24
Type Declaration Instruction

Let Us C

This instruction is used to declare the type of variables being used in the program. Any variable used in the program must be declared before using it in any statement. The type declaration statement is written at the beginning of main( ) function.
Ex.: int bas ; float rs, grosssal ; char name, code ;

There are several subtle variations of the type declaration instruction. These are discussed below:
(a) While declaring the type of variable we can also initialize it as

shown below. int i = 10, j = 25 ; float a = 1.5, b = 1.99 + 2.4 * 1.44 ;
(b) The order in which we define the variables is sometimes

important sometimes not. For example, int i = 10, j = 25 ;

is same as int j = 25, j = 10 ;

However, float a = 1.5, b = a + 3.1 ;

is alright, but float b = a + 3.1, a = 1.5 ;

Chapter 1: Getting Started

25

is not. This is because here we are trying to use a even before defining it.
(c) The following statements would work

int a, b, c, d ; a = b = c = 10 ;

However, the following statement would not work int a = b = c = d = 10 ;

Once again we are trying to use b (to assign to a) before defining it.

Arithmetic Instruction
A C arithmetic instruction consists of a variable name on the left hand side of = and variable names & constants on the right hand side of =. The variables and constants appearing on the right hand side of = are connected by arithmetic operators like +, -, *, and /.
Ex.: int ad ; float kot, deta, alpha, beta, gamma ; ad = 3200 ; kot = 0.0056 ; deta = alpha * beta / gamma + 3.2 * 2 / 5 ;

Here, *, /, -, + are the arithmetic operators. = is the assignment operator. 2, 5 and 3200 are integer constants. 3.2 and 0.0056 are real constants. ad is an integer variable. kot, deta, alpha, beta, gamma are real variables.

26

Let Us C

The variables and constants together are called ‘operands’ that are operated upon by the ‘arithmetic operators’ and the result is assigned, using the assignment operator, to the variable on lefthand side. A C arithmetic statement could be of three types. These are as follows:
(a) Integer mode arithmetic statement - This is an arithmetic

statement in which all operands are either integer variables or integer constants.
Ex.: int i, king, issac, noteit ; i=i+1; king = issac * 234 + noteit - 7689 ;
(b) Real mode arithmetic statement - This is an arithmetic

statement in which all operands are either real constants or real variables.
Ex.: float qbee, antink, si, prin, anoy, roi ; qbee = antink + 23.123 / 4.5 * 0.3442 ; si = prin * anoy * roi / 100.0 ;
(c) Mixed mode arithmetic statement - This is an arithmetic

statement in which some of the operands are integers and some of the operands are real.
Ex.: float si, prin, anoy, roi, avg ; int a, b, c, num ; si = prin * anoy * roi / 100.0 ; avg = ( a + b + c + num ) / 4 ;

It is very important to understand how the execution of an arithmetic statement takes place. Firstly, the right hand side is evaluated using constants and the numerical values stored in the variable names. This value is then assigned to the variable on the left-hand side.

Chapter 1: Getting Started

27

Though Arithmetic instructions look simple to use one often commits mistakes in writing them. Let us take a closer look at these statements. Note the following points carefully. (a) C allows only one variable on left-hand side of =. That is, z = k * l is legal, whereas k * l = z is illegal. (b) In addition to the division operator C also provides a modular division operator. This operator returns the remainder on dividing one integer with another. Thus the expression 10 / 2 yields 5, whereas, 10 % 2 yields 0. Note that the modulus operator (%) cannot be applied on a float. Also note that on using % the sign of the remainder is always same as the sign of the numerator. Thus –5 % 2 yields –1, whereas, 5 % -2 yields 1. (c) An arithmetic instruction is often used for storing character constants in character variables. char a, b, d ; a = 'F' ; b = 'G' ; d = '+' ;

When we do this the ASCII values of the characters are stored in the variables. ASCII values are used to represent any character in memory. The ASCII values of ‘F’ and ‘G’ are 70 and 71 (refer the ASCII Table in Appendix E). (d) Arithmetic operations can be performed on ints, floats and chars. Thus the statements, char x, y ; int z ; x = 'a' ; y = 'b' ; z=x+y;

28

Let Us C

are perfectly valid, since the addition is performed on the ASCII values of the characters and not on characters themselves. The ASCII values of ‘a’ and ‘b’ are 97 and 98, and hence can definitely be added. (e) No operator is assumed to be present. It must be written explicitly. In the following example, the multiplication operator after b must be explicitly written. a = c.d.b(xy) b=c*d*b*(x*y) usual arithmetic statement C statement

(f) Unlike other high level languages, there is no operator for performing exponentiation operation. Thus following statements are invalid. a = 3 ** 2 ; b=3^2;

If we want to do the exponentiation we can get it done this way:
#include main( ) { int a ; a = pow ( 3, 2 ) ; printf ( “%d”, a ) ; }

Here pow( ) function is a standard library function. It is being used to raise 3 to the power of 2. #include is a preprocessor directive. It is being used here to ensure that the pow( ) function works correctly. We would learn more about standard library functions in Chapter 5 and about preprocessor in Chapter 7.

Chapter 1: Getting Started

29

Integer and Float Conversions
In order to effectively develop C programs, it will be necessary to understand the rules that are used for the implicit conversion of floating point and integer values in C. These are mentioned below. Note them carefully.
(a) An arithmetic operation between an integer and integer

always yields an integer result.
(b) An operation between a real and real always yields a real

result.
(c) An operation between an integer and real always yields a real

result. In this operation the integer is first promoted to a real and then the operation is performed. Hence the result is real. I think a few practical examples shown in the following figure would put the issue beyond doubt.

Operation 5/2 5.0 / 2 5 / 2.0 5.0 / 2.0

Result 2 2.5 2.5 2.5

Operation 2/5 2.0 / 5 2 / 5.0 2.0 / 5.0

Result 0 0.4 0.4 0.4

Figure 1.6

Type Conversion in Assignments
It may so happen that the type of the expression and the type of the variable on the left-hand side of the assignment operator may not be same. In such a case the value of the expression is promoted or

30

Let Us C

demoted depending on the type of the variable on left-hand side of =. For example, consider the following assignment statements. int i ; float b ; i = 3.5 ; b = 30 ;

Here in the first assignment statement though the expression’s value is a float (3.5) it cannot be stored in i since it is an int. In such a case the float is demoted to an int and then its value is stored. Hence what gets stored in i is 3. Exactly opposite happens in the next statement. Here, 30 is promoted to 30.000000 and then stored in b, since b being a float variable cannot hold anything except a float value. Instead of a simple expression used in the above examples if a complex expression occurs, still the same rules apply. For example, consider the following program fragment. float a, b, c ; int s ; s = a * b * c / 100 + 32 / 4 - 3 * 1.1 ;

Here, in the assignment statement some operands are ints whereas others are floats. As we know, during evaluation of the expression the ints would be promoted to floats and the result of the expression would be a float. But when this float value is assigned to s it is again demoted to an int and then stored in s. Observe the results of the arithmetic statements shown in Figure 1.7. It has been assumed that k is an integer variable and a is a real variable.

Chapter 1: Getting Started

31

Arithmetic Instruction Result Arithmetic Instruction Result k=2/9 k = 2.0 / 9 k = 2 / 9.0 k = 2.0 / 9.0 k=9/2 k = 9.0 / 2 k = 9 / 2.0 k = 9.0 / 2.0 0 0 0 0 4 4 4 4 a=2/9 a = 2.0 / 9 a = 2 / 9.0 a = 2.0 / 9.0 a=9/2 a = 9.0 / 2 a = 9 / 2.0 a = 9.0 / 2.0 0.0 0.2222 0.2222 0.2222 4.0 4.5 4.5 4.5

Figure 1.7 Note that though the following statements give the same result, 0, the results are obtained differently. k=2/9; k = 2.0 / 9 ;

In the first statement, since both 2 and 9 are integers, the result is an integer, i.e. 0. This 0 is then assigned to k. In the second statement 9 is promoted to 9.0 and then the division is performed. Division yields 0.222222. However, this cannot be stored in k, k being an int. Hence it gets demoted to 0 and then stored in k.

Hierarchy of Operations
While executing an arithmetic statement, which has two or more operators, we may have some problems as to how exactly does it get executed. For example, does the expression 2 * x - 3 * y correspond to (2x)-(3y) or to 2(x-3y)? Similarly, does A / B * C correspond to A / (B * C) or to (A / B) * C? To answer these questions satisfactorily one has to understand the ‘hierarchy’ of operations. The priority or precedence in which the operations in

32

Let Us C

an arithmetic statement are performed is called the hierarchy of operations. The hierarchy of commonly used operators is shown in Figure 1.8.

Priority 1 2nd 3rd st Operators */% +=

Description multiplication, division, modular division addition, subtraction assignment

Figure 1.8 Now a few tips about usage of operators in general.
(a) Within parentheses the same hierarchy as mentioned in Figure

1.11 is operative. Also, if there are more than one set of parentheses, the operations within the innermost parentheses would be performed first, followed by the operations within the second innermost pair and so on.
(b) We must always remember to use pairs of parentheses. A

careless imbalance of the right and left parentheses is a common error. Best way to avoid this error is to type ( ) and then type an expression inside it. A few examples would clarify the issue further. Example 1.1: Determine the hierarchy of operations and evaluate the following expression: i=2*3/4+4/4+8-2+5/8 Stepwise evaluation of this expression is shown below: i=2*3/4+4/4+8-2+5/8 Chapter 1: Getting Started i=6/4+4/4+8-2+5/8 i=1+4/4+8-2+5/8 i = 1 + 1+ 8 - 2 + 5 / 8 i=1+1+8-2+0 i=2+8-2+0 i = 10 - 2 + 0 i=8+0 i=8 operation: * operation: / operation: / operation: / operation: + operation: + operation : operation: +

33

Note that 6 / 4 gives 1 and not 1.5. This so happens because 6 and 4 both are integers and therefore would evaluate to only an integer constant. Similarly 5 / 8 evaluates to zero, since 5 and 8 are integer constants and hence must return an integer value. Example 1.2: Determine the hierarchy of operations and evaluate the following expression: kk = 3 / 2 * 4 + 3 / 8 + 3

Stepwise evaluation of this expression is shown below: kk = 3 / 2 * 4 + 3 / 8 + 3 kk = 1 * 4 + 3 / 8 + 3 kk = 4 + 3 / 8 + 3 kk = 4 + 0 + 3 kk = 4 + 3 kk = 7

operation: / operation: * operation: / operation: + operation: +

Note that 3 / 8 gives zero, again for the same reason mentioned in the previous example. All operators in C are ranked according to their precedence. And mind you there are as many as 45 odd operators in C, and these can affect the evaluation of an expression in subtle and unexpected ways if we aren't careful. Unfortunately, there are no simple rules that one can follow, such as “BODMAS” that tells algebra students in which order does an expression evaluate. We have not

34

Let Us C

encountered many out of these 45 operators, so we won’t pursue the subject of precedence any further here. However, it can be realized at this stage that it would be almost impossible to remember the precedence of all these operators. So a full-fledged list of all operators and their precedence is given in Appendix A. This may sound daunting, but when its contents are absorbed in small bites, it becomes more palatable. So far we have seen how the computer evaluates an arithmetic statement written in C. But our knowledge would be incomplete unless we know how to convert a general arithmetic statement to a C statement. C can handle any complex expression with ease. Some of the examples of C expressions are shown in Figure 1.9.

Algebric Expression

C Expression

axb–cxd (m + n) (a + b) 3x2 + 2x + 5 a+b+c d+e

a*b–c*d (m + n) * (a + b) 3*x*x+2*x+ 5 (a+b+c)/(d+e) 2 * b * y / ( d + 1 ) – x / 3 * ( z + y )

⎡ 2 BY x ⎤ ⎢ d + 1 − 3( z + y ) ⎥ ⎦ ⎣
Figure 1.9

Associativity of Operators
When an expression contains two operators of equal priority the tie between them is settled using the associativity of the operators. Associativity can be of two types—Left to Right or Right to Left. Left to Right associativity means that the left operand must be

Chapter 1: Getting Started

35

unambiguous. Unambiguous in what sense? It must not be involved in evaluation of any other sub-expression. Similarly, in case of Right to Left associativity the right operand must be unambiguous. Let us understand this with an example. Consider the expression a=3/2*5 ; Here there is a tie between operators of same priority, that is between / and *. This tie is settled using the associativity of / and *. But both enjoy Left to Right associativity. Figure 1.10 shows for each operator which operand is unambiguous and which is not.

Operator Left / * 3 3 / 2 or 2

Right

Remark

2 or 2 * Left operand is 5 unambiguous, Right is not 5 Right operand is unambiguous, Left is not

Figure 1.10 Since both / and * have L to R associativity and only / has unambiguous left operand (necessary condition for L to R associativity) it is performed earlier. Consider one more expression a=b=3; Here both assignment operators have the same priority and same associativity (Right to Left). Figure 1.11 shows for each operator which operand is unambiguous and which is not.

36

Let Us C

Operator Left = a

Right

Remark

b or b = Left operand is 3 unambiguous, Right is not 3 Right operand is unambiguous, Left is not

=

b or a = b

Figure 1.11 Since both = have R to L associativity and only the second = has unambiguous right operand (necessary condition for R to L associativity) the second = is performed earlier. Consider yet another expression z=a*b+c/d; Here * and / enjoys same priority and same associativity (Left to Right). Figure 1.12 shows for each operator which operand is unambiguous and which is not.

Operator Left * / Figure 1.12 a c

Right Remark b d Both operands are unambiguous Both operands are unambiguous

Here since left operands for both operators are unambiguous Compiler is free to perform * or / operation as per its convenience

Chapter 1: Getting Started

37

since no matter which is performed earlier the result would be same. Appendix A gives the associativity of all the operators available in C.

Control Instructions in C
As the name suggests the ‘Control Instructions’ enable us to specify the order in which the various instructions in a program are to be executed by the computer. In other words the control instructions determine the ‘flow of control’ in a program. There are four types of control instructions in C. They are: (a) (b) (c) (d) Sequence Control Instruction Selection or Decision Control Instruction Repetition or Loop Control Instruction Case Control Instruction

The Sequence control instruction ensures that the instructions are executed in the same order in which they appear in the program. Decision and Case control instructions allow the computer to take a decision as to which instruction is to be executed next. The Loop control instruction helps computer to execute a group of statements repeatedly. In the following chapters we are going to learn these instructions in detail. Try your hand at the Exercise presented on the following pages before proceeding to the next chapter, which discusses the decision control instruction.

Summary
(a) The three primary constants and variable types in C are integer, float and character. (b) A variable name can be of maximum 31 characters. (c) Do not use a keyword as a variable name.

38

Let Us C

(d) An expression may contain any sequence of constants, variables and operators. (e) Operators having equal precedence are evaluated using associativity. (f) Left to right associativity means that the left operand of a operator must be unambiguous whereas right to left associativity means that the right operand of a operator must be unambiguous. (g) Input/output in C can be achieved using scanf( ) and printf( ) functions.

Exercise
[A] Which of the following are invalid variable names and why? BASICSALARY #MEAN population in 2006 FLOAT team’svictory _basic group. over time hELLO Plot # 3 basic-hra 422 mindovermatter queue. 2015_DDay

[B] Point out the errors, if any, in the following C statements:

(a) int = 314.562 * 150 ; (b) name = ‘Ajay’ ; (c) varchar = ‘3’ ; (d) 3.14 * r * r * h = vol_of_cyl ; (e) k = ( a * b ) ( c + ( 2.5a + b ) ( d + e ) ; (f) m_inst = rate of interest * amount in rs ;

Chapter 1: Getting Started
(g) si = principal * rateofinterest * numberofyears / 100 ; (h) area = 3.14 * r ** 2 ; (i) volume = 3.14 * r ^ 2 * h ; (j) k = ( (a * b ) + c ) ( 2.5 * a + b ) ; (k) a = b = 3 = 4 ; (l) count = count + 1 ; (m) date = '2 Mar 04' ;

39

[C] Evaluate the following expressions and show their hierarchy.

(a) g = big / 2 + big * 4 / big - big + abc / 3 ; (abc = 2.5, big = 2, assume g to be a float) (b) on = ink * act / 2 + 3 / 2 * act + 2 + tig ; (ink = 4, act = 1, tig = 3.2, assume on to be an int) (c) s = qui * add / 4 - 6 / 2 + 2 / 3 * 6 / god ; (qui = 4, add = 2, god = 2, assume s to be an int) (d) s = 1 / 3 * a / 4 - 6 / 2 + 2 / 3 * 6 / g ; (a = 4, g = 3, assume s to be an int)
[D] Fill the following table for the expressions given below and then evaluate the result. A sample entry has been filled in the table for expression (a).

40

Let Us C

Operator /

Left 10

Right

Remark

5 or 5 / 2 Left operand is /1 unambiguous, Right is not .. ..

..

..

(a) g = 10 / 5 /2 / 1 ; (b) b = 3 / 2 + 5 * 4 / 3 ; (c) a = b = c = 3 + 4 ;

[E] Convert the following equations into corresponding C statements.

(a)

Z= X= R= A=

8.8 ( a + b ) 2 / c - 0.5 + 2 a / ( q + r ) ( a + b ) * (1 / m ) - b + ( b * b ) + 2 4ac 2a 2v + 6.22 ( c + d ) g+v 7.7b ( xy + a ) / c - 0.8 + 2b ( x + a ) (1 / y )

(b)

(c)

(d)

[F] What would be the output of the following programs: (a) main( ) {

Chapter 1: Getting Started int i = 2, j = 3, k, l ; float a, b ; k=i/j*j; l=j/i*i; a=i/j*j; b=j/i*i; printf( "%d %d %f %f", k, l, a, b ) ; } (b) main( ) { int a, b ; a = -3 - - 3 ; b = -3 - - ( - 3 ) ; printf ( "a = %d b = %d", a, b ) ; } (c) main( ) { float a = 5, b = 2 ; int c ; c=a%b; printf ( "%d", c ) ; }

41

(d) main( ) { printf ( "nn \n\n nn\n" ) ; printf ( "nn /n/n nn/n" ) ; } (e) main( ) { int a, b ; printf ( "Enter values of a and b" ) ; scanf ( " %d %d ", &a, &b ) ; printf ( "a = %d b = %d", a, b ) ; }

42
(f) main( ) { int p, q ; printf ( "Enter values of p and q" ) ; scanf ( " %d %d ", p, q ) ; printf ( "p = %d q =%d", p, q ) ; }

Let Us C

[G] Pick up the correct alternative for each of the following questions:

(a) C language has been developed by (1) Ken Thompson (2) Dennis Ritchie (3) Peter Norton (4) Martin Richards (b) C can be used on (1) Only MS-DOS operating system (2) Only Linux operating system (3) Only Windows operating system (4) All the above (c) C programs are converted into machine language with the help of (1) An Editor (2) A compiler (3) An operating system (4) None of the above (d) The real constant in C can be expressed in which of the following forms (1) Fractional form only (2) Exponential form only (3) ASCII form only

Chapter 1: Getting Started
(4) Both fractional and exponential forms (e) A character variable can at a time store (1) 1 character (2) 8 characters (3) 254 characters (4) None of the above (f) The statement char ch = ‘Z’ would store in ch (1) The character Z (2) ASCII value of Z (3) Z along with the single inverted commas (4) Both (1) and (2) (g) Which of the following is NOT a character constant (1) ‘Thank You’ (2) ‘Enter values of P, N, R’ (3) ‘23.56E-03’ (4) All the above (h) The maximum value that an integer constant can have is (1) -32767 (2) 32767 (3) 1.7014e+38 (4) –1.7014e+38 (i) A C variable cannot start with (1) An alphabet (2) A number (3) A special symbol other than underscore (4) Both (2) & (3) above (j) Which of the following statement is wrong (1) mes = 123.56 ; (2) con = 'T' * 'A' ; (3) this = 'T' * 20 ; (4) 3 + a = b ;

43

44

Let Us C

(k) Which of the following shows the correct hierarchy of arithmetic operators in C (1) **, * or /, + or (2) **, *, /, +, (3) **, /, *, +, (4) / or *, - or + (l) In b = 6.6 / a + 2 * n ; which operation will be performed first? (1) 6.6 / a (2) a + 2 (3) 2 * n (4) Depends upon compiler (m) Which of the following is allowed in a C Arithmetic instruction (1) [ ] (2) { } (3) ( ) (4) None of the above (n) Which of the following statements is false (1) Each new C instruction has to be written on a separate line (2) Usually all C statements are entered in small case letters (3) Blank spaces may be inserted between two words in a C statement (4) Blank spaces cannot be inserted within a variable name (o) If a is an integer variable, a = 5 / 2 ; will return a value (1) 2.5 (2) 3 (3) 2 (4) 0 (p) The expression, a = 7 / 22 * ( 3.14 + 2 ) * 3 / 5 ; evaluates to

Chapter 1: Getting Started
(1) (2) (3) (4) 8.28 6.28 3.14 0

45

(q) The expression, a = 30 * 1000 + 2768 ; evaluates to (1) 32768 (2) -32768 (3) 113040 (4) 0 (r) The expression x = 4 + 2 % - 8 evaluates to (1) -6 (2) 6 (3) 4 (4) None of the above (s) Hierarchy decides which operator (1) is most important (2) is used first (3) is fastest (4) operates on largest numbers (t) An integer constant in C must have: (1) At least one digit (2) Atleast one decimal point (3) A comma along with digits (4) Digits separated by commas (u) A character variable can never store more than (1) 32 characters (2) 8 characters (3) 254 characters (4) 1 character (v) In C a variable cannot contain (1) Blank spaces

46
(2) Hyphen (3) Decimal point (4) All the above (w) Which of the following is FALSE in C (1) Keywords can be used as variable names (2) Variable names can contain a digit (3) Variable names do not contain a blank space (4) Capital letters can be used in variable names (x) In C, Arithmetic instruction cannot contain (1) variables (2) constants (3) variable names on right side of = (4) constants on left side of =

Let Us C

(y) Which of the following shows the correct hierarchy of arithmetic operations in C (1) / + * (2) * - / + (3) + - / * (4) * / + (z) What will be the value of d if d is a float after the operation d = 2 / 7.0? (1) 0 (2) 0.2857 (3) Cannot be determined (4) None of the above
[H] Write C programs for the following:

(a) Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.

Chapter 1: Getting Started

47

(b) The distance between two cities (in km.) is input through the keyboard. Write a program to convert and print this distance in meters, feet, inches and centimeters. (c) If the marks obtained by a student in five different subjects are input through the keyboard, find out the aggregate marks and percentage marks obtained by the student. Assume that the maximum marks that can be obtained by a student in each subject is 100. (d) Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to convert this temperature into Centigrade degrees. (e) The length & breadth of a rectangle and radius of a circle are input through the keyboard. Write a program to calculate the area & perimeter of the rectangle, and the area & circumference of the circle. (f) Two numbers are input through the keyboard into two locations C and D. Write a program to interchange the contents of C and D. (g) If a five-digit number is input through the keyboard, write a program to calculate the sum of its digits. (Hint: Use the modulus operator ‘%’) (h) If a five-digit number is input through the keyboard, write a program to reverse the number. (i) If a four-digit number is input through the keyboard, write a program to obtain the sum of the first and last digit of this number. (j) In a town, the percentage of men is 52. The percentage of total literacy is 48. If total percentage of literate men is 35 of the total population, write a program to find the total number

48

Let Us C of illiterate men and women if the population of the town is 80,000.

(k) A cashier has currency notes of denominations 10, 50 and 100. If the amount to be withdrawn is input through the keyboard in hundreds, find the total number of currency notes of each denomination the cashier will have to give to the withdrawer. (l) If the total selling price of 15 items and the total profit earned on them is input through the keyboard, write a program to find the cost price of one item. (m) If a five-digit number is input through the keyboard, write a program to print a new number by adding one to each of its digits. For example if the number that is input is 12391 then the output should be displayed as 23402.

2

The Decision Control Structure
• Decisions! Decisions! • The if Statement The Real Thing Multiple Statements within if • The if-else Statement Nested if-elses Forms of if • Use of Logical Operators The else if Clause The ! Operator Hierarchy of Operators Revisited • A Word of Caution • The Conditional Operators • Summary • Exercise

49

50

Let Us C

e all need to alter our actions in the face of changing circumstances. If the weather is fine, then I will go for a stroll. If the highway is busy I would take a diversion. If the pitch takes spin, we would win the match. If she says no, I would look elsewhere. If you like this book, I would write the next edition. You can notice that all these decisions depend on some condition being met. C language too must be able to perform different sets of actions depending on the circumstances. In fact this is what makes it worth its salt. C has three major decision making instructions—the if statement, the if-else statement, and the switch statement. A fourth, somewhat less important structure is the one that uses conditional operators. In this chapter we will explore all these ways (except switch, which has a separate chapter devoted to it, later) in which a C program can react to changing circumstances.

W

Decisions! Decisions!
In the programs written in Chapter 1 we have used sequence control structure in which the various steps are executed sequentially, i.e. in the same order in which they appear in the program. In fact to execute the instructions sequentially, we don’t have to do anything at all. By default the instructions in a program are executed sequentially. However, in serious programming situations, seldom do we want the instructions to be executed sequentially. Many a times, we want a set of instructions to be executed in one situation, and an entirely different set of instructions to be executed in another situation. This kind of situation is dealt in C programs using a decision control instruction. As mentioned earlier, a decision control instruction can be implemented in C using: (a) The if statement (b) The if-else statement (c) The conditional operators

Chapter 2: The Decision Control Structure
Now let us learn each of these and their variations in turn.

51

The if Statement
Like most languages, C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this: if ( this condition is true ) execute this statement ;

The keyword if tells the compiler that what follows is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is, is true, then the statement is executed. If the condition is not true then the statement is not executed; instead the program skips past it. But how do we express the condition itself in C? And how do we evaluate its truth or falsity? As a general rule, we express a condition using C’s ‘relational’ operators. The relational operators allow us to compare two values to see whether they are equal to each other, unequal, or whether one is greater than the other. Here’s how they look and how they are evaluated in C.

this expression x == y x != y xy x = y

is true if x is equal to y x is not equal to y x is less than y x is greater than y x is less than or equal to y x is greater than or equal to y

Figure 2.1

52

Let Us C

The relational operators should be familiar to you except for the equality operator == and the inequality operator !=. Note that = is used for assignment, whereas, == is used for comparison of two quantities. Here is a simple program, which demonstrates the use of if and the relational operators.
/* Demonstration of if statement */ main( ) { int num ; printf ( "Enter a number less than 10 " ) ; scanf ( "%d", &num ) ; if ( num 10

53

no

yes

PRINT What an obedient servant you are !

STOP

Figure 2.2 To make you comfortable with the decision control instruction one more example has been given below. Study it carefully before reading further. To help you understand it easily, the program is accompanied by an appropriate flowchart. Example 2.1: While purchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000. If quantity and price per item are input through the keyboard, write a program to calculate the total expenses.

54
START dis = 0 INPUT qty, rate is qty > 1000 yes

Let Us C

no

dis = 10

tot = qty * rate – qty * rate * dis / 100 PRINT tot STOP

Figure 2.3
/* Calculation of total expenses */ main( ) { int qty, dis = 0 ; float rate, tot ; printf ( "Enter quantity and rate " ) ; scanf ( "%d %f", &qty, &rate) ; if ( qty > 1000 ) dis = 10 ;

Chapter 2: The Decision Control Structure tot = ( qty * rate ) - ( qty * rate * dis / 100 ) ; printf ( "Total expenses = Rs. %f", tot ) ; }

55

Here is some sample interaction with the program.
Enter quantity and rate 1200 15.50 Total expenses = Rs. 16740.000000 Enter quantity and rate 200 15.50 Total expenses = Rs. 3100.000000

In the first run of the program, the condition evaluates to true, as 1200 (value of qty) is greater than 1000. Therefore, the variable dis, which was earlier set to 0, now gets a new value 10. Using this new value total expenses are calculated and printed. In the second run the condition evaluates to false, as 200 (the value of qty) isn’t greater than 1000. Thus, dis, which is earlier set to 0, remains 0, and hence the expression after the minus sign evaluates to zero, thereby offering no discount. Is the statement dis = 0 necessary? The answer is yes, since in C, a variable if not specifically initialized contains some unpredictable value (garbage value).

The Real Thing
We mentioned earlier that the general form of the if statement is as follows if ( condition ) statement ;

Truly speaking the general form is as follows:

56 if ( expression ) statement ;

Let Us C

Here the expression can be any valid expression including a relational expression. We can even use arithmetic expressions in the if statement. For example all the following if statements are valid if ( 3 + 2 % 5 ) printf ( "This works" ) ; if ( a = 10 ) printf ( "Even this works" ) ; if ( -5 ) printf ( "Surprisingly even this works" ) ;

Note that in C a non-zero value is considered to be true, whereas a 0 is considered to be false. In the first if, the expression evaluates to 5 and since 5 is non-zero it is considered to be true. Hence the printf( ) gets executed. In the second if, 10 gets assigned to a so the if is now reduced to if ( a ) or if ( 10 ). Since 10 is non-zero, it is true hence again printf( ) goes to work. In the third if, -5 is a non-zero number, hence true. So again printf( ) goes to work. In place of -5 even if a float like 3.14 were used it would be considered to be true. So the issue is not whether the number is integer or float, or whether it is positive or negative. Issue is whether it is zero or non-zero.

Multiple Statements within if
It may so happen that in a program we want more than one statement to be executed if the expression following if is satisfied. If such multiple statements are to be executed then they must be

Chapter 2: The Decision Control Structure

57

placed within a pair of braces as illustrated in the following example. Example 2.2: The current year and the year in which the employee joined the organization are entered through the keyboard. If the number of years for which the employee has served the organization is greater than 3 then a bonus of Rs. 2500/is given to the employee. If the years of service are not greater than 3, then the program should do nothing.
/* Calculation of bonus */ main( ) { int bonus, cy, yoj, yr_of_ser ; printf ( "Enter current year and year of joining " ) ; scanf ( "%d %d", &cy, &yoj ) ; yr_of_ser = cy - yoj ; if ( yr_of_ser > 3 ) { bonus = 2500 ; printf ( "Bonus = Rs. %d", bonus ) ; } }

Observe that here the two statements to be executed on satisfaction of the condition have been enclosed within a pair of braces. If a pair of braces is not used then the C compiler assumes that the programmer wants only the immediately next statement after the if to be executed on satisfaction of the condition. In other words we can say that the default scope of the if statement is the immediately next statement after it.

58
START INPUT cy, yoj

Let Us C

yr_of_ser = cy - yoj

no

yr_of_ser > 3

yes

bonus = 2500

PRINT bonus

STOP

Figure 2.4

The if-else Statement
The if statement by itself will execute a single statement, or a group of statements, when the expression following if evaluates to true. It does nothing when the expression evaluates to false. Can we execute one group of statements if the expression evaluates to true and another group of statements if the expression evaluates to false? Of course! This is what is the purpose of the else statement that is demonstrated in the following example: Example 2.3: In a company an employee is paid as under:

Chapter 2: The Decision Control Structure

59

If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and DA = 90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary. If the employee's salary is input through the keyboard write a program to find his gross salary.
/* Calculation of gross salary */ main( ) { float bs, gs, da, hra ; printf ( "Enter basic salary " ) ; scanf ( "%f", &bs ) ; if ( bs < 1500 ) { hra = bs * 10 / 100 ; da = bs * 90 / 100 ; } else { hra = 500 ; da = bs * 98 / 100 ; } gs = bs + hra + da ; printf ( "gross salary = Rs. %f", gs ) ; }

60

Let Us C

START INPUT bs is bs < 1500 hra = 500 da = bs * 98 / 100 hra = bs * 10 / 100

da = bs * 90 / 100

gs = bs + hra + da PRINT gs STOP

Figure 2.5 Figure 2.5 A few points worth noting...

(a) The group of statements after the if upto and not including the

else is called an ‘if block’. Similarly, the statements after the else form the ‘else block’. (b) Notice that the else is written exactly below the if. The statements in the if block and those in the else block have been indented to the right. This formatting convention is

Chapter 2: The Decision Control Structure

61

followed throughout the book to enable you to understand the working of the program better.
(c) Had there been only one statement to be executed in the if

block and only one statement in the else block we could have dropped the pair of braces.
(d) As with the if statement, the default scope of else is also the

statement immediately after the else. To override this default scope a pair of braces as shown in the above example must be used.

Nested if-elses
It is perfectly all right if we write an entire if-else construct within either the body of the if statement or the body of an else statement. This is called ‘nesting’of ifs. This is shown in the following program.
/* A quick demo of nested if-else */ main( ) { int i ; printf ( "Enter either 1 or 2 " ) ; scanf ( "%d", &i ) ; if ( i == 1 ) printf ( "You would go to heaven !" ) ; else { if ( i == 2 ) printf ( "Hell was created with you in mind" ) ; else printf ( "How about mother earth !" ) ; } }

62

Let Us C

Note that the second if-else construct is nested in the first else statement. If the condition in the first if statement is false, then the condition in the second if statement is checked. If it is false as well, then the final else statement is executed. You can see in the program how each time a if-else construct is nested within another if-else construct, it is also indented to add clarity to the program. Inculcate this habit of indentation, otherwise you would end up writing programs which nobody (you included) can understand easily at a later date. In the above program an if-else occurs within the else block of the first if statement. Similarly, in some other program an if-else may occur in the if block as well. There is no limit on how deeply the ifs and the elses can be nested.

Forms of if
The if statement can take any of the following forms:
(a) if ( condition ) do this ; (b) if ( condition ) { do this ; and this ; } (c) if ( condition ) do this ; else do this ; (d) if ( condition ) { do this ;

Chapter 2: The Decision Control Structure and this ; } else { do this ; and this ; } (e) if ( condition ) do this ; else { if ( condition ) do this ; else { do this ; and this ; } } (f) if ( condition ) { if ( condition ) do this ; else { do this ; and this ; } } else do this ;

63

64
Use of Logical Operators

Let Us C

C allows usage of three logical operators, namely, &&, || and !. These are to be read as ‘AND’ ‘OR’ and ‘NOT’ respectively. There are several things to note about these logical operators. Most obviously, two of them are composed of double symbols: || and &&. Don’t use the single symbol | and &. These single symbols also have a meaning. They are bitwise operators, which we would examine in Chapter 14. The first two operators, && and ||, allow two or more conditions to be combined in an if statement. Let us see how they are used in a program. Consider the following example. Example 2.4: The marks obtained by a student in 5 different subjects are input through the keyboard. The student gets a division as per the following rules: Percentage above or equal to 60 - First division Percentage between 50 and 59 - Second division Percentage between 40 and 49 - Third division Percentage less than 40 - Fail Write a program to calculate the division obtained by the student. There are two ways in which we can write a program for this example. These methods are given below.
/* Method – I */ main( ) { int m1, m2, m3, m4, m5, per ; printf ( "Enter marks in five subjects " ) ; scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ; per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;

Chapter 2: The Decision Control Structure if ( per >= 60 ) printf ( "First division ") ; else { if ( per >= 50 ) printf ( "Second division" ) ; else { if ( per >= 40 ) printf ( "Third division" ) ; else printf ( "Fail" ) ; } } }

65

This is a straight forward program. Observe that the program uses nested if-elses. This leads to three disadvantages:
(a) As the number of conditions go on increasing the level of

indentation also goes on increasing. As a result the whole program creeps to the right. (b) Care needs to be exercised to match the corresponding ifs and elses. (c) Care needs to be exercised to match the corresponding pair of braces. All these three problems can be eliminated by usage of ‘Logical operators’. The following program illustrates this.
/* Method – II */ main( ) { int m1, m2, m3, m4, m5, per ; printf ( "Enter marks in five subjects " ) ; scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ; per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;

66 if ( per >= 60 ) printf ( "First division" ) ; if ( ( per >= 50 ) && ( per < 60 ) ) printf ( "Second division" ) ; if ( ( per >= 40 ) && ( per < 50 ) ) printf ( "Third division" ) ; if ( per < 40 ) printf ( "Fail" ) ; }

Let Us C

As can be seen from the second if statement, the && operator is used to combine two conditions. ‘Second division’ gets printed if both the conditions evaluate to true. If one of the conditions evaluate to false then the whole thing is treated as false. Two distinct advantages can be cited in favour of this program:
(a) The matching (or do I say mismatching) of the ifs with their

corresponding elses gets avoided, since there are no elses in this program. (b) In spite of using several conditions, the program doesn't creep to the right. In the previous program the statements went on creeping to the right. This effect becomes more pronounced as the number of conditions go on increasing. This would make the task of matching the ifs with their corresponding elses and matching of opening and closing braces that much more difficult.

The else if Clause
There is one more way in which we can write program for Example 2.4. This involves usage of else if blocks as shown below:

Chapter 2: The Decision Control Structure
/* else if ladder demo */ main( ) { int m1, m2, m3, m4, m5, per ; per = ( m1+ m2 + m3 + m4+ m5 ) / per ; if ( per >= 60 ) printf ( "First division" ) ; else if ( per >= 50 ) printf ( "Second division" ) ; else if ( per >= 40 ) printf ( "Third division" ) ; else printf ( "fail" ) ; }

67

You can note that this program reduces the indentation of the statements. In this case every else is associated with its previous if. The last else goes to work only if all the conditions fail. Even in else if ladder the last else is optional. Note that the else if clause is nothing different. It is just a way of rearranging the else with the if that follows it. This would be evident if you look at the following code: if ( i == 2 ) printf ( "With you…" ) ; else { if ( j == 2 ) printf ( "…All the time" ) ; } if ( i == 2 ) printf ( "With you…" ) ; else if ( j == 2 ) printf ( "…All the time " ) ;

Another place where logical operators are useful is when we want to write programs for complicated logics that ultimately boil down

68

Let Us C

to only two answers. For example, consider the following example: Example 2.5: A company insures its drivers in the following cases: − − − If the driver is married. If the driver is unmarried, male & above 30 years of age. If the driver is unmarried, female & above 25 years of age.

In all other cases the driver is not insured. If the marital status, sex and age of the driver are the inputs, write a program to determine whether the driver is to be insured or not. Here after checking a complicated set of instructions the final output of the program would be one of the two—Either the driver should be ensured or the driver should not be ensured. As mentioned above, since these are the only two outcomes this problem can be solved using logical operators. But before we do that let us write a program that does not make use of logical operators.
/* Insurance of driver - without using logical operators */ main( ) { char sex, ms ; int age ; printf ( "Enter age, sex, marital status " ) ; scanf ( "%d %c %c", &age, &sex, &ms ) ; if ( ms == 'M' ) printf ( "Driver is insured" ) ; else { if ( sex == 'M' ) {

Chapter 2: The Decision Control Structure if ( age > 30 ) printf ( "Driver is insured" ) ; else printf ( "Driver is not insured" ) ; } else { if ( age > 25 ) printf ( "Driver is insured" ) ; else printf ( "Driver is not insured" ) ; } } }

69

From the program it is evident that we are required to match several ifs and elses and several pairs of braces. In a more real-life situation there would be more conditions to check leading to the program creeping to the right. Let us now see how to avoid these problems by using logical operators. As mentioned above, in this example we expect the answer to be either ‘Driver is insured’ or ‘Driver is not insured’. If we list down all those cases in which the driver is insured, then they would be:
(a) Driver is married. (b) Driver is an unmarried male above 30 years of age. (c) Driver is an unmarried female above 25 years of age.

Since all these cases lead to the driver being insured, they can be combined together using && and || as shown in the program below:
/* Insurance of driver - using logical operators */ main( ) { char sex, ms ;

70 int age ; printf ( "Enter age, sex, marital status " ) ; scanf ( "%d %c %c" &age, &sex, &ms ) ; if ( ( ms == 'M') || ( ms == 'U' && sex == 'M' && age > 30 ) || ( ms == 'U' && sex == 'F' && age > 25 ) ) printf ( "Driver is insured" ) ; else printf ( "Driver is not insured" ) ; }

Let Us C

In this program it is important to note that: − − The driver will be insured only if one of the conditions enclosed in parentheses evaluates to true. For the second pair of parentheses to evaluate to true, each condition in the parentheses separated by && must evaluate to true. Even if one of the conditions in the second parentheses evaluates to false, then the whole of the second parentheses evaluates to false. The last two of the above arguments apply to third pair of parentheses as well.





Thus we can conclude that the && and || are useful in the following programming situations:
(a) When it is to be tested whether a value falls within a

particular range or not. (b) When after testing several conditions the outcome is only one of the two answers (This problem is often called yes/no problem).

Chapter 2: The Decision Control Structure

71

There can be one more situation other than checking ranges or yes/no problem where you might find logical operators useful. The following program demonstrates it. Example 2.6: Write a program to calculate the salary as per the following table:

Gender Male

Years of Service >= 10 >= 10 < 10 < 10

Qualifications Post-Graduate Graduate Post-Graduate Graduate Post-Graduate Graduate Post-Graduate Graduate

Salary 15000 10000 10000 7000 12000 9000 10000 6000

Female

>= 10 >= 10 < 10 < 10

Figure 2.6 main( ) { char g ; int yos, qual, sal ; printf ( "Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):" ) ; scanf ( "%c%d%d", &g, &yos, &qual ) ; if ( g == 'm' && yos >= 10 && qual == 1 ) sal = 15000 ; else if ( ( g == 'm' && yos >= 10 && qual == 0 ) || ( g == 'm' && yos < 10 && qual == 1 ) ) sal = 10000 ;

72 else if ( g == 'm' && yos < 10 && qual == 0 ) sal = 7000 ; else if ( g == 'f' && yos >= 10 && qual == 1 ) sal = 12000 ; else if ( g == 'f' && yos >= 10 && qual == 0 ) sal = 9000 ; else if ( g == 'f' && yos < 10 && qual == 1 ) sal = 10000 ; else if ( g == 'f' && yos < 10 && qual == 0 ) sal = 6000 ; printf ( "\nSalary of Employee = %d", sal ) ; }

Let Us C

The ! Operator
So far we have used only the logical operators && and ||. The third logical operator is the NOT operator, written as !. This operator reverses the result of the expression it operates on. For example, if the expression evaluates to a non-zero value, then applying ! operator to it results into a 0. Vice versa, if the expression evaluates to zero then on applying ! operator to it makes it 1, a non-zero value. The final result (after applying !) 0 or 1 is considered to be false or true respectively. Here is an example of the NOT operator applied to a relational expression.
! ( y < 10 )

This means “not y less than 10”. In other words, if y is less than 10, the expression will be false, since ( y < 10 ) is true. We can express the same condition as ( y >= 10 ). The NOT operator is often used to reverse the logical value of a single variable, as in the expression if ( ! flag )

Chapter 2: The Decision Control Structure
This is another way of saying if ( flag == 0 )

73

Does the NOT operator sound confusing? Avoid it if you want, as the same thing can be achieved without using the NOT operator.

Hierarchy of Operators Revisited
Since we have now added the logical operators to the list of operators we know, it is time to review these operators and their priorities. Figure 2.7 summarizes the operators we have seen so far. The higher the position of an operator is in the table, higher is its priority. (A full-fledged precedence table of operators is given in Appendix A.)
Operators ! * / % + < > = == != && || = Type Logical NOT Arithmetic and modulus Arithmetic Relational Relational Logical AND Logical OR Assignment

Figure 2.7

A Word of Caution
What will be the output of the following program:

74 main( ) { int i ; printf ( "Enter value of i " ) ; scanf ( "%d", &i ) ; if ( i = 5 ) printf ( "You entered 5" ) ; else printf ( "You entered something other than 5" ) ; }

Let Us C

And here is the output of two runs of this program...
Enter value of i 200 You entered 5 Enter value of i 9999 You entered 5

Surprising? You have entered 200 and 9999, and still you find in either case the output is ‘You entered 5’. This is because we have written the condition wrongly. We have used the assignment operator = instead of the relational operator ==. As a result, the condition gets reduced to if ( 5 ), irrespective of what you supply as the value of i. And remember that in C ‘truth’ is always nonzero, whereas ‘falsity’ is always zero. Therefore, if ( 5 ) always evaluates to true and hence the result. Another common mistake while using the if statement is to write a semicolon (;) after the condition, as shown below: main( ) { int i ; printf ( "Enter value of i " ) ; scanf ( "%d", &i ) ;

Chapter 2: The Decision Control Structure if ( i == 5 ) ; printf ( "You entered 5" ) ; }

75

The ; makes the compiler to interpret the statement as if you have written it in following manner: if ( i == 5 ) ; printf ( "You entered 5" ) ;

Here, if the condition evaluates to true the ; (null statement, which does nothing on execution) gets executed, following which the printf( ) gets executed. If the condition fails then straightaway the printf( ) gets executed. Thus, irrespective of whether the condition evaluates to true or false the printf( ) is bound to get executed. Remember that the compiler would not point out this as an error, since as far as the syntax is concerned nothing has gone wrong, but the logic has certainly gone awry. Moral is, beware of such pitfalls. The following figure summarizes the working of all the three logical operators.

Operands x 0 0 non-zero non-zero y 0 non-zero 0 non-zero !x 1 1 0 0 !y 1 0 1 0

Results x && y 0 0 0 1 x || y 0 0 1 1

Figure 2.8

76
The Conditional Operators

Let Us C

The conditional operators ? and : are sometimes called ternary operators since they take three arguments. In fact, they form a kind of foreshortened if-then-else. Their general form is, expression 1 ? expression 2 : expression 3

What this expression says is: “if expression 1 is true (that is, if its value is non-zero), then the value returned will be expression 2, otherwise the value returned will be expression 3”. Let us understand this with the help of a few examples:
(a) int x, y ; scanf ( "%d", &x ) ; y=(x>5?3:4);

This statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y. The equivalent if statement will be, if ( x > 5 ) y=3; else y=4; (b) char a ; int y ; scanf ( "%c", &a ) ; y = ( a >= 65 && a =65 && a = 'a' ? a : '!' ) ) ;

Ex.:

(b) The conditional operators can be nested as shown below.

int big, a, b, c ; big = ( a > b ? ( a > c ? 3: 4 ) : ( b > c ? 6: 8 ) ) ;
(c) Check out the following conditional expression:

a>b?g=a:g=b;

This will give you an error ‘Lvalue Required’. The error can be overcome by enclosing the statement in the : part within a pair of parenthesis. This is shown below: a>b?g=a:(g=b); In absence of parentheses the compiler believes that b is being assigned to the result of the expression to the left of second =. Hence it reports an error. The limitation of the conditional operators is that after the ? or after the : only one C statement can occur. In practice rarely is this the requirement. Therefore, in serious C programming conditional operators aren’t as frequently used as the if-else.

Summary
(a) There are three ways for taking decisions in a program. First way is to use the if-else statement, second way is to use the

78

Let Us C conditional operators and third way is to use the switch statement. The default scope of the if statement is only the next statement. So, to execute more than one statement they must be written in a pair of braces. An if block need not always be associated with an else block. However, an else block is always associated with an if statement. If the outcome of an if-else ladder is only one of two answers then the ladder should be replaced either with an else-if clause or by logical operators. && and || are binary operators, whereas, ! is a unary operator. In C every test expression is evaluated in terms of zero and non-zero values. A zero value is considered to be false and a non-zero value is considered to be true. Assignment statements used with conditional operators must be enclosed within a pair of parenthesis.

(b)

(c)

(d)

(e) (f)

(g)

Exercise if, if-else, Nested if-elses
[A] What would be the output of the following programs: (a) main( ) { int a = 300, b, c ; if ( a >= 400 ) b = 300 ; c = 200 ; printf ( "\n%d %d", b, c ) ; } (b) main( ) { int a = 500, b, c ; if ( a >= 400 )

Chapter 2: The Decision Control Structure b = 300 ; c = 200 ; printf ( "\n%d %d", b, c ) ; } (c) main( ) { int x = 10, y = 20 ; if ( x == y ) ; printf ( "\n%d %d", x, y ) ; }

79

(d) main( ) { int x = 3, y = 5 ; if ( x == 3 ) printf ( "\n%d", x ) ; else ; printf ( "\n%d", y ) ; } (e) main( ) { int x = 3 ; float y = 3.0 ; if ( x == y ) printf ( "\nx and y are equal" ) ; else printf ( "\nx and y are not equal" ) ; (f) } main( ) { int x = 3, y, z ; y = x = 10 ; z = x < 10 ; printf ( "\nx = %d y = %d z = %d", x, y, z ) ; }

80
(g) main( ) { int k = 35 ; printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ; } (h) main( ) { int i = 65 ; char j = ‘A’ ; if ( i == j ) printf ( “C is WOW” ) ; else printf( "C is a headache" ) ; } (i) main( ) { int a = 5, b, c ; b = a = 15 ; c = a < 15 ; printf ( "\na = %d b = %d c = %d", a, b, c ) ; } main( ) { int x = 15 ; printf ( "\n%d %d %d", x != 15, x = 20, x < 30 ) ; }

Let Us C

(j)

[B] Point out the errors, if any, in the following programs: (a) main( ) { float a = 12.25, b = 12.52 ; if ( a = b ) printf ( "\na and b are equal" ) ;

Chapter 2: The Decision Control Structure
} (b) main( ) { int j = 10, k = 12 ; if ( k >= j ) { { k=j; j=k; } } } (c) main( ) { if ( 'X' < 'x' ) printf ( "\nascii value of X is smaller than that of x" ) ; }

81

(d) main( ) { int x = 10 ; if ( x >= 2 ) then printf ( "\n%d", x ) ; } (e) main( ) { int x = 10 ; if x >= 2 printf ( "\n%d", x ) ; } (f) main( ) { int x = 10, y = 15 ; if ( x % 2 = y % 3 )

82 printf ( "\nCarpathians" ) ; } (g) main( ) { int x = 30 , y = 40 ; if ( x == y ) printf( "x is equal to y" ) ; elseif ( x > y ) printf( "x is greater than y" ) ; elseif ( x < y ) printf( "x is less than y" ) ; } (h) main( ) { int x = 10 ; if ( x >= 2 ) then printf ( "\n%d", x ) ; } (i) main( ) { int a, b ; scanf ( "%d %d",a, b ) ; if ( a > b ) ; printf ( "This is a game" ) ; else printf ( "You have to play it" ) ; }

Let Us C

[C] Attempt the following:
(a) If cost price and selling price of an item is input through the

keyboard, write a program to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred.

Chapter 2: The Decision Control Structure

83

(b) Any integer is input through the keyboard. Write a program to

find out whether it is an odd number or even number.
(c) Any year is input through the keyboard. Write a program to

determine whether the year is a leap year or not. (Hint: Use the % (modulus) operator)
(d) According to the Gregorian calendar, it was Monday on the

date 01/01/1900. If any year is input through the keyboard write a program to find out what is the day on 1st January of this year.
(e) A five-digit number is entered through the keyboard. Write a

program to obtain the reversed number and to determine whether the original and reversed numbers are equal or not.
(f)

If the ages of Ram, Shyam and Ajay are input through the keyboard, write a program to determine the youngest of the three.

(g) Write a program to check whether a triangle is valid or not,

when the three angles of the triangle are entered through the keyboard. A triangle is valid if the sum of all the three angles is equal to 180 degrees.
(h) Find the absolute value of a number entered through the

keyboard.
(i)

Given the length and breadth of a rectangle, write a program to find whether the area of the rectangle is greater than its perimeter. For example, the area of the rectangle with length = 5 and breadth = 4 is greater than its perimeter. Given three points (x1, y1), (x2, y2) and (x3, y3), write a program to check if all the three points fall on one straight line.

(j)

84

Let Us C write a program which will determine whether a point lies inside the circle, on the circle or outside the circle.

(k) Given the coordinates (x, y) of a center of a circle and it’s radius,

(Hint: Use sqrt( ) and pow( ) functions)
(l)

Given a point (x, y), write a program to find out if it lies on the x-axis, y-axis or at the origin, viz. (0, 0).

Logical Operators
If a = 10, b = 12, c = 0, find the values of the expressions in the following table:
Expression a != 6 && b > 5 a == 9 || b < 3 ! ( a < 10 ) ! ( a > 5 && c ) 5 && c != 8 || !c Value 1

[D] What would be the output of the following programs: (a) main( ) { int i = 4, z = 12 ; if ( i = 5 || z > 50 ) printf ( "\nDean of students affairs" ) ; else printf ( "\nDosa" ) ; } (b) main( ) { int i = 4, z = 12 ;

Chapter 2: The Decision Control Structure if ( i = 5 && z > 5 ) printf ( "\nLet us C" ) ; else printf ( "\nWish C was free !" ) ; } (c) main( ) { int i = 4, j = -1, k = 0, w, x, y, z ; w = i || j || k ; x = i && j && k ; y = i || j && k ; z = i && j || k ; printf ( "\nw = %d x = %d y = %d z = %d", w, x, y, z ) ; }

85

(d) main( ) { int i = 4, j = -1, k = 0, y, z ; y = i + 5 && j + 1 || k + 2 ; z = i + 5 || j + 1 && k + 2 ; printf ( "\ny = %d z = %d", y, z ) ; } (e) main( ) { int i = -3, j = 3 ; if ( !i + !j * 1 ) printf ( "\nMassaro" ) ; else printf ( "\nBennarivo" ) ; } (f) main( ) { int a = 40 ; if ( a > 40 && a < 45 ) printf ( "a is greater than 40 and less than 45" ) ;

86 else printf ( "%d", a ) ; } (g) main( ) { int p = 8, q = 20 ; if ( p == 5 && q > 5 ) printf ( "\nWhy not C" ) ; else printf ( "\nDefinitely C !" ) ; } (h) main( ) { int i = -1, j = 1, k ,l ; k = i && j ; l = i || j ; printf ( "%d %d", I, j ) ; } (i) main( ) { int x = 20 , y = 40 , z = 45 ; if ( x > y && x > z ) printf( "x is big" ) ; else if ( y > x && y > z ) printf( "y is big" ) ; else if ( z > x && z > y ) printf( "z is big" ) ; } main( ) { int i = -1, j = 1, k ,l ; k = !i && j ; l = !i || j ; printf ( "%d %d", i, j ) ;

Let Us C

(j)

Chapter 2: The Decision Control Structure
} (k) main( ) { int j = 4, k ; k = !5 && j ; printf ( "\nk = %d", k ) ; }

87

[E] Point out the errors, if any, in the following programs: (a) /* This program /* is an example of /* using Logical operators */ main( ) { int i = 2, j = 5 ; if ( i == 2 && j == 5 ) printf ( "\nSatisfied at last" ) ; } (b) main( ) { int code, flag ; if ( code == 1 & flag == 0 ) printf ( "\nThe eagle has landed" ) ; } (c) main( ) { char spy = 'a', password = 'z' ; if ( spy == 'a' or password == 'z' ) printf ( "\nAll the birds are safe in the nest" ) ; }

(d) main( ) {

88 int i = 10, j = 20 ; if ( i = 5 ) && if ( j = 10 ) printf ( "\nHave a nice day" ) ; } (a) main( ) { int x = 10 , y = 20; if ( x >= 2 and y 5 ? ( num 1 ? printf ( "\nHello" ) ? printf ( "\nHi" ) ) ; else printf ( "\nHello Hi !!" ) ; } (b) main( ) { int ji = 65 ; printf ( "\nji >= 65 ? %d : %c", ji ) ; } (c) main( ) { int i = 10, j ; i >= 5 ? ( j = 10 ) : ( j = 15 ) ; printf ( "\n%d %d", i, j ) ; }

93

(d) main( ) { int a = 5 , b = 6 ; ( a == b ? printf( "%d",a) ) ; } (e) main( ) { int n = 9 ; ( n == 9 ? printf( "You are correct" ) ; : printf( "You are wrong" ) ;) ; }

94
(f)

Let Us C main( ) { int kk = 65 ,ll ; ll = ( kk == 65 : printf ( "\n kk is equal to 65" ) : printf ( "\n kk is not equal to 65" ) ) ; printf( "%d", ll ) ; }

(g) main( ) { int x = 10, y = 20 ; x == 20 && y != 10 ? printf( "True" ) : printf( "False" ) ; } [I] Rewrite the following programs using conditional operators. (a) main( ) { int x, min, max ; scanf ( "\n%d %d", &max, &x ) ; if ( x > max ) max = x ; else min = x ; } (b) main( ) { int code ; scanf ( "%d", &code ) ; if ( code > 1 ) printf ( "\nJerusalem" ) ; else if ( code < 1 ) printf ( "\nEddie" ) ; else printf ( "\nC Brain" ) ; }

Chapter 2: The Decision Control Structure
(c) main( ) { float sal ; printf ("Enter the salary" ) ; scanf ( "%f", &sal ) ; if ( sal < 40000 && sal > 25000 ) printf ( "Manager" ) ; else if ( sal < 25000 && sal > 15000 ) printf ( "Accountant" ) ; else printf ( "Clerk" ) ; }

95

[J] Attempt the following:
(a) Using conditional operators determine:

(1) Whether the character entered through the keyboard is a lower case alphabet or not. (2) Whether a character entered through the keyboard is a special symbol or not.
(b) Write a program using conditional operators to determine

whether a year entered through the keyboard is a leap year or not.
(c) Write a program to find the greatest of the three numbers

entered through the keyboard using conditional operators.

96

Let Us C

• Loops • The while Loop Tips and Traps More Operators • The for Loop Nesting of Loops Multiple Initialisations in the for Loop • The Odd Loop • The break Statement The continue Statement • The do-while Loop • Summary • Exercise

T

he programs that we have developed so far used either a sequential or a decision control instruction. In the first one, the calculations were carried out in a fixed order, while in the second, an appropriate set of instructions were executed depending upon the outcome of the condition being tested (or a logical decision being taken).

97

98

Let Us C

These programs were of limited nature, because when executed, they always performed the same series of actions, in the same way, exactly once. Almost always, if something is worth doing, it’s worth doing more than once. You can probably think of several examples of this from real life, such as eating a good dinner or going for a movie. Programming is the same; we frequently need to perform an action over and over, often with variations in the details each time. The mechanism, which meets this need, is the ‘loop’, and loops are the subject of this chapter.

Loops
The versatility of the computer lies in its ability to perform a set of instructions repeatedly. This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied. This repetitive operation is done through a loop control instruction. There are three methods by way of which we can repeat a part of a program. They are: (a) Using a for statement (b) Using a while statement (c) Using a do-while statement Each of these methods is discussed in the following pages.

The while Loop
It is often the case in programming that you want to do something a fixed number of times. Perhaps you want to calculate gross salaries of ten different persons, or you want to convert temperatures from centigrade to fahrenheit for 15 different cities.

Chapter 3: The Loop Control Structure

99

The while loop is ideally suited for such cases. Let us look at a simple example, which uses a while loop. The flowchart shown below would help you to understand the operation of the while loop.
START

count = 1

is count >. It needs two operands. It shifts each bit in its left operand to the right. The number of places the bits are shifted depends on the number following the operator (i.e. its right operand). Thus, ch >> 3 would shift all bits in ch three places to the right. Similarly, ch >> 5 would shift all bits 5 places to the right. For example, if the variable ch contains the bit pattern 11010111, then, ch >> 1 would give 01101011 and ch >> 2 would give 00110101. Note that as the bits are shifted to the right, blanks are created on the left. These blanks must be filled somehow. They are always filled with zeros. The following program demonstrates the effect of right shift operator. main( ) {

Chapter 14: Operations On Bits int i = 5225, j, k ; printf ( "\nDecimal %d is same as binary ", i ) ; showbits ( i ) ; for ( j = 0 ; j >j ; printf ( "\n%d right shift %d gives ", i, j ) ; showbits ( k ) ; } }

487

The output of the above program would be...
Decimal 5225 is same as binary 0001010001101001 5225 right shift 0 gives 0001010001101001 5225 right shift 1 gives 0000101000110100 5225 right shift 2 gives 0000010100011010 5225 right shift 3 gives 0000001010001101 5225 right shift 4 gives 0000000101000110 5225 right shift 5 gives 0000000010100011

Note that if the operand is a multiple of 2 then shifting the operand one bit to right is same as dividing it by 2 and ignoring the remainder. Thus,
64 >> 1 gives 32 64 >> 2 gives 16 128 >> 2 gives 32

but,
27 >> 1 is 13 49 >> 1 is 24 .

488
A Word of Caution

Let Us C

In the explanation a >> b if b is negative the result is unpredictable. If a is negative than its left most bit (sign bit) would be 1. On some computer right shifting a would result in extending the sign bit. For example, if a contains -1, its binary representation would be 1111111111111111. Without sign extension, the operation a >> 4 would be 0000111111111111. However, on the machine on which we executed this expression the result turns out to be 1111111111111111. Thus the sign bit 1 continues to get extended.

Left Shift Operator
This is similar to the right shift operator, the only difference being that the bits are shifted to the left, and for each bit shifted, a 0 is added to the right of the number. The following program should clarify my point. main( ) { int i = 5225, j, k ; printf ( "\nDecimal %d is same as ", i ) ; showbits ( i ) ; for ( j = 0 ; j 12 ) ; day = ( (date > 11 ) ; printf ( "\nYear = %u ", year ) ; printf ( "Month = %u ", month ) ; printf ( "Day = %u", day ) ; }

493

And here is the output...
Date = 5225 Year = 1990 Month = 3 Day = 9

Bitwise AND Operator
This operator is represented as &. Remember it is different than &&, the logical AND operator. The & operator operates on two operands. While operating upon these two operands they are compared on a bit-by-bit basis. Hence both the operands must be of the same type (either char or int). The second operand is often called an AND mask. The & operator operates on a pair of bits to yield a resultant bit. The rules that decide the value of the resultant bit are shown below:

First bit 0 0 1 1

Second bit 0 1 0 1

First bit & Second bit 0 0 0 1

Figure 14.7

494

Let Us C

This can be represented in a more understandable form as a ‘Truth Table’ shown in Figure 14.8.

& 0 1

0 0 0

1 0 1

Figure 14.8 The example given below shows more clearly what happens while ANDing one operand with another. The rules given in the Figure 14.8 are applied to each pair of bits one by one.

7 1 7 1

6 0 6 1

5 1 5 0

4 0 4 0

3 1 3 0

2 0 2 0

1 1 1 1

0 0 0 1 With this operand yields This operand when ANDed bitwise

7 1

6 0

5 0

4 0

3 0

2 0

1 1

0 0 this result

Figure 14.9 Work through the Truth Table and confirm that the result obtained is really correct. Thus, it must be clear that the operation is being performed on individual bits, and the operation performed on one pair of bits is

Chapter 14: Operations On Bits

495

completely independent of the operation performed on the other pairs. Probably, the best use of the AND operator is to check whether a particular bit of an operand is ON or OFF. This is explained in the following example. Suppose, from the bit pattern 10101101 of an operand, we want to check whether bit number 3 is ON (1) or OFF (0). Since we want to check the bit number 3, the second operand for the AND operation should be 1 * 23, which is equal to 8. This operand can be represented bitwise as 00001000. Then the ANDing operation would be,
10101101 00001000 -------------00001000 Original bit pattern AND mask Resulting bit pattern

The resulting value we get in this case is 8, i.e the value of the second operand. The result turned out to be 8 since the third bit of the first operand was ON. Had it been OFF, the bit number 3 in the resulting bit pattern would have evaluated to 0 and the complete bit pattern would have been 00000000. Thus, depending upon the bit number to be checked in the first operand we decide the second operand, and on ANDing these two operands the result decides whether the bit was ON or OFF. If the bit is ON (1), the resulting value turns out to be a non-zero value which is equal to the value of second operand, and if the bit is OFF (0) the result is zero as seen above. The following program puts this logic into action.
/* To test whether a bit in a number is ON or OFF */ main( ) {

496 int i = 65, j ; printf ( "\nvalue of i = %d", i ) ; j = i & 32 ; if ( j == 0 ) printf ( "\nand its fifth bit is off" ) ; else printf ( "\nand its fifth bit is on" ) ; j = i & 64 ; if ( j == 0 ) printf ( "\nwhereas its sixth bit is off" ) ; else printf ( "\nwhereas its sixth bit is on" ) ; }

Let Us C

And here is the output...
Value of i = 65 and its fifth bit is off whereas its sixth bit is on

In every file entry present in the directory, there is an attribute byte. The status of a file is governed by the value of individual bits in this attribute byte. The AND operator can be used to check the status of the bits of this attribute byte. The meaning of each bit in the attribute byte is shown in Figure 14.10.

Chapter 14: Operations On Bits

497

Bit numbers 7 . . . . . . . 1 6 . . . . . . 1 . 5 . . . . . 1 . . 4 . . . . 1 . . . 3 . . . 1 . . . . 2 . . 1 . . . . . 1 . 1 . . . . . . 0 1 . . . . . . .

Meaning

Read only Hidden System Volume label entry Sub-directory entry Archive bit Unused Unused

Figure 14.10 Now, suppose we want to check whether a file is a hidden file or not. A hidden file is one, which is never shown in the directory, even though it exists on the disk. From the above bit classification of attribute byte, we only need to check whether bit number 1 is ON or OFF. So, our first operand in this case becomes the attribute byte of the file in question, whereas the second operand is the 1 * 21 = 2, as discussed earlier. Similarly, it can be checked whether the file is a system file or not, whether the file is read-only file or not, and so on. The second, and equally important use of the AND operator is in changing the status of the bit, or more precisely to switch OFF a particular bit.

498

Let Us C

If the first operand happens to be 00000111, then to switch OFF bit number 1, our AND mask bit pattern should be 11111101. On applying this mask, we get,
00000111 11111101 -------------00000101 Original bit pattern AND mask Resulting bit pattern

Here in the AND mask we keep the value of all other bits as 1 except the one which is to be switched OFF (which is purposefully kept as 0). Therefore, irrespective of whether the first bit is ON or OFF previously, it is switched OFF. At the same time the value 1 provided in all the other bits of the AND mask (second operand) keeps the bit values of the other bits in the first operand unaltered. Let’s summarize the uses of bitwise AND operator: (a) It is used to check whether a particular bit in a number is ON or OFF. (b) It is used to turn OFF a particular bit in a number.

Bitwise OR Operator
Another important bitwise operator is the OR operator which is represented as |. The rules that govern the value of the resulting bit obtained after ORing of two bits is shown in the truth table below.

| 0 1

0 0 1

1 1 1

Figure 14.11

Chapter 14: Operations On Bits

499

Using the Truth table confirm the result obtained on ORing the two operands as shown below.
11010000 00000111 ------------11010111 Original bit pattern OR mask Resulting bit pattern

Bitwise OR operator is usually used to put ON a particular bit in a number. Let us consider the bit pattern 11000011. If we want to put ON bit number 3, then the OR mask to be used would be 00001000. Note that all the other bits in the mask are set to 0 and only the bit, which we want to set ON in the resulting value is set to 1.

Bitwise XOR Operator
The XOR operator is represented as ^ and is also called an Exclusive OR Operator. The OR operator returns 1, when any one of the two bits or both the bits are 1, whereas XOR returns 1 only if one of the two bits is 1. The truth table for the XOR operator is given below.

^ 0 1

0 0 1

1 1 0

Figure 14.12 XOR operator is used to toggle a bit ON or OFF. A number XORed with another number twice gives the original number. This is shown in the following program.

500 main( ) { int b = 50 ; b = b ^ 12 ; printf ( "\n%d", b ) ; /* this will print 62 */ b = b ^ 12 ; printf ( "\n%d", b ) ; /* this will print 50 */ }

Let Us C

The showbits( ) Function
We have used this function quite often in this chapter. Now we have sufficient knowledge of bitwise operators and hence are in a position to understand it. The function is given below followed by a brief explanation. showbits ( int n ) { int i, k, andmask ; for ( i = 15 ; i >= 0 ; i-- ) { andmask = 1 5 ; printf ( "\nk = %d l = %d m = %d", k, l, m ) ; printf ( "\nn = %d o = %d p = %d", n, o, p ) ; }

16

C Under Windows
• • • • • • • • • • • Which Windows… Integers The Use of typedef Pointers in the 32-bit World Memory Management Device Access DOS Programming Model Windows Programming Model Event Driven Model Windows Programming, a Closer Look The First Windows Program Hungarian Notation Summary Exercise

535

536

Let Us C

S

o far we have learnt every single keyword, operator and instruction available in C. Thus we are through with the language elements that were there to learn. We did all this learning by compiling our programs using a 16-bit compiler like Turbo C/C++. Now it is time to move on to more serious stuff. To make a beginning one has to take a very important decision— should we attempt to build programs that are targeted towards 16bit environments like MS-DOS or 32-bit environments like Windows/Linux. Obviously we should choose the 32-bit platform because that is what is in commercial use today and would remain so until 64-bit environment takes over in future. That raises a very important question—is it futile to learn C programming using 16bit compiler like Turbo C/C++? Absolutely not! The typical 32-bit environment offers so many features that the beginner is likely to feel lost. Contrasted with this, 16-bit compilers offer a very simple environment that a novice can master quickly. Now that the C fundamentals are out of the way and you are confident about the language features it is time for us to delve into the modern 32-bit operating environments. In today’s commercial world 16-bit operating environments like DOS are more or less dead. More and more software is being created for 32-bit environments like Windows and Linux. In this chapter we would explore how C programming is done under Windows. Chapters 20 & 21 are devoted to exploring C under Linux.

Which Windows…
To a common user the differences amongst various versions of Windows like Windows 95,98, ME, NT, 2000, XP, Server 2003 is limited to only visual appearances—things like color of the title bar, shape of the buttons, desktop, task bar, programs menu etc. But the truth is much farther than that. Architecturally there are huge differences amongst them. So many are the differences that Microsoft categorizes the different versions under two major heads—Consumer Windows and Windows NT Family. Windows

Chapter 16: C Under Windows

537

95, 98, ME fall under the Consumer Windows, whereas Windows NT, 2000, XP, Server 2003 fall under the Windows NT Family. Consumer Windows was targeted towards the home or small office users, whereas NT family was targeted towards business users. Microsoft no longer provides support for Consumer Windows. Hence in this book we would concentrate only on NT Family Windows. So in the rest of this book whenever I refer to Windows I mean Windows NT family, unless explicitly specified. Before we start writing C programs under Windows let us first see some of the changes that have happened under Windows environment.

Integers
Under 16-bit environment the size of integer is of 2 bytes. As against this, under 32-bit environment an integer is of 4 bytes. Hence its range is -2147483648 to +2147483647. Thus there is no difference between an int and a long int. But what if we wish to store the age of a person in an integer? It would be improper to sacrifice a 4-byte integer when we know that the number to be stored in it is hardly going to exceed hundred. In such as case it would be more sensible to use a short int since it is only 2 bytes long.

The Use of typedef
Take a look at the following declarations:
COLORREF color ; HANDLE h ; WPARAM w ; LPARAM l ; BOOL b ;

538

Let Us C

Are COLORREF, HANDLE, etc. new datatypes that have been added in C under Windows compiler? Not at all. They are merely typedef’s of the normal integer datatype. A typical C under Windows program would contain several such typedefs. There are two reasons why Windows-based C programs heavily make use of typedefs. These are: (a) A typical Windows program is required to perform several complex tasks. For example a program may print documents, send mails, perform file I/O, manage multiple threads of execution, draw in a window, play sound files, perform operations over the network apart from normal data processing tasks. Naturally a program that carries out so many tasks would be very big in size. In such a program if we start using the normal integer data type to represent variables that hold different entities we would soon lose track of what that integer value actually represents. This can be overcome by suitably typedefining the integer as shown above. (b) At several places in Windows programming we are required to gather and work with dissimilar but inter-related data. This can be done using a structure. But when we define any structure variable we are required to precede it with the keyword struct. This can be avoided by using typedef as shown below: struct rect { int top ; int left ; int right ; int bottom ; }; typedef struct rect RECT ; typedef struct rect* PRECT ;

Chapter 16: C Under Windows
RECT r ; PRECT pr ;

539

What have we achieved out of this? It makes user-defined data types like structures look, act and behave similar to standard data types like integers, floats, etc. You would agree that the following declarations
RECT r ; int i ;

are more logical than struct RECT r ; int i ;

Imagine a situation where each programmer typedefs the integer to represent a color in different ways. Some of these could be as follows: typedef int COL ; typedef int COLOR ; typedef int COLOUR ; typedef int COLORREF ;

To avoid this chaos Microsoft has done several typedefs for commonly required entities in Windows programming. All these have been stored in header files. These header files are provided as part of 32-bit compiler like Visual C++.

Pointers in the 32-bit World
In a 16-bit world (like MS-DOS) we could run only one application at a time. If we were to run another program we were required to terminate the first one before launching the second. As only one program (task) could run at a time this environment was

540

Let Us C

called single-tasking environment. Since only one program could run at any given time entire resources of the machine like memory and hardware devices were accessible to this program. Under 32bit environment like Windows several programs reside and work in memory at the same time. Hence it is known as a multi-tasking environment. But the moment there are multiple programs running in memory there is a possibility of conflict if two programs simultaneously access the machine resources. To prevent this, Windows does not permit any application direct access to any machine resource. To channelize the access without resulting into conflict between applications several new mechanisms were created in the Microprocessor & OS. This had a direct bearing on the way the application programs are created. This is not a Windows OS book. So we would restrict our discussion about the new mechanisms that have been introduced in Windows to topics that are related, to C programming. These topics are ‘Memory Management and Device Access’.

Memory Management
Since users have become more demanding, modern day applications have to contend with these demands and provide several features in them. To add to this, under Windows several such applications run in memory simultaneously. The maximum allowable memory—1 MB—that was used in 16-bit environment was just too small for this. Hence Windows had to evolve a new memory management model. Since Windows runs on 32-bit microprocessors each CPU register is 32-bit long. Whenever we store a value at a memory location the address of this memory location has to be stored in the CPU register at some point in time. Thus a 32-bit address can be stored in these registers. This means that we can store 232 unique addresses in the registers at different times. As a result, we can access 4 GB of memory locations using 32-bit registers. As pointers store addresses, every pointer under 32-bit environment also became a 4-byte entity.

Chapter 16: C Under Windows

541

However, if we decide to install 4 GB memory it would cost a lot. Hence Windows uses a memory model which makes use of as much of physical memory (say 128 MB) as has been installed and simulates the balance amount of memory (4 GB – 128 MB) on the hard disk. Be aware that this balance memory is simulated as and when the need to do so arises. Thus memory management is demand based. Note that programs cannot execute straight-away from hard disk. They have to be first brought into physical memory before they can get executed. Suppose there are multiple programs already in memory and a new program starts executing. If this new program needs more memory than what is available right now, then some of the existing programs (or their parts) would be transferred to the disk in order to free the physical memory to accommodate the new program. This operation is often called page-out operation. Here page stands for a block of memory (usually of size 4096 bytes). When that part of the program that was paged out is needed it is brought back into memory (called page-in operation) and some other programs (or their parts) are paged out. This keeps on happening without a common user’s knowledge all the time while working with Windows. A few more facts that you must note about paging are as follows: (a) Part of the program that is currently executing might also be paged out to the disk. (b) When the program is paged in (from disk to memory) there is no guarantee that it would be brought back to the same physical location where it was before it was paged out. Now imagine how the paging operations would affect our programming. Suppose we have a pointer pointing to some data present in a page. If this page gets paged out and is later paged in to a different physical location then the pointer would obviously have a wrong address. Hence under Windows the pointer never holds the physical address of any memory location. It always holds a virtual address of that location. What is this virtual address? At

542

Let Us C

its name suggests it is certainly not a real address. It is a number, which contains three parts. These parts when used in conjunction with a CPU register called CR3 and contents of two tables called Page Directory Table and Page Table leads to the actual physical address. This is shown in Figure 16.1.

Page Dir. Index Page Table Index Page Byte Offset 31 Page Directory PT0 PT1 21 Page Table P0 P1 … Page Frames 11 Target Page PFN n 0

PT n Page Directory Register

Pn …

PFN 0 Physical Memory

Figure 16.1 The CR3 register holds the physical location of Page Directory Table. The left part of the 32-bit virtual address holds the index into the Page Directory Table. The value present at this index is the starting address of the Page Table. The middle part of the 32bit virtual address holds the index into the Page Table. The value present at this index is the starting address of the physical page in memory. The right part of the 32-bit virtual address holds the byte

Chapter 16: C Under Windows

543

offset (from the start of the page) of the physical memory location to be accessed. Note that the CR3 register is not accessible from an application. Hence an application can never directly reach a physical address. Also, as the paging activity is going on the OS would suitably keep updating the values in the two tables.

Device Access
All devices under Windows are shared amongst all the running programs. Hence no program is permitted a direct access to any of the devices. The access to a device is routed through a device driver program, which finally accesses the device. There is a standard way in which an application can communicate with the device driver. It is device driver’s responsibility to ensure that multiple requests coming from different applications are handled without causing any conflict. This standard way of communication is discussed in detail in Chapter 17.

DOS Programming Model
Typical 16-bit environments like DOS use a sequential programming model. In this model programs are executed from top to bottom in an orderly fashion. The path along which the control flows from start to finish may vary during each execution depending on the input that the program receives or the conditions under which it is run. However, the path remains fairly predictable. C programs written in this model begin execution with main( ) (often called entry point) and then call other functions present in the program. If you assume some input data you can easily walk through the program from beginning to end. In this programming model it is the program and not the operating system that determines which function gets called and when. The operating system simply loads and executes the program and then waits for it to finish. If the program wishes it can take help of the OS to carry

544

Let Us C

out jobs like console I/O, file I/O, printing, etc. For other operations like generating graphics, carrying out serial communication, etc. the program has to call another set of functions called ROM-BIOS functions. Unfortunately the DOS functions and the BIOS functions do not have any names. Hence to call them the program had to use a mechanism called interrupts. This is a messy affair since the programmer has to remember interrupt numbers for calling different functions. Moreover, communication with these functions has to be done using CPU registers. This lead to lot of difficulties since different functions use different registers for communication. To an extent these difficulties are reduced by providing library functions that in turn call the DOS/BIOS functions using interrupts. But the library doesn’t have a parallel function for every DOS/BIOS function. DOS functions either call BIOS functions or directly access the hardware. At times the programs are needed to directly interact with the hardware. This has to be done because either there are no DOS/BIOS functions to do this, or if they are there their reach is limited. Figure 16.2 captures the essence of the DOS programming model.

Chapter 16: C Under Windows

545

main( ) { fun( ) ; } fun( ) { … … }

On execution transfer control to program Interrupt & CPU Registers Interrupt & CPU Registers

DOS OS

DOS Functions

BIOS Function

Hardware Sequentially Executing DOS program

Figure 16.2 From the above discussion you can gather that there are several limitations in the DOS programming model. These have been listed below:

No True Reuse
The library functions that are called from each program become part of the executable file (.EXE) for that program. Thus the same functions get replicated in several EXE files, thereby wasting precious disk space.

546
Inconsistent Look and Feel

Let Us C

Every DOS program has a different user interface that the user has to get used to before he can start getting work out of the program. For example, successful DOS-based software like Lotus 1-2-3, Foxpro, Wordstar offered different types of menus. This happened because DOS/BIOS doesn’t provide any functions for creating user interface elements like menus. As the look and feel of all DOS based programs is different, the user takes a lot of time in learning how to interact with the program

Messy Calling Mechanism
It is difficult to remember interrupt numbers and the registers that are to be used for communication with DOS/BIOS functions. For example, if we are to position the cursor on the screen using a BIOS function we are required to remember the following details:
Interrupt number – 16 CPU Registers to be used: AH – 2 (service number) DH – Row number where cursor is to be positioned DL – Column number where cursor is to be positioned

While using these interrupt numbers and registers there is always a chance of error.

Hardware Dependency
DOS programs are always required to bother about the details of the hardware on which they are running. This is because for every new piece of hardware introduced there are new interrupt numbers and new register details. Hence DOS programmers are under the constant fear that if the hardware on which the programs are running changes then the program may crash.

Chapter 16: C Under Windows

547

Moreover the DOS programmer has to write lot of code to detect the hardware on which his program is running and suitably make use of the relevant interrupts and registers. Not only does this make the program lengthy, the programmer has to understand a lot of technical details of the hardware. As a result the programmer has to spend more time in understanding the hardware than in the actual application programming.

Windows Programming Model
From the perspective of the user the shift from MS-DOS to Windows OS involves switching over to a Graphical User Interface from the typical Text Interface that MS-DOS offers. Another change that the user may feel and appreciate is the ability of Windows OS to execute several programs simultaneously, switching effortlessly from one to another by pointing at windows and clicking them with the mouse. Mastering this new GUI environment and getting comfortable with the multitasking feature is at the most a matter of a week or so. However, from the programmer’s point of view programming for Windows is a whole new ball game! Windows programming model is designed with a view to: (a) (b) (c) (d) Eliminate the messy calling mechanism of DOS Permit true reuse of commonly used functions Provide consistent look and feel for all applications Eliminate hardware dependency

Let us discuss how Windows programming model achieves this.

Better Calling Mechanism
Instead of calling functions using Interrupt numbers and registers Windows provides functions within itself which can be called using names. These functions are called API (Application Programming Interface) functions. There are literally hundreds of

548

Let Us C

API functions available. They help an application to perform various tasks such as creating a window, drawing a line, performing file input/output, etc.

True Reuse
A C under Windows program calls several API functions during course of its execution. Imagine how much disk space would have been wasted had each of these functions become part of the EXE file of each program. To avoid this, the API functions are stored in special files that have an extension .DLL. DLL stands for Dynamic Link Libraries. A DLL is a binary file that provides a library of functions. The functions present in DLLs can be linked during execution. These functions can also be shared between several applications running in Windows. Since linking is done dynamically the functions do not become part of the executable file. As a result, the size of EXE files does not go out of hand. It is also possible to create your own DLLs. You would like to do this for two reasons: (a) Sharing common code between different executable files. (b) Breaking an application into component parts to provide a way to easily upgrade application’s components. The Windows API functions come in three DLL files. Figure 16.3 lists these filenames along with purpose of each.

Chapter 16: C Under Windows

549

DLL USER32.DLL

Description Contains functions that are responsible for window management, including menus, cursors, communications, timer etc. Contains functions for graphics drawing and painting Contains functions to handle memory management, threading, etc.

GDI32.DLL KERNEL32.DLL

Figure 16.3

Consistent Look and Feel
Consistent look and feel means that each program offers a consistent and similar user interface. As a result, user doesn’t have to spend long periods of time mastering a new program. Every program occupies a window—a rectangular area on the screen. A window is identified by a title bar. Most program functions are initiated through the program’s menu. The display of information too large to fit on a single screen can be viewed using scroll bars. Some menu items invoke dialog boxes, into which the user enters additional information. One dialog box is found in almost every Windows program. It opens a file. This dialog box looks the same (or very similar) in many different Windows programs, and it is almost always invoked from the same menu option. Once you know how to use one Windows program, you’re in a good position to easily learn another. The menus and dialog boxes allow user to experiment with a new program and explore its features. Most Windows programs have both a keyboard interface and a mouse interface. Although most functions of Windows programs can be controlled through the keyboard, using the mouse is often easier for many chores.

550

Let Us C

From the programmer’s perspective, the consistent user interface results from using the Windows API functions for constructing menus and dialog boxes. All menus have the same keyboard and mouse interfaces because Windows—rather than the application program—handles this job.

Hardware Independent Programming
As we saw earlier a Windows program can always call Windows API functions. Thus an application can easily communicate with OS. What is new in Windows is that the OS can also communicate with application. Let us understand why it does so with the help of an example. Suppose we have written a program that contains a menu item, which on selection is supposed to display a string “Hello World” in the window. The menu item can be selected either using the keyboard or using the mouse. On executing this program it will perform the initializations and then wait for the user input. Sooner or later the user would press the key or click the mouse to select the menu-item. This key-press or mouse-click is known as an ‘event’. The occurrence of this event is sensed by the keyboard or mouse device driver. The device driver would now inform Windows about it. Windows would in turn notify the application about the occurrence of this event. This notification is known as a ‘message’. Thus the OS has communicated with the application. When the application receives the message it communicates back with the OS by calling a Windows API function to display the string “Hello World” in the window. This API function in turn communicates with the device driver of the graphics card (that drives the screen) to display the string. Thus there is a two-way communication between the OS and the application. This is shown in Figure 16.4.

Chapter 16: C Under Windows

551

Application API Call Message

Windows OS

Device Driver

Hardware

Figure 16.4 Suppose the keyboard and the mouse are now replaced with a new keyboard and mouse. Doing so would not affect the application at all. This is because at no time does the application carry out any direct communication with the devices. Any differences that may be there in the new set of mouse and keyboard would be handled the device driver and not by the application program. Similarly, if the screen or the graphics card is replaced no change would be required in the program. In short hardware independence at work! At times a change of device may necessitate a change in the device driver program, but never a change in the application.

Event Driven Model
When a user interacts with a Windows program a lot of events occur. For each event a message is sent to the program and the program reacts to it. Since the order in which the user would interact with the user-interface elements of the program cannot be predicted the order of occurrence of events, and hence the order of messages, also becomes unpredictable. As a result, the order of

552

Let Us C

calling the functions in the program (that react to different messages) is dictated by the order of occurrence of events. Hence this programming model is called ‘Event Driven Programming Model’. That’s really all that is there to event-driven programming. Your job is to anticipate what users are likely to do with your application’s user interface objects and have a function waiting, ready to execute at the appropriate time. Just when that time is, no one except the user can really say.

Windows Programming, a Closer Look
There can be hundreds of ways in which the user may interact with an application. In addition to this some events may occur without any user interaction. For example, events occur when we create a window, when the window’s contents are to be drawn, etc. Not only this, occurrence of one event may trigger a few more events. Thus literally hundreds of messages may be sent to an application thereby creating a chaos. Naturally, a question comes—in which order would these messages get processed by the application. Order is brought to this chaos by putting all the messages that reach the application into a ‘Queue’. The messages in the queue are processed in First In First Out (FIFO) order. In fact the OS maintains several such queues. There is one queue, which is common for all applications. This queue is known as ‘System Message Queue’. In addition there is one queue per application. Such queues are called ‘Application Message Queues’. Let us understand the need for maintaining so many queues. When we click a mouse and an event occurs the device driver posts a message into the System Message Queue. The OS retrieves this message finds out with regard to which application the message has been sent. Next it posts a message into the

Chapter 16: C Under Windows

553

Application Message Queue of the application in which the mouse was clicked. Refer Figure 16.5.

Event Device Driver Msg.

Event Device Driver Msg. System Msg. Queue

Other Mess

OS

Other Messa

Application1

Application1 Msg. Queue

Application2 Msg. Queue

Application2

Figure 16.5 I think now we have covered enough ground to be able to actually start C under Windows programming. Here we go…

554
The First Windows Program

Let Us C

To keep things simple we would begin with a program that merely displays a “Hello” message in a message box. Here is the program…
#include int _stdcall WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdline, int nCmdShow ) { MessageBox ( 0, “Hello!”, “Title”, 0 ) ; return ( 0 ) ; }

Naturally a question would come to your mind—how do I create and run this program and what output does it produce. Firstly take a look at the output that it produces. Here it is…

Figure 16.6 Let us now look at the steps that one needs to carry to create and execute this program: (a) Start VC++ from ‘Start | Programs | Microsoft Visual C++ 6.0’. The VC++ IDE window will get displayed. (b) From the File | New menu, select ‘Win32 Application’, and give a project name, say, ‘sample1’. Click on OK. (c) From the File | New menu, select ‘C++ Source File’, and give a suitable file name, say, ‘sample1’. Click on OK. (d) The ‘Win32 Application-Step 1 of 1’ window will appear. Select ‘An empty project’ option and click ‘Finish’ button.

Chapter 16: C Under Windows

555

(e) A ‘New Project Information’ dialog will appear. Close it by clicking on OK. (f) Again select ‘File | New | C++ Source File’. Give the file name as ‘sample1.c’. Click on OK. (g) Type the program in the ‘sample1.c’ file that gets opened in the VC++ IDE. (h) Save this file using ‘Save’ option from the File menu. To execute the program follow the steps mentioned below: (a) From the Build menu, select ‘Build sample1.exe’. (b) Assuming that no errors were reported in the program, select ‘Execute sample1.exe’ from the Build menu. Let us now try to understand the program. The way every C under DOS program begins its execution with main( ), every C under Windows program begins its execution with WinMain( ). Thus WinMain( ) becomes the entry point for a Windows program. A typical WinMain( ) looks like this: int __stdcall WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow )

Note the __stdcall before WinMain( ). It indicates the calling convention used by the WinMain( ) function. Calling Conventions indicate two things: (a) The order (left to right or right to left) in which the arguments are pushed onto the stack when a function call is made. (b) Whether the caller function or called function removes the arguments from the stack at the end of the call.

Out of the different calling conventions available most commonly used conventions are __cdecl and __stdcall . Both these calling conventions pass arguments to functions from right to left. In __cdecl the stack is cleaned up by the calling function, whereas in case of __stdcall the stack is cleaned up by the called function. All

556

Let Us C

API functions use __stdcall calling convention. If not mentioned, __cdecl calling convention is assumed by the compiler. HINSTANCE and LPSTR are nothing but typedefs. The first is an unsigned int and the second is a pointer to a char. These macros are defined in ‘windows.h’. This header file must always be included while writing a C program under Windows. hInstance, hPrevInstance, lpszCmdLine and nCmdShow are simple variable names. In place of these we can use i, j, k and l respectively. Let us now understand the meaning of these parameters as well as the rest of the program. − WinMain( ) receives four parameters which are as under: hInstance: This is the ‘instance handle’ for the running application. Windows creates this ID number when the application starts. We will use this value in many Windows functions to identify an application’s data. A handle is simply a 32-bit number that refers to an entity. The entity could be an application, a window, an icon, a brush, a cursor, a bitmap, a file, a device or any such entity. The actual value of the handle is unimportant to your programs, but the Windows module that gives your program the handle knows how to use it to refer to an entity. What is important is that there is a unique handle for each entity and we can refer and reach the entity only using its handle. hPrevInstance: This parameter is a remnant of earlier versions of Windows and is no longer significant. Now it always contains a value 0. It is being persisted with only to ensure backward compatibility. lpszCmdLine: This is a pointer to a character string containing the command line arguments passed to the program. This is similar to the argv, argc parameters passed to main( ) in a DOS program.

Chapter 16: C Under Windows

557

nCmdShow: This is an integer value that is passed to the function. This integer tells the program whether the window that it creates should appear minimized, as an icon, normal, or maximized when it is displayed for the first time. − − − The MessageBox( ) function pops up a message box whose title is ‘Title’ and which contains a message ‘Hello!’. Returning 0 from WinMain( ) indicates success, whereas, returning a nonzero value indicates failure. Instead of printing ‘Hello!’ in the message box we can print the command line arguments that the user may supply while executing the program. The command line arguments can be supplied to the program by executing it from Start | Run as shown in Figure 16.7.

Figure 16.7 Note from Figure 16.7 that ‘myapp.exe’ is the name of our application, whereas, ‘abc ijk xyz’ represents command line arguments. The parameter lpszCmdline points to the string “abc ijk xyz”. This string can be printed using the following statement:
MessageBox ( 0, lpszCmdline, "Title", 0 ) ;

If the entire command line including the filename is to be retrieved we can use the GetCommandLine( ) function.

558
Hungarian Notation

Let Us C

Hungarian Notation is a variable-naming convention so called in the honor of the legendary Microsoft programmer Charles Simonyi. According to this convention the variable name begins with a lower case letter or letters that denotes the data type of the variable. For example, the sz prefix in szCmdLine stands for ‘string terminated by zero’; the prefix h in hInstance stands for ‘handle’; the prefix n in nCmdShow stands for int. Prefixes are often combined to form other prefixes, as lpsz in lpszCmdLine stands for ‘long pointer to a zero terminated string’. Though basically this notation is a good idea nowadays its usage is discouraged. This is because when a transition happens from say a 16-bit code to 32-bit code then a whole lot of variable names have to be changed. For example, suppose the 16-bit code used 2-byte and 4-byte integer variables called wParam and lParam, where w indicated a 16-bit integer (word) and a 32-bit integer (long) respectively. When this code is ported to a 32-bit environment wParam had to be changed to lParam since in this environment every integer is 4 bytes long. You would agree that if we follow the Hungarian notation then we would have to make a whole lot of changes in the variable names when we port the code to a 32-bit or a 64-bit environment. Hence the usage of this convention is nowadays discouraged.

Summary
(a) Under Windows an integer is four bytes long. To use a twobyte integer pre-qualify it with short. (b) Under Windows a pointer is four bytes long. (c) Windows programming involves a heavy usage of typedefs. (d) DOS uses a Sequential Programming Model, whereas, Windows uses an Event Driven Programming Model. (e) Entry point of every Windows program is a function called WinMain( ).

Chapter 16: C Under Windows

559

(f) Windows does not permit direct access to memory or hardware devices. (g) Windows uses a Demand-based Virtual Memory Model to manage memory. (h) Under Windows there is two-way communication between the program and the OS. (i) Windows maintains a system message queue common for all applications. (j) Windows maintains an application message queue per running application. (k) Calling convention decides the order in which the parameters are passed to a function and whether the calling function or the called function clears the stack. (l) Commonly used calling conventions are __cdecl and __stdcall. (m) Hungarian notation though good its usage is not recommended any more.

Exercise
[A] State True or False:

(a) MS-DOS uses a procedural programming model. (b) A Windows program can directly call a device driver program for a device. (c) API functions under Windows do not have names. (d) DOS functions are called using an interrupt mechanism. (e) Windows uses a 4 GB virtual memory space. (f) Size of a pointer under Windows depends upon whether it is near or far. (g) Under Windows the address stored in a pointer is a virtual address and not a physical address. (h) One of the parameters of WinMain( ) called hPrevInstance is no longer relevant.

560
[B] Answer the following:

Let Us C

(a) Why is Event-driven Programming Model better than the Sequential Programming Model? (b) What is the meaning of different parts of the address stored in a pointer under Windows environment? (c) Why Windows does not permit direct access to hardware? (d) What is the difference between an event and a message? (e) Why Windows maintains a different message queue for each application? (f) In which different situations messages get posted into an application message queue?
[C] Attempt the following:

(a) Write a program that prints the value of hInstance in a message box. (b) Write a program that displays three buttons ‘Yes’, ‘No’ ‘Cancel’ in the message box. (c) Write a program that receives a number as a command line argument and prints its factorial value in a message box. (d) Write a program that displays command line arguments including file name in a message box.

17

Windows Programming
• • • • The Role of a Message Box Here comes the window… More Windows A Real-World Window Creation and Displaying of Window Interaction with Window Reacting to Messages • Program Instances • Summary • Exercise

561

562

Let Us C

E

event driven programming requires a change in mind set. I hope Chapter 16 has been able to bring about this change. However this change would be bolstered by writing event driven programs. This is what this chapter intends to do. I am hopeful that by the time you reach the end of this chapter you would be so comfortable with it as if you have been using it all your life.

The Role of a Message Box
Often we are required to display certain results on the screen during the course of execution of a program. We do this to ascertain whether we are getting the results as per our expectations. In a sequential DOS based program we can easily achieve this using printf( ) statements. Under Windows screen is a shared resource. So you can imagine what chaos would it create if all running applications are permitted to write to the screen. You would not be able to make out which output is of what application. Hence no Windows program is permitted to write anything directly to the screen. That’s where a message box enters the scene. Using it we can display intermediate results during the course of execution of a program. It can be dismissed either by clicking the ‘close button’ in its title bar or by clicking the OK button present in it. There are numerous variations that you can try with the MessageBox( ). Some of these are given below
MessageBox ( 0, “Are you sure”, “Caption”, MB_YESNO ) ; MessageBox ( 0, “Print to the Printer”, “Caption”, MB_YESNO CANCEL) ; MessageBox ( 0, “icon is all about style”, “Caption”, MB_OK | MB_ICONINFORMATION ) ;

You can put the above statements within WinMain( ) and see the results for yourself. Though the above message boxes give you flexibility in displaying results, button, icons, there is a limit to which you can stretch them. What if we want to draw a free hand drawing or display an image, etc. in the message box. This would

Chapter 17: Windows Programming

563

not be possible. To achieve this we need to create a full-fledged window. The next section discusses how this can be done.

Here Comes the window…
Before we proceed with the actual creation of a window it would be a good idea to identify the various elements of it. These are shown in Figure 17.1.
Minimize Box Close Butt on

Caption Bar Icon

Menu Vertical Client Area Scrol

Horizontal Scroll

Figure 17.1 Note that every window drawn on the screen need not necessarily have every element shown in the above figure. For example, a window may not contain the minimize box, the maximize box, the scroll bars and the menu. Let us now create a simple program that creates a window on the screen. Here is the program…
#include

564

Let Us C

int _stdcall WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow ) { HWND h ; h = CreateWindow ( “BUTTON”, “Hit Me”, WS_OVERLAPPEDWINDOW, 10, 10, 150, 100, 0, 0, i, 0 ) ; ShowWindow ( h, nCmdShow ) ; MessageBox ( 0, “Hi!”, “Waiting”, MB_OK ) ; return 0 ; }

Here is the output of the program…

Figure 17.2 Let us now understand the program. Every window enjoys certain properties—background color, shape of cursor, shape of icon, etc. All these properties taken together are known as ‘window class’. The meaning of ‘class’ here is ‘type’. Windows insists that a window class should be registered with it before we attempt to create windows of that type. Once a window class is registered we can create several windows of that type. Each of these windows would enjoy the same properties that have been registered through the window class. There are several predefined window classes. Some of these are BUTTON, EDIT, LISTBOX, etc. Our program has created one such window using the predefined BUTTON class.

Chapter 17: Windows Programming

565

To actually create a window we need to call the API function CreateWindow( ). This function requires several parameters starting with the window class. The second parameter indicates the text that is going to appear on the button surface. The third parameter specifies the window style. WS_OVERLAPPEDWINDOW is a commonly used style. The next four parameters specify the window’s initial position and size—the x and y screen coordinates of the window’s top left corner and the window’s width and height in pixels. The next three parameters specify the handles to the parent window, the menu and the application instance respectively. The last parameter is the pointer to the window-creation data. We can easily devote a section of this book to CreateWindow( ) and its parameters. But don’t get scared of it. Nobody is supposed to remember all the parameters, their meaning and their order. You can always use MSDN (Microsoft Developer Network) help to understand the minute details of each parameter. This help is available as part of VC++ 6.0 product. It is also available on the net at http://www.msdn.microsoft.com/library. Note that CreateWindow( ) merely creates the window in memory. We still are to display it on the screen. This can be done using the ShowWindow( ) API function. CreateWindow( ) returns handle of the created window. Our program uses this handle to refer to the window while calling ShowWindow( ). The second parameter passed to ShowWindow( ) signifies whether the window would appear minimized, maximized or normal. If the value of this parameter is SW_SHOWNORMAL we get a normal sized window, if it is SW_SHOWMINIMIZED we get a minimized window and if it is SW_SHOWMINIMIZED we get a maximized window. We have passed nCmdShow as the second parameter. This variable contains SW_SHOWNORMAL by default. Hence our program displays a normal sized window.

566

Let Us C

The WS_OVERLAPPEDWINDOW style is a collection of the following styles:
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX

As you can make out from these macros they essentially control the look and feel of the window being created. All these macros are #defined in the ‘Windows.h’ header file. On executing this program a window and a message box appears on the screen as shown in the Figure 17.2. The window and the message box disappear as soon as we click on OK. This is because on doing so execution of WinMain( ) comes to an end and moreover we have made no provision to interact with the window. You can try to remove the call to MessageBox( ) and see the result. You would observe that no sooner does the window appear it disappears. Thus a call to MessageBox( ) serves the similar purpose as getch( ) does in sequential programming.

More Windows
Now that we know how to create a window let us create several windows on the screen. The program to do this is given below.
#include int _stdcall WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow ) { HWND h[10] ; int x ; for ( x = 0 ; x ( r.right - wd ) ) { x = r.right - wd ; dx = -10 ; PlaySound ("chord.wav", NULL, SND_FILENAME | SND_ASYNC ) ; } y += dy ; if ( y < 0 ) { y=0; dy = 10 ; PlaySound ("chord.wav", NULL, SND_FILENAME | SND_ASYNC ) ; } else if ( y > ( r.bottom - ht ) ) { y = r.bottom - ht ; dy = -10 ; PlaySound ( "chord.wav", NULL, SND_FILENAME | SND_ASYNC ); } BitBlt ( hdc, x, y, wd, ht, hmemdc, 0, 0, SRCCOPY ) ; ReleaseDC ( hWnd, hdc ) ; }

From the WndProc( ) function you can observe that we have handled two new messages here—WM_CREATE and WM_TIMER. For these messages we have called the handlers OnCreate( ) and OnTimer( ) respectively. Let us now understand these handlers one by one

WM_CREATE and OnCreate( )

Chapter 18: Graphics Under Windows

611

The WM_CREATE message arrives whenever a new window is created. Since usually a window is created only once, the one-time activity that is to be carried out in a program is usually done in OnCreate( ) handler. In our program to make the ball move we need to display it at different places at different times. To do this it would be necessary to blit the ball image several times. However, we need to load the image only once. As this is a one-time activity it has been done in the handler function OnCreate( ). You are already familiar with the steps involved in preparing the image for blitting—loading the bitmap, creating a memory DC, making it compatible with screen DC and selecting the bitmap in the memory DC. Apart from preparing the image for blitting we have also done some intialialisations like setting up values in some variables to indicate the initial position of the ball. We have also called the SetTimer( ) function. This function tells Windows to post a message WM_TIMER into the message queue of our application every 50 milliseconds.

WM_TIMER and OnTimer( )
If we are to perform an activity at regular intervals we have two choices: (a) Use a loop and monitor within the loop when is it time to perform that activity. (b) Use a Windows mechanism of timer. This mechanism when used posts a WM_TIMER message at regular intervals to our application. The first method would seriously hamper the responsiveness of the program. If the control is within the loop and a new message arrives the message would not get processed unless the control goes out of the loop. The second choice is better because it makes the program event driven. That is, whenever WM_TIMER arrives

612

Let Us C

that time its handler does the job that we want to get executed periodically. At other times the application is free to handle other messages that come to its queue. All that we have done in the OnTimer( ) handler is erase the ball from previous position and draw it at a new position. We have also checked if the ball has hit the boundaries of the window. If so we have played a sound file using the PlaySound( ) API function and then changed the direction of the ball.

A Few More Points…
A few more points worth noting before we close our discussion on animation… (a) One application can set up multiple timers to do different jobs at different intervals. Hence we need to pass the id of the timer that we want to set up to the SetTimer( ) function. In our case we have specified the id as 1. (b) For multiple timers Windows would post multiple WM_TIMER messages. Each time it would pass the timer id as additional information about the message. (c) For drawing as well as erasing the ball we have used the same function—BitBlt( ). While erasing we have used the raster operation code WHITENESS. When we use this code the color values of the source pixels get ignored. Thus red colored pixels of ball would get ignored leading to erasure of the ball in the window. (d) The size of client area of the window can be obtained using the GetClientRect( ) API function. (e) We want that every time we run the application the initial position of the ball should be different. To ensure this we have generated its initial x, y coordinates using the standard library function rand( ). However, this function doesn’t

Chapter 18: Graphics Under Windows

613

generate true random numbers. To ensure that we do get true random numbers, somehow we need to tie the random number generation with time, as time of each execution of our program would be different. This has been achieved by making the call srand ( time ( NULL ) ) ;

Here time( ) is function that returns the time. We have further passed this time to the srand( ) function. (f) To be able to use rand( ) and srand( ) functions include the file ‘stdlib.h’. Similarly for time( ) function to work include the file ‘time.h’. (g) In the call to the PlaySound( ) function the first parameter is the name of the wave file that is to be played. If first parameter is filename then the second has to be NULL. The third parameter is a set of flags. SND_FILENAME indicates that the first parameter is the filename, whereas SND_ASYNC indicates that the sound should be played in the background. (h) To be able to use the PlaySound( ) function we need to link the library ‘winmm.lib’. This is done by using ‘Project | Settings’ menu item. On selection of this item a dialog pops up. In the ‘Link’ tab of this dialog mention the name ‘winmm.lib’ in the ‘Object / Library modules’ edit box. (i) When the application terminates we have to instruct Windows not to send WM_TIMER messages to our application any more. For this we have called the KillTimer( ) API function passing to it the ID of the timer.

Windows, the Endless World…
The biggest hurdle in Windows programming is a sound understanding of its programming model. In this chapter and in the

614

Let Us C

last two I have tried to catch the essence of Windows’ Event Driven Programming model. Once you have understood it thoroughly rest is just a matter of understanding and calling the suitable API functions to get your job done. Windows API is truly an endless world. It covers areas like Networking, Internet programming, Telephony, Drawing and Printing, Device I/O, Imaging, Messaging, Multimedia, Windowing, Database programming, Shell programming, to name a few. The programs that we have written have merely scratched the surface. No matter how many programs that we write under Windows, several still remain to be written. The intention of this chapter was to unveil before you, to give you the first glimpse of what is possible under Windows. The intention all along was not to catch fish for you but to show you how to catch fish so that you can do fishing all your life. Having made a sound beginning, rest is for you to explore. Good luck and happy fishing!

Summary
(a) In DOS, programmers had to write separate graphics code for every new video adapter. In Windows, the code once written works on any video adapter. (b) A Windows program cannot draw directly on an output device like screen or printer. Instead, it draws to the logical display surface using device context. (c) When the window is displayed for the first time, or when it is moved or resized OnPaint( ) handler gets called. (d) It is necessary to obtain the device context before drawing text or graphics in the client area. (j) A device context is a structure containing information required to draw on a display surface. The information includes color of pen and brush, screen resolution, color palettes, etc. (e) To draw using a new pen or brush it is necessary to select them into the device context.

Chapter 18: Graphics Under Windows

615

(f) If we don’t select any brush or pen into the device context then the drawing drawn in the client area would be drawn with the default pen (black pen) and default brush (white brush). (g) RGB is a macro representing the Red, Green and Blue elements of a color. RGB ( 0, 0, 0 ) gives black color, whereas, RGB ( 255, 255, 255 ) gives white color. (h) Animation involves repeatedly drawing the same image at successive positions.

Exercise
[A] State True or False:

(a) Device independence means the same program is able to work using different screens, keyboards and printers without modifications to the program. (b) The WM_PAINT message is generated whenever the client area of the window needs to be redrawn. (c) The API function EndPaint( ) is used to release the DC. (d) The default pen in the DC is a solid pen of white color. (e) The pen thickness for the pen style other than PS_SOLID has to be 1 pixel. (f) BeginPaint( ) and GetDC( ) can be used interchangeably. (g) If we drag the mouse from (10, 10) to (110, 100), 100 WM_MOUSEMOVE messages would be posted into the message queue. (h) WM_PAINT message is raised when the window contents are scrolled. (i) With each DC a default monochrome bitmap of size 1 pixel x 1 pixel is associated. (j) The WM_CREATE message arrives whenever a window is displayed. [B] Answer the following: (a) What is meant by Device Independent Drawing and how it is achieved?

616
(b) Explain the significance of WM_PAINT message.

Let Us C

(c) How Windows manages the code and various resources of a program? (d) Explain the Windows mechanism of timer. (e) What do you mean by capturing a mouse? (f) Write down the steps that need to be carried out to animate an object.
[C] Attempt the following:

(a) Write a program, which displays "hello" at any place in the window where you click the left mouse button. If you click the right mouse button the color of subsequent hellos should change. (b) Write a program that would draw a line by joining the new point where you have clicked the left mouse button with the last point where you clicked the left mouse button. (c) Write a program to gradient fill the entire client area with shades of blue color. (d) Write a program to create chessboard like boxes (8 X 8) in the client area. If the window is resized the boxes should also get resized so that all the 64 boxes are visible at all times. (e) Write a program that displays only the upper half of a bitmap of size 40 x 40. (f) Write a program that displays different text in different colors and fonts at different places after every 10 seconds.

19

Interaction With Hardware
• • • • • • • • • • • • • Hardware Interaction Hardware Interaction, DOS Perspective Hardware Interaction, Windows Perspective Communication with Storage Devices The ReadSector( ) Function Accessing Other Storage Devices Communication with Keyboard Dynamic Linking Windows Hooks Caps Locked, Permanently Did You Press It TTwwiiccee…. Mangling Keys KeyLogger Where is This Leading Summary Exercise

617

618

Let Us C

T

here are two types of Windows programmers those who are happy in knowing the things the way they are under Windows and those who wish to know why the things are the way they are. This chapter is for the second breed of programmers. They are the real power users of Windows. Because it is they who first understand the default working of different mechanisms that Windows uses and then are able to make those mechanisms work to their advantage. The focus here would be restricted to mechanisms that are involved in interaction with the hardware under the Windows world. Read on and I am sure you would be on your path to become a powerful Windows programmer.

Hardware Interaction
Primarily interaction with hardware suggests interaction with peripheral devices. However, its reach is not limited to interaction with peripherals. The interaction may also involve communicating with chips present on the motherboard. Thus more correctly, interaction with hardware would mean interaction with any chip other than the microprocessor. During this interaction one or more of the following activities may be performed: (a) Reacting to events that occur because of user’s interaction with the hardware. For example, if the user presses a key or clicks the mouse button then our program may do something. (b) Reacting to events that do not need explicit user’s interaction. For example, on ticking of a timer our program may want to do something. (c) Explicit communication from a program without the occurrence of an event. For example, a program may want to send a character to the printer, or a program may want to read/write the contents of a sector from the hard disk.

Chapter 19: Interaction With Hardware

619

Let us now see how this interaction is done under different platforms.

Hardware Interaction, DOS Perspective
Under DOS whenever an external event (like pressing a key or ticking of timer) occurs a signal called hardware interrupt gets generated. For different events there are different interrupts. As a reaction to the occurrence of an interrupt a table called Interrupt Vector Table (IVT) is looked up. IVT is present in memory. It is populated with addresses of different BIOS routines during booting. Depending upon which interrupt has occurred the Microprocessor picks the address of the appropriate BIOS routine from IVT and transfers execution control to it. Once the control reaches the BIOS routine, the code in the BIOS routine interacts with the hardware. Naturally, for different interrupts different BIOS routines are called. Since these routines serve the interrupts they are often called ‘Interrupt Service Routines’ or simply ISRs. Refer Figure 19.1 to understand this mechanism.

620

Let Us C

Key hit / Mouse click generates an interrupt Microprocessor Microprocessor looks up IVT Suitable ISR is called ISR1

Address of ISR1 IVT

Address of ISR2

ISR2

BIOS Routines

Figure 19.1 If we want that instead of the default ISR our routine should get called then it is necessary to store the address of this routine in IVT. Once this is done whenever a hardware interrupt occurs our routine’s address from IVT is picked up and the control is transferred to our routine. For example, we may register our ISR in IVT to gain finer control over the way key-hits from the keyboard are tackled. This finer control may involve changing codes of keys or handling hitting of multiple keys simultaneously. Explicit communication with the hardware can be done in four different ways. These are shown in Figure 19.2.

Chapter 19: Interaction With Hardware

621

C Program

BIOS Functions

DOS Functions

Library Functions

Direct Interaction

Hardware

Figure 19.2 Let us now discuss the pros and cons of using these different methods to interact with the hardware. (a) Calling DOS Functions To interact with the hardware a program can call DOS functions. These functions can either directly interact with the hardware or they may call BIOS functions which in turn interact with the hardware. As a result, the programmer is not required to know all the hardware details to be able to interact with it. However, since DOS functions do not have names they have to be called through the mechanism of interrupts. This is difficult since the programmer has to remember interrupt service numbers for calling different DOS functions. Moreover, communication with these functions has to be done using CPU registers. This leads to lot of difficulties since different functions use different registers for communication. So one has to know details of different CPU registers, how to use them, which one to use when, etc. (b) Calling BIOS Functions

622

Let Us C

DOS functions can carry out jobs like console I/O, file I/O, printing, etc. For other operations like generating graphics, carrying out serial communication, etc. the program has to call another set of functions called ROM-BIOS functions. Note that there are some functions in ROM-BIOS that do same jobs as equivalent DOS functions. BIOS functions suffer from the same difficulty as DOS functions—they do not have names. Hence they have to be called using interrupts and involve heavy usage of registers. (c) Calling Library Functions We can call library functions which in turn can call DOS/BIOS functions to carry out the interaction with hardware. Good examples of these functions are printf( ) / scanf( ) / getch( ) for interaction with console, absread( ) / abswrite( ) for interaction with disk, bioscom( ) for interaction with serial port, etc. But the library doesn’t have a parallel function for every DOS/BIOS function. Hence at some point of time one has to learn how to call DOS/BIOS functions. (d) Directly interacting with the hardware At times the programs are needed to directly interact with the hardware. This has to be done because either there are no library functions or DOS/BIOS functions to do this, or if they are there their reach is limited. For example, while writing good video games one is required to watch the status of multiple keys simultaneously. The library functions as well as the DOS/BIOS functions are unable to do this. At such times we have to interact with the keyboard controller chip directly. However, direct interaction with the hardware is difficult because one has to have good knowledge of technical details of the chip to be able to do so. Moreover, not every technical detail about how the hardware from a particular manufacturer works is well documented.

Chapter 19: Interaction With Hardware

623

Hardware Interaction, Windows Perspective
Like DOS, under Windows too a hardware interrupt gets generated whenever an external event occurs. As a reaction to this signal a table called Interrupt Descriptor Table (IDT) is looked up and a corresponding routine for the interrupt gets called. Unlike DOS the IDT contains addresses of various kernel routines (instead of BIOS routines). These routines are part of the Windows OS itself. When the kernel routine is called, it in turn calls the ISR present in the appropriate device driver. This ISR interacts with the hardware. Two questions may now occur to you: (a) Why the kernel routine does not interact with the hardware directly? (b) Why the ISR of the device driver not registered directly in the IDT? Let us find answer to the first question. Every piece of hardware works differently than the other. As new pieces of hardware come into existence new code has to be written to be able to interact with them. If this code is written in the kernel then the kernel would have to be rewritten and recompiled every time a new hardware comes into existence. This is practically impossible. Hence the new code to interact with the device is written in a separate program called device driver. With every new piece of hardware a new device driver is provided. This device driver is an extension of the OS itself. Let us now answer the second question. Out of the several components of Windows OS a component called kernel is tightly integrated with the processor architecture. If the processor architecture changes then the kernel is bound to change. One of goals of Windows NT family was to keep the other components of OS and the device drivers portable across different microprocessor architectures. All processor architectures may not use IDT for the registration and lookup mechanism. So, had registration of the device driver’s ISR in IDT been allowed, then the mechanism

624

Let Us C

would fail on processors which do not use IDT, thereby compromising portability of device drivers. Refer Figure 19.3 for understanding the interrupt handling mechanism under Windows.

Key hit / Mouse click generates an interrupt Microprocessor Microprocessor looks up IDT Suitable ISR is called ISR Device Driver Suitable ISR is called ISR Device Driver

Address of ISR1

Address of ISR2

Suitable Kernel routine is Kernel called routine1

IDT

Kernel routine2

Figure 19.3 If we are to gain finer control while reacting to interrupts we would be required to write a device driver containing a new ISR to do so. Under Windows explicit communication with hardware is much different than the way it was done under DOS. This is primarily because under Windows every device is shared amongst multiple applications running in memory. To avoid conflict between different programs accessing the same device simultaneously

Chapter 19: Interaction With Hardware

625

Windows does not permit an application program to directly access any of the devices. Instead it provides several API functions to carry out the interaction. These functions have names so calling them is much easier than calling DOS/BIOS functions. When we call an API function to interact with a device, it in turn accesses the device driver program for the device. It is the device driver program that finally accesses the device. There is a standard way in which an application can communicate with the device driver. It is device driver’s responsibility to ensure that multiple requests coming from different applications are handled without causing any conflict. In the sections to follow we would see how to communicate with the device driver to be able to interact with the hardware. One last question—won’t the API change if a new device comes into existence? No it won’t. That is the beauty of the Windows architecture. All that would change is the device driver program for the new device. The API functions that we would need to interact with this new device driver would remain same. This is shown in Figure 19.4

C Program

Windows API

Device Driver

Hardware

Figure 19.4

626
Communication with Storage Devices

Let Us C

Since DOS is commercially dead the rest of the chapter would focus on communication with the devices under Windows platform. We would illustrate this with the help of several programs. Let us begin with the one that interacts with the simplest storage device, namely the floppy disk. Rather than the physical structure of the floppy disk it is the way the stored information is laid out and managed that concerns programmers most. Let us understand how the information is laid out on a floppy disk. Each floppy disk consists of four logical parts—Boot Sector, File Allocation Table (FAT), Directory and Data space. Of these, the Boot Sector contains information about how the disk is organized. That is, how many sides does it contain, how many tracks are there on each side, how many sectors are there per track, how many bytes are there per sector, etc. The files and the directories are stored in the Data Space. The Directory contains information about the files like its attributes, name, size, etc. The FAT contains information about where the files and directories are stored in the data space. Figure 19.5 shows the four logical parts of a 1.44 MB disk.

Chapter 19: Interaction With Hardware

627
14

13 12 11 10 9 8 F2 F2 F1

14

15 16 12 D F2 11 17 F2 D F2 18 10 D

13

15

F2 F2 F2 DS

F1 F1

BS 1 F1 F1 F1 2 F1 F1 F1 3 7 4 6 5 Side 0, Track 0 BS - Boot Sector F2 - Second copy of FAT DS - Data space

16 DS 17 DS DS F2 18 D 1 9 D D D 8 D D 2 D D D 3 7 4 6 5 D D DS Side 0, Track 1

F1 - First copy of FAT D - Root directory structure

Figure 19.5 With the logical structure of the floppy disk behind us let us now write a program that reads the boot sector of a floppy disk and displays its contents on the screen. But why on earth would we ever like to do this? Well, that’s what all Windows-based Antiviral softwares do when they scan for boot sector viruses. A good enough reason for us to add the capability to read a boot sector to our knowledge! Here is the program…
# include # include # include # include # pragma pack ( 1 ) struct boot { BYTE jump [ 3 ] ;

628 char bsOemName [ 8 ] ; WORD bytesperSector ; BYTE sectorspercluster ; WORD sectorsreservedarea ; BYTE copiesFAT ; WORD maxrootdirentries ; WORD totalSectors ; BYTE mediaDescriptor ; WORD sectorsperFAT ; WORD sectorsperTrack ; WORD sides ; WORD hiddenSectors ; char reserve [ 480 ] ; }; void ReadSector ( char*src, int ss, int num, void* buff ) ; void main( ) { struct boot b ; ReadSector ( "\.\A:", 0, 1, &b ) ;

Let Us C

printf ( "Boot Sector name: %s\n", b.id ) ; printf ( "Bytes per Sector: %d\n", b.bps ) ; printf ( "Sectors per Cluster: %d\n", b.spc ) ; /* rest of the statements can be written by referring Figure 19.6 and Appendix G*/ } void ReadSector ( char *src, int ss, int num, void* buff ) { HANDLE h ; unsigned int br ; h = CreateFile ( src, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0 ) ; SetFilePointer ( h, ( ss * 512 ), NULL, FILE_BEGIN ) ; ReadFile ( h, buff, 512 * num, &br, NULL ) ) CloseHandle ( h ) ;

Chapter 19: Interaction With Hardware
}

629

The boot sector contains two parts—‘Boot Parameters’ and ‘Disk Bootstrap Program’. The Boot Parameters are useful while performing read/write operations on the disk. Figure 19.6 shows the break up of the boot parameters for a floppy disk.

Description Jump instruction OEM name Bytes per sector Sectors per cluster Reserved sectors Number of FAT copies Max. Root directory entries Total sectors Media descriptor Sectors per FAT Sectors per track No. Of sides Hidden sectors Huge sectors BIOS drive number Reserved sectors Boot signature Volume ID Volume label File system type

Length 3 8 2 1 2 1 2 2 1 2 2 2 4 4 1 1 1 4 11 8

Typical Values EB3C90 MSWIN4.1 512 1 1 2 224 2880 F0 9 18 2 0 0 0 0 41 349778522 ICIT FAT12

Figure 19.6

630

Let Us C

Using the breakup of bytes shown in Figure 19.6 our program has first created a structure called boot. Notice the usage of #pragma pack to ensure that all elements of the structure are aligned on a 1byte boundary, rather than the default 4-byte boundary. Then comes the surprise—there is no WinMain( ) in the program. This is because we want to display the boot sector contents on the screen rather than in a window. This has been done only for the sake of simplicity. Remember that our aim is to interact with the floppy, and not in drawing and painting in a window. If you wish you can of course adapt this program to display the same contents in a window. So the program is still a Windows application. Only difference is that it is built as a ‘Win32 Console Application’ using VC++. A console application always begins with main( ) rather than WinMain( ). To actually read the contents of boot sector of the floppy disk the program makes a call to a user-defined function called ReadSector( ). The ReadSector( ) function is quite similar to the absread( ) library function available in Turbo C/C++ under DOS. The first parameter passed to ReadSector( ) is a string that indicates the storage device from where the reading has to take place. The syntax for this string is \machine-name\storagedevice name. In \.\A:, we have used ‘.’ for the machine name. A ‘.’ means the same machine on which the program is executing. Needless to say, A: refers to the floppy drive. The second parameter is the logical sector number. We have specified this as 0 which means the boot sector in case of a floppy disk. The third parameter is the number of sectors that we wish to read. This parameter is specified as 1 since the boot sector occupies only a single sector. The last parameter is the address of a buffer/variable that would collect the data that is read from the floppy. Here we have passed the address of the boot structure variable b. As a result, the structure variable would be setup with the contents of the boot sector data at the end of the function call.

Chapter 19: Interaction With Hardware

631

Once the contents of the boot sector have been read into the structure variable b we have displayed the first few of them on the screen using printf( ). If you wish you can print the rest of the contents as well.

The ReadSector( ) Function
With the preliminaries over let us now concentrate on the real stuff in this program, i.e. the ReadSector( ) function. This function begins by making a call to the CreateFile( ) API function as shown below: h = CreateFile ( src, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0 ) ;

The CreateFile( ) API function is very versatile. Anytime we are to communicate with a device we have to firstly call this API function. The CreateFile( ) function opens the specified device as a file. Windows treats all devices just like files on disk. Reading from this file means reading from the device. The CreateFile( ) API function takes several parameters. The first parameter is the string specifying the device to be opened. The second parameter is a set of flags that are used to specify the desired access to the file (representing the device) about to be opened. By specifying the GENERIC_READ flag we have indicated that we just wish to read from the file (device). The third parameter specifies the sharing access for the file (device). Since floppy drive is a shared resource across all the running applications we have specified the FILE_SHARE_READ flag. In general while interacting with any hardware the sharing flag for the file (device) must always be set to this value since every piece of hardware is shared amongst all the running applications. The fourth parameter indicates security access for the file (device). Since we are not concerned with security here we have specified the value as 0. The fifth parameter specifies what action to take if

632

Let Us C

the file already exists. When using CreateFile( ) for device access we must always specify this parameter as OPEN_EXISTING. Since the floppy disk file was already opened by the OS a long time back during the booting. The remaining two parameters are not used when using CreateFile( ) API function for device access. Hence we have passed a 0 value for them. If the call to CreateFile( ) succeeds then we obtain a handle to the file (device). The device file mechanism allows us to read from the file (device) by setting the file pointer using the SetFilePointer( ) API function and then reading the file using the ReadFile( ) API function. Since every sector is 512 bytes long, to read from the nth sector we need to set the file pointer to the 512 * n bytes from the start of the file. The first parameter to SetFilePointer( ) is the handle of the device file that we obtained by calling the CreateFile( ) function. The second parameter is the byte offset from where the reading is to begin. This second parameter is relative to the third parameter. We have specified the third parameter as FILE_BEGIN which means the byte offset is relative to the start of the file. To actually read from the device file we have made a call to the ReadFile( ) API function. The ReadFile( ) function is very easy to use. The first parameter is the handle of the file (device), the second parameter is the address of a buffer where the read contents should be dumped. The third parameter is the count of bytes that have to be read. We have specified the value as 512 * num so as to read num sectors. The fourth parameter to ReadFile( ) is the address of an unsigned int variable which is set up with the count of bytes that the function was successfully able to read. Lastly, once our work with the device is over we should close the file (device) using the CloseHandle( ) API function. Though ReadSector( ) doesn’t need it, there does exist a counterpart of the ReadFile( ) function. Its name is WriteFile( ). This API function can be used to write to the file (device). The parameters of WriteFile( ) are same as that of ReadFile( ). Note

Chapter 19: Interaction With Hardware

633

that when WriteFile( ) is to be used we need to specify the GENERIC_WRITE flag in the call to CreateFile( ) API function. Given below is the code of WriteSector( ) function that works exactly opposite to the ReadSector( ) function. void WriteSector ( char *src, int ss, int num, void* buff ) { HANDLE h ; unsigned int br ; h = CreateFile ( src, GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0 ) ; SetFilePointer ( h, ( ss * 512 ), NULL, FILE_BEGIN ) ; WriteFile ( h, buff, 512 * num, &br, NULL ) ) CloseHandle ( h ) ; }

Accessing Other Storage Devices
Note that the mechanism of reading from or writing to any device remains standard under Windows. We simply need to change the string that specifies the device. Here are some sample calls for reading/writing from/to various devices:
ReadSector ( "\.\a:", 0, 1, &b ) ; /* reading from 2nd floppy drive */ ReadSector ( "\.\d:", 0, 1, buffer ) ; /* reading from a CD-ROM drive */ WriteSector ( "\.\c:", 0, 1, &b ) ; /* writing to a hard disk */ ReadSector ( "\.\physicaldrive0", 0, 1, &b ) ; /* reading partition table */

Here are a few interesting points that you must note. (a) If we are to read from the second floppy drive we should replace A: with B: while calling ReadSector( ). (b) To read from storage devices like hard disk drive or CD-ROM or ZIP drive, etc. use the string with appropriate drive letter. The string can be in the range \.\C: to \.\Z:.

634

Let Us C

(c) To read from the CD-ROM just specify the drive letter of the drive. Note that CD-ROMs follow a different storage organization known as CD File System (CDFS). (d) The hard disk is often divided into multiple partitions. Details like the place at which each partition begins and ends, the size of each partition, whether it is a bootable partition or not, etc. are stored in a table on the disk. This table is often called ‘Partition Table’. If we are to read the partition table contents we can do so by using the string \.\physicaldrive0. (e) Using \.\physicaldrive0 we can also read contents of any other parts of the disk. Here 0 represents the first hard disk in the system. If we are to read from the second hard disk we need to use 1 in place of 0.

Communication with Keyboard
Like mouse messages there also exist messages for keyboard. These are WM_KEYDOWN, WM_KEYUP and WM_CHAR. Of these, WM_KEYDOWN and WM_KEYUP are sent to an application (which has the input focus) whenever the key is pressed and released respectively. The additional information in case of these messages is the code of the key being pressed or released. When we tackle WM_KEYDOWN or WM_KEYUP we need to ourselves check the status of toggle keys like NumLock and CapsLock and shift keys like Ctrl, Alt and Shift. If we wish to avoid all this checking we can tackle the WM_CHAR message instead. What is mentioned above is the normal procedure followed by most Windows applications. However, if we wish to go a step further and deal with the keyboard we need to tackle it differently. For example, suppose we are to perform one of the following jobs: (a) Once you hit any key CapsLock should become on. Once it becomes on it should remain permanently on.

Chapter 19: Interaction With Hardware

635

(b) If we hit a key once it should appear twice on the screen. (c) If we hit a key A then B should appear on the screen, if we hit a B then C should occur and so on. Note that all these effects should work on a system-wide basis for all Win32 applications. To be able to achieve these effect we need understand two important mechanisms—‘Dynamic Linking’ and ‘Windows Hooks’. Let us understand these mechanisms one by one.

Dynamic Linking
As we saw in Chapter 16, Windows permits linking of libraries stored in a .DLL file during execution. A .DLL file is a binary file that cannot execute on its own. It contains functions that can be shared between several applications running in memory.

Windows Hooks
As the name suggests, the hook mechanism permits us to intercept and alter the flow of messages in the OS before they reach the application. Since hooks are used to alter the messaging mechanism on a system-wide basis the code for hooking has to be written in a DLL. The hooking mechanism involves writing a hook procedure in a DLL file and registering this procedure with the OS. Since the DLL cannot execute on its own we need a separate program that would load and execute the DLL. For different messages there are different types of hooks. For example, for keyboard messages there is a keyboard hook, for mouse messages there is mouse hook, etc. You can refer MSDN for nearly a dozen more types of hooks. Here we would restrict our discussion only to the keyboard hook.

636

Let Us C

Before we proceed to write our own hook procedure let us understand the normal working of the keyboard messages. This is illustrated in Figure 19.7.

Interrupt Kernel Routine Obtain key code by interacting with KB controller

Device Driver ISR Place key code in System Msg. queue by calling keybd_event( )

System Msg. queue

OS

Application1

Application1 Msg. Queue

Application2 Msg. Queue

Application2

Figure 19.7 With reference to Figure 19.7 here is a list of steps that are carried out when we press a key from the keyboard:

Chapter 19: Interaction With Hardware

637

(a) On pressing a key an interrupt occurs and the corresponding kernel routine gets called. (b) The kernel routine calls the ISR of the keyboard device driver. (c) The ISR communicates with the keyboard controller and obtains the code of the key pressed. (d) The ISR calls a OS function keybd_event( ) to post the key code to the System Message Queue. (e) The OS retrieves the message from the System Message Queue and posts it into the message queue of the application with regard to which the key has been pressed. Let us now see what needs to be done if we are to alter this procedure. We simply need to register our hook procedure with the OS. As a result, our hook procedure would receive the message before it is dispatched to the appropriate Application Message Queue. Since our hook procedure gets a first shot at the message it can now alter the working in the following three ways: (a) It can suppress the message altogether (b) It can change the message (c) It can post more messages into the System Message Queue using the keybd_event( ) function. Let us now put all this theory into practice by writing a few programs.

Caps Locked, Permanently
Let us now write a program that keeps the CapsLock permanently on. This effect would come into being when the first key is hit subsequent to the execution of our program. In fact there would be two programs: (a) A DLL containing a hook procedure that achieves the CapsLock effect. (b) An application EXE which loads the DLL in memory. Given below is the source code of the DLL program.

638
/* hook.c */ # include static HHOOK hkb = NULL ; HANDLE h ;

Let Us C

BOOL __stdcall DllMain ( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { h = hModule ; return TRUE ; } BOOL __declspec ( dllexport ) installhook( ) { hkb = SetWindowsHookEx ( WH_KEYBOARD, ( HOOKPROC ) KeyboardProc, ( HINSTANCE ) h, 0 ) ; if ( hkb == NULL ) return FALSE ; return TRUE ; } LRESULT __declspec ( dllexport ) __stdcall KeyboardProc ( int nCode, WPARAM wParam, LPARAM lParam ) { short int state ; if ( nCode < 0 ) return CallNextHookEx ( hkb, nCode, wParam, lParam ) ; if ( ( nCode == HC_ACTION ) && ( ( DWORD ) lParam & 0x40000000 ) ) { state = GetKeyState ( VK_CAPITAL ) ; if ( (state & 1 )== 0) /* if off */

Chapter 19: Interaction With Hardware
{

639

keybd_event ( VK_CAPITAL , 0, KEYEVENTF_EXTENDEDKEY, 0 ) ; keybd_event ( VK_CAPITAL , 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 ) ; } } return CallNextHookEx ( hkb, nCode, wParam, lParam ) ; } BOOL __declspec ( dllexport ) removehook( ) { return UnhookWindowsHookEx ( hkb ) ; }

Follow the steps mentioned below to create this program: (a) Select ‘File | New’ option to start a new project in VC++. (b) From the ‘Project’ tab select ‘Win32 Dynamic-Link Library’ and click on the ‘Next’ button. (c) In the ‘Win32 Dynamic-link Library Step 1 of 1’ select “An empty DLL project” and click on the ‘Finish’ button. (d) Select ‘File | New’ option. (e) From the ‘File’ tab select ‘C++ source file’ and give the file name as ‘hook.c’. Type the code listed above in this file. (f) Compile the program to generate the .DLL file. Note that this program doesn’t contain WinMain( ) since the program on compilation should not execute on its own. It has been replaced by a function called DllMain( ). This function acts as entry point of the DLL program. It gets called when the DLL is loaded or unloaded. When the application loads the DLL the DllMain( ) function would be called. In this function we have merely stored the handle to the DLL that has been loaded in memory into a global variable h for later use.

640

Let Us C

Those functions in a DLL that can be called from outside it are called exported functions. Our DLL contains three such functions—installhook( ), removehook( ) and KeyboardProc( ). To indicate to the compiler that a function in a DLL is an exported function we have to pre-qualify it with __declspec ( dllexport ). These functions would be called from the second program. This second program is a normal GUI application created in the same way that we did applications in Chapters 17 and 18. The handlers for messages WM_CREATE and WM_DESTROY are given below:
/* capslocked.c */ HINSTANCE h ; void OnCreate ( HWND hWnd ) { BOOL ( CALLBACK *p )( ) ; h = LoadLibrary ( "hook.dll" ) ; if ( h != NULL ) { p = GetProcAddress ( h, "installhook" ) ; ( *p )( ) ; /* calls installhoook( ) function */ } } void OnDestroy ( HWND hWnd ) { BOOL ( CALLBACK *p )( ) ; p = GetProcAddress ( h, "removehook" ) ; ( *p )( ) ; /* calls removehoook( ) function */ FreeLibrary ( h ) ; PostQuitMessage ( 0 ) ; }

Chapter 19: Interaction With Hardware

641

As we know, the OnCreate( ) and OnDestroy( ) handlers would be called when the WM_CREATE and WM_DESTROY messages arrive respectively. In OnCreate( ) we have loaded the DLL containing the hook procedure. To do this we have called the LoadLibrary( ) API function. Once the DLL is loaded we have obtained the address of the exported function installhook( ) using the GetProcAddress( ) API function. The returned address is stored in p, where p is a pointer to the installhook( ) function. Using this pointer we have then called the installhook( ) function. In the installhook( ) function we have called the API function SetWindowsHookEx( ) to register our hook procedure with the OS as shown below: hkb = SetWindowsHookEx ( WH_KEYBOARD, ( HOOKPROC ) KeyboardProc, ( HINSTANCE ) h, 0 ) ;

Here the first parameter is the type of hook that we wish to register, whereas the second parameter is the address of our hook procedure KeyboardProc( ). hkb stores the handle of the hook installed. From now on whenever a keyboard message is retrieved by the OS from the System Message Queue the message is firstly passed to our hook procedure, i.e. to KeyboardProc( ) function. Inside this function we have written code to ensure that the CapsLock always remains on. To begin with we have checked whether nCode parameter is less than 0. If it so then it necessary to call the next hook procedure. The MSDN documentation suggests that “if code is less than zero, the hook procedure must pass the message to the CallNextHookEx( ) function without further processing and should return the value returned by CallNextHookEx( )”. Note that there can be several hook procedures installed by different programs, thus forming a chain of hook procedures. These hook procedures always get called in an order that is

642

Let Us C

opposite to their order of installation. This means the last hook procedure installed is the first one to get called. If the nCode parameter contains a value HC_ACTION it means that the message that was just removed form the system message queue was a keyboard message. If it is so, then we have checked the previous state of the key before the message was sent. If the state of the key was ‘depressed’ (30th bit of lParam is 1) then we have obtained the state of the CapsLock key by calling the GetKeyState( ) API function. If it is off (0th bit of state variable is 0) then we have turned on the CapsLock by simulating a keypress. For this simulation we have called the function keybd_event( ) twice—first call is for pressing the CapsLock and second is for releasing it. Note that keybd_event( ) creates a keyboard message from the parameters that we pass to it and posts it into the system message queue. The parameter VK_CAPITAL represents the code for the CapsLock key. A word of caution! When we use keybd_event( ) to post keyboard message for a simulated CapsLock keypress, once again our hook procedure would be called when these messages are retrieved from the system message queue. But this time the CapsLock would be on so we would end up passing control to the next hook procedure through a call to CallNextHookEx( ). When we close the application window as usual the OnDestroy( ) would be called. In this handler we have obtained the address of the removehook( ) exported function and called it. In the removehook( ) function we have unregistered our hook procedure by calling the UnhookWindowsHookEx( ) API function. Note that to this function we have passed the handle to our hook. As a result our hook procedure is now removed from the hook chain. Hereafter the CapsLock would behave normally. Having unhooked our hook procedure the control would return to OnDestroy( ) handler where we have promptly unload the DLL from memory by calling the FreeLibrary( ) API function.

Chapter 19: Interaction With Hardware

643

One last point about this program—the ‘hook.dll’ file should be copied into the directory of the application’s EXE before executing the EXE.

Did You Press It TTwwiiccee….
With the power of windows hooks below your belt you are into the league of power programmers of Windows. So how about tasting the power some bit more. How about writing a program that would make every key pressed in any Windows application appear twice. Here is the code for the hook procedure.
LRESULT __declspec ( dllexport ) __stdcall KeyboardProc ( int nCode, WPARAM wParam, LPARAM lParam ) { static BYTE key ; static BOOL flag = FALSE ; if ( nCode < 0 ) return CallNextHookEx ( hkb, nCode, wParam, lParam ) ; if ( ( nCode == HC_ACTION ) && ( ( DWORD ) lParam & 0x80000000 ) == 0 ) { if ( flag == FALSE ) { key = wParam ; keybd_event ( key , 0, KEYEVENTF_EXTENDEDKEY, 0 ) ; flag = TRUE ; } else { if ( key == ( BYTE ) wParam ) flag = FALSE ; } } return CallNextHookEx ( hkb, nCode, wParam, lParam ) ;

644
}

Let Us C

In this hook procedure once again we have checked if the nCode parameter contains a value HC_ACTION. If it does then we have checked the present state of the key in question. If the present state of the key is ‘pressed’ (31th bit of lParam is 0) then we have posted the message for the same key into the system message queue by calling the keybd_event( ). However, this may lead to a serious problem. Can you imagine which? The message that we post, once retrieved, would again bring the control to our hook procedure. Once again the conditions would become true and we would post the same message again. This would go on and on. This can be prevented by using a using a simple flag variable as shown in the code. Note that the rest of the functions in the DLL file are exactly same as in the previous program. So also is the application program.

Mangling Keys
How about one more program to bolster your confidence? Let us try one that would mangle every key that is pressed. That is, convert an A to a B, B to C, C to D, etc. This would be fairly straight-forward. We simply have to increment the key code before posting it into the system message queue. Also, further processing of key has to be prevented. This can be achieved by simply returning a non-zero value from the hook procedure (thus bypassing the call to CallNextHookEx( )). This is shown in the following hook procedure.
LRESULT __declspec ( dllexport ) __stdcall KeyboardProc ( int nCode, WPARAM wParam, LPARAM lParam ) { static BYTE key ; static BOOL flag = FALSE ;

Chapter 19: Interaction With Hardware if ( nCode < 0 ) return CallNextHookEx ( hkb, nCode, wParam, lParam ) ;

645

if ( ( nCode == HC_ACTION ) && ( ( DWORD ) lParam & 0x80000000 ) == 0 ) { if ( flag == FALSE ) { key = wParam ; key ++ ; keybd_event ( key , 0, KEYEVENTF_EXTENDEDKEY, 0 ) ; flag = TRUE ; return 1 ; } else { if ( key == ( BYTE ) wParam ) flag = FALSE ; } } return CallNextHookEx ( hkb, nCode, wParam, lParam ) ; }

KeyLogger
There are several malicious programs that are floating on the net that steal away your passwords. These programs keep a log of every key that is pressed while entering passwords or credit card numbers. These programs make use of windows hooks to trap every key that is pressed. With the knowledge that you have gained from the past three programs this may not be a big deal. However, such key logger programs deviate from the ones that we developed in three fundamental ways: (a) They do not pop any window on the screen; otherwise the program’s presence would get detected.

646

Let Us C

(b) These programs also hide themselves from the Task Manager so that the user cannot terminate them. (c) The logged keys are secretly sent over the net to the malicious users who write such programs. Once the logged keys are known it would be possible to break into the system.

Where is This Leading
Even for a moment do not create an impression in you mind that Windows Hooks are only for notorious activities. There are many good things that they can be put to use for. These activities include: (a) Multimedia keyboards have special key like Cut, Copy, Paste, etc. Such keyboards also come with special programs which when installed know how to tackle these special keys. On pressing these keys these programs use the hook mechanism to place the simulated keys in the system message queue. (b) Many demo programs once executed automatically move the mouse pointer to a menu or a toolbar or any such item to demonstrate some feature of the software. To manage these actions a windows hook called Journal hook is used. (c) For physically impaired persons a keyboard can be simulated on the screen and the mouse clicks on this keyboard can be communicated to Windows as actual key hits. This again can be achieved using mouse and keyboard hook. There can be many more such examples. But the above three I believe would be ample to prove to you the constructive side of the powerful mechanism called Windows Hooks.

Chapter 19: Interaction With Hardware

647

Summary
(a) Hardware interaction can happen in two ways: (1) When the user interacts with the hardware and the program reacts to it. (2) When the program interacts with the hardware without any user intervention. (b) In DOS when the user interacts with the hardware an ISR gets called which interacts with the hardware. In Windows the same thing is done by the device driver’s ISR. (c) In DOS when the program has to interact with the hardware it can do so by using library functions, DOS/BIOS routines or by directly interacting with the hardware. In Windows the same thing can be done by using API functions. (d) Under Windows to gain finer control over the hardware we are required to write a device driver program. (e) Interaction with the any device can be done using API functions like CreateFile( ), ReadFile( ), WriteFile( ) and CloseHandle( ). (f) Different strings have to be passed to the CreateFile( ) functions for interacting with different devices. (g) Windows provides a powerful mechanism called hooks that can alter the flow of messages before they reach the application. (h) Windows hook procedures should be written in a DLL since they work on a system wide basis. (i) Windows hooks can be put to many good uses.

Exercise
[A] State True or False:

(a) In MS-DOS on occurrence of an interrupt values from IDT are used to call the appropriate kernel routine. (b) Under Windows on occurrence of an interrupt the kernel routine calls the appropriate device driver’s ISR. (c) Under Windows an application can interact with the hardware by directly calling its device driver’s routines.

648

Let Us C

(d) Under Windows we can write device drivers to extend the OS itself. (e) ReadSector( ) and WriteSector( ) are API functions. (f) While reading a sector from the disk the CreateFile( ) function creates a file on the disk. (g) The Windows API function to stop communication with a device is CloseFile( ). (h) The ReadFile( ) and WriteFile( ) API functions can only perform reading or writing from/to a disk file.
[B] Answer the following:

(a) How is hardware interaction under Windows different that that under DOS? (b) What is the advantage of writing code in a DLL? (c) Explain the Windows hooks mechanism. (d) What is the standard way of communicating with a device under Windows? (e) Write a program to read the contents of Boot Sector of a 32bit FAT file system and print them on the screen. Refer Appendix G for details about the contents of the boot sector. (f) Write a program that ensures that the key ‘A’ is completely disabled across all applications. (g) Write a program that closes any window just by placing the cursor on the ‘Close’ button in the title bar of it.

20

C Under Linux
• • • • • • • • • • What is Linux C Programming Under Linux The ‘Hello Linux’ Program Processes Parent and Child Processes More Processes Zombies and Orphans One Interesting Fact Summary Exercise

649

650

Let Us C

T

oday the programming world is divided into two major camps—the Windows world and the Linux world. Since its humble beginning about a decade ago, Linux has steadily drawn the attention of programmers across the globe and has successfully created a community of its own. How big and committed is this community is one of the hottest debates that is raging in all parts of the world. You can look at the hot discussions and the flame wars on this issue on numerous sites on the internet. Before you decide to join the Windows or the Linux camp you should first get familiar with both of them. The last 4 chapters concentrated on Windows programming. This and the next one would deal with Linux programming. Without any further discussions let us now set out on the Linux voyage. I hope you find the journey interesting and exciting.

What is Linux
Linux is a clone of the Unix operating system. Its kernel was written from scratch by Linus Torvalds with assistance from a loosely-knit team of programmers across the world on Internet. It has all the features you would expect in a modern OS. Moreover, unlike Windows or Unix, Linux is available completely free of cost. The kernel of Linux is available in source code form. Anybody is free to change it to suit his requirement, with a precondition that the changed kernel can be distributed only in the source code form. Several programs, frameworks, utilities have been built around the Linux kernel. A common user may not want the headaches of downloading the kernel, going through the complicated compilation process, then downloading the frameworks, programs and utilities. Hence many organizations have come forward to make this job easy. They distribute the precompiled kernel, programs, utilities and frameworks on a common media. Moreover, they also provide installation scripts for easy installations of the Linux OS and applications. Some of the popular distributions are RedHat, SUSE, Caldera, Debian, Mandrake, Slackware, etc. Each of them contain the same kernel

Chapter 19: Interaction With Hardware

651

but may contain different application programs, libraries, frameworks, installation scripts, utilities, etc. Which one is better than the other is only a matter of taste. Linux was first developed for x86-based PCs (386 or higher). These days it also runs on Compaq Alpha AXP, Sun SPARC, Motorola 68000 machines (like Atari ST and Amiga), MIPS, PowerPC, ARM, Intel Itanium, SuperH, etc. Thus Linux works on literally every conceivable microprocessor architecture. Under Linux one is faced with simply too many choices of Linux distributions, graphical shells and managers, editors, compilers, linkers, debuggers, etc. For simplicity (in my opinion) I have chosen the following combination: Linux Distribution - Red Hat Linux 9.0 Console Shell - BASH Graphical Shell - KDE 3.1-10 Editor - KWrite Compiler - GNU C and C++ compiler (gcc) We would be using and discussing these in the sections to follow.

C Programming Under Linux
How is C under Linux any different than C under DOS or C under Windows? Well, it is same as well as different. It is same to the extent of using language elements like data types, control instructions and the overall syntax. The usage of standard library functions is also same even though the implementation of each might be different under different OS. For example, a printf( ) would work under all OSs, but the way it is defined is likely to be different for different OSs. The programmer however doesn’t suffer because of this since he can continue to call printf( ) the same way no matter how it is implemented.

652

Let Us C

But there the similarity ends. If we are to build programs that utilize the features offered by the OS then things are bound to be different across OSs. For example, if we are to write a C program that would create a Window and display a message “hello” at the point where the user clicks the left mouse button. The architecture of this program would be very closely tied with the OS under which it is being built. This is because the mechanisms for creating a window, reporting a mouse click, handling a mouse click, displaying the message, closing the window, etc. are very closely tied with the OS for which the program is being built. In short the programming architecture (better known as programming model) for each OS is different. Hence naturally the program that achieves the same task under different OS would have to be different.

The ‘Hello Linux’ Program
As with any new platform we would begin our journey in the Linux world by creating a ‘hello world’ program. Here is the source code.... int main( ) { printf ( "Hello Linux\n" ) ; return 0 ; }

The program is exactly same as compared to a console program under DOS/Windows. It begins with main( ) and uses printf( ) standard library function to produce its output. So what is the difference? The difference is in the way programs are typed, compiled and executed. The steps for typing, compiling and executing the program are discussed below. The first hurdle to cross is the typing of this program. Though any editor can be used to do so, we have preferred to use the editor called ‘KWrite’. This is because it is a very simple yet elegant

Chapter 19: Interaction With Hardware

653

editor compared to other editors like ‘vi’ or ‘emacs’. Note that KWrite is a text editor and is a part of K Desktop environment (KDE). Installation of Linux and KDE is discussed in Appendix H. Once KDE is started select the following command from the desktop panel to start KWrite: K Menu | Accessories | More Accessories | KWrite If you face any difficulty in starting the KWrite editor please refer Appendix H. Assuming that you have been able to start KWrite successfully, carry out the following steps: (a) Type the program and save it under the name ‘hello.c’. (b) At the command prompt switch to the directory containing ‘hello.c’ using the cd command. (c) Now compile the program using the gcc compiler as shown below:
# gcc hello.c

(d) On successful compilation gcc produces a file named ‘a.out’. This file contains the machine code of the program which can now be executed. (e) Execute the program using the following command.
# ./a.out

(f) Now you should be able to see the output ‘Hello Linux’ on the screen. Having created a Hello Linux program and gone through the editcompile-execute cycle once let us now turn our attention to Linux specific programming. We will begin with processes.

Processes
Gone are the days when only one job (task) could be executed in memory at any time. Today the modern OSs like Windows and

654

Let Us C

Linux permit execution of several tasks simultaneously. Hence these OSs are aptly called ‘Multitasking’ OSs. In Linux each running task is known as a ‘process’. Even though it may appear that several processes are being executed by the microprocessor simultaneously, in actuality it is not so. What happens is that the microprocessor divides the execution time equally among all the running processes. Thus each process gets the microprocessor’s attention in a round robin manner. Once the time-slice allocated for a process expires the operation that it is currently executing is put on hold and the microprocessor now directs its attention to the next process. Thus at any given moment if we take the snapshot of memory only one process is being executed by the microprocessor. The switching of processes happens so fast that we get a false impression that the processor is executing several processes simultaneously. The scheduling of processes is done by a program called ‘Scheduler’ which is a vital component of the Linux OS. This scheduler program is fairly complex. Before switching over to the next thread it stores the information about the current process. This includes current values of CPU registers, contents of System Stack and Application Stack, etc. When this process again gets the time slot these values are restored. This process of shifting over from one thread to another is often called a Context Switch. Note that Linux uses preemptive scheduling, meaning thereby that the context switch is performed as soon as the time slot allocated to the process is over, no matter whether the process has completed its job or not. Kernel assigns each process running in memory a unique ID to distinguish it from other running processes. This ID is often known as processes ID or simply PID. It is very simple to print the PID of a running process programmatically. Here is the program that achieves this…

Chapter 19: Interaction With Hardware int main( ) { printf ( "Process ID = %d", getpid( ) ) ; }

655

Here getpid( ) is a library function which returns the process ID of the calling process. When the execution of the program comes to an end the process stands terminated. Every time we run the program a new process is created. Hence the kernel assigns a new ID to the process each time. This can be verified by executing the program several times—each time it would produce a different output.

Parent and Child Processes
As we know, our running program is a process. From this process we can create another process. There is a parent-child relationship between the two processes. The way to achieve this is by using a library function called fork( ). This function splits the running process into two processes, the existing one is known as parent and the new process is known as child. Here is a program that demonstrates this…
# include int main( ) { printf ( "Before Forking\n" ) ; fork( ) ; printf ( "After Forking\n" ) ; }

Here is the output of the program…
Before Forking After Forking After Forking

656

Let Us C

Watch the output of the program. You can notice that all the statements after the fork( ) are executed twice—once by the parent process and second time by the child process. In other words fork( ) has managed to split our process into two. But why on earth would we like to do this? At times we want our program to perform two jobs simultaneously. Since these jobs may be inter-related we may not want to create two different programs to perform them. Let me give you an example. Suppose we want perform two jobs—copy contents of source file to target file and display an animated GIF file indicating that the file copy is in progress. The GIF file should continue to play till file copy is taking place. Once the copying is over the playing of the GIF file should be stopped. Since both these jobs are inter-related they cannot be performed in two different programs. Also, they cannot be performed one after another. Both jobs should be performed simultaneously. At such times we would want to use fork( ) to create a child process and then write the program in such a manner that file copy is done by the parent and displaying of animated GIF file is done by the child process. The following program shows how this can be achieved. Note that the issue here is to show how to perform two different but inter-related jobs simultaneously. Hence I have skipped the actual code for file copying and playing the animated GIF file.
# include int main( ) { int pid ; pid = fork( ) ; if ( pid == 0 ) { printf ( "In child process\n" ) ; /* code to play animated GIF file */

Chapter 19: Interaction With Hardware
} else { printf ( "In parent process\n" ) ; /* code to copy file */ } }

657

As we know, fork( ) creates a child process and duplicates the code of the parent process in the child process. There onwards the execution of the fork( ) function continues in both the processes. Thus the duplication code inside fork( ) is executed once, whereas the remaining code inside it is executed in both the parent as well as the child process. Hence control would come back from fork( ) twice, even though it is actually called only once. When control returns from fork( ) of the parent process it returns the PID of the child process, whereas when control returns from fork( ) of the child process it always returns a 0. This can be exploited by our program to segregate the code that we want to execute in the parent process from the code that we want to execute in the child process. We have done this in our program using an if statement. In the parent process the ‘else block’ would get executed, whereas in the child process the ‘if block’ would get executed. Let us now write one more program. This program would use the fork( ) call to create a child process. In the child process we would print the PID of child and its parent, whereas in the parent process we would print the PID of the parent and its child. Here is the program…
# include int main( ) { int pid ; pid = fork( ) ; if ( pid == 0 )

658
{ printf ( "Child : Hello I am the child process\n" ) ; printf ( "Child : Child’s PID: %d\n", getpid( ) ) ; printf ( "Child : Parent’s PID: %d\n”, getppid( ) ) ; } else { printf ( "Parent : Hello I am the parent process\n" ) ; printf ( "Parent : Parent’s PID: %d\n”, getpid( ) ) ; printf ( "Parent : Child’s PID: %d\n", pid ) ; } }

Let Us C

Given below is the output of the program:
Child : Hello I am the child process Child : Child's PID: 4706 Child : Parent's PID: 4705 Parent : Hello I am the Parent process Parent : Parent's PID: 4705 Parent : Child's PID: 4706

In addition to getpid( ) there is another related function that we have used in this program—getppid( ). As the name suggests, this function returns the PID of the parent of the calling process. You can tally the PIDs from the output and convince yourself that you have understood the fork( ) function well. A lot of things that follow use the fork( ) function. So make sure that you understand it thoroughly. Note that even Linux internally uses fork( ) to create new child processes. Thus there is a inverted tree like structure of all the processes running in memory. The father of all these processes is a process called init. If we want to get a list of all the running processes in memory we can do so using the ps command as shown below.

Chapter 19: Interaction With Hardware
# ps –A

659

Here the switch –A indicates that we want to list all the running processes.

More Processes
Suppose we want to execute a program on the disk as part of a child process. For this first we should create a child process using fork( ) and then from within the child process we should call an exec function to execute the program on the disk as part of a child process. Note that there is a family of exec library functions, each basically does the same job but with a minor variation. For example, execl( ) function permits us to pass a list of command line arguments to the program to be executed. execv( ) also does the same job as execl( ) except that the command line arguments can be passed to it in the form of an array of pointers to strings. There also exist other variations like execle( ) and execvp( ). Let us now see a program that uses execl( ) to run a new program in the child process.
# include int main( ) { int pid ; pid = fork( ) ; if ( pid == 0 ) { execl ( "/bin/ls","-al", "/etc", NULL ) ; printf ( "Child: After exec( )\n") ; } else printf ( "Parent process\n" ) ; }

660

Let Us C

After forking a child process we have called the execl( ) function. This function accepts variable number of arguments. The first parameter to execl( ) is the absolute path of the program to be executed. The remaining parameters describe the command line arguments for the program to be executed. The last parameter is an end of argument marker which must always be NULL. Thus in our case the we have called upon the execl( ) function to execute the ls program as shown below ls -al /etc

As a result, all the contents of the /etc directory are listed on the screen. Note that the printf( ) below the call to execl( ) function is not executed. This is because the exec family functions overwrite the image of the calling process with the code and data of the program that is to be executed. In our case the child process’s memory was overwritten by the code and data of the ls program. Hence the call to printf( ) did not materialize. It would make little sense in calling execl( ) before fork( ). This is because a child would not get created and execl( ) would simply overwrite the main process itself. As a result, no statement beyond the call to execl( ) would ever get executed. Hence fork( ) and execl( ) usually go hand in hand.

Zombies and Orphans
We know that the ps –A command lists all the running processes. But from where does the ps program get this information? Well, Linux maintains a table containing information about all the processes. This table is called ‘Process Table’. Apart from other information the process table contains an entry of ‘exit code’ of the process. This integer value indicates the reason why the process was terminated. Even though the process comes to an end its entry would remain in the process table until such time that the parent of the terminated process queries the exit code. This act of querying

Chapter 19: Interaction With Hardware

661

deletes the entry of the terminated process from the process table and returns the exit code to the parent that raised the query. When we fork a new child process and the parent and the child continue to execute there are two possibilities—either the child process ends first or the parent process ends first. Let us discuss both these possibilities. (a) Child terminates earlier than the parent In this case till the time parent does not query the exit code of the terminated child the entry of the child process would continue to exist. Such a process in Linux terminology is known as a ‘Zombie’ process. Zombie means ghost, or in plain simple Hindi a ‘Bhoot’. Moral is, a parent process should query the process table immediately after the child process has terminated. This would prevent a zombie. What if the parent terminates without querying. In such a case the zombie child process is treated as an ‘Orphan’ process. Immediately, the father of all processes—init—adopts the orphaned process. Next, as a responsible parent init queries the process table as a result of which the child process entry is eliminated from the process table. (b) Parent terminates earlier than the child Since every parent process is launched from the Linux shell, the parent of the parent is the shell process. When our parent process terminates, the shell queries the process table. Thus a proper cleanup happens for the parent process. However, the child process which is still running is left orphaned. Immediately the init process would adopt it and when its execution is over init would query the process table to clean up the entry for the child process. Note that in this case the child process does not become a zombie. Thus, when a zombie or an orphan gets created the OS takes over and ensures that a proper cleanup of the relevant process table

662

Let Us C

entry happens. However, as a good programming practice our program should get the exit code of the terminated process and thereby ensure a proper cleanup. Note that here cleanup is important (it happens anyway). Why is it important to get the exit code of the terminated process. It is because, it is the exit code that would give indication about whether the job assigned to the process was completed successfully or not. The following program shows how this can be done.
# include # include int main( ) { unsigned int i = 0 ; int pid, status ; pid = fork( ) ; if ( pid == 0 ) { while ( i < 4294967295U ) i++ ; printf ( "The child is now terminating\n" ) ; } else { waitpid ( pid, &status, 0 ) ; if ( WIFEXITED ( status ) ) printf ( "Parent: Child terminated normally\n" ) ; else printf ( "Parent: Child terminated abnormally\n" ) ; } return 0 ; }

In this program we have applied a big loop in the child process. This loop ensures that the child does not terminate immediately. From within the parent process we have made a call to the waitpid( ) function. This function makes the parent process wait

Chapter 19: Interaction With Hardware

663

till the time the execution of the child process does not come to an end. This ensures that the child process never becomes orphaned. Once the child process, terminates the waitpid( ) function queries its exit code and returns back to the parent. As a result of querying, the child process does not become a zombie. The first parameter of waitpid( ) function is the pid of the child process for which the wait has to be performed. The second parameter is the address of an integer variable which is set up with the exit status code of the child process. The third parameter is used to specify some options to control the behavior of the wait operation. We have not used this parameter and hence we have passed a 0. Next we have made use of the WIFEXITED( ) macro to test if the child process exited normally or not. This macro takes the status value as a parameter and returns a non-zero value if the process terminated normally. Using this macro the parent suitably prints a message to report the status (normal/abnormal) termination of its child process.

One Interesting Fact
When we use fork( ) to create a child process the child process does not contain the entire data and code of the parent process. Then does it mean that the child process contains the data and code below the fork( ) call. Even this is not so. In actuality the code never gets duplicated. Linux internally manages to intelligently share it. As against this, some data is shared, some is not. Till the time both the processes do not change the value of the variables they keep getting shared. However, if any of the processes (either child or parent) attempt to change the value of a variable it is no longer shared. Instead a new copy of the variable is made for the process that is attempting to change it. This not only ensures data integrity but also saves precious memory.

664
Summary

Let Us C

(a) Linux is a free OS whose kernel was built by Linus Trovalds and friends. (b) A Linux distribution consists of the kernel with source code along with a large collection of applications, libraries, scripts, etc. (c) C programs under Linux can be compiled using the popular gcc compiler. (d) Basic scheduling unit in Linux is a ‘Process’. Processes are scheduled by a special program called ‘Scheduler’. (e) fork( ) library function can be used to create child processes. (f) Init process is the father of all processes. (g) execl( ) library function is used to execute another program from within a running program,. (h) execl( ) function overwrites the image (code and data) of the calling process. (i) execl( ) and fork( ) usually go hand in hand. (j) ps command can be used to get a list of all processes. (k) kill command can be used to terminate a process. (l) A ‘Zombie’ is a child process that has terminated but its parent is running and has not called a function to get the exit code of the child process. (m) An ‘Orphan’ is a child process whose parent has terminated. (n) Orphaned processes are adopted by init process automatically. (o) A parent process can avoid creation of a Zombie and Orphan processes using waitpid( ) function.

Exercise
[A] State True or False:

(a) We can modify the kernel of Linux OS. (b) All distributions of Linux contain the same collection of applications, libraries and installation scripts. (c) Basic scheduling unit in Linux is a file.

Chapter 19: Interaction With Hardware

665

(d) execl( ) library function can be used to create a new child process. (e) The scheduler process is the father of all processes. (f) A family of fork( ) and exec( ) functions are available, each doing basically the same job but with minor variations. (g) fork( ) completely duplicates the code and data of the parent process into the child process. (h) fork( ) overwrites the image (code and data) of the calling process. (i) fork( ) is called twice but returns once. (j) Every zombie process is essentially an orphan process. (k) Every orphan process is essentially an orphan process.
[B] Answer the following:

(a) If a program contains four calls to fork( ) one after the other how many total processes would get created? (b) What is the difference between a zombie process and an orphan process? (c) Write a program that prints the command line arguments that it receives. What would be the output of the program if the command line argument is * ? (d) What purpose do the functions getpid( ), getppid( ), getpppid( ) serve? (e) Rewrite the program in the section ‘Zombies and Orphans’ replacing the while loop with a call to the sleep( ) function. Do you observe any change in the output of the program? (f) How does waitpid( ) prevent creation of Zombie or Orphan processes?

666

Let Us C

21

More Linux Programming
• • • • • • • •

Communication using Signals Handling Multiple Signals Registering a Common Handler Blocking Signals Event driven programming Where Do You Go From Here Summary Exercise

667

668

Let Us C

C

ommunication is the essence of all progress. This is true in real life as well as in programming. In today’s world a program that runs in isolation is of little use. A worthwhile program has to communicate with the outside world in general and with the OS in particular. In Chapters 16 and 17 we saw how a Windows based program communicates with Windows. In this chapter let us explore how this communication happens under Linux.

Communication using Signals
In the last chapter we used fork( ) and exec( ) library function to create a child process and to execute a new program respectively. These library functions got the job done by communication with the Linux OS. Thus the direction of communication was from the program to the OS. The reverse communication—from the OS to the program—is achieved using a mechanism called ‘Signal’. Let us now write a simple program that would help you experience the signal mechanism. int main( ) { while ( 1 ) printf ( "Pogram Running\n" ) ; return 0 ; }

The program is fairly straightforward. All that we have done here is we have used an infinite while loop to print the message "Program Running" on the screen. When the program is running we can terminate it by pressing the Ctrl + C. When we press Ctrl + C the keyboard device driver informs the Linux kernel about pressing of this special key combination. The kernel reacts to this by sending a signal to our program. Since we have done nothing to handle this signal the default signal handler gets called. In this

Chapter 21: More Linux Programming

669

default signal handler there is code to terminate the program. Hence on pressing Ctrl + C the program gets terminated. But how on earth would the default signal handler get called. Well, it is simple. There are several signals that can be sent to a program. A unique number is associated with each signal. To avoid remembering these numbers, they have been defined as macros like SIGINT, SIGKILL, SIGCONT, etc. in the file ‘signal.h’. Every process contains several ‘signal ID - function pointer’ pairs indicating for which signal which function should be called. If we do not decide to handle a signal then against that signal ID the address of the default signal handler function is present. It is precisely this default signal handler for SIGINT that got called when we pressed Ctrl + C when the above program was executed. INT in SIGINT stands for interrupt. Let us know see how can we prevent the termination of our program even after hitting Ctrl + C. This is shown in the following program:
# include void sighandler ( int signum ) { printf ( "SIGINT received. Inside sighandler\n" ) ; } int main( ) { signal ( SIGINT, ( void* ) sighandler ) ; while ( 1 ) printf ( "Program Running\n" ) ; return 0 ; }

In this program we have registered a signal handler for the SIGINT signal by using the signal( ) library function. The first parameter

670

Let Us C

of this function specifies the ID of the signal that we wish to register. The second parameter is the address of a function that should get called whenever the signal is received by our program. This address has to be typecasted to a void * before passing it to the signal( ) function. Now when we press Ctrl + C the registered handler, namely, sighandler( ) would get called. This function would display the message ‘SIGINT received. Inside sighandler’ and return the control back to main( ). Note that unlike the default handler, our handler does not terminate the execution of our program. So only way to terminate it is to kill the running process from a different terminal. For this we need to open a new instance of command prompt (terminal). How to start a new instace of command prompt is discussed in Appendix H. Next do a ps –a to obtain the list of processes running at all the command prompts that we have launched. Note down the process id of a.out. Finally kill ‘a.out’ process by saying
# kill 3276

In my case the terminal on which I executed a.out was tty1 and its process id turned out to be 3276. In your case the terminal name and the process id might be a different number. If we wish we can abort the execution of the program in the signal handler itself by using the exit ( 0 ) beyond the printf( ). Note that signals work asynchronously. That is, when a signal is received no matter what our program is doing, the signal handler would immediately get called. Once the execution of the signal handler is over the execution of the program is resumed from the point where it left off when the signal was received.

Chapter 21: More Linux Programming

671

Handling Multiple Signals
Now that we know how to handle one signal, let us try to handle multiple signals. Here is the program to do this…
# include # include # include void inthandler ( int signum ) { printf ( "\nSIGINT Received\n" ) ; } void termhandler ( int signum ) { printf ( "\nSIGTERM Received\n" ) ; } void conthandler ( int signum ) { printf ( "\nSIGCONT Received\n" ) ; } int main( ) { signal ( SIGINT, inthandler ) ; signal ( SIGTERM, termhandler ) ; signal ( SIGCONT, conthandler ) ; while ( 1 ) printf ( "\rProgram Running" ) ; return 0 ; }

672

Let Us C

In this program apart from SIGINT we have additionally registered two new signals, namely, SIGTERM and SIGCONT. The signal( ) function is called thrice to register a different handler for each of the three signals. After registering the signals we enter a infinite while loop to print the ‘Program running’ message on the screen. As in the previous program, here too, when we press Ctrl + C the handler for the SIGINT i.e. inthandler( ) is called. However, when we try to kill the program from the second terminal using the kill command the program does not terminate. This is because when the kill command is used it sends the running program a SIGTERM signal. The default handler for the message terminates the program. Since we have handled this signal ourselves, the handler for SIGTERM i.e. termhandler( ) gets called. As a result the printf( ) statement in the termhandler( ) function gets executed and the message ‘SIGTERM Received’ gets displayed on the screen. Once the execution of termhandler( ) function is over the program resumes its execution and continues to print ‘Program Running’. Then how are we supposed to terminate the program? Simple. Use the following command from the another terminal: kill –SIGKILL 3276

As the command indicates, we are trying to send a SIGKILL signal to our program. A SIGKILL signal terminates the program. Most signals may be caught by the process, but there are a few signals that the process cannot catch, and they cause the process to terminate. Such signals are often known as un-catchable signals. The SIGKILL signal is an un-catchable signal that forcibly terminates the execution of a process. Note that even if a process attempts to handle the SIGKILL signal by registering a handler for it still the control would always land in the default SIGKILL handler which would terminate the program.

Chapter 21: More Linux Programming

673

The SIGKILL signal is to be used as a last resort to terminate a program that gets out of control. One such process that makes uses of this signal is a system shutdown process. It first sends a SIGTERM signal to all processes, waits for a while, thus giving a ‘grace period’ to all the running processes. However, after the grace period is over it forcibly terminates all the remaining processes using the SIGKILL signal. That leaves only one question—when does a process receive the SIGCONT signal? Let me try to answer this question. A process under Linux can be suspended using the Ctrl + Z command. The process is stopped but is not terminated, i.e. it is suspended. This gives rise to the un-catchable SIGSTOP signal. To resume the execution of the suspended process we can make use of the fg (foreground) command. As a result of which the suspended program resumes its execution and receives the SIGCONT signal (CONT means continue execution).

Registering a Common Handler
Instead of registering a separate handler for each signal we may decide to handle all signals using a common signal handler. This is shown in the following program:
# include # include # include void sighandler ( int signum ) { switch ( signum ) { case SIGINT :

674 printf ( "SIGINT Received\n" ) ; break ; case SIGTERM : printf ( "SIGTERM Received\n" ) ; break ; case SIGCONT : printf ( "SIGCONT Received\n" ) ; break ; } } int main( ) { signal ( SIGINT, sighandler ) ; signal ( SIGTERM, sighandler ) ; signal ( SIGCONT, sighandler ) ; while ( 1 ) printf ( "\rProgram running" ) ; return 0 ; }

Let Us C

In this program during each call to the signal( ) function we have specified the address of a common signal handler named sighandler( ). Thus the same signal handler function would get called when one of the three signals are received. This does not lead to a problem since the sighandler( ) we can figure out inside the signal ID using the first parameter of the function. In our program we have made use of the switch-case construct to print a different message for each of the three signals. Note that we can easily afford to mix the two methods of registering signals in a program. That is, we can register separate signal handlers for some of the signals and a common handler for

Chapter 21: More Linux Programming

675

some other signals. Registering a common handler makes sense if we want to react to different signals in exactly the same way.

Blocking Signals
Sometimes we may want that flow of execution of a critical/timecritical portion of the program should not be hampered by the occurrence of one or more signals. In such a case we may decide to block the signal. Once we are through with the critical/timecritical code we can unblock the signals(s). Note that if a signal arrives when it is blocked it is simply queued into a signal queue. When the signals are unblocked the process immediately receives all the pending signals one after another. Thus blocking of signals defers the delivery of signals to a process till the execution of some critical/time-critical code is over. Instead of completely ignoring the signals or letting the signals interrupt the execution, it is preferable to block the signals for the moment and deliver them some time later. Let us now write a program to understand signal blocking. Here is the program…
# include # include # include # include void sighandler ( int signum ) { switch ( signum ) { case SIGTERM : printf ( "SIGTERM Received\n" ) ; break ; case SIGINT : printf ( "SIGINT Received\n" ) ; break ;

676 case SIGCONT : printf ( "SIGCONT Received\n" ) ; break ; } } int main( ) { char buffer [ 80 ] = "\0” ; sigset_t block ; signal ( SIGTERM, sighandler ) ; signal ( SIGINT, sighandler ) ; signal ( SIGCONT, sighandler ) ; sigemptyset ( &block ) ; sigaddset ( &block, SIGTERM ) ; sigaddset ( &block, SIGINT ) ; sigprocmask ( SIG_BLOCK, &block, NULL ) ; while ( strcmp ( buffer,"n" ) != 0 ) { printf ( "Enter a String: " ) ; gets ( buffer ) ; puts ( buffer ) ; } sigprocmask ( SIG_UNBLOCK, &block, NULL ) ; while ( 1 ) printf ( "\rProgram Running" ) ; return 0 ; }

Let Us C

In this program we have registered a common handler for the SIGINT, SIGTERM and SIGCONT signals. Next we want to

Chapter 21: More Linux Programming

677

repeatedly accept strings in a buffer and display them on the screen till the time the user does not enter an ‘n’ from the keyboard. Additionally, we want that this activity of receiving input should not be interrupted by the SIGINT or the SIGTERM signals. However, a SIGCONT should be permitted. So before we proceed with the loop we must block the SIGINT and SIGTERM signals. Once we are through with the loop we must unblock these signals. This blocking and unblocking of signals can be achieved using the sigprocmask( ) library function. The first parameter of the sigprocmask( ) function specifies whether we want to block/unblock a set of signals. The next parameter is the address of a structure (typedefed as sigset_t) that describes a set of signals that we want to block/unblock. The last parameter can be either NULL or the address of sigset_t type variable which would be set up with the existing set of signals before blocking/unblocking signals. There are library functions that help us to populate the sigset_t structure. The sigemptyset( ) empties a sigset_t variable so that it does not refer to any signals. The only parameter that this function accepts is the address of the sigset_t variable. We have used this function to quickly initialize the sigset_t variable block to a known empty state. To block the SIGINT and SIGTERM we have to add the signals to the empty set of signals. This can be achieved using the sigaddset( ) library function. The first parameter of sigaddset( ) is the address of the sigset_t variable and the second parameter is the ID of the signal that we wish to add to the existing set of signals. After the loop we have also used an infinite while loop to print the ‘Program running’ message. This is done so that we can easily check that till the time the loop that receives input is not over the program cannot be terminated using Ctrl + C or kill command since the signals are blocked. Once the user enters ‘n’ from the keyboard the execution comes out of the while loop and unblocks

678

Let Us C

the signals. As a result, pending signals, if any, are immediately delivered to the program. So if we press Ctrl + C or use the kill command when the execution of the loop that receives input is not over these signals would be kept pending. Once we are through with the loop the signal handlers would be called.

Event Driven programming
Having understood the mechanism of signal processing let us now see how signaling is used by Linux – based libraries to create event driven GUI programs. As you know, in a GUI program events occur typically when we click on the window, type a character, close the window, repaint the window, etc. We have chosen the GTK library version 2.0 to create the GUI applications. Here, GTK stands for Gimp’s Tool Kit. Refer Appendix H for installation of this toolkit. Given below is the first program that uses this toolkit to create a window on the screen.
/* mywindow.c */ # include int main ( int argc, char *argv[ ] ) { GtkWidget *p ; gtk_init ( &argc, &argv ) ; p = gtk_window_new ( GTK_WINDOW_TOPLEVEL ) ; gtk_window_set_title ( p , "Sample Window" ) ; g_signal_connect ( p, "destroy", gtk_main_quit, NULL ) ; gtk_widget_set_size_request ( p, 300, 300 ) ; gtk_widget_show ( p ) ; gtk_main( ) ; return 0 ; }

Chapter 21: More Linux Programming
We need to compile this program as follows: gcc mywindow.c `pkg-config gtk+-2.0 - -cflags - -libs`

679

Here we are compiling the program ‘mywindow.c’ and then linking it with the necessary libraries from GTK toolkit. Note the quotes that we have used in the command. Here is the output of the program…

Figure 21.1 The GTK library provides a large number of functions that makes it very easy for us to create GUI programs. Every window under GTK is known as a widget. To create a simple window we have to carry out the following steps:

680

Let Us C

(a) Initialize the GTK library with a call to gtk_init( ) function. This function requires the addresses of the command line arguments received in main( ). (b) Next, call the gtk_window_new( ) function to create a top level window. The only parameter this function takes is the type of windows to be created. A top level window can be created by specifying the GTK_WINDOW_TOPLEVEL value. This call creates a window in memory and returns a pointer to the widget object. The widget object is a structure (GtkWidget) variable that stores lots of information including the attributes of window it represents. We have collected this pointer in a GtkWidget structure pointer called p. (c) Set the title for the window by making a call to gtk_window_set_title( ) function. The first parameter of this function is a pointer to the GtkWidget structure representing the window for which the title has to be set. The second parameter is a string describing the text to be displayed in the title of the window. (d) Register a signal handler for the destroy signal. The destroy signal is received whenever we try to close the window. The handler for the destroy signal should perform clean up activities and then shutdown the application. GTK provides a ready-made function called gtk_main_quit( ) that does this job. We only need to associate this function with the destroy signal. This can be achieved using the g_signal_connect( ) function. The first parameter of this function is the pointer to the widget for which destroy signal handler has to be registered. The second parameter is a string that specifies the name of the signal. The third parameter is the address of the signal handler routine. We have not used the fourth parameter. (e) Resize the window to the desired size using the gtk_widget_set_size_request( ) function. The second and the

Chapter 21: More Linux Programming

681

third parameters specify the height and the width of the window respectively. (f) Display the window on the screen using the function gtk_widget_show( ). (g) Wait in a loop to receive events for the window. This can be accomplished using the gtk_main( ) function.

How about another program that draws a few shapes in the window? Here is the program…
/* myshapes.c */ # include int expose_event ( GtkWidget *widget, GdkEventExpose *event ) { GdkGC* p ; GdkPoint arr [ 5] = { 250, 150, 250, 300, 300, 350, 400, 300, 320, 190 } ; p = gdk_gc_new ( widget -> window ) ; gdk_draw_line ( widget -> window, p, 10, 10, 200, 10 ) ; gdk_draw_rectangle ( widget -> window, p, TRUE, 10, 20, 200, 100 ) ; gdk_draw_arc ( widget -> window, p, TRUE, 200, 10, 200, 200, 2880, -2880*2 ) ; gdk_draw_polygon ( widget -> window, p, TRUE , arr, 5 ) ; gdk_gc_unref ( p ) ; return TRUE ; } int main( int argc, char *argv[ ] ) { GtkWidget *p ; gtk_init ( &argc, &argv ) ;

682

Let Us C

p = gtk_window_new ( GTK_WINDOW_TOPLEVEL ) ; gtk_window_set_title ( p, "Sample Window" ) ; g_signal_connect ( p, "destroy", gtk_main_quit, NULL ) ; g_signal_connect ( p , "expose_event", expose_event, NULL ) ; gtk_widget_set_size_request ( p, 500, 500 ) ; gtk_widget_show ( p ) ; gtk_main( ) ; return 0 ; }

Given below is the output of the program.

Chapter 21: More Linux Programming

683

Figure 21.2 This program is similar to the first one. The only difference is that in addition to the destroy signal we have registered a signal handler for the expose_event using the g_signal_connect( ) function. This signal is sent to our process whenever the window needs to be redrawn. By writing the code for drawing shapes in the handler for this signal we are assured that the drawing would never vanish if the windows is dragged outside the screen and then brought back in, or some other window uncovers a portion of our window which was previously overlapped, and so on. This is

684

Let Us C

because a expose_event signal would be sent to our application which would immediately redraw the shapes in our window. The way in Windows we have a device context, under Linux we have a graphics context. In order to draw in the window we need to obtain a graphics context for the window using the gdk_gc_new( ) function. This function returns a pointer to the graphics context structure. This pointer must be passed to the drawing functions like gdk_draw_line( ), gdk_draw_rectangle(), gdk_draw_arc( ), gdk_draw_polygon( ), etc. Once we are through with drawing we should release the graphics context using the gdk_gc_unref( ) function.

Where Do You Go From Here
You have now understood signal processing, the heart of programming under Linux. With that knowledge under your belt you are now capable of exploring the vast world of Linux on your own. Complete Linux programming deserves a book on its own. Idea here was to raise the hood and show you what lies underneath it. I am sure that if you have taken a good look at it you can try the rest yourselves. Good luck!

Summary
(a) Programs can communicate with the Linux OS using library functions. (b) The Linux OS communicates with a program by means of signals. (c) The interrupt signal (SIGINT) is sent by the kernel to our program when we press Ctrl + C. (d) A term signal (SIGTERM) is sent to the program when we use the kill command. (e) A process cannot handle an un-catchable signal. (f) The kill –SIGKILL variation of the kill command generates an un-catchable SIGKILL signal that terminates a process.

Chapter 21: More Linux Programming

685

(g) A process can block a signal or a set of signals using the sigprocmask( ) function. (h) Blocked signals are delivered to the process when the signals are unblocked. (i) A SIGSTOP signal is generated when we press Ctrl + Z. (j) A SIGSTOP signal is un-catchable signal. (k) A suspended process can be resumed using the fg command. (l) A process receives the SIGCONT signal when it resumes execution. (m) In GTK, the g_signal_connect( ) function can be used to connect a function with an event.

Exercise
[A] State True or False:

(a) All signals registered signals must have a separate signal handler. (b) Blocked signals are ignored by a process. (c) Only one signal can be blocked at a time. (d) Blocked signals are ignored once the signals are unblocked. (e) If our signal handler gets called the default signal handler automatically gets called. (f) gtk_main( ) function makes uses of a loop to prevent the termination of the program. (g) Multiple signals can be registered at a time using a single call to signal( ) function. (h) The sigprocmask( ) function can block as well as unblock signals.
[B] Answer the following:

(a) How does the Linux OS know if we have registered a signal or not? (b) What happens when we register a handler for a signal?

686

Let Us C

(c) Write a program to verify that SIGSTOP and SIGKILL signals are un-catchable signals. (d) Write a program to handle the SIGINT and SIGTERM signals. From inside the handler for SIGINT signal write an infinite loop to print the message ‘Processing Signal’. Run the program and make use of Ctrl + C more than once. Run the program once again and press Ctrl + C once then use the kill command. What are your observations? (e) Write a program that blocks the SIGTERM signal during execution of the SIGINT signal.

A

Precedence Table

687

688
Description Function expression Array Expression Structure operator Structure operator Unary minus Increment/Decrement One’s compliment Negation Address of Value of address Type cast Size in bytes Multiplication Division Modulus Addition Subtraction Left shift Right shift Less than Less than or equal to Greater than Greater than or equal to Equal to Not equal to Operator () [] -> . ++ ~ ! & * ( type ) sizeof * / % + > < >= == != --

Let Us C

Associativity Left to Right Left to Right Left to Right Left to Right Right to left Right to Left Right to left Right to Left Right to left Right to left Right to left Right to left Left to right Left to right Left to right Left to right Left to right Left to right Left to right Left to right Left to right Left to right Left to right Left to right Left to right

Continued…

Appendix A: Precedence Table
Continued… Description Bitwise AND Bitwise exclusive OR Bitwise inclusive OR Logical AND Logical OR Conditional Assignment Operator & ^ | && || ? : = *= += ^= = %= &=

689

Associativity Left to right Left to right Left to right Left to right Left to right Right to left Right to left Right to left Right to left Right to left Right to left Right to left

Figure A1.1

690

Let Us C

B

Standard Library Functions
• • • • • • • • • • • • • • • • • Standard Library Functions Arithmetic Functions Data Conversion Functions Character Classification Functions String Manipulation Functions Searching and Sorting Functions I/O Functions File Handling Functions Directory Control Functions Buffer Manipulation Functions Disk I/O Functions Memory Allocation Functions Process Control Functions Graphics Functions Time Related Functions Miscellaneous Functions DOS Interface Functions

691

692

Let Us C

L

et alone discussing each standard library function in detail, even a complete list of these functions would occupy scores of pages. However, this book would be incomplete if it has nothing to say about standard library functions. I have tried to reach a compromise and have given a list of standard library functions that are more popularly used so that you know what to search for in the manual. An excellent book dedicated totally to standard library functions is Waite group’s, Turbo C Bible, written by Nabjyoti Barkakti. Following is the list of selected standard library functions. The functions have been classified into broad categories.

Arithmetic Functions
Function abs cos cosh exp fabs floor fmod hypot log log10 modf pow sin sinh sqrt tan tanh Use Returns the absolute value of an integer Calculates cosine Calculates hyperbolic cosine Raises the exponential e to the xth power Finds absolute value Finds largest integer less than or equal to argument Finds floating-point remainder Calculates hypotenuse of right triangle Calculates natural logarithm Calculates base 10 logarithm Breaks down argument into integer and fractional parts Calculates a value raised to a power Calculates sine Calculates hyperbolic sine Finds square root Calculates tangent Calculates hyperbolic tangent

Appendix B: Standard Library Functions

693

Data Conversion Functions
Function atof atoi atol ecvt fcvt gcvt itoa ltoa strtod strtol strtoul ultoa Use Converts string to float Converts string to int Converts string to long Converts double to string Converts double to string Converts double to string Converts int to string Converts long to string Converts string to double Converts string to long integer Converts string to an unsigned long integer Converts unsigned long to string

Character classification Functions
Function isalnum isalpha isdigit islower isspace isupper isxdigit tolower toupper Use Tests for alphanumeric character Tests for alphabetic character Tests for decimal digit Tests for lowercase character Tests for white space character Tests for uppercase character Tests for hexadecimal digit Tests character and converts to lowercase if uppercase Tests character and converts to uppercase if lowercase

694
String Manipulation Functions
Function strcat strchr strcmp strcmpi strcpy strdup stricmp strlen strlwr strncat strncmp strncpy strnicmp strrchr strrev strset strstr strupr Use

Let Us C

Appends one string to another Finds first occurrence of a given character in a string Compares two strings Compares two strings without regard to case Copies one string to another Duplicates a string Compares two strings without regard to case (identical to strcmpi) Finds length of a string Converts a string to lowercase Appends a portion of one string to another Compares a portion of one string with portion of another string Copies a given number of characters of one string to another Compares a portion of one string with a portion of another without regard to case Finds last occurrence of a given character in a string Reverses a string Sets all characters in a string to a given character Finds first occurrence of a given string in another string Converts a string to uppercase

Searching and Sorting Functions
Function bsearch lfind qsort Use Performs binary search Performs linear search for a given value Performs quick sort

Appendix B: Standard Library Functions

695

I/O Functions
Function Close fclose feof fgetc fgetchar fgets fopen fprintf fputc fputchar fputs fscanf fseek ftell getc getch getche getchar gets inport inportb kbhit lseek open outport outportb printf putc putch putchar puts read Use Closes a file Closes a file Detects end-of-file Reads a character from a file Reads a character from keyboard (function version) Reads a string from a file Opens a file Writes formatted data to a file Writes a character to a file Writes a character to screen (function version) Writes a string to a file Reads formatted data from a file Repositions file pointer to given location Gets current file pointer position Reads a character from a file (macro version) Reads a character from the keyboard Reads a character from keyboard and echoes it Reads a character from keyboard (macro version) Reads a line from keyboard Reads a two-byte word from the specified I/O port Reads one byte from the specified I/O port Checks for a keystroke at the keyboard Repositions file pointer to a given location Opens a file Writes a two-byte word to the specified I/O port Writes one byte to the specified I/O port Writes formatted data to screen Writes a character to a file (macro version) Writes a character to the screen Writes a character to screen (macro version) Writes a line to file Reads data from a file

696 rewind scanf sscanf sprintf tell write Repositions file pointer to beginning of a file Reads formatted data from keyboard Reads formatted input from a string Writes formatted output to a string Gets current file pointer position Writes data to a file

Let Us C

File Handling Functions
Function remove rename unlink Use Deletes file Renames file Deletes file

Directory Control Functions
Function chdir getcwd fnsplit findfirst findnext mkdir rmdir Use Changes current working directory Gets current working directory Splits a full path name into its components Searches a disk directory Continues findfirst search Makes a new directory Removes a directory

Buffer Manipulation Functions
Function memchr memcmp Use Returns a pointer to the first occurrence, within a specified number of characters, of a given character in the buffer Compares a specified number of characters from two buffers

Appendix B: Standard Library Functions memcpy memicmp memmove memset

697

Copies a specified number of characters from one buffer to another Compares a specified number of characters from two buffers without regard to the case of the characters Copies a specified number of characters from one buffer to another Uses a given character to initialize a specified number of bytes in the buffer

Disk I/O Functions
Function absread abswrite biosdisk getdisk setdisk Use Reads absolute disk sectors Writes absolute disk sectors Performs BIOS disk services Gets current drive number Sets current disk drive

Memory Allocation Functions
Function calloc farmalloc farfree free malloc realloc Use Allocates a block of memory Allocates memory from far heap Frees a block from far heap Frees a block allocated with malloc Allocates a block of memory Reallocates a block of memory

Process Control Functions
Function abort atexit Use Aborts a process Executes function at program termination

698 execl exit spawnl spawnlp system

Let Us C
Executes child process with argument list Terminates the process Executes child process with argument list Executes child process using PATH variable and argument list Executes an MS-DOS command

Graphics Functions
Function arc ellipse floodfill getimage getlinestyle getpixel lineto moveto pieslice putimage rectangle setcolor setlinestyle putpixel setviewport Use Draws an arc Draws an ellipse Fills an area of the screen with the current color Stores a screen image in memory Obtains the current line style Obtains the pixel’s value Draws a line from the current graphic output position to the specified point Moves the current graphic output position to a specified point Draws a pie-slice-shaped figure Retrieves an image from memory and displays it Draws a rectangle Sets the current color Sets the current line style Plots a pixel at a specified point Limits graphic output and positions the logical origin within the limited area

Time Related Functions
Function clock difftime Use Returns the elapsed CPU time for a process Computes the difference between two times

Appendix B: Standard Library Functions ftime strdate strtime time setdate getdate Gets current system time as structure Returns the current system date as a string Returns the current system time as a string Gets current system time as long integer Sets DOS date Gets system date

699

Miscellaneous Functions
Function delay getenv getpsp perror putenv random randomize sound nosound Use Suspends execution for an interval (milliseconds) Gets value of environment variable Gets the Program Segment Prefix Prints error message Adds or modifies value of environment variable Generates random numbers Initializes random number generation with a random value based on time Turns PC speaker on at specified frequency Turns PC speaker off

DOS Interface Functions
Function FP_OFF FP_SEG getvect keep int86 int86x intdos intdosx MK_FP Use Returns offset portion of a far pointer Returns segment portion of a far pointer Gets the current value of the specified interrupt vector Installs terminate-and-stay-resident (TSR) programs Issues interrupts Issues interrupts with segment register values Issues interrupt 21h using registers other than DX and AL Issues interrupt 21h using segment register values Makes a far pointer

700 segread setvect

Let Us C
Returns current values of segment registers Sets the current value of the specified interrupt vector

C

Chasing The Bugs

701

702

Let Us C

C

programmers are great innovators of our times. Unhappily, among their most enduring accomplishments are several new techniques for wasting time. There is no shortage of horror stories about programs that took twenty times to ‘debug’ as they did to ‘write’. And one hears again and again about programs that had to be rewritten all over again because the bugs present in it could not be located. A typical C programmer’s ‘morning after’ is red eyes, blue face and a pile of crumpled printouts and dozens of reference books all over the floor. Bugs are C programmer's birthright. But how do we chase them away. No sure-shot way for that. I thought if I make a list of more common programming mistakes it might be of help. They are not arranged in any particular order. But as you would realize surely a great help! [1] Omitting the ampersand before the variables used in scanf( ). For example, int choice ; scanf ( "%d", choice ) ;

Here, the & before the variable choice is missing. Another common mistake with scanf( ) is to give blanks either just before the format string or immediately after the format string as in, int choice ; scanf ( " %d ", choice ) ;

Note that this is not a mistake, but till you don't understand scanf( ) thoroughly, this is going to cause trouble. Safety is in eliminating the blanks. Thus, the correct form would be, int choice ; scanf ( "%d", &choice ) ;

Appendix C: Chasing The Bugs
[2] Using the operator = instead of the operator = =.

703

What do you think will be the output of the following program: main( ) { int i = 10 ; while ( i = 10 ) { printf ( "got to get out" ) ; i++ ; } }

At first glance it appears the message will be printed once and the control will come out of the loop since i becomes 11. But, actually we have fallen in an indefinite loop. This is because the = used in the condition always assigns the value 10 to i, and since i is non-zero the condition is satisfied and the body of the loop is executed over and over again. [3] Ending a loop with a semicolon. Observe the following program. main( ) { int j = 1 ; while ( j >age ) ; }

Let Us C

[17] Forgetting to use the far keyword for referring memory locations beyond the data segment. main( ) { unsigned int *s ; s = 0x413 ; printf ( "\n%d", *s ) ; }

Here, it is necessary to use the keyword far in the declaration of variable s, since the address that we are storing in s (0x413) is a address of location present in BIOS Data Area, which is far away from the data segment. Thus, the correct declaration would look like, unsigned int far *s ;

The far pointers are 4-byte pointers and are specific to DOS. Under Windows every pointer is 4-byte pointer. [18] Exceeding the range of integers and chars.

Appendix C: Chasing The Bugs main( ) { char ch ; for ( ch = 0 ; ch

Similar Documents

Free Essay

None

...01000001 * 100= 01100100 * 255= 11111111 2. Use what you’ve learned about the binary numbering system in this chapter to convert the following binary numbers to decimal? * 00001101=13 * 00001000=8 * 00101011=43 3. Look at the ASCII chart in Appendix A on CD that accompanies this book and determine the codes for each letter of your name? * 67 97 114 108 111 115 78 97 106 101 114 97 * C a r l o s N a j e r a 4. Use the web to research the history of the BASIC, C++, Java, and Python programming languages, and answer the following questions? * Who was the creator of each language? * When was each of these languages created? * Was there a specific motivation behind the creation of these languages? If so, what was it? * Basic was created by John George Kemeny. It was created on 1964. * C++ was created by Bjarne Stroustrup. It was created on 1979. * Java was created by Dr. James A. Gosling. It was created on 1995. * Python was created by Guido van Rossum. It was created on 1991. * The motivation behind the development of computer programming languages such as C ++, Basic, Java, and Python is that once written, a function may be used several times without rewriting it over and over. Programming languages can be used to develop programs that control the performance of a machine and/or to convey algorithms...

Words: 259 - Pages: 2

Premium Essay

C Tutorial

...C Programming Tutorial C PROGRAMMING TUTORIAL Simply Easy Learning by tutorialspoint.com tutorialspoint.com i COPYRIGHT & DISCLAIMER NOTICE  All the content and graphics on this tutorial are the property of tutorialspoint.com. Any content from tutorialspoint.com or this tutorial may not be redistributed or reproduced in any way, shape, or form without the written permission of tutorialspoint.com. Failure to do so is a violation of copyright laws. This tutorial may contain inaccuracies or errors and tutorialspoint provides no guarantee regarding the accuracy of the site or its contents including this tutorial. If you discover that the tutorialspoint.com site or this tutorial content contains some errors, please contact us at webmaster@tutorialspoint.com ii T able of Contents C Language Overview .............................................................. 1 Facts about C ............................................................................................... 1 Why to use C ? ............................................................................................. 2 C Programs .................................................................................................. 2 C Environment Setup ............................................................... 3 Text Editor ................................................................................................... 3 The C Compiler ................................................

Words: 14106 - Pages: 57

Free Essay

C++ Objects Solutions

...C++ LOCATION OF VIDEONOTES IN THE TEXT Chapter 1 Designing a Program with Pseudocode, p. 19 Designing the Account Balance Program, p. 24 Predicting the Output of Problem 30, p. 24 Solving the Candy Bar Sales Problem, p. 25 Using cout to Display Output, p. 32 Assignment Statements, p. 59 Arithmetic Operators, p. 61 Solving the Restaurant Bill Problem, p. 72 Using cin to Read Input, p. 75 Evaluating Mathematical Expressions, p. 81 Combined Assignment Operators, p. 102 Solving the Stadium Seating Problem, p. 151 Using an if Statement, p. 162 Using an if/else Statement, p. 172 Using an if/else if Statement, p. 175 Solving the Time Calculator Problem, p. 236 The while Loop, p. 249 The for Loop, p. 263 Nested Loops, p. 277 Solving the Ocean Levels Problem, p. 299 Defining and Calling Functions, p. 306 Using Function Arguments, p. 316 Value-Returning Functions, p. 326 Solving the Markup Problem, p. 380 Creating a Class, p. 391 Creating and Using Class Objects, p. 393 Creating and Using Structures, p. 436 Solving the Car Class Problem, p. 480 Accessing Array Elements, p. 487 Passing an Array to a Function, p. 517 Two-Dimensional Arrays, p. 526 Solving the Chips and Salsa Problem, p. 567 Performing a Binary Search, p. 580 Sorting a Set of Data, p. 587 Solving the Lottery Winners Problem, p. 616 (continued on next page) Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 LOCATION OF VIDEONOTES IN THE TEXT Chapter 10 Pointer Variables...

Words: 11246 - Pages: 45

Premium Essay

Learn C Programming Language in 24 Hours

...Yourself C in 24 Hours Previous | Table of Contents | Next Hour 1 - Getting Started A journey of a thousand miles is started by taking the first step. —Chinese proverb High thoughts must have high language. —Aristophanes Welcome to Teach Yourself C in 24 Hours. In this first lesson you'll learn the following:     What C is Why you need to learn C The ANSI standard Hardware and software required in order to run the C program What Is C? C is a programming language. The C language was first developed in 1972 by Dennis Ritchie at AT&T Bell Labs. Ritchie called his newly developed language C simply because there was a B programming language already. (As a matter of fact, the B language led to the development of C.) C is a high-level programming language. In fact, C is one of the most popular general-purpose programming languages. In the computer world, the further a programming language is from the computer architecture, the higher the language's level. You can imagine that the lowest-level languages are machine languages that computers understand directly. The high-level programming languages, on the other hand, are closer to our human languages. (See Figure 1.1.) Figure 1.1. The language spectrum. High-level programming languages, including C, have the following advantages:    Readability: Programs are easy to read. Maintainability: Programs are easy to maintain. Portability: Programs are easy to port across different computer platforms. The C language's...

Words: 73255 - Pages: 294

Premium Essay

Java

...Release Team[oR] 2001 [x] java Java 2: The Complete Reference by Patrick Naughton and Herbert Schildt Osborne/McGraw-Hill © 1999, 1108 pages ISBN: 0072119764 This thorough reference reads like a helpful friend. Includes servlets, Swing, and more. Table of Contents Back Cover Synopsis by Rebecca Rohan Java 2: The Complete Reference blends the expertise found in Java 1: The Complete Reference with Java 2 topics such as "servlets" and "Swing." As before, there's help with Java Beans and migrating from C++ to Java. A special chapter gives networking basics and breaks out networking-related classes. This book helps you master techniques by doing as well as reading. Projects include a multi-player word game with attention paid to network security. The book is updated where appropriate throughout, and the rhythm of text, code, tables, and illustrations is superb. It's a valuable resource for the developer who is elbow-deep in demanding projects. Table of Contents Java 2 Preface - 7 Part l The Java Language - The Complete Reference - 4 Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 hapter 10 - The Genesis of Java - 9 - An Overview of Java - 20 - Data Types, Variables, and Arrays - 36 - Operators - 57 - Control Statements - 75 - Introducing Classes - 94 - A Closer Look at Methods and Classes - 111 - Inheritance - 134 - Packages and Interfaces - 156 - Exception Handling - 174 Chapter 11 - Multithreaded Programming...

Words: 78285 - Pages: 314

Free Essay

Technological Essays

...Object-Oriented Programming in C++, Fourth Edition Robert Lafore 800 East 96th St., Indianapolis, Indiana 46240 USA Copyright  2002 by Sams Publishing All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein. International Standard Book Number: 0-672-32308-7 Library of Congress Catalog Card Number: 2001094813 Printed in the United States of America First Printing: December 2001 04 03 02 01 4 3 2 1 EXECUTIVE EDITOR Michael Stephens ACQUISITIONS EDITOR Michael Stephens MANAGING EDITOR Matt Purcell PROJECT EDITORS Angela Boley Christina Smith INDEXER Rebecca Salerno PROOFREADER Matt Wynalda Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. TECHNICAL EDITOR Mark Cashman TEAM COORDINATOR ...

Words: 608 - Pages: 3

Premium Essay

Mundra

...|Sunita Mundra |Contact | | | | |MBA – Finance ▪ Date of birth : 00 July 1900 |Tel : +91 93467 00281 | | |e-mail : sunitamundra@ymail.com | | | | | | |Address | | | |Sri Ramachandra Résidency, Tanuku – 00000 , Andhra pradesh | | ...

Words: 321 - Pages: 2

Premium Essay

Computers

...C Primer Plus Sixth Edition Developer’s Library ESSENTIAL REFERENCES FOR PROGRAMMING PROFESSIONALS Developer’s Library books are designed to provide practicing programmers with unique, high-quality references and tutorials on the programming languages and technologies they use in their daily work. All books in the Developer’s Library are written by expert technology practitioners who are especially skilled at organizing and presenting information in a way that’s useful for other programmers. Key titles include some of the best, most widely acclaimed books within their topic areas: PHP & MySQL Web Development Luke Welling & Laura Thomson ISBN 978-0-672-32916-6 Python Essential Reference David Beazley ISBN-13: 978-0-672-32978-4 MySQL Paul DuBois ISBN-13: 978-0-321-83387-7 PostgreSQL Korry Douglas ISBN-13: 978-0-672-32756-8 Linux Kernel Development Robert Love ISBN-13: 978-0-672-32946-3 C++ Primer Plus Stephen Prata ISBN-13: 978-0-321-77640-2 Developer’s Library books are available in print and in electronic formats at most retail and online bookstores, as well as by subscription from Safari Books Online at safari. informit.com Developer’s Library informit.com/devlibrary C Primer Plus Sixth Edition Stephen Prata Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Cape Town • Sydney • Tokyo • Singapore • Mexico City C Primer Plus Sixth...

Words: 125302 - Pages: 502

Free Essay

The God of War

...Modern C++ Topics include: ■■ The pros and cons of braced initialization, noexcept specifications, perfect forwarding, and smart pointer make functions ■■ The relationships among std::move, std::forward, rvalue references, and universal references ■■ Techniques for writing clear, correct, effective lambda expressions ■■ How std::atomic differs from volatile, how each should be used, and how they relate to C++'s concurrency API ■■ How best practices in "old" C++ programming (i.e., C++98) require revision for software development in modern C++ After the C++ “ I learnedlearned basics, I then how to use C++ in production code from Meyers' series of Effective C++ books. Effective Modern C++ is the most important how-to book for advice on key guidelines, styles, and idioms to use modern C++ effectively and well. Don't own it yet? Buy this one. Now. ” —Herb Sutter Chair of ISO C++ Standards Committee and C++ Software Architect at Microsoft Effective Modern C++ Coming to grips with C++11 and C++14 is more than a matter of familiarizing yourself with the features they introduce (e.g., auto type declarations, move semantics, lambda expressions, and concurrency support). The challenge is learning to use those features effectively—so that your software is correct, efficient, maintainable, and portable. That’s where this practical book comes in. It describes how to write truly great software using C++11 and C++14—i...

Words: 8814 - Pages: 36

Premium Essay

Let Us C - Yashwant Kanetkar.Pdf

...Let Us C Fifth Edition Yashavant P. Kanetkar Dedicated to baba Who couldn’t be here to see this day... About the Author Destiny drew Yashavant Kanetkar towards computers when the IT industry was just making a beginning in India. Having completed his education from VJTI Mumbai and IIT Kanpur in Mechanical Engineering he started his training company in Nagpur. Yashavant has a passion for writing and is an author of several books in C, C++, VC++, C#, .NET, DirectX and COM programming. He is a much sought after speaker on various technology subjects and is a regular columnist for Express Computers and Developer 2.0. His current affiliations include being a Director of KICIT, a training company and DCube Software Technologies, a software development company. In recognition to his contribution Microsoft awarded him the prestigious “Best .NET Technical Contributor” award recently. He can be reached at kanetkar@kicit.com. Preface to the Fifth Edition It is mid 2004. World has left behind the DOTCOM bust, 9/11 tragedy, the economic downturn, etc. and moved on. Countless Indians have relentlessly worked for close to two decades to successfully establish “India” as a software brand. At times I take secret pleasure in seeing that a book that I have been part of, has contributed in its own little way in shaping so many budding careers that have made the “India” brand acceptable. Computing and the way people use C for doing it keeps changing as years go by. So overwhelming...

Words: 46379 - Pages: 186

Free Essay

C++Language Compasion

...++Starting Out with Programming Logic and Design, 3rd Edition By Tony Gaddis C++ Language Companion or Copyright © 2013 Pearson Education, Inc. Table of Contents Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Introduction 3 Introduction to Computers and Programming 4 Input, Processing, and Output 9 Functions 19 Decision Structures and Boolean Logic 27 Repetition Structures 41 Value-Returning Functions 49 Input Validation 59 Arrays 61 Sorting and Searching Arrays 72 Files 77 Menu-Driven Programs 86 Text Processing 89 Recursion 95 Object-Oriented Programming 97 Page 2 Introduction Welcome to the C++ Language Companion for Starting Out with Programming Logic and Design, 3rd Edition, by Tony Gaddis. You can use this guide as a reference for the C++ Programming Language as you work through the textbook. Each chapter in this guide corresponds to the same numbered chapter in the textbook. As you work through a chapter in the textbook, you can refer to the corresponding chapter in this guide to see how the chapter's topics are implemented in the C++ programming language. In this book you will also find C++ versions of many of the pseudocode programs that are presented in the textbook. Note: This booklet does not have a chapter corresponding to Chapter 15 of your textbook because C++ does not provide a GUI programming library. Page 3 Chapter 1 This chapter accompanies...

Words: 1609 - Pages: 7

Premium Essay

Lg C Program

...About The Tutorial C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language. It keeps fluctuating at number one scale of popularity along with Java programming language, which is also equally popular and most widely used among modern software programmers. Audience This tutorial is designed for software programmers with a need to understand the C programming language starting from scratch. This tutorial will give you enough understanding on C programming language from where you can take yourself to higher level of expertise. Prerequisites Before proceeding with this tutorial, you should have a basic understanding of Computer Programming terminologies. A basic understanding of any of the programming languages will help you in understanding the C programming concepts and move fast on the learning track. Copyright & Disclaimer  Copyright 2014 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the...

Words: 13419 - Pages: 54

Premium Essay

Java

...only way to build these applications is programming. To program we need to know one of the programming languages which are very similar each other. If you are professional in one of these languages you can be professional in the other language in a short period of time. It is acceptable if you have the same application written with Java once and with C++ or C sharp at the same time. So for this reason you cannot say that a programming language is better than others. There are three types of programming languages procedural, functional and object-oriented languages. The most uses of these languages are object-oriented and one of these languages is Java you can write any application you need using it. Also you can translate any application to its word. The message of the High-Level programming languages such as Algol and Pascal in first programming revolution was Control abstraction. In the next revolution data abstraction was the massage of proposed languages such as Modula and Ada. In the 1960s the object-oriented revolution began slowly with Simula, thin it moved onto more languages such as Smalltalk, Objective-C and C++. The Java™ programming language was originally called Oak, and was designed for use in embedded consumer-electronic applications by...

Words: 2352 - Pages: 10

Free Essay

Ali94

...Programs and Designs Scott Meyers More Effective C ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES ++ Conforms to the new ISO/ANSI C++ standard! From the Library of Yuri Khan Praise for More Effective C++: 35 New Ways to Improve Your Programs and Designs “This is an enlightening book on many aspects of C++: both the regions of the language you seldom visit, and the familiar ones you THOUGHT you understood. Only by understanding deeply how the C++ compiler interprets your code can you hope to write robust software using this language. This book is an invaluable resource for gaining that level of understanding. After reading this book, I feel like I've been through a code review with a master C++ programmer, and picked up many of his most valuable insights.” — Fred Wild, Vice President of Technology, Advantage Software Technologies “This book includes a great collection of important techniques for writing programs that use C++ well. It explains how to design and implement the ideas, and what hidden pitfalls lurk in some obvious alternative designs. It also includes clear explanations of features recently added to C++. Anyone who wants to use these new features will want a copy of this book close at hand for ready reference.” — Christopher J. Van Wyk, Professor, Mathematics and Computer Science, Drew University “Industrial strength C++ at its best. The perfect companion to those who have read Effective C++.” — Eric Nagler, C++ Instructor and Author, University of California...

Words: 43056 - Pages: 173

Free Essay

Computer Science C++

...Report Bundle C++Ox: The Dawning of a New Standard Contents C++0x: The Dawning of a New Standard It's been 10 years since the first ISO C++ standard, and 2009 will bring us the second. In this special report, DevX delves into the new features being discussed by the standards team. Learn how these new features will revolutionize the way you code. Overview: C++ Gets an Overhaul It's been 10 years since the first ISO C++ standard, and 2009 will bring us the second. Learn about the new features being added and how they will revolutionize the language. Easier C++: An Introduction to Concepts C++0x concepts bring the full power of the Generic Programming paradigm to C++, making templates more expressive, easier to write, and easier to use. Spectacularly poor template error messages are a thing of the past! Simpler Multithreading in C++0x The new standard will support multithreading, with a new thread library. Find out how this will improve porting code, and reduce the number of APIs and syntaxes you use. The State of the Language: An Interview with Bjarne Stroustrup C++ founding father assesses the language on the eve of its new standard. Timeline: C++ in Retrospect From its nascent pre-processor in 1979 to today's incredibly sophisticated language features and libraries, we've documented each step along the way. C++0x: The Dawning of a New Standard Overview: C++ Gets an Overhaul Overview: C++ Gets an Overhaul C++Ox: The Dawning...

Words: 6570 - Pages: 27