home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
257_01
/
chap2.txt
< prev
next >
Wrap
Text File
|
1988-03-30
|
16KB
|
389 lines
Chapter 2 - Getting started in Turbo C
YOUR FIRST C PROGRAM
The best way to get started with C is to actually look
at a program, so load the file named TRIVIAL.C with the
Integrated Environment for display on the monitor. You are
looking at the simplest possible C program. There is no way
to simplify this program or to leave anything out.
Unfortunately, the program doesn't do anything.
The word "main" is very important, and must appear
once, and only once in every C program. This is the point
where execution is begun when the program is run. We will
see later that this does not have to be the first statement
in the program but it must exist as the entry point.
Following the "main" program name is a pair of parentheses
which are an indication to the compiler that this is a
function. We will cover exactly what a function is in due
time. For now, I suggest that you simply include the pair
of parentheses.
The two curly brackets, properly called braces, are
used to define the limits of the program itself. The actual
program statements go between the two braces and in this
case, there are no statements because the program does
absolutely nothing. You can compile and run this program by
hitting Alt-R if in the Integrated Environment, but since it
has no executable statements, it does nothing. Keep in mind
however, that it is a valid C program.
A PROGRAM THAT DOES SOMETHING
For a much more interesting program, load the program
named WRTSOME.C and display it on your monitor. It is the
same as the previous program except that it has one
executable statement between the braces.
The executable statement is a call to a function
supplied as a part of your Turbo C library. Once again, we
will not worry about what a function is, but only how to use
this one named "printf". In order to output text to the
monitor, it is put within the function parentheses and
bounded by quotation marks. The end result is that whatever
is included between the quotation marks will be displayed on
the monitor when the program is run.
Notice the semi-colon at the end of the line. C uses a
semi-colon as a statement terminator, so the semi-colon is
required as a signal to the compiler that this line is
complete. This program is also executable, so you can
compile and run it to see if it does what you think it
should.
Page 7
Chapter 2 - Getting started in Turbo C
ANOTHER PROGRAM WITH MORE OUTPUT
Load the program WRTMORE.C and display it on your
monitor for an example of more output and another small but
important concept. You will see that there are four program
statements in this program, each one being a call to the
function "printf". The top line will be executed first,
then the next, and so on, until the fourth line is complete.
The statements are executed in order from top to bottom.
Notice the funny character near the end of the first
line, namely the backslash. The backslash is used in the
printf statement to indicate that a special control
character is following. In this case, the "n" indicates
that a "newline" is requested. This is an indication to
return the cursor to the left side of the monitor and move
down one line. It is commonly referred to as a carriage
return/line feed. Any place within text that you desire,
you can put a newline character and start a new line. You
could even put it in the middle of a word and split the word
between two lines. The C compiler considers the combination
of the backslash and letter n as one character.
A complete description of this program is now possible.
The first printf outputs a line of text and returns the
carriage. The second printf outputs a line but does not
return the carriage so that the third line is appended to
the second, then followed by two carriage returns, resulting
in a blank line. Finally the fourth "printf" outputs a line
followed by a carriage return and the program is complete.
Compile and run this program to see if it does what you
expect it to do. It would be a good idea at this time for
you to experiment by adding additional lines of printout to
see if you understand how the statements really work.
LETS PRINT SOME NUMBERS
Load the file named ONEINT.C and display it on the
monitor for our first example of how to work with data in a
C program. The entry point "main" should be clear to you by
now as well as the beginning brace. The first new thing we
encounter is the line containing "int index;", which is used
to define an integer variable named "index". The "int" is a
reserved word in C, and can therefore not be used for
anything else. It defines a variable that can have a value
from -32768 to 32767 in Turbo C, and in most other C
compilers for microcomputers. The variable name, "index",
can be any name that follows the rules for an identifier and
Page 8
Chapter 2 - Getting started in Turbo C
is not one of the reserved words for Turbo C. The Turbo C
User's Guide has a list of reserved words on page 199. The
final character on the line, the semi-colon, is the
statement terminator used in C.
Note that, even though we have defined a variable, we
have not yet assigned a value to it. We will see in a later
chapter that additional integers could also be defined on
the same line, but we will not complicate the present
situation.
Observing the main body of the program, you will notice
that there are three statements that assign a value to the
variable "index", but only one at a time. The first one
assigns the value of 13 to "index", and its value is printed
out. (We will see how shortly.) Later, the value of 27 is
assigned to "index", and finally 10 is assigned to it, each
value being printed out. It should be intuitively clear
that "index" is indeed a variable and can store many
different values. Please note that many times the words
"printed out" are used to mean "displayed on the monitor".
You will find that in many cases experienced programmers
take this liberty, probably due to the "printf" function
being used for monitor display.
HOW DO WE PRINT NUMBERS
To keep our promise, let's return to the "printf"
statements for a definition of how they work. Notice that
they are all identical and that they all begin just like the
"printf" statements we have seen before. The first
difference occurs when we come to the % character. This is
a special character that signals the output routine to stop
copying characters to the output and do something different,
namely output a variable. The % sign