Let Us C Fourteenth Edition Yashavant Kanetkar i Dedicated to baba Who couldn’t be here to see this day... ii About the Author Through his books and Quest Video Courseware DVDs on C, C++, Data Structures, VC++, .NET, Embedded Systems, etc. Yashavant Kanetkar has created, moulded and groomed lacs of IT careers in the last two decades. Yashavant’s books and Quest DVDs have made a significant contribution in creating top-notch IT manpower in India and abroad. Yashavant’s books are globally recognized and millions of students / professionals have benefitted from them. Yashavant's books have been translated into Hindi, Gujarati, Japanese, Korean and Chinese languages. Many of his books are published in India, USA, Japan, Singapore, Korea and China. Yashavant is a much sought after speaker in the IT field and has conducted seminars/workshops at TedEx, IITs, RECs and global software companies. Yashavant has recently been honored with the prestigious “Distinguished Alumnus Award” by IIT Kanpur for his entrepreneurial, professional and academic excellence. This award was given to top 50 alumni of IIT Kanpur who have made significant contribution towards their profession and betterment of society in the last 50 years. In recognition of his immense contribution to IT education in India, he has been awarded the "Best .NET Technical Contributor" and "Most Valuable Professional" awards by Microsoft for 5 successive years. Yashavant holds a BE from VJTI Mumbai and M.Tech. from IIT Kanpur. iii Acknowledgments Let Us C has become an important part of my life. I have created and nurtured it for last decade and half. While doing so, I have received, in addition to the compliments, a lot of suggestions from students, developers, professors, publishers and authors. So much have their inputs helped me in taking this book up to its fourteenth edition that ideally I should put their names too on the cover page. 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. I hope every author gets a publisher who is as cooperative, knowledgeable and supportive as Manish. The Fourteen editions of this book saw several changes and facelifts. During this course many people helped in executing programs and spotting bugs. I trust that with their collective acumen, all the programs in this book would run correctly. I value the work that they did a lot. Any errors, omissions or inconsistencies that remain are, alas, my responsibility. I thank Dada, Ammi—my parents, Seema—my wife, Aditya, Anuj—my sons for enduring the late nights, the clicking keyboard, and mostly for putting up with yet another marathon book effort. Thinking of a book cover idea is one thing, putting it into action is a different cup of tea. This edition’s cover idea has been implemented by Jayant. Many thanks to him. And finally my heartfelt gratitude to the countless students who made me look into every nook and cranny of C. I want to remain in their debt. It is only because of them that Let Us C is now published from India, Singapore, USA, Japan, Korea and China in multiple languages. iv Preface In this I have reorganized the contents of the book in a major way. After going through the thirteenth edition several times I decided to realign all the chapters in such a manner that if a C programming course is taught using Let Us C, it can roughly be finished in 23 lectures of one hour each, with one chapter's contents devoted to one lecture. I hope this would make the learning path trouble-free. Some end-of-chapter exercises in the book needed a second look to make them more practical. That also stands done now. Many readers told me that they have immensely benefitted from the inclusion of the chapter on Interview FAQs. I have improved this chapter further. The rationale behind this chapter is simple—ultimately all the readers of Let Us C sooner or later end up in an interview room where they are required to take questions on C programming. I now have a proof that this chapter has helped to make that journey smooth and fruitful. 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’. If you want some more problems for practice they are available in the book titled ‘Let Us C Workbook’. As usual, new editions of these two books have also been launched along with 14th edition of Let Us C. If you like ‘Let Us C’ and want to hear the complete video-recorded lectures created by me on C language (and other subjects like C++, VC++, C#, Java, .NET, Embedded Systems, etc.), then you can visit http://quest.ksetindia.com for more details. ‘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 [email protected]. 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 Let Us C has contributed in its own little way in shaping so many careers that have made the “India” brand acceptable. v Recently I was presented with “Distinguished Alumnus Award” by IIT Kanpur. It was great to figure in a list that contained Narayan Murthy, Chief Mentor, Infosys, Dr. D. Subbarao, former Governor, Reserve Bank of India, Dr. Rajeev Motwani of Stanford University, Prof. H. C. Verma, Mr. Som Mittal President of NASSCOM, Prof. Minwalla of Harvard University, Dr. Sanjay Dhande former Director of IIT Kanpur, Prof. Arvind and Prof. Sur of MIT USA and Prof. Ashok Jhunjhunwala of IIT Chennai. I think Let Us C amongst my other books has been primarily responsible for helping me get the “Distinguished Alumnus” award. What was a bit surprising was that almost all who were present knew about the book already and wanted to know from me what it takes to write a book that sells in millions of copies. My reply was—make an honest effort to make the reader understand what you have to say and keep it simple. I don’t know how convincing was this answer, but well, that is what I have been doing with this book in all its previous thirteen editions. I have followed the same principle with this edition too. All the best and happy programming! Yashavant Kanetkar vi Contents 1. Getting Started 1 What is C? 2 Getting Started with C 3 The C Character Set 4 Constants, Variables and Keywords 4 Types of C Constants 5 Rules for Constructing Integer Constants 6 Rules for Constructing Real Constants 7 Rules for Constructing Character Constants 8 Types of C Variables 8 Rules for Constructing Variable Names 8 C Keywords 9 The First C Program 10 Form of a C Program 11 Comments in a C Program 11 What is main( )? 12 Variables and their Usage 13 printf( ) and its Purpose 14 Compilation and Execution 15 Receiving Input 15 Summary 17 Exercise 18 2. C Instructions 21 Types of Instructions 22 Type Declaration Instruction 22 Arithmetic Instruction 23 Integer and Float Conversions 26 Type Conversion in Assignments 27 Hierarchy of Operations 28 Associativity of Operators 31 Control Instructions 32 Summary 32 Exercise 33 3. Decision Control Instruction 39 Decisions! Decisions! 40 The if Statement 40 The Real Thing 44 vii Multiple Statements within if 45 The if-else Statement 47 Nested if-elses 49 Forms of if 50 Summary 51 Exercise 51 4. More Complex Decision Making 57 Use of Logical Operators 58 The else if Clause 61 The ! Operator 66 Hierarchy of Operators Revisited 66 A Word of Caution 67 The Conditional Operators 69 Summary 71 Exercise 71 5. Loop Control Instruction 81 Loops 82 The while Loop 82 Tips and Traps 85 More Operators 88 Summary 90 Exercise 90 6. More Complex Repetitions 95 The for Loop 96 Nesting of Loops 101 Multiple Initializations in the for Loop 102 The break Statement 102 The continue Statement 104 The do-while Loop 105 The Odd Loop 107 Summary 109 Exercise 110 7. Case Control Instruction 117 Decisions using switch 118 The Tips and Traps 121 switch versus if-else Ladder 126 The goto Keyword 126 viii Summary 129 Exercise 129 8. Functions 135 What is a Function? 136 Why use Functions? 142 Passing Values between Functions 143 Scope Rule of Functions 147 Order of Passing Arguments 148 Using Library Functions 149 One Dicey Issue 150 Return Type of Function 150 Summary 151 Exercise 151 9. Pointers 157 Call by Value and Call by Reference 158 An Introduction to Pointers 158 Pointer Notation 159 Back to Function Calls 164 Conclusions 167 Summary 167 Exercise 168 10. Recursion 173 Recursion 174 Recursion and Stack 178 Summary 181 Exercise 181 11. Data Types Revisited 183 Integers, long and short 184 Integers, signed and unsigned 186 Chars, signed and unsigned 187 Floats and Doubles 188 A Few More Issues… 191 Storage Classes in C 192 Automatic Storage Class 193 Register Storage Class 194 Static Storage Class 195 External Storage Class 198 ix