Cover | Table of Contents | Colophon
int total; /* Total number accounts */
total. We let the compiler decide
what particular bytes of memory to use; that decision is a minor
bookkeeping detail that we don't want to worry about.total is a simple variable. It can
hold only one integer and describe only one total. A series of
integers can be organized into an array as follows:int balance[100]; /* Balance (in cents) for all 100 accounts */
struct rectangle {
int width; /* Width of rectangle in pixels */
int height; /* Height of rectangle in pixels */
color_type color; /* Color of the rectangle */
fill_type fill; /* Fill pattern */
};?LSTUIT User is a twit
= instead of ==. These programs
let you learn how to spot mistakes in a small program. Then, when you
make similar mistakes
in a big program, and you
% mkdir hello % cd hello
C:> MKDIR HELLO C:> CD HELLO
[File: hello/hello.c]
#include <stdio.h>
int main()
{
printf("Hello World\n");
return (0);
}% cc -g -ohello hello.c-g option enables debugging. (The compiler
adds extra information to the program to make the program easier to
debug.) The switch -ohello tells the compiler that
the program is to be called hello, and the final
hello.c is the name of the source file. See your
compiler manual for details on all the possible options. There are
several different C compilers for UNIX, so your command line may be
slightly different.C:> MKDIR HELLO C:> CD HELLO
C:> TC
INSERT key to add a file to the project. The file we want
to add is HELLO.C as seen in Figure 2-7.
ESC to get out of the add-file cycle.UP-ARROW to go up one line. The line with HELLO.C
should now be highlighted as seen in Figure 2-8.
ENTER to edit this file.man command.
(UNIX uses man as an abbreviation for manual.) To
get information about a particular subject, use the following
command:man subjectprintf function, you would type:man printfman -k keywordman -k output| 1. |
Window|Close All |
Clean out any old junk. |
| 2. |
Options|Compiler|Code Generation
Memory Model = Large |
For simple program, use large memory model. |
| 3. |
Options|Compiler|Entry/Exit
Test stack overflow = On |
Turn on test for a common programming error. |
| 4. |
Options|Compiler|Messages|Display
Display warnings = All |
Tell compiler that you want all diagnostics that it can give you. |
| 5. |
Options|Save |
Save options. |
| 6. |
Project|Open
Project file = program.PRJ |
Create a new project. |
| 7. |
Insert
Add file program.c |
Add program file to project. |
| 8. |
ESC |
Get out of "add-file" cycle. |
| 9. |
UP-ARROW |
Move to program.c line. |
| 10. |
RETURN |
Edit program file. |
| 11. |
Type in the program |
Enter text of program. |
| 12. |
Run|Run |
Execute program. |
| 13. |
Window|User |
Display results of the program. |
| 14. |
File|Save |
Save the program. |
| 15. |
File|Quit |
Exit Turbo C++ IDE. |
| 1. |
Window|Close All |
Clean out any old junk. |
| 2. |
Project|New Project
Project Path and Name = c.\
program\program.ide
Target Type = EasyWin(.exe)
Target Model = Large |
Create new project. |
| 3. |
Click on Advanced button
Set .c Node
Clear .rc and .def |
Setup a simple C program. |
| 4. |
Click on OK |
Return to New Target window. |
| 5. |
Click on OK |
Return to main window. |
| 6. |
ALT-F10 |
Select node submenu. |
| 7. |
Edit|Node Attributes
Style Sheet = Debug Info and Diagnostics |
Turn on debugging. |
| 8. |
Click on OK button |
Return to main menu. |
| 9. |
Options|Project Options
Click on + under Compiler
Test stack overflow = On |
Turn on valuable run-time test. |
| 10. |
Click on OK button |
Save options. |
| 11. |
Click on OK button |
Return to main window. |
| 12. |
DOWN-ARROW |
Move to program[.c] line. |
| 13. |
RETURN |
Edit program file. |
| 14. |
Type in the program |
Enter text of program. |
| 15. |
Debug|Run |
Run program. |
| 1. |
Window|Close All |
Clean out any old junk. |
| 2. |
Project|New
Project Name = \program\program.mak
Project Type =
QuickWin application (.EXE) |
Start project.
Set up project.
Click on OK button. |
p,
q, and r:int p,q,r;
int account_number; int balance_owed;
balance_owed in dollars or cents? We should have
added a comment after each declaration to explain what we were doing.
For example:int account_number; /* Index for account table */ int balance_owed; /* Total owed us (in pennies)*/
grep can also help you
quickly find a variable's definition.)/******************************************************** * Note: I have no idea what the input units are, nor * * do I have any idea what the output units are, * * but I have discovered that if I divide by 3 * * the plot sizes look about right. * ********************************************************/
while (! done) {
printf("Processing\n");
next_entry();
}
if (total <= 0) {
printf("You owe nothing\n");
total = 0;
} else {
printf("You owe %d dollars\n", total);
all_totals = all_totals + total;
}while (! done)
{
printf("Processing\n");
next_entry();
}
if (total <= 0)
{
printf("You owe nothing\n");
total = 0;
}
else
{
printf("You owe %d dollars\n", total);
all_totals = all_totals + total;
}/* poor programming practice */ temp = box_x1; box_x1 = box_x2; box_x2 = temp; temp = box_y1; box_y1 = box_y2; box_y2 = temp;
/* * Swap the two corners */ /* Swap X coordinate */ temp = box_x1; box_x1 = box_x2; box_x2 = temp; /* Swap Y coordinate */ temp = box_y1; box_y1 = box_y2; box_y2 = temp;
/**************************************************** * ...Heading comments... * ****************************************************/ ...Data declarations... int main() { ...Executable statements... return (0); }
/**************************************************** * ...Heading comments... * ****************************************************/ ...Data declarations... int main() { ...Executable statements... return (0); }
main. The name
main is special, because it is the first function
called. Other functions are called directly or indirectly from
main. The function main begins
with:int main()
{return (0); }
return(0); is used to tell the operating system
(UNIX or MS-DOS/Windows) that the program exited normally (Status=0).
A nonzero status indicates an error—the bigger the return
value, the more severe the error. Typically, a status of 1 is used
for the most simple errors, like a missing file or bad command-line
syntax./* and */. Following this box
is the line:#include <stdio.h>
printf from this package.printf("Hello World\n");;) to end a statement in much the
same way we
use a period to end a sentence. Unlike line-oriented languages such
as BASIC, an end-of-line does not end a statement. The sentences in
this book can span several lines—the end of a line is treated
just like space between words. C works the same way. A single
statement can span several lines. Similarly, you can put several
sentences on the same line, just as you can put several C statements
on the same line. However, most of the time your program is more
readable if each statement starts on a separate line.
|
Operator |
Meaning |
|---|---|
|
* |
Multiply |
|
/ |
Divide |
|
+ |
Add |
|
- |
Subtract |
|
% |
Modulus (return the remainder after division) |
*), divide
(/), and modulus (%) have
precedence over add (+) and subtract (-).
Parentheses, ( ), may be used to group terms. Thus:(1 + 2) * 4
1 + 2 * 4
(1 + 2) * 4. int main()
{
(1 + 2) * 4;
return (0);
}"Take your wheelbarrow and go back and forth between the truck and the building site.""Do you want me to carry bricks in it?""No. Just go back and forth."
sam,
Sam, and SAM specify three
different variables. However, to avoid confusion, you should use
different names for variables and not depend on case differences.average /* average of all grades */ pi /* pi to 6 decimal places */ number_of_students /* number students in this class */
3rd_entry /* Begins with a number */ all$done /* Contains a "$" */ the end /* Contains a space */ int /* Reserved word */
total /* total number of items in current entry */ totals /* total of all entries */
entry_total /* total number of items in current entry */ all_total /* total of all entries */