2. How Do I Get Started in C?

With the main() Function

image

You get to see your first C program in this chapter! Please don’t try to understand every character of the C programs discussed here. Relax and just get familiar with the look and feel of C. After a while you will begin to recognize elements common to all C programs.

Getting a Glimpse

This section shows you a short but complete C program and discusses another program that appears in Appendix B. Both programs contain common and different elements. The first program is extremely simple. Here it is:

image

If you were to type this program using your C compiler’s editor, compile the program, and run it, you would see this message appear on the screen:

This C stuff is easy!

Note

image

It took a lot of work to produce that one-line message! Actually, of the seven lines in the program, only one—the one that starts with printf—does the work that produces the output. The other lines provide “housekeeping chores” common to most C programs.

Clue

image

To see a really long program, glance at Appendix B. Although the Blackjack game there spans several pages, it contains elements common to the shorter program you just saw.

Look through both the programs just discussed and notice any similarities. One of the first things you might notice is the use of braces ({}), parentheses (()), and backslashes (). Be careful when typing C programs into your C compiler. C gets picky, for instance, if you accidentally type a square bracket ([) when you should type a brace.

Note

image

C isn’t picky about everything. For instance, most of the spacing you see in C programs serves to make the programs clearer to people, not to C. As you program, add blank lines and indent sections of code that go together to help the appearance of the program and to make it easier for you to find what you are looking for.

Clue

image

Use the Tab key to indent instead of typing a bunch of spaces. Most C editors let you adjust the tab spacing (the number of spaces that appear when you press Tab). Some C program lines get long, so a tab setting of three provides ample indention without making lines too long.

C requires that you use lowercase letters for all commands and predefined functions. (You’ll learn what a function is in the next section.) About the only time you use uppercase letters is on a line with #define and inside the printed messages you write.

The main() Function

The most important part of a C program is its main() function. Both of the programs discussed earlier have main() functions. Although at this point the distinction is not critical, main() is a C function, not a C command. A function is a routine that comes with C or that you write. A function is nothing more than a routine that performs some task. C programs are made up of one or more functions. Each program must always include a main() function. A function is distinguished from a command by the parentheses that follow the function name. These are functions:

main()    calcIt()    printf()    strlen()

and these are commands:

return    while    int    if   float

When you read other C programming books and manuals, the author might decide to omit the parenthesis from the end of function names. For example, you might read about the printf function instead of printf(). You’ll learn to recognize function names soon enough so such differences won’t matter a lot to you. Most of the time, authors want to clarify the differences between functions and non-functions as much as possible so you’ll more often see the parenthesis than not.

Warning

image

One of the functions just listed, calcIt(), contains an uppercase letter. However, the preceding section said you should stay away from uppercase. If a name has multiple parts, such as doReportPrint(), it’s common practice to use uppercase letters to begin the separate words to increase readability. (Spaces aren’t allowed in function names.) Stay away from typing words in all uppercase. An uppercase letter for clarity once in a while is okay.

Clue

image

The required main() function and all of C’s supplied function names must contain lowercase letters. You can use uppercase for the functions that you write, but most C programmers stay with the lowercase function name convention.

Just as Chapter 1 marks the beginning place to read in a book, main() is always the first place the computer begins when running your program. If main() is not the first function listed in your program, main() still determines the beginning of the program’s execution. Therefore, make main() the first function in every program you write. The programs in the next several chapters have only one main() function. As you improve your C skills, you’ll learn why adding additional functions after main() improves your programming power even more.

After the word main(), you always see an opening brace ({). When you find a matching closing brace (}), main() is finished. There might be additional pairs of braces within a main() function as well. For practice, look again at the long program in Appendix B. main() is the first function with code, and several other functions follow—each with braces and code.

Note

image

The statement #include <stdio.h> is needed in almost every C program. It helps with printing and getting data. For now, always put this statement somewhere before main(). You will understand why the #include is important in Chapter 7, “What Do #include and #define Mean?”

Kinds of Data

Your C programs must use data made up of numbers, words, and characters; programs process that data into meaningful information. Although there are many different kinds of data, the following three data types are by far the most common used in C programming:

• Characters

• Integers

• Floating-points (also called real numbers)

C’s Characters

A C character is any single character that your computer can represent. Your computer knows 256 different characters. Each of them is found in something called the ASCII table, located in Appendix C. (ASCII is pronounced ask-ee. If you don’t know-ee, you can just ask-ee.) Anything your computer can represent can be a character. Any or all of the following can be considered characters:

A     a     4     %     Q     !     +     =     ]

Clue

image

Even the spacebar produces a character. Just as C needs to keep track of the letters of the alphabet, digits, and all the other characters, it has to keep track of any blanks your program needs.

