Preface – C Programming Essentials

Preface

Why Another New Book on C?

The popularity of C as a tool for teaching programming to students across the globe has resulted in a huge number of books on C. However, the authors feel that almost all the books available in the Indian market are lacking in an in-depth teaching of the programming techniques.

C is merely a language, which facilitates writing of programs for computers. While an in-depth understanding of the language is indeed the cornerstone to success, what separates the master programmers from the novice is usually the superior programming skills acquired by the latter over the years. While learning and understanding C is science, programming is an art. There are a few thumb rules and some good practices, but all of them have their exceptions too. Thus, this book not only aims at teaching the best practices and standard techniques, but also gives the background of the technique and why it should be used. Sometimes alternate approaches are also discussed, which have their own benefits and used in some typical cases.

Relevance of C in Today’s World

C was developed in 1970 by Brian W. Kernighan and Dennis M. Ritchie, and has stood the test of time in being one of the most successful computer languages of all time. C was originally developed for UNIX systems, but the ease of usage soon made it spread to other operating systems as well. Currently, C compilers are available for all the known major system architectures and operations systems. Most of the present day successful operating systems were originally written in C, for example, UNIX and Microsoft Windows. In fact, that the C compiler is amongst the first set of programs that are written for any new architecture, is proof to reveal the power of the language.

Although the advent of high-level languages like Java, C#, C++, and many others have made software development easier, the significance of C has not ended. Today, C is being increasingly used to write low-level code for smaller processors that are embedded in electronic devices that surround us. Mobile phones, washing machines, set-top boxes, and many other consumer electronics devices utilize embedded processors. C is almost the only language that is used to write software routines for these devices.

In the software industry, C is widely used to write programs which act with the hardware directly like OS kernels and device drivers. C is often touted as a middle-level language, which has the features of a low-level language like bit manipulation and usage of pointers along with the features of high-level languages like structured programming. This gives C a very stable position in the scenario where we cannot have an operating system like in low-end mobile phones, portable music players, and other similar electronics goods which surround our lives. This is fast becoming a very big market with a huge potential for growth. With the prices of electronics devices dropping at a steady state, this market will continue to increase at a faster pace.

Last, but not the least, C has a huge role in being a language for teaching programming concepts to serious programmers. This is mainly due to the fact that most modern-day languages like Java, C++, and C# (along with various scripting languages like JavaScript and PHP) use the C style of syntax. Nearly all the procedural languages that have been developed after C have been greatly influenced by C. So knowing C makes the transition to such languages very easy. Most of the modern day high-end tools that facilitate programming were originally developed for C like code level debuggers, diassemblers, code profilers, and syntax checkers.

Features of This Book

C support for design and programming has improved dramatically over the years, and lots of new helpful techniques have been developed for its use. This book introduces standard C and the key programming and design techniques supported by C. This book also presents every major C language feature and the standard library.

It is organized around language, syntax, programming and library functions of C. However, features are presented in the context of their use. That is, the focus is on the language as the tool for design and programming rather than on the language itself. This book demonstrates the key techniques that make C effective and teaches the fundamental concepts necessary for mastery.

Chapter 1 presents an overview of programming concepts. Chapter 2 deals with the foundation where we explain keywords of C language, data types, expressions, operators, and functions together with the structure of a complete C program. We also discuss the storage classes available in the language. In Chapter 3, the control structures available in the C programming language is thoroughly explained.

In Chapter 4, we demonstrate how functions are designed, how two or more functions are put together, and how they communicate with each other. Recursive functions are also discussed in detail in this chapter. Multi-dimensional arrays and strings are the pivotal concept in C language. It is explained in the light of their applications in Chapter 5 whereas Chapter 6 deals with pointers. The basics, benefits, mid pitfalls of using pointers are too explained. Moreover, the relationship between arrays and pointers are discussed and how pointers can elegantly be used in a program to make it efficient is addressed.

User-defined data types are addressed in Chapter 7, where the main issue is the discussion of how one could create their own data types, so that data becomes convenient to handle and simplify the programming task.

In Chapter 8, we present the ways of handling disk data files in C. The C pre-processor is a program that processes a C source file before the compiler translates the program into an object code. We present the pre-processor in Chapter 9.

The last six topics are six appendices from A to F. Appendix A helps to remove common errors from a C program. Bitwise operators and their use are presented elegantly in Appendix B through some examples. Using functions from stdlibs.h is shown in Appendix C. The technique of creating own libraries is given in Appendix D. A brief introduction to C-99 is presented in Appendix E. Finally, Appendix E is offered as a practice set of short answer type problems and a set of programming problems too. Some of these programming problems are solved.

Target Audience for This Book

This book is aimed mainly at the beginners of programming. No prior programming knowledge is assumed. The target audience includes:

  • First-time programmers and students of computer programming courses. The book can be used as course book for the standard one-semester-long courses in programming using C language. Some of the likely sources in the Indian scenario are B.Sc., B.Tech., and BCA.

  • Hobbyist programmer trying to jump into the programming world. Although programming is a huge industry, today there are many hobbyists who pursue this as a fun. Such enthusiastic people can benefit immensely by going through this book.

  • The programmers who already know C but want to brush up their skills. This book can be looked upon as a reference material by the users familiar with the basics of the language, although it is meant to be read in a serial manner for learners.

  • Last but not the least, the authors hope that the experienced C programmer will also benefit from this book while solving advanced exercises.

K. N. Dey

S. K. Bandyopadhyay

Acknowledgements

It has been a privilege to write this book, successfully accomplished with the cooperation and help of many individuals. Our sincere gratitude to each one of them goes beyond any barriers. We would like to take this opportunity to express our regards and henceforth urge the reader to take a few minutes to read this section before moving onto the rest of this book.

We, as faculty members of the Department of Computer Science and Engineering, University of Calcutta, are indebted to our colleagues and students. Their inspiration and constant support was a driving force that requires special mention. Dr Rajib Das played an instrumental role in supporting us with some programming problems and debugging them wherever required.

Sanjit Bhattacharjee, an ex-student of our department, presently with Cognizant Technology Solutions (San Francisco, CA) as a Systems Designer Lead—Data warehousing, gave some invaluable suggestions and helped in shaping this book. Bigyan Bhar, a student of our department, presently an M.Tech. student of Indian Institute of Science Bangalore, came up with new valuable suggestions, which were judiciously incorporated in our book. He also helped in framing some of the appendices.

Debanjana Banerjee, a competent technical assistant of our department, helped in testing many of the programs provided in the book. A special mention to Sovonesh Das and Kaushik Mondal is required for their inspiration and interest behind this book.

We would like to heartily thank Sachin Saxena and Gaurav Jain of Pearson Education who managed to decipher our work and prepared this book with utmost accuracy.

Finally, we are thankful to our families and friends for being supportive and who uncomplainingly beared with our preoccupation.

K. N. Dey

S. K. Bandyopadhyay