IT training let us c (5th ed ) kanetkar 2004 11 30 1

728 31 0
  • Loading ...
    Loading ...
    Loading ...

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Tài liệu liên quan

Thông tin tài liệu

Ngày đăng: 05/11/2019, 15:56

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 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 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 Also, all the programs present in the book are available in source code form at You are free to download them, improve them, change them, 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 All the best and happy programming! vii Contents 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 10 11 11 12 13 19 21 23 24 25 29 29 31 34 37 37 38 The Decision Control Structure 49 Decisions! Decisions! The if Statement The Real Thing Multiple Statements within if The if-else Statement 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 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 The Case Control Structure Decisions Using switch The Tips and Traps switch Versus if-else Ladder The goto Keyword Summary Exercise ix 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 Functions & Pointers 157 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 Data Types Revisited 158 165 166 171 172 173 174 175 178 178 179 186 189 189 194 197 201 201 213 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 x 214 216 217 219 221 223 224 226 227 230 233 234 235 724 Let Us C F Helper.h File 725 726 Let Us C LRESULT CALLBACK WndProc ( HWND, UINT, WPARAM, LPARAM ) ; HINSTANCE hInst ; // current instance /* FUNCTION: InitInstance ( HANDLE, int PURPOSE: Saves instance handle and creates main window COMMENTS: In this function, we save the instance handle in a global variable and create and display the main program window */ BOOL InitInstance ( HINSTANCE hInstance, int nCmdShow, char* pTitle ) { char classname[ ] = "MyWindowClass" ; HWND hWnd ; WNDCLASSEX wcex ; wcex.cbSize = sizeof ( WNDCLASSEX ) ; = CS_HREDRAW | CS_VREDRAW ; wcex.lpfnWndProc = ( WNDPROC ) WndProc ; wcex.cbClsExtra =0; wcex.cbWndExtra =0; wcex.hInstance = hInstance ; wcex.hIcon = NULL ; wcex.hCursor = LoadCursor ( NULL, IDC_ARROW ) ; wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + ) ; wcex.lpszMenuName = NULL ; wcex.lpszClassName = classname ; wcex.hIconSm = NULL ; if ( !RegisterClassEx ( &wcex ) ) return FALSE ; hInst = hInstance ; // Store instance handle in our global variable hWnd = CreateWindow ( classname, pTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL ) ; if ( !hWnd ) Appendix F: Helper.h return FALSE ; ShowWindow ( hWnd, nCmdShow ) ; UpdateWindow ( hWnd ) ; return TRUE ; } 727 728 Let Us C G Boot Parameters 729 730 Let Us C T he disk drives in DOS and Windows are organized as zerobased drives That is, drive A is drive number 0, drive B is drive number 1, drive C is drive number 2, etc The hard disk drive can be further partitioned into logical partitions Each drive consists of four logical parts—Boot Sector, File Allocation Table (FAT), Directory and Data space When a file/directory is created on the disk, instead of allocating a sector for it, a group of sectors is allocated This group of sectors is often known as a cluster How many sectors together form one cluster depends upon the capacity of the disk As the capacity goes on increasing, so also does the maximum cluster number Accordingly, we have 12-bit, 16-bit or 32-bit FAT In a 12-bit FAT each entry is of 12 bits Since each entry in FAT represents a cluster number, the maximum cluster number possible in a 12-bit FAT is 212 (4096) Similarly, in case of a 16-bit FAT the maximum cluster number is 216 (65536) Also, for a 32-bit FAT the maximum cluster number is 228 (268435456 Only 28 of the 32 bits are used in this FAT) All FAT systems are not supported by all versions of DOS and Windows For example, the 32-bit FAT system is supported only in Win 95 OSR2 version or later There are differences in the organization of contents of Boot Sector, FAT and Directory in FAT12/FAT16 system on one hand and FAT32 on the other In Chapter 19 Figure 19.6 we saw the breakup of the contents of the boot sector of a 12-bit FAT Given below are the contents of a boot sector of 16-bit FAT and a 32-bit FAT 731 Appendix G: Boot Parameters Description Length Typical Values Jump instruction EB3C90 OEM name MSWIN4.1 Bytes per sector 512 Sectors per cluster 64 Reserved sectors Number of FAT copies Max Root directory entries 512 Total sectors Media descriptor F8 Sectors per FAT 256 Sectors per track 63 No of sides 255 Hidden sectors 63 Huge sectors 4192902 BIOS drive number 128 Reserved sectors 1 Boot signature 41 Volume ID 4084677574 Volume label 11 ICIT File system type FAT16 Figure G.1 Let us now take a look at the 32-bit FAT system’s boot sector contents These are shown in Figure G.2 732 Let Us C Description Length Typical Values Jump instruction EB5890 OEM name MSWIN4.1 Bytes per sector 512 Sectors per cluster Reserved sectors 51 Number of FAT copies Root directory entries Total sectors Media descriptor F8 Sectors per FAT Sectors per track 63 No of sides 255 Hidden sectors 63 High word of hidden sectors 63 Huge sectors 4192902 High word of huge sectors 4192902 Sectors per FAT 4095 High word of sectors per FAT 4095 Drive description flag File system version Root directory starting cluster 2 High word of root directory starting cluster 2 File system information sector Back up boot sector Reserved continued… 733 Appendix G: Boot Parameters …continued BIOS drive number 128 Reserved Boot signature 41 Volume ID 649825316 Volume label 11 ICIT File system type FAT32 Figure G.2 There are significant changes in the contents of the boo t sector of a 32-bit FAT system The entries ‘Number of hidden sectors’ and ‘Huge sectors’ have now been made 4-byte entries The first two bytes contain the low word of the value, whereas, the next two bytes contain the high word value The number of sectors per FAT in a 32-bit file system is likely to exceed what can be accommodated in two bytes Hence the entry ‘Sectors per FAT’ for a disk with a 32-bit file system would typically have a value The value of ‘Sectors per FAT’ is now stored as a 4-byte entity, with the similar arrangement of low word and high word as discussed earlier The boot sector of a 32-bit FAT system also has new entries like ‘Drive description flag’, ‘File system version’ ‘Starting cluster number of the root directory’, ‘Sector number of the file system information sector’, and the sector number of the ‘Backup boot sector’ The ‘Drive description flag’ is a two-byte entity Bit of this flag indicates whether or not the information written to the active FAT will be written to all copies of the FAT The low four bits of this entry contains the 0-based FAT number of the active FAT These bits are meaningful only if bit is set 734 Let Us C In the entry ‘File system version number’ the high byte contains the major version number, whereas, the low byte contains the minor version number The entry ‘File system information sector’ contains a value indicating the sector number where the file system information is present This file system information consists of the fields shown in Figure G.3 Description Length File system signature Total number of free clusters Sector number of the next free cluster Reserved Figure G.3 The entry ‘File information sector’ contains a value OFFFFh if there is no such sector The entry ‘Backup boot sector’ contains a value 0FFFFh is there is no backup boot sector Otherwise this value is any non-zero value less than the reserved sector count H Linux Installation 735 736 Let Us C T his appendix gives the steps that are to be carried out for installing Red Hat Linux 9.0 In addition I have also indicated a few commands that are necessary to compile and execute the programs given in Chapters 20 and 21 Follow the steps mentioned below to carry out the installation (a) (b) (c) (d) (e) (f) (g) (h) (i) (j) (k) (l) (m) (n) (o) (p) (q) (r) Configure the system to boot from CDROM drive Insert the first CD in the drive and boot the system from it Hit ‘Enter’ key when the ‘boot’ prompt appears Select the ‘Skip’ option in the "CD Found" dialog box Click on the ‘Next’ button in the ‘Welcome’ screen Click on the ‘Next’ button in the ‘Language selection’ screen Click on the ‘Next’ button in the ‘Keyboard’ screen Click on the ‘Next’ button in the ‘Mouse Configuration’ screen Select the ‘Custom’ option in the ‘Installation Type’ screen and then click on the ‘Next’ button Click on the ‘Next’ button in the ‘Disk Partitioning Setup’ screen Select the ‘Keep all partitions and use existing free space’ option in the ‘Automatic Partitioning’ screen and then click on the ‘Next’ button Ignore any warnings generated by clicking on the ‘OK’ button Click on the ‘Next’ button in the ‘Boot loader configuration’ screen Click on the ‘Next’ button in the ‘Network configuration’ screen Click on the ‘Next’ button in the ‘Firewall configuration’ screen Click on the ‘Next’ button in the ‘additional language support’ screen Select a suitable option in the ‘Time zone offset’ screen and click on the ‘Next’ button Type a password for the root account in the ‘Set root password’ screen and then click on the ‘Next’ button Click on the ‘Next’ button in the ‘Authentication configuration’ screen Appendix H: Linux Installation 737 (s) In the ‘Package group selection’ screen make sure that you select the following options—X window system, K desktop environment, Development tools, GNOME software development and then click on the ‘Next’ button (t) Select ‘No’ option in the ‘Boot diskette creation’ screen (u) Click on the ‘Next’ button in the ‘Graphical Interface (x) configuration’ screen (v) Click on the ‘Next’ button in the ‘Monitor configuration’ screen (w) In the ‘Customize graphical configuration’ screen select the ‘Graphical’ option and then click on ‘Next’ button (x) Once the system restarts configure the system to boot from Hard Disk Using Red Hat Linux For logging into the system enter the username and password and select the session as KDE (K Desktop Environment) Once you have logged in, to start typing the program use the following menu options: KMenu | Run Command A dialog would now pop up In this dialog in the command edit box type KWrite and then click on the Ok button Now you can type the program and save it To compile the program you need to go the command prompt This can be done using the following menu option KMenu | System Tools | Terminal Once at the command prompt you can use the gcc compiler to compile and execute your programs You can launch another instance of the command prompt by repeating the step mentioned above 738 Let Us C ... 99 10 1 10 5 10 7 11 4 11 5 11 6 11 8 12 0 12 1 12 4 12 4 13 5 13 6 14 0 14 4 14 5 14 8 14 9 Functions & Pointers 15 7 What is a Function Why Use Functions Passing Values between Functions Scope Rule of Functions... Function Calls Conclusions Recursion Recursion and Stack Adding Functions to the Library Summary Exercise Data Types Revisited 15 8 16 5 16 6 17 1 17 2 17 3 17 4 17 5 17 8 17 8 17 9 18 6 18 9 18 9 19 4 19 7 2 01. .. Hierarchy of Operations Associativity of Operators Control Instructions in C Summary Exercise 10 11 11 12 13 19 21 23 24 25 29 29 31 34 37 37 38 The Decision Control Structure 49 Decisions! Decisions!
- Xem thêm -

Xem thêm: IT training let us c (5th ed ) kanetkar 2004 11 30 1 , IT training let us c (5th ed ) kanetkar 2004 11 30 1