C Programming
For Beginners
Noel Kalicharan
Senior Lecturer, Computer Science
The University of the West Indies
St. Augustine, Trinidad
First published September 2005
Republished August 2008
Revised June 2015
Noel Kalicharan, 2005, 2008, 2015
noel.kalicharan@sta.uwi.edu
noelk@hotmail.com
All rights reserved
The text of this publication, or any part thereof, may not be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, storage in an information retrieval system, the Internet, or otherwise, without prior written permission of the author.
Table of Contents
Preface
This book attempts to teach computer programming to the complete beginner using the C language. As such, it assumes you have no knowledge whatsoever about programming. And if you are worried that you are not good at high-school mathematics, dont be. It is a myth that you must be good at mathematics to learn programming. In this book, a knowledge of primary school mathematics is all that is requiredbasic addition, subtraction, multiplication, division, finding the percentage of some quantity, finding an average or the larger of two quantities.
Some of our most outstanding students over the last thirty years have been people with little mathematics background from all walks of lifepoliticians, civil servants, sports people, housewives, secretaries, clerical assistants, artists, musicians and teachers. On the other hand, weve had mathematical folks who didnt do as well as might be expected.
What will be an asset is the ability to think logically or to follow a logical argument. If you are good at presenting convincing arguments, you will probably be a good programmer. Even if you arent, programming is the perfect vehicle for teaching logical thinking skills. You should learn programming for these skills even if you never intend to become a serious programmer.
The main goal of this book is to teach fundamental programming principles using C, one of the most widely used programming languages in the world today. C is considered a modern language even though its roots date back to the 1970s. Originally, C was designed for writing systems programsthings like operating systems, editors, compilers, assemblers and input/output utility programs. But, today, C is used for writing all kinds of applications programs as wellwordprocessing programs, spreadsheet programs, database management programs, accounting programs, games, educational softwarethe list is endless.
However, this book is more about teaching programming basics than it is about teaching C. We discuss only those features and statements in C that are necessary to achieve our goal. Once you learn the principles well, they can be applied to any language.
Chapter 1 gives an overview of the programming process. Chapter 2 describes the basic building blocks needed to write programs. Chapter 3 explains how to write programs with the simplest kind of logicsequence logic. Chapter 4 shows how to write programs which can make decisions. Chapter 5 explains the notion of looping and how to use this powerful programming idea to solve more interesting problems. Chapter 6 deals with the oft-neglected, but important, topic of working with characters. Chapter 7 introduces functionsthe key concept needed for writing large programs. Chapter 8 tackles the nemesis of many would-be programmersarray processing. And Chapter 9 explains how lists of items stored in arrays can be searched, sorted and merged.
The first step in becoming a good programmer is learning the syntax rules of the programming language. This is the easy part and many people mistakenly believe that this makes them a programmer. They get carried away by the cosmeticsthey learn the features of a language without learning how to use them to solve problems.
Of course, you must learn some features. But it is far better to learn a few features and be able to use them to solve many problems rather than learn many features but cant use them to solve anything. For this reason, this book introduces a feature (like an if
statement, say) and then discusses many examples to illustrate how the feature can be used to solve different problems.
This book is intended for anyone who is learning programming for the first time, regardless of age or institution. The material has been taught successfully to students preparing for high-school examinations in Computer Studies or Information Technology, students at college, university and other tertiary-level institutions.
The presentation is based on the experience that many people have difficulty in learning programming. To try and overcome this, we use an approach which provides clear examples, detailed explanations of very basic concepts and numerous interesting problems (not just artificial exercises whose only use is to illustrate some language feature).
While computer programming is essentially a mental activity and you can learn a fair amount of programming from just reading the book, it is important that you get your hands dirty by writing and running programs. One of lifes thrills is to write your first program and get it to run successfully on a computer. Dont miss out on it.
But do not stop there. The only way to learn programming well is to write programs to solve new problems. The end-of-chapter exercises are a very rich source of problems, a result of the authors more than 40 years in the teaching of programming.
Thank you for taking the time to read this book. I hope your venture into programming is a successful and enjoyable one.
Noel Kalicharan
Dedicated to
Christian and Barbara Posthoff
Two of the finest, kindest people
it's been my pleasure to know
CHAPTER 1
Elementary Programming Concepts
In this chapter, we will explain the following:
- How a computer solves a problem
- The various stages in the development of a computer program: from problem definition to finished program
- How a computer executes a program
- What is a data type and its fundamental role in writing a program
- The role of charactersthe basic building blocks of all programs
- The concepts of constants and variables
- The distinction between syntax and logic errors
- How to produce basic output in C using the
printf
statement - What is an escape sequence
- How descriptive or explanatory comments can be included in your program
- What is an assignment statement and how to write one in C
1.1 Programs, Languages and Compilers
We are all familiar with the computers ability to perform a wide variety of tasks. For instance, we can use it to play games, write a letter or a book, perform accounting functions for a company, learn a foreign language, listen to music on a CD, send a fax or search for information on the Internet. How is this possible, all on the same machine? The answer lies with programmingthe creation of a sequence of instructions which the computer can perform (we say execute) to accomplish each task. This sequence of instructions is called a program. Each task requires a different program:
- To play a game, we need a game-playing program.
- To write a letter or a book, we need a word processing program.
- To do accounts, we need an accounting program.
- To learn Spanish, we need a program that teaches Spanish.
- To listen to a CD, we need a music-playing program.
- To send a fax, we need a fax-sending program.
- To use the Internet, we need a program called a Web browser.
For every task we want to perform, we need an appropriate program. And in order for the computer to run a program, the program must be stored (we sometimes say
Next page