ebook img

Let Us C: Authentic Guide to C PROGRAMMING Language (17th Edition) PDF

357 Pages·17·5.42 MB·
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Let Us C: Authentic Guide to C PROGRAMMING Language (17th Edition)

Let Us C Seventeenth Edition Yashavant Kanetkar SEVENTEENTH REVISED AND UPDATED EDITION 2020 FIRST EDITION 2007 Reprinted 2019 Copyright © BPB Publications, India © Let Us C is a registered trademark of BPB Publications, New Delhi, under Registration No: 1135514 ISBN: 978-93-89845-686 All Rights Reserved. No part of this publication may be reproduced or distributed in any form or by any means or stored in a database or retrieval system, without the prior written permission of the publisher. LIMITS OF LIABILITY AND DISCLAIMER OF WARRANTY The Author and Publisher of this book have tried their best to ensure that the programmes, procedures and functions described in the book arc correct. However, the author and the publishers make no warranty, of any kind, expressed or implied, with regard to these programmes or the documentation contained in the book. The author and publisher shall not be liable in any event of any damages, incidental or consequential, in connection with, or arising out of the furnishing, performance or use of these programmes, procedures and functions. Produce name mentioned are used for identification purposes only and may be trademarks of their respective companies. All trademarks referred to in the book are acknowledged as properties of their respective owners. Distributors: BPB PUBLICATIONS 20, Ansari Road, Darya Ganj New Delhi-110002 Ph: 23254990/23254991 MICRO MEDIA Shop No. 5, Mahendra Chambers, 150 DN Rd. Next to Capital Cinema, V.T. (C.S.T.) Station, MUMBAI-400 001 Ph: 22078296/22078297 DECCAN AGENCIES 4-3-329, Bank Street, Hyderabad-500195 Ph: 24756967/24756400 BPB BOOK CENTRE 376 Old Lajpat Rai Market, Delhi-110006 Ph: 23861747 Published by Manish Jain for BPB Publications, 20Ansari Road, Darya Ganj, New Delhi-110002 and Printed at Repro India Ltd, Mumbai Dedicated to baba Who couldn't be here to see this day… About the Author Through his books and online Quest Video Courses 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 and half decades. Yashavant's books and online courses 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. His 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, NITs, IIITs and global software companies. Yashavant has 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. His current affiliations include being a Director of KICIT Pvt. Ltd. He can be reached at [email protected] or through http://www.kicit.com. Acknowledgments Let Us C has become an important part of my life. I have created and nurtured it for last two decades. 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 seventeenth 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 previous 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 all my family members for enduring the late nights, the clicking keyboard, and mostly for putting up with a 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 Vinay Indoria. 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, Dubai, Korea and China in multiple languages. Preface Let Us C has been part of learning and teaching material in most Engineering and Science Institutes round the country for years now. From last year or so, I received several suggestions that its size be pruned a bit, as many learners who learn C language in their Engineering or Science curriculum have some familiarity with it. I am happy to fulfill this request. I hope the readers would appreciate the lean look of the current edition. In one of the previous edition I had realigned the chapters in such a manner that if a C programming course is taught using Let Us C, it can be finished in 22 lectures of one hour each, with one chapter’s contents devoted to one lecture. I am happy that many readers liked this idea and reported that this has made their learning path trouble-free. A more rational reorganization of end-of-chapter Exercises in the book has also been well-received. Riding on that feedback I had introduced one more feature in the fifteenth edition—KanNotes. These are hand-crafted notes on C programming. From the reader's emails I gather that they have turned out to be very useful to help revise their concepts on the day before the examination, viva-voce or interview. Many readers also 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. In this edition I have added a separate section titled ‘Programs’ in each chapter. It contains interesting programs based on the topics covered in the chapter. 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 ‘Exploring C’. As usual, new editions of these two books have also been launched along with 17th 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] or [email protected] Countless Indians have relentlessly worked for close to three decades to successfully establish “India” as a software brand. At times, I take secret pleasure in seeing that Let Us C has contributed in its own small little way in shaping so many careers that have made the “India” brand acceptable. 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 sixteen editions. I have followed the same principle with this edition too. All the best and happy programming! Yashavant Kanetkar Table of Contents 1. Getting Started What is C? Getting Started with C Alphabets, Digits and Special Symbols Constants, Variables and Keywords Types of 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 Form of a C Program Comments in a C Program What is main() ? Variables and their Usage printf() and its Purpose Compilation and Execution Receiving Input Programs Exercises KanNotes 1. C Instructions Types of Instructions Type Declaration Instruction Arithmetic Instruction Integer and Float Conversions Type Conversion in Assignments Hierarchy of Operations Associativity of Operators Control Instructions Programs Exercises KanNotes 1. Decision Control Instruction The if - else Statement Multiple Statements within if - else Nested if-else s A Word of Caution Programs Exercises KanNotes 1. More Complex Decision Making Use of Logical Operators - Checking Ranges The else if Clause Use of Logical Operators - Yes / No Problem The ! Operator Hierarchy of Operators Revisited The Conditional Operators Programs Exercises KanNotes 1. Loop Control Instruction Loops The while Loop Tips and Traps More Operators Programs Exercises KanNotes 1. More Complex Repetitions The for Loop Nesting of Loops Multiple Initializations in the for Loop The break Statement The continue Statement The do-while Loop The Odd Loop Programs Exercises KanNotes 1. Case Control Instruction Decisions using switch The Tips and Traps switch versus if-else Ladder The goto Keyword Programs Exercises KanNotes 1. Functions What is a Function? Why use Functions? Passing Values between Functions Order of Passing Arguments Using Library Functions One Dicey Issue Return Type of Function Programs Exercises KanNotes 1. Pointers Call by Value and Call by Reference An Introduction to Pointers Back to Function Calls Conclusions Programs Exercises KanNotes 1. Recursion Recursion Programs Exercises KanNotes 1. Data Types Revisited Integers— long, short, signed , unsigned Chars— signed, unsigned Reals— float , double, long double A Few More Issues… Storage Classes in C Automatic Storage Class Register Storage Class Static Storage Class External Storage Class A Few Subtle Issues Which to Use When Exercises KanNotes 1. 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 The Build Process Programs Exercises KanNotes 1. Arrays What are Arrays? A Simple Program using Array More on Arrays Array Initialization Array Elements in Memory Bounds Checking Passing Array Elements to a Function Pointers and Arrays Accessing Array Elements using Pointers Passing an Array to a Function Flexible Arrays Programs Exercises KanNotes 1. Multidimensional Arrays Two-Dimensional Arrays Initializing a 2-D Array Memory Map of a 2-D Array Pointers and 2-D Arrays Pointer to an Array Passing 2-D Array to a Function Array of Pointers 3-D Array Programs Exercises KanNotes 1. Strings What are Strings More about Strings Pointers and Strings Standard Library String Functions strlen() strcpy() strcat() strcmp() Programs Exercises KanNotes 1. Handling Multiple Strings 2-D Array of Characters Array of Pointers to Strings Limitation of Array of Pointers to Strings Programs Exercises KanNotes 1. Structures Why use Structures? Array of Structures Intricacies of Structures Structure Declaration Storage of Structure Elements Copying of Structure Elements Nested Structures Passing Structure Elements / Structure Variables Packing Structure Elements Uses of Structures Programs Exercises KanNotes 1. Console Input/Output Types of I/O Console I/O Functions Formatted Console I/O Functions sprintf() and sscanf() Functions Unformatted Console I/O Functions Exercises KanNotes 1. File Input/Output File Operations Opening a File Reading from a File Closing the File Counting Characters, Tabs, Spaces, … A File-Copy Program File Opening Modes String (Line) I/O in Files Text Files and Binary Files Record I/O in Files Modifying Records Low-Level File I/O A Low-Level File-Copy Program Programs Exercises KanNotes

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.