Sams Teach Yourself C in 24 Hours (11 page)

BOOK: Sams Teach Yourself C in 24 Hours
13.62Mb size Format: txt, pdf, ePub
ads

Here is a brief note on binary code and executable files. You’ll learn more details later in this book. Basically, before you can run a C program on your computer you need to use a C compiler to translate the C program into

machine-understandable code (that is, binary code). When the translation is done, the binary code can be saved into an application file. The binary code or application file is called executable code, or an executable file, because it can be executed directly on your computer.

Summary

In this first lesson you learned the following basic things about the C language:

• C is a general-purpose programming language.

• C is a high-level language that has the advantages of readability, maintainability, and portability.

• C is a very efficient language that allows you to control computer hardware and peripherals.

• C is a small language that you can easily learn in a relatively short time.

• Programs written in C can be reused.

• Programs written in C must be compiled and translated into machine-readable code before the computer can execute them.

• Many other programming languages, such as Perl, C++, and Java, have adopted basic concepts and useful features from the C language. Once you learn C, learning these other languages is much easier.

• The ANSI standard for C is the standard supported by C compiler vendors to maintain the portability of C programs.

• You can use any ANSI C compliant compiler to compile all of the C programs in this book.

In the next lesson you’ll learn to write your first C program.

03 067231861x CH01 4.10.2000 10:59 AM Page 25

Taking the First Step

25

Q&A

1

Q What is the lowest-level language in the computer world?

A
The computer’s machine language is the lowest because the machine language, made up of 0s and 1s, is the only language that the computer can understand directly.

Q What are the advantages of high-level programming languages?

A
Readability, maintainability, and portability are the main advantages of high-level programming languages.

Q What is C, anyway?

A
C is a general-purpose programming language. It’s a high-level language that has advantages such as readability, maintainability, and portability. Also, C allows you to get down to the hardware level to increase the performance speed if needed. A C

compiler is needed to translate programs written in C into machine-understandable code. The portability of C is realized by recompiling the C programs with different C compilers intended for different types of computers.

Q Can I learn C in a short time?

A
Yes. C is a small programming language. There are not many C keywords or commands to remember. Also, it’s very easy to read and write C programs because C is a high-level programming language that is close to human languages, especially English. You can learn C in a relatively short time.

Workshop

To help solidify your understanding of this hour’s lesson, you are encouraged to answer the quiz questions provided in the workshop before you move to next lesson. The answers and hints to the questions are given in Appendix D, “Answers to Quiz Questions and Exercises.”

Quiz

1. What are the lowest-level and highest-level languages mentioned in this book?

2. Can a computer directly understand a program written in C? What do you need to translate a program written in C into the machine-understandable code (that is, binary code)?

3. If needed, can a C program be reused in another C program?

4. Why do we need the ANSI standard for the C language?

03 067231861x CH01 4.10.2000 10:59 AM Page 26

04 067231861x CH02 1/25/00 10:42 AM Page 27

HOUR 2

Writing Your First C

Program

Cut your own wood and it will warm you twice.

—Chinese proverb

In Hour 1, “Taking the First Step,” you learned that C is a high-level programming language and that you need a C compiler to translate your C programs into binary code that your computer can understand and execute. In this lesson you’ll write your first C program and learn the basics of a C program, such as

• The #include directive

• Header files

• Comments

• The main() function

• The return statement

• The exit() function

04 067231861x CH02 1/25/00 10:42 AM Page 28

28

Hour 2

• The newline character (\n)

• Translating a C program into an executable file

• Debugging

A Simple C Program

Let’s have a look at your first C program, demonstrated in Listing 2.1. Later in this lesson you’re going to write your own C program for the first time.

LISTING 2.1

A Simple C Program

1: /* 02L01.c: This is my first C program */

2: #include

3:

4: main()

5: {

6: printf (“Howdy, neighbor! This is my first C program.\n”);

7: return 0;

8: }

This is a very simple C program, which is saved in a file called 02L01.c. Note that the name of a C program file must have an extension of .c. If you’ve installed a C compiler and set up the proper development environment, you should be able to compile this C

program, and make it into an executable file. I’ll discuss how to make an executable file later in this chapter.

In the previous hour, you learned how to enter a program into your text editor and save it as a C program file. Here, you may notice that unlike the sample in the last chapter, each line is numbered. This is only done here as a reference for when I discuss what each line of a program does. Unlike other languages such as BASIC, the C language does not have line numbers at all. In fact, if you do enter the line numbers in the listing, your program will not work! So when you enter these programs, remember not to enter the line numbers that are shown in the book.

Two things you might notice by glancing at Listing 2.1 are the semicolons and indenting on lines 6 and 7. Unlike other languages, such as BASIC, the end of a line has no special significance in C. It is perfectly legal (and in many cases, advisable) to break a statement into several lines for clarity.

Generally, an individual C statement ends in a semicolon, but there is more about this later in the book. The indenting serves to identify the various lev-04 067231861x CH02 1/25/00 10:42 AM Page 29

Writing Your First C Program

29

