08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 2 ■ C – The Basics

when writing programs. If you violate a rule, your program will contain a syntax error. When you

attempt to compile the program, the compiler will inform you of the error. You must correct it and

try again.

The first step to 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. It is like saying that learning some rules of English grammar and being able

to write some correctly formed sentences makes one a novelist. Novel-writing skills require much

more than learning some rules of grammar. Among other things, it requires insight, creativity, and

a knack for using the right words in a given situation.

In the same vein, a good programmer must be able to creatively use the features of the

language to solve a wide variety of problems in an elegant and efficient manner. This is the

difficult part and can be achieved only by long, hard study of problem-solving algorithms and

writing programs to solve a wide range of problems. But we must start with baby steps.

2.2 The C Alphabet

In Section 1.4 we introduced the idea of a character. We can think of the C alphabet as consisting

of all the characters one could type on a standard English keyboard: for example, the digits;

uppercase and lowercase letters; and special characters such as +, =, <, >, &, and %.

More formally, C uses the ASCII (American Standard Code for Information Interchange,

pronounced ass-key) character set. This is a character standard that includes the letters, digits,

and special characters found on a standard keyboard. It also includes control characters such as

backspace, tab, line feed, form feed, and carriage return. Each character is assigned a numeric

code. The ASCII codes run from 0 to 127.

The programs in this book will be written using the ASCII character set. The characters in the

ASCII character set are shown in Appendix B.

Character handling will be discussed in detail in Chapter 6.

2.3 C Tokens

The tokens of a language are the basic building blocks that can be put together to construct

programs. A token can be a reserved word (such as int or while), an identifier (such as b or sum),

a constant (such as 25 or "Alice in Wonderland"), a delimiter (such as } or ;) or an operator

(such as + or =).

For example, consider the following portion of Program P1.4 given at the end of the last

chapter:

int main() {

int a, b, sum;

a = 14;

b = 25;

sum = a + b;

printf("%d + %d = %d\n", a, b, sum);

}

24

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!