Quick & Indepth C With Data Structures Sudripta Nandy 1 Quick & Indepth C With Data Structures ABOUT THE AUTHOR Sudripta has almost 20 years of experience working on various computer technologies ranging from desktop computers to Mainframes and to data centers. He has worked on multiple projects for many Fortune 500 companies and in areas like operating system, media technologies, content protection, data processing, backup technologies, services, setup and application development. Professionally, he has spent his spare time educating and training upcoming young minds to the intricacies of programming. In the personal front, he divides his time playing with his sports fanatic son, being a couch potato and serving 'Her Majesty' - His wife :-). He can be reached at [email protected]. To receive a soft copy of the program source codes discussed in this book, you may email a request to the author with the subject line as 'Quick and Indepth C - Source codes'. Please provide your name, your country of residence, the dealer details and the purchase date in the body of the email. 2 ACKNOWLEDGMENTS I may be the author of this book, but, it would have been impossible without the love and support of many people around me. People who helped me grow up, get educated, guided me, inspired me, made this book a possibility. There are so many people who touched my life, that I would need hundreds of pages to dedicate them individually. If someone is left out over here, do remember that you reside in the most precious place within me – my heart. The first person I want to dedicate this book to, is my late grandfather, whose life principles I carry to this day. I can relate many of my characteristics to him – some may not suit this modern era but, I am proud of them. The next person who helped me shape up as a person and still doing so, is my mother. She spent so many sleepless nights when I got those asthma attacks. I slept because she stayed awake, I am alive because she stayed strong. She is my pillar of strength. When I think about my life, its incomplete without my father – the person who never got angry on me, who died working so that I could live. The most simple yet straight forward person I know. He left a void in my heart which can never be filled. The person who fills my life with the warmth I look for, is my son. Being a father is the best feeling a person can have. He has helped me grow so much as a person and as a father. The next person who completes me is my wife – the best friend in my entire life. The love and support, I get from her help me in life’s every journey I take. A person’s life is incomplete without a sister, and I am lucky to have two. The biggest support pillars of my life, the persons I can always count on when in need. My grandmother is the person in whose lap I started off my journey called life. My niece is the person who gave me the first feelings of being the older generation – and what a beautiful feeling. Still visualize those chubby cheeks and big round eyes. There are many others who played an important role in my life and guided me towards this journey. My father-in-law, mother-in-law, moni, mesho, mezdabhai, brothers-in-law, sister-in-law, nephews, Sumit – all helped me become the person I am today. They may not have directly helped me in writing this book but, have helped the author within me evolve. This book is also dedicated to the many friends and colleagues I had over the years. I learned from every colleague (special mention A Chakrabarti, A Ray) I had during my entire professional career. The work I did within my organization also helped me grow as a professional and come to this point where I can confidently write a book. 3 © 2018 Sudripta Nandy, Salt Lake, Kolkata-700091, India, All Rights Reserved. No part of this book, including texts, source codes, drawings, images, interior design, cover design, icons, may be reproduced or transmitted in any form except with the permission of the author 'Sudripta Nandy' of Salt Lake, Kolkata-700091, India. Legal Disclaimer: The author of this book has made every attempt to ensure the accuracy and reliability of the information provided in this book. However, the information is provided 'as is' without warranty of any kind. The book provides reference information on the subject discussed, and the use and application of the provided information is entirely dependent on the discretion of the reader. The author and the publisher assume no responsibility (and liability) for the use of such information in whatever possible manner. The author and the publisher will not be responsible for any loss or damage of whatever nature (direct, indirect, consequential or other) whether arising in contract, tort or otherwise, which may arise as a result of the use of (or inability to use) this book, or from the use of (or failure to use) the information in this book. The author and publisher also assume no responsibility regarding the web links and their contents specified in this book. The web links are provided to help the reader have easy and quick availability of matter discussed in this book. The web links may further redirect the user to other web locations. The web links and their locations, the redirected locations and their contents are provided ‘as is’ without warranty of any kind. The author and publisher of this book hold no responsibility (and liability) for the web links and their locations, their redirected locations and their contents. Their access, use, view, download, execution are entirely at the sole discretion of the person doing so. The author and the publisher will not be responsible for any loss or damage of whatever nature (direct, indirect, consequential or other) whether arising in contract, tort or otherwise, which may arise as a result of the use of (or inability to use) those links and locations, or from the use of (or failure to use) the contents available through those links or locations. 4 CONTENTS INTRODUCTION TO C..................................................................................................13 1.1 IMPORTANCE OF C............................................................................................................................13 1.2 WRITING PROGRAMS........................................................................................................................13 1.3 OUR FIRST PROGRAM.......................................................................................................................14 1.4 STRUCTURE OF C PROGRAMS..........................................................................................................16 1.5 EXECUTING A 'C' PROGRAM.............................................................................................................16 1.6 IDENTIFIERS IN C.............................................................................................................................17 DATA TYPES, VARIABLES AND CONSTANTS................................................................18 2.1 INTRODUCTION.................................................................................................................................18 2.2 CHARACTER REPRESENTATION........................................................................................................18 2.3 TOKENS IN C......................................................................................................................................19 2.4 KEYWORDS.........................................................................................................................................19 2.5 CONSTANTS.......................................................................................................................................20 2.5.1 INTEGER CONSTANTS...............................................................................................................20 2.5.2 REAL CONSTANTS.....................................................................................................................21 2.5.3 SINGLE CHARACTER CONSTANTS............................................................................................21 2.5.4 STRING CONSTANTS.................................................................................................................22 2.6 IDENTIFIERS......................................................................................................................................22 2.7 VARIABLES.........................................................................................................................................22 2.7.1 VARIABLE DECLARATION..........................................................................................................23 2.8 DATA TYPES........................................................................................................................................23 2.8.1 PRIMARY DATA TYPES...............................................................................................................23 2.8.2 SIZE AND RANGE OF PRIMARY DATA TYPES............................................................................25 2.8.3 DERIVED DATA TYPES................................................................................................................26 2.8.4 ALIAS DATA TYPES....................................................................................................................26 2.8.5 void DATA TYPE..........................................................................................................................28 2.9 STORAGE CLASSES............................................................................................................................28 2.10 DEFINING SYMBOLIC CONSTANTS AND MACROS.........................................................................29 2.11 DECLARING A VARIABLE AS A CONSTANT....................................................................................31 2.12 VOLATILE VARIABLES......................................................................................................................31 5 Quick & Indepth C With Data Structures 2.13 DATA OVERFLOW AND UNDERFLOW................................................................................................32 2.14 SAMPLE PROGRAM..........................................................................................................................32 OPERATORS AND OPERATIONS....................................................................................34 3.1 INTRODUCTION.................................................................................................................................34 3.2 ARITHMETIC OPERATORS.................................................................................................................34 3.3 INCREMENT AND DECREMENT OPERATORS.....................................................................................35 3.4 ASSIGNMENT OPERATORS................................................................................................................37 3.5 BITWISE OPERATORS........................................................................................................................37 3.6 RELATIONAL OPERATORS..................................................................................................................40 3.7 LOGICAL OPERATORS........................................................................................................................41 3.8 CONDITIONAL (TERNARY) OPERATOR..............................................................................................42 3.9 SPECIAL OPERATORS........................................................................................................................43 3.9.1 COMMA OPERATOR....................................................................................................................43 3.9.2 sizeof OPERATOR.......................................................................................................................43 3.10 ARITHMETIC EXPRESSIONS...........................................................................................................43 3.11 TYPE CONVERSIONS.......................................................................................................................45 3.12 OPERATOR PRECEDENCE AND ASSOCIATIVITY.............................................................................47 3.13 SAMPLE PROGRAMS........................................................................................................................48 INPUT, OUTPUT AND FORMAT SPECIFIERS................................................................52 4.1 INTRODUCTION.................................................................................................................................52 4.2 READING INPUT.................................................................................................................................52 4.2.1 SIMPLE INPUT FUNCTIONS......................................................................................................52 4.2.2 FORMATTED INPUT...................................................................................................................53 4.3 FORMAT SPECIFIERS.........................................................................................................................54 4.3.1 FORMATTED INPUT [EXAMPLES]..............................................................................................56 4.4 GENERATING OUTPUT.......................................................................................................................56 4.4.1 SIMPLE OUTPUT FUNCTIONS..................................................................................................56 4.4.2 FORMATTED OUTPUT................................................................................................................57 4.5 SAMPLE PROGRAMS..........................................................................................................................58 ARRAYS........................................................................................................................61 5.1 INTRODUCTION.................................................................................................................................61 5.2 ONE-DIMENSIONAL ARRAYS.............................................................................................................62 6 5.2.1 INITIALIZATION OF ONE-DIMENSIONAL ARRAYS...................................................................62 5.2.2 STRINGS....................................................................................................................................63 5.3 TWO-DIMENSIONAL ARRAYS............................................................................................................64 5.3.1 INITIALIZATION OF TWO-DIMENSIONAL ARRAYS...................................................................68 5.3.2 STRING ARRAYS........................................................................................................................69 5.4 MULTI-DIMENSIONAL ARRAYS..........................................................................................................69 5.5 SAMPLE PROGRAMS..........................................................................................................................70 CONDITIONAL STATEMENTS AND BRANCHING..........................................................76 6.1 INTRODUCTION.................................................................................................................................76 6.2 DECISION CONTROL USING if STATEMENT......................................................................................76 6.3 DECISION CONTROL USING if-else STATEMENT..............................................................................78 6.4 DECISION CONTROL USING if-else LADDER STATEMENTS.............................................................80 6.5 DECISION CONTROL USING NESTED if STATEMENTS.....................................................................82 6.6 DECISION CONTROL USING switch STATEMENT.............................................................................84 6.7 DECISION CONTROL USING CONDITIONAL (TERNARY) OPERATOR...............................................87 6.8 DECISION CONTROL USING goto STATEMENT................................................................................88 6.9 SAMPLE PROGRAMS..........................................................................................................................89 CONDITIONAL STATEMENTS WITH LOOPS..................................................................94 7.1 INTRODUCTION.................................................................................................................................94 7.2 THE while LOOP..................................................................................................................................95 7.3 THE do while LOOP............................................................................................................................96 7.4 THE for LOOP.....................................................................................................................................97 7.5 NESTED LOOPS................................................................................................................................100 7.6 JUMPING WITHIN LOOPS................................................................................................................102 7.6.1 THE break STATEMENT...........................................................................................................102 7.6.2 THE continue STATEMENT......................................................................................................105 7.7 CONCISE TEST CONDITIONS.........................................................................................................108 7.8 SAMPLE PROGRAMS........................................................................................................................108 POINTERS..................................................................................................................114 8.1 INTRODUCTION...............................................................................................................................114 8.2 DECLARING, INITIALIZING AND ASSIGNING POINTERS..............................................................116 8.3 ACCESSING A VARIABLE USING ITS POINTER..............................................................................118 7 Quick & Indepth C With Data Structures 8.4 ARRAY OF POINTERS AND POINTER TO AN ARRAY.......................................................................119 8.4.1 ARRAY OF POINTERS..............................................................................................................119 8.4.2 POINTER TO AN ARRAY...........................................................................................................120 8.5 POINTER ARITHMETIC....................................................................................................................122 8.6 POINTER TO ANOTHER POINTER....................................................................................................129 8.7 SAMPLE PROGRAMS........................................................................................................................131 CHARACTER STRING OPERATIONS...........................................................................136 9.1 INTRODUCTION...............................................................................................................................136 9.2 UNICODE CHARACTERS..................................................................................................................138 9.3 ACCEPTING STRINGS FROM USER.................................................................................................139 9.4 PRINTING STRINGS TO CONSOLE..................................................................................................142 9.5 ARITHMETIC OPERATIONS WITH CHARACTERS...........................................................................143 9.6 ARITHMETIC OPERATIONS WITH STRINGS...................................................................................144 9.7 STRING PROCESSING FUNCTIONS................................................................................................144 9.7.1 GENERAL STRING PROCESSING FUNCTIONS.......................................................................144 9.7.2 MEMORY COPY AND COMPARISON FUNCTIONS...................................................................150 9.7.3 SINGLE CHARACTER PROCESSING FUNCTIONS..................................................................151 9.7.4 STRING CONVERSION FUNCTIONS.......................................................................................153 9.8 SAMPLE PROGRAMS........................................................................................................................158 STRUCTURES AND UNIONS.......................................................................................162 10.1 INTRODUCTION............................................................................................................................162 10.2 DEFINING A STRUCTURE..............................................................................................................162 10.3 ACCESSING MEMBERS.................................................................................................................164 10.4 STRUCTURE INITIALIZATION......................................................................................................166 10.5 ARRAY OF STRUCTURE.................................................................................................................167 10.6 POINTER TO STRUCTURE.............................................................................................................169 10.7 NESTED STRUCTURES..................................................................................................................170 10.8 BIT FIELDS....................................................................................................................................173 10.9 UNIONS.........................................................................................................................................176 USER DEFINED FUNCTIONS......................................................................................179 11.1 INTRODUCTION............................................................................................................................179 11.2 THE STRUCTURE OF A C FUNCTION............................................................................................180 8 11.2.1 FUNCTION NAME..................................................................................................................180 11.2.2 FUNCTION RETURN TYPE AND VALUES..............................................................................181 11.2.3 FUNCTION ARGUMENTS......................................................................................................182 11.2.4 FUNCTION BODY...................................................................................................................183 11.2.5 EXAMPLE PROGRAMS...........................................................................................................183 11.3 FUNCTION VARIABLES: SCOPE AND LIFETIME...........................................................................184 11.3.1 AUTOMATIC VARIABLES.......................................................................................................185 11.3.2 GLOBAL AND EXTERNAL VARIABLES...................................................................................186 11.3.3 STATIC VARIABLES...............................................................................................................189 11.3.4 REGISTER VARIABLES..........................................................................................................191 11.4 FUNCTIONS WITH POINTER ARGUMENTS..................................................................................191 11.4.1 FUNCTION POINTER AS ARGUMENT...................................................................................193 11.5 FUNCTIONS WITH ARRAY ARGUMENTS......................................................................................195 11.6 FORWARD DECLARATION OF FUNCTIONS...................................................................................197 11.7 VARIABLE NUMBER OF ARGUMENTS...........................................................................................199 11.8 RECURSION...................................................................................................................................200 11.9 SAMPLE PROGRAMS......................................................................................................................203 FILE MANAGEMENT...................................................................................................210 12.1 INTRODUCTION.............................................................................................................................210 12.2 CREATING AND OPENING FILE.....................................................................................................211 12.3 CLOSING A FILE............................................................................................................................212 12.4 WRITING-TO AND READING-FROM A TEXTUAL FILE..................................................................213 12.4.1 WRITING AND READING SINGLE CHARACTERS.................................................................213 12.4.2 WRITING AND READING INTEGERS.....................................................................................215 12.4.3 WRITING AND READING STRINGS.......................................................................................215 12.4.4 WRITING AND READING FORMATTED DATA........................................................................216 12.5 WRITING-TO AND READING-FROM A BINARY FILE.....................................................................221 12.6 MISCELLANEOUS FILE MANAGEMENT FUNCTIONS....................................................................226 DYNAMIC MEMORY ALLOCATION..............................................................................228 13.1 INTRODUCTION.............................................................................................................................228 13.2 C PROGRAM – MEMORY LAYOUT...................................................................................................228 13.3 ALLOCATING MEMORY..................................................................................................................230 13.4 ALTERING ALLOCATED MEMORY SIZE..........................................................................................232 9