els of a program in a kind of outline format. The function main() is, well, the main level of the program, so it goes on the far left. Lines 6 and 7 are part of main() so they are indented one level to the right. Usually, you use your Tab key to indent a level before you start typing. It should be noted that indenting, like line numbers, is not enforced — or even noticed! — by the compiler. The programmer is free to use line breaks and indenting, commonly referred to as
whitespace
, to make the program look readable. It is generally a matter of style, but it’s a good idea to follow generally accepted
2

conventions so that other programmers can understand your programs and

vice-versa. Take a look at the use of whitespace in the programs in this book, and feel free to develop your own style as you go.

I set up my development environment in such a way that all C programs in this book can be compiled and made into console applications. For instance, 02L01.exe is the name of the console application made from 02L01.c. Note that .exe is included as the extension to the name of a DOS or Windows application program (that is, an executable file).

Also, on my machine, I save all the executable files made from the C programs in this book into a dedicated directory called C:\app. Therefore, if I type in 02L01 from a DOS

prompt and press the Enter key, I can run the 02L01.exe executable file and display the message Howdy, neighbor! This is my first C program. on the screen. The following output is a copy from the screen:

Howdy, neighbor! This is my first C program.

OUTPUT

Comments

Now let’s take a close look at the C program in Listing 2.1.

The first line contains a comment:

/* 02L01.C: This is my first C program */

You notice that this line starts with a combination of slash and asterisk, /*, and ends with

*/. In C, /* is called the
opening comment mark
, and */ is the
closing comment mark
.

The C compiler ignores everything between the opening comment mark and closing comment mark. That means the comment in the first line of Listing 2.1, 02L01.C: This is my first C program, is completely ignored by the compiler.

The only purpose of including comments in your C program is to help you document what the program or some specific sections in the programs do. Remember, comments are written for yourself and other programmers. For example, when you read someone else’s 04 067231861x CH02 1/25/00 10:42 AM Page 30

30

Hour 2

code, the comments in the code help you to understand what the code does, or at least what the code intends to do. As your programs get larger and more complicated, you can use comments to write notes to yourself about what you are trying to do and why.

You don’t need to worry about the size or performance speed of your C program if you add many comments into it. Adding comments into a C program does not increase the size of the binary code of the program (that is, the executable file), although the size of the program itself (that is, the source code) may become larger. The performance speed of the executable file made from your C program is not affected in any way by the comments inside your C program.

The C language allows you to write a comment that crosses more than one line. For instance, you can write a comment in C like this:

/*

This comment does not increase the size of

the executable file (binary code), nor does

it affect the performance speed.

*/

which is equivalent to this:

/* This comment does not increase the size of */

/* the executable file (binary code), nor does */

/* it affect the performance speed. */

These days, there is another way to put comments into a C program. C++

started using two slashes (//) to mark the beginning of a comment line; many C compilers now use this convention as well. The comment ends at the end of the line. For instance, if I write a C program in Borland C++ or Visual C++, the following two comments are identical:

/*

This comment does not increase the size of

the executable file (binary code), nor does

it affect the performance speed.

*/

// This comment does not increase the size of

// the executable file (binary code), nor does

// it affect the performance speed.

Note that this new style of using // as the beginning mark of a comment has not been approved by ANSI. Make sure your C compiler supports //

before you use it.

04 067231861x CH02 1/25/00 10:42 AM Page 31

Writing Your First C Program

31

One thing that needs to be pointed out is that the ANSI standard does not support
nested
comments
, that is, comments within comments. For instance, the following is not allowed by the ANSI standard:

/* This is the first part of the first comment

/* This is the second comment */

This is the second part of the first comment */

2

You can use the opening comment mark, /*, and closing comment mark, */, to help you test and fix any errors found in your C program. This is commonly referred to as
commenting out
a block of code. You can comment out one or more C statements in your C program with /* and */ when you need to focus on other statements and watch their behaviors closely. The C compiler will ignore the statements you comment out.

Later, you can always restore the previously commented-out statements simply by removing the opening comment and closing comment marks. In this

way, you don’t need to erase or rewrite any statements during testing and debugging.

However, since you can’t nest comments made with /* and */, if you

attempt to comment out code that already contains these comments, your

program will not compile. One reason why the //-type comments are so useful is because you can nest them, and therefore legally comment them out with /* and */.

If your text editor supports color syntax highlighting, you can use this to tell at a glance if sections of code are actually commented out as you intended.

The
#include
Directive

Let’s now move to line 2 in the C program of Listing 2.1:

#include

You see that this line starts with a pound sign, #, which is followed by include. In C,

#include forms a
preprocessor directive
that tells the C preprocessor to look for a file and place the contents of that file in the location where the #include directive indicates.

BOOK: Sams Teach Yourself C in 24 Hours
13.62Mb size Format: txt, pdf, ePub
ads

Other books

PillowFace by Kristopher Rufty
Rouge by Leigh Talbert Moore
Black Order by James Rollins
Destination Wedding ~ A Novel by Sletten, Deanna Lynn
Tribal Law by Jenna Kernan
Very Private Duty by Rochelle Alers
Bad Boy From Rosebud by Gary M. Lavergne
Fire in the Blood by Irene Nemirovsky


readsbookonline.com Copyright 2016 - 2024