As you can see, every letter, number, and space is a character to C. Sure, a 4 looks like a number, and it sometimes is, but it is also a character. If you indicate that a particular 4 is a character, you can’t do math with it. If you indicate that another 4 is to be a number, you can do math with that 4. The same holds for the special symbols. The plus sign (+) is a character, but the plus sign also performs addition. (There I go, bringing math back into the conversation!)

All of C’s character data is enclosed in apostrophes ('). Some people call apostrophes single quotation marks. Apostrophes differentiate character data from other kinds of data, such as numbers and math symbols. For example, in a C program, all of the following are character data:

'A'     'a'     '4'     '%'     ' '     '-'

None of the following can be character data because they have no apostrophes around them:

A     a     4     %     -

Clue

image

None of the following are valid characters. Only single characters, not multiple characters, can go inside apostrophes.

'C is fun'  'C is hard'  'I should be sailing!'

The first program in this chapter contains the character ' '. At first, you might not think that is a single character, but it’s one of the few two-character combinations that C interprets as a single character. This will make more sense later.

If you need to specify more than one character (except for the special characters that you’ll learn, like the just described), enclose the characters in quotation marks (" "). A group of multiple characters is called a string. The following is a C string:

"C is fun to learn."

Note

image

That’s really all you need to know about characters and strings for now. Later in this book you’ll learn how to use them in programs. When you see how to store characters in variables, you’ll see why the apostrophe and quotation marks are important.

Numbers in C

Although you might not have thought about it before now, numbers take on many different sizes and shapes. Your C program must have a way to store numbers, no matter what the numbers look like. You must store numbers in numeric variables. Before you look at variables, a review of the kinds of numbers will help.

Whole numbers are called integers. Integers have no decimal points. (Remember this rule: Like most members of Congress, integers have no point whatsoever.) Any number without a decimal point is an integer. All of the following are integers:

10     54     0     -121     -68     752

Warning

image

Never begin an integer with a leading 0 (unless the number is zero), or C will think you typed the number in hexadecimal or octal. Hexadecimal and octal, sometimes called base-16 and base-8, respectively, are weird ways of representing numbers. 053 is an octal number, and 0x45 is a hexadecimal number. If you don’t know what all that means, just remember for now that C puts a hex on you if you mess around with leading zeroes before integers.

Numbers with decimal points are called floating-point numbers. All of the following are floating-point numbers:

547.43     0.0     0.44384     9.1923     -168.470     .22

Clue

image

As you can see, leading zeroes are okay in front of floating-point numbers.

The choice of using integers or floating-point numbers depends on the data your programs are working with. Some values (such as ages and quantities) make great integers, while other values (such as money amounts) make great floating-point numbers. Internally, C stores integers differently from floating-point values. As you can see from Figure 2.1, a floating-point value usually takes twice as much memory as an integer. Therefore, if you can get away with using integers, do so and save floating-points for values that need the decimal point.

Figure 2.1. It often takes more memory to store floating-point values than integers.

image

Note

image

Figure 2.1 shows you that integers generally take less memory than floating-point values, no matter how large or small the values stored there are. On any given day, a large post office box might get much less mail than a smaller one. The contents of the box don’t affect what the box happens to hold. The size of C’s number storage is affected not by the value of the number, but by the type of the number.

Different C compilers use different amounts of storage for integers and floating-point values. As you will learn later, there are ways of finding out exactly how much memory your C compiler uses for each type of data.

Rewards

image

• Keep the Caps Lock key off! Most C commands and functions require lowercase letters.

• Put lots of extra spacing in your C programs to make them more readable.

• A C function must have parentheses following its name. A C program consists of one or more functions. The main() function is always required. C executes main() before any other function.

• If you use a character, enclose it in single quotes. Strings go inside quotation marks. Integers are whole numbers without decimal points. Floating-point numbers have decimal points.

Pitfalls

image

• Don’t be sloppy about your typing. When C needs a certain special character such as a brace, a square bracket will not do.

• Don’t put leading zeroes before integers unless the integer is zero.

In Review

This chapter’s goal was to familiarize you with the “look and feel” of a C program, primarily the main() function that includes executable C statements. As you saw, C is a free-form language that isn’t picky about spacing. C is, however, picky about lowercase letters. C requires lowercase spellings of all its commands and functions, such as printf().

At this point, don’t worry about the specifics of the code you see in this chapter. The rest of the book explains all the details.

Code Example

image

Code Analysis

This short program does nothing more than print three messages on-screen. Each message includes one of the three data types mentioned in this chapter, namely a character (B), an integer (87), and a floating-point number (85.9).

The main() function is the only function in the program written by the programmer. The left and right braces ({ and }) always enclose main()’s code as well as any other function’s code that you might add to your programs. You’ll see another function, printf(), that is a built-in C function that produces output. Here is the program’s output:

image

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.105.137