Chapter 2: Optimization in Operations Research
-
Upload
zawadzahin -
Category
Documents
-
view
243 -
download
0
Transcript of Chapter 2: Optimization in Operations Research
-
8/20/2019 Chapter 2: Optimization in Operations Research
1/16
Chapter 2: C Fundamentals
Copyright © 2008 W. W. Norton & Company. All rights reserved.
1
Chapter 2
C Fundamentals
Chapter 2: C Fundamentals
Program: Printing a Pun
#i ncl ude
i nt mai n( voi d){
pr i nt f ( "To C, or not t o C: t hat i s t he quest i on. \ n") ;
r et ur n 0;
}
• This program might be stored in a file named pun. c.
• The file name doesn’t matter, but the . c extension is
often required.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
2
Chapter 2: C Fundamentals
Compiling and Linking
• Before a program can be executed, three steps are
usually necessary:
– Preprocessing. The preprocessor obeys commands that begin with # (known as directives)
– Compiling. A compiler translates then translates the
program into machine instructions ( object code).
– Linking. A linker combines the object code produced
by the compiler with any additional code needed to
yield a complete executable program.
• The preprocessor is usually integrated with thecompiler.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
3
Chapter 2: C Fundamentals
Compiling and Linking Using cc
• To compile and link the pun. c program under
UNIX, enter the following command in a terminal
or command-line window:% cc pun. c
The %character is the UNIX prompt.
• Linking is automatic when using cc ; no separate
link command is necessary.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
4
-
8/20/2019 Chapter 2: Optimization in Operations Research
2/16
Chapter 2: C Fundamentals
Compiling and Linking Using cc
• After compiling and linking the program, cc
leaves the executable program in a file nameda. out by default.
• The - o option lets us choose the name of the file
containing the executable program.
• The following command causes the executableversion of pun. c to be named pun:
% cc - o pun pun. c
Copyright © 2008 W. W. Norton & Company. All rights reserved.
5
Chapter 2: C Fundamentals
The GCC Compiler
• GCC is one of the most popular C compilers.
• GCC is supplied with Linux but is available formany other platforms as well.
• Using this compiler is similar to using cc :
% gcc - o pun pun. c
Copyright © 2008 W. W. Norton & Company. All rights reserved.
6
Chapter 2: C Fundamentals
Integrated Development Environments
• An integrated development environment (IDE) is
a software package that makes it possible to edit,
compile, link, execute, and debug a programwithout leaving the environment.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
7
Chapter 2: C Fundamentals
The General Form of a Simple Program
• Simple C programs have the form
directives
i nt mai n( voi d){
statements
}
Copyright © 2008 W. W. Norton & Company. All rights reserved.
8
-
8/20/2019 Chapter 2: Optimization in Operations Research
3/16
Chapter 2: C Fundamentals
The General Form of a Simple Program
• C uses { and } in much the same way that some
other languages use words like begi n and end.• Even the simplest C programs rely on three key
language features:
– Directives
– Functions
– Statements
Copyright © 2008 W. W. Norton & Company. All rights reserved.
9
Chapter 2: C Fundamentals
Directives
• Before a C program is compiled, it is first edited
by a preprocessor.• Commands intended for the preprocessor are
called directives.
• Example:
#i ncl ude
• is a header containing information
about C’s standard I/O library.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
10
Chapter 2: C Fundamentals
Directives
• Directives always begin with a #character.
• By default, directives are one line long; there’s no
semicolon or other special marker at the end.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
11
Chapter 2: C Fundamentals
Functions
• A function is a series of statements that have been
grouped together and given a name.
• Library functions are provided as part of the Cimplementation.
• A function that computes a value uses a return
statement to specify what value it “returns”:
r etur n x + 1;
Copyright © 2008 W. W. Norton & Company. All rights reserved.
12
-
8/20/2019 Chapter 2: Optimization in Operations Research
4/16
Chapter 2: C Fundamentals
The main Function
• The mai n function is mandatory.
• mai n is special: it gets called automatically whenthe program is executed.
• mai n returns a status code; the value 0 indicates
normal program termination.
• If there’s no return statement at the end of themai n function, many compilers will produce a
warning message.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
13
Chapter 2: C Fundamentals
Statements
• A statement is a command to be executed when
the program runs.• pun. c uses only two kinds of statements. One is
the return statement; the other is the function
call.
• Asking a function to perform its assigned task is
known as calling the function.
• pun. c calls pr i nt f to display a string:pr i nt f ( "To C, or not t o C: t hat i s the quest i on. \ n") ;
Copyright © 2008 W. W. Norton & Company. All rights reserved.
14
Chapter 2: C Fundamentals
Statements
• C requires that each statement end with a
semicolon.
– There’s one exception: the compound statement.
• Directives are normally one line long, and they
don’t end with a semicolon.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
15
Chapter 2: C Fundamentals
Printing Strings
• When the pr i nt f function displays a string
literal —characters enclosed in double quotation
marks—it doesn’t show the quotation marks.• pr i nt f doesn’t automatically advance to the
next output line when it finishes printing.
• To make pr i nt f advance one line, include \ n
(the new-line character) in the string to be
printed.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
16
-
8/20/2019 Chapter 2: Optimization in Operations Research
5/16
Chapter 2: C Fundamentals
Printing Strings
• The statement
pr i nt f ( "To C, or not t o C: t hat i s t he quest i on. \ n") ;
could be replaced by two calls of pr i nt f :
pr i nt f ( "To C, or not t o C: ") ;
pri nt f ( "t hat i s the quest i on. \ n") ;
• The new-line character can appear more than once in a
string literal:
pri nt f ( "Br evi t y i s the soul of wi t . \ n - - Shakespeare\ n") ;
Copyright © 2008 W. W. Norton & Company. All rights reserved.
17
Chapter 2: C Fundamentals
Comments
• A comment begins with / * and end with */ .
/ * Thi s i s a comment */
• Comments may appear almost anywhere in a
program, either on separate lines or on the same
lines as other program text.
• Comments may extend over more than one line.
/ * Name: pun. c
Pur pose: Pr i nt s a bad pun.Aut hor : K. N. Ki ng */
Copyright © 2008 W. W. Norton & Company. All rights reserved.
18
Chapter 2: C Fundamentals
Comments
• Warning: Forgetting to terminate a comment may cause
the compiler to ignore part of your program:
pr i nt f ( "My ") ; / * f or got t o cl ose t hi s comment . . .pr i nt f ( "cat " ) ;
pr i nt f ( "has ") ; / * so i t ends her e */
pr i nt f ( " f l eas" ) ;
Copyright © 2008 W. W. Norton & Company. All rights reserved.
19
Chapter 2: C Fundamentals
Comments in C99
• In C99, comments can also be written in the
following way:
/ / Thi s i s a comment
• This style of comment ends automatically at the
end of a line.
• Advantages of / / comments:
– Safer: there’s no chance that an unterminated comment
will accidentally consume part of a program.
– Multiline comments stand out better.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
20
-
8/20/2019 Chapter 2: Optimization in Operations Research
6/16
Chapter 2: C Fundamentals
Variables and Assignment
• Most programs need to a way to store data
temporarily during program execution.• These storage locations are called variables.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
21
Chapter 2: C Fundamentals
Types
• Every variable must have a type.
• C has a wide variety of types, including i nt andf l oat .
• A variable of type i nt (short for integer ) can
store a whole number such as 0, 1, 392, or –2553.
– The largest i nt value is typically 2,147,483,647 but
can be as small as 32,767.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
22
Chapter 2: C Fundamentals
Types
• A variable of type f l oat (short for floating- point ) can store much larger numbers than an i nt
variable.• Also, a f l oat variable can store numbers with
digits after the decimal point, like 379.125.
• Drawbacks of f l oat variables:
– Slower arithmetic
– Approximate nature of f l oat values
Copyright © 2008 W. W. Norton & Company. All rights reserved.
23
Chapter 2: C Fundamentals
Declarations
• Variables must be declared before they are used.
• Variables can be declared one at a time:
i nt hei ght ;f l oat prof i t ;
• Alternatively, several can be declared at the same
time:
i nt hei ght , l engt h, wi dt h, vol ume;f l oat prof i t , l oss;
Copyright © 2008 W. W. Norton & Company. All rights reserved.
24
-
8/20/2019 Chapter 2: Optimization in Operations Research
7/16
Chapter 2: C Fundamentals
Declarations
• When mai n contains declarations, these must
precede statements:i nt mai n( voi d){
declarations
statements
}
• In C99, declarations don’t have to come before
statements.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
25
Chapter 2: C Fundamentals
Assignment
• A variable can be given a value by means of
assignment:hei ght = 8;
The number 8 is said to be a constant.
• Before a variable can be assigned a value—or
used in any other way—it must first be declared.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
26
Chapter 2: C Fundamentals
Assignment
• A constant assigned to a f l oat variable usually
contains a decimal point:
pr of i t = 2150. 48;• It’s best to append the letter f to a floating-point
constant if it is assigned to a f l oat variable:
pr of i t = 2150. 48f ;
Failing to include the f may cause a warning from
the compiler.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
27
Chapter 2: C Fundamentals
Assignment
• An i nt variable is normally assigned a value oftype i nt , and a f l oat variable is normally
assigned a value of type f l oat .• Mixing types (such as assigning an i nt value to a
f l oat variable or assigning a f l oat value to ani nt variable) is possible but not always safe.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
28
-
8/20/2019 Chapter 2: Optimization in Operations Research
8/16
Chapter 2: C Fundamentals
Assignment
• Once a variable has been assigned a value, it can
be used to help compute the value of anothervariable:
hei ght = 8;l engt h = 12;wi dt h = 10;vol ume = hei ght * l engt h * wi dt h;
/ * vol ume i s now 960 */
• The right side of an assignment can be a formula(or expression, in C terminology) involving
constants, variables, and operators.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
29
Chapter 2: C Fundamentals
Printing the Value of a Variable
• pr i nt f can be used to display the current value
of a variable.• To write the message
Hei ght : h
where h is the current value of the hei ghtvariable, we’d use the following call of pr i nt f :
pr i nt f ( "Hei ght : %d\ n", hei ght ) ;
• %d is a placeholder indicating where the value ofhei ght is to be filled in.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
30
Chapter 2: C Fundamentals
Printing the Value of a Variable
• %d works only for i nt variables; to print af l oat variable, use %f instead.
• By default, %f displays a number with six digitsafter the decimal point.
• To force %f to display p digits after the decimal point, put . p between %and f .
• To print the line
Pr of i t : $2150. 48
use the following call of pr i nt f :pr i nt f ( "Prof i t : $%. 2f \ n" , prof i t ) ;
Copyright © 2008 W. W. Norton & Company. All rights reserved.
31
Chapter 2: C Fundamentals
Printing the Value of a Variable
• There’s no limit to the number of variables that can be printed by a single call of pr i nt f :
pr i nt f ( "Hei ght : %d Lengt h: %d\ n", hei ght , l engt h) ;
Copyright © 2008 W. W. Norton & Company. All rights reserved.
32
-
8/20/2019 Chapter 2: Optimization in Operations Research
9/16
Chapter 2: C Fundamentals
Program: Computing the
Dimensional Weight of a Box• Shipping companies often charge extra for boxes that
are large but very light, basing the fee on volumeinstead of weight.
• The usual method to compute the “dimensional
weight” is to divide the volume by 166 (the allowable
number of cubic inches per pound).
• The dwei ght . c program computes the dimensional
weight of a particular box:
Di mensi ons: 12x10x8Vol ume ( cubi c i nches) : 960Di mensi onal wei ght ( pounds) : 6
Copyright © 2008 W. W. Norton & Company. All rights reserved.
33
Chapter 2: C Fundamentals
Program: Computing the
Dimensional Weight of a Box• Division is represented by / in C, so the obvious way
to compute the dimensional weight would bewei ght = vol ume / 166;
• In C, however, when one integer is divided by
another, the answer is “truncated”: all digits after the
decimal point are lost.
– The volume of a 12” × 10” × 8” box will be 960 cubic
inches.
– Dividing by 166 gives 5 instead of 5.783.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
34
Chapter 2: C Fundamentals
Program: Computing the
Dimensional Weight of a Box
• One solution is to add 165 to the volume before
dividing by 166:
wei ght = ( vol ume + 165) / 166;
• A volume of 166 would give a weight of 331/166,
or 1, while a volume of 167 would yield 332/166,
or 2.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
35
Chapter 2: C Fundamentals
dweight.c
/ * Comput es t he di mensi onal wei ght of a 12" x 10" x 8" box */
#i ncl ude
i nt mai n( voi d){
i nt hei ght , l engt h, wi dt h, vol ume, wei ght ;
hei ght = 8;l engt h = 12;wi dt h = 10;vol ume = hei ght * l ength * wi dth;wei ght = ( vol ume + 165) / 166;
pr i nt f ( "Di mensi ons: %dx%dx%d\ n", l engt h, wi dt h, hei ght ) ;pr i nt f ( "Vol ume (cubi c i nches): %d\ n", vol ume);pr i nt f ( "Di mensi onal wei ght ( pounds) : %d\ n", wei ght ) ;
r eturn 0;}
Copyright © 2008 W. W. Norton & Company. All rights reserved.
36
-
8/20/2019 Chapter 2: Optimization in Operations Research
10/16
Chapter 2: C Fundamentals
Initialization
• Some variables are automatically set to zero when
a program begins to execute, but most are not.• A variable that doesn’t have a default value and
hasn’t yet been assigned a value by the program is
said to be uninitialized.
• Attempting to access the value of an uninitialized
variable may yield an unpredictable result.
• With some compilers, worse behavior—even a program crash—may occur.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
37
Chapter 2: C Fundamentals
Initialization
• The initial value of a variable may be included in
its declaration:i nt hei ght = 8;
The value 8 is said to be an initializer.
• Any number of variables can be initialized in the
same declaration:
i nt hei ght = 8, l engt h = 12, wi dt h = 10;
• Each variable requires its own initializer.i nt hei ght , l engt h, wi dt h = 10;
/ * i ni t i al i zes onl y wi dt h */
Copyright © 2008 W. W. Norton & Company. All rights reserved.
38
Chapter 2: C Fundamentals
Printing Expressions
• pr i nt f can display the value of any numeric
expression.
• The statementsvol ume = hei ght * l engt h * wi dt h;pr i nt f ( "%d\ n", vol ume) ;
could be replaced by
pr i nt f ( "%d\ n", hei ght * l engt h * wi dt h) ;
Copyright © 2008 W. W. Norton & Company. All rights reserved.
39
Chapter 2: C Fundamentals
Reading Input
• scanf is the C library’s counterpart to pr i nt f .
• scanf requires a format string to specify the
appearance of the input data.• Example of using scanf to read an i nt value:
scanf ( "%d", &i ) ;/ * r eads an i nt eger ; st or es i nt o i */
• The &symbol is usually (but not always) requiredwhen using scanf .
Copyright © 2008 W. W. Norton & Company. All rights reserved.
40
-
8/20/2019 Chapter 2: Optimization in Operations Research
11/16
Chapter 2: C Fundamentals
Reading Input
• Reading a f l oat value requires a slightly
different call of scanf :scanf ( "%f ", &x) ;
• "%f " tells scanf to look for an input value inf l oat format (the number may contain a decimal
point, but doesn’t have to).
Copyright © 2008 W. W. Norton & Company. All rights reserved.
41
Chapter 2: C Fundamentals
Program: Computing the Dimensional
Weight of a Box (Revisited)• dwei ght 2. c is an improved version of the
dimensional weight program in which the userenters the dimensions.
• Each call of scanf is immediately preceded by acall of pr i nt f that displays a prompt.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
42
Chapter 2: C Fundamentals
dweight2.c
/ * Comput es t he di mensi onal wei ght of a box fr omi nput pr ovi ded by t he user */
#i ncl ude
i nt mai n(voi d){
i nt hei ght , l ength, wi dth, vol ume, wei ght;
pri ntf (" Enter hei ght of box: ") ;scanf ( "%d", &hei ght ) ;pri ntf (" Enter l ength of box: ") ;scanf ( "%d", &l ength);pri ntf (" Enter wi dth of box: ") ;scanf( "%d", &wi dth) ;vol ume = hei ght * l ength * wi dth;wei ght = ( vol ume + 165) / 166;
pri ntf ( "Vol ume (cubi c i nches): %d\n", vol ume);pri ntf ( "Di mensi onal wei ght ( pounds) : %d\n", wei ght ) ;
r eturn 0;}
Copyright © 2008 W. W. Norton & Company. All rights reserved.
43
Chapter 2: C Fundamentals
Program: Computing the Dimensional
Weight of a Box (Revisited)
• Sample output of program:
Ent er hei ght of box: 8
Ent er l engt h of box: 12Ent er wi dt h of box: 10Vol ume ( cubi c i nches) : 960Di mensi onal wei ght ( pounds) : 6
• Note that a prompt shouldn’t end with a new-line
character.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
44
-
8/20/2019 Chapter 2: Optimization in Operations Research
12/16
Chapter 2: C Fundamentals
Defining Names for Constants
• dwei ght . c and dwei ght 2. c rely on the
constant 166, whose meaning may not be clear tosomeone reading the program.
• Using a feature known as macro definition, we
can name this constant:
#def i ne I NCHES_PER_POUND 166
Copyright © 2008 W. W. Norton & Company. All rights reserved.
45
Chapter 2: C Fundamentals
Defining Names for Constants
• When a program is compiled, the preprocessor replaces
each macro by the value that it represents.• During preprocessing, the statement
wei ght = ( vol ume + I NCHES_PER_POUND - 1) / I NCHES_PER_POUND;
will becomewei ght = ( vol ume + 166 - 1) / 166;
Copyright © 2008 W. W. Norton & Company. All rights reserved.
46
Chapter 2: C Fundamentals
Defining Names for Constants
• The value of a macro can be an expression:
#def i ne RECI PROCAL_OF_PI ( 1. 0f / 3. 14159f )
• If it contains operators, the expression should beenclosed in parentheses.
• Using only upper-case letters in macro names is a
common convention.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
47
Chapter 2: C Fundamentals
Program: Converting from
Fahrenheit to Celsius• The cel si us. c program prompts the user to
enter a Fahrenheit temperature; it then prints the
equivalent Celsius temperature.• Sample program output:
Ent er Fahr enhei t t emperatur e: 212Cel si us equi val ent : 100. 0
• The program will allow temperatures that aren’t
integers.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
48
-
8/20/2019 Chapter 2: Optimization in Operations Research
13/16
Chapter 2: C Fundamentals
celsius.c
/ * Conver t s a Fahr enhei t t emper ature t o Cel si us * /
#i ncl ude
#def i ne FREEZI NG_PT 32. 0f #def i ne SCALE_FACTOR ( 5. 0f / 9. 0f )
i nt mai n( voi d){
f l oat f ahrenhei t , cel si us;
pr i nt f ( "Ent er Fahr enhei t t emper at ur e: ") ;scanf ( "%f ", &f ahr enhei t ) ;
cel si us = ( f ahr enhei t - FREEZI NG_PT) * SCALE_FACTOR;
pri nt f ( "Cel si us equi val ent : %. 1f \ n", cel si us);
r et ur n 0;}
Copyright © 2008 W. W. Norton & Company. All rights reserved.
49
Chapter 2: C Fundamentals
Program: Converting from
Fahrenheit to Celsius• Defining SCALE_FACTOR to be ( 5. 0f / 9. 0f )
instead of ( 5 / 9) is important.• Note the use of %. 1f to display cel s i us with
just one digit after the decimal point.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
50
Chapter 2: C Fundamentals
Identifiers
• Names for variables, functions, macros, and other
entities are called identifiers.
• An identifier may contain letters, digits, andunderscores, but must begin with a letter or
underscore:
t i mes10 get _next _char _done
It’s usually best to avoid identifiers that begin with
an underscore.
• Examples of illegal identifiers:10t i mes get - next - char
Copyright © 2008 W. W. Norton & Company. All rights reserved.
51
Chapter 2: C Fundamentals
Identifiers
• C is case-sensitive: it distinguishes between
upper-case and lower-case letters in identifiers.
• For example, the following identifiers are alldifferent:
j ob j oB j Ob j OB J ob J oB J Ob J OB
Copyright © 2008 W. W. Norton & Company. All rights reserved.
52
-
8/20/2019 Chapter 2: Optimization in Operations Research
14/16
Chapter 2: C Fundamentals
Identifiers
• Many programmers use only lower-case letters in
identifiers (other than macros), with underscoresinserted for legibility:
symbol _t abl e cur r ent _page name_and_address
• Other programmers use an upper-case letter to
begin each word within an identifier:
symbol Tabl e cur r entPage nameAndAddr ess
• C places no limit on the maximum length of an
identifier.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
53
Chapter 2: C Fundamentals
Keywords
• The following keywords can’t be used as
identifiers:aut o enum r est r i ct * unsi gned
br eak exter n r et ur n voi d
case f l oat shor t vol at i l e
char f or si gned whi l e
const got o si zeof _Bool *
cont i nue i f st at i c _Compl ex*
def aul t i nl i ne* st r uct _I magi nar y*
do i nt swi t ch
doubl e l ong t ypedef el se r egi st er uni on
*C99 only
Copyright © 2008 W. W. Norton & Company. All rights reserved.
54
Chapter 2: C Fundamentals
Keywords
• Keywords (with the exception of _Bool , _Compl ex, and _I magi nar y) must be written
using only lower-case letters.• Names of library functions (e.g., pr i nt f ) are
also lower-case.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
55
Chapter 2: C Fundamentals
Layout of a C Program
• A C program is a series of tokens.
• Tokens include:
– Identifiers – Keywords
– Operators
– Punctuation
– Constants
– String literals
Copyright © 2008 W. W. Norton & Company. All rights reserved.
56
-
8/20/2019 Chapter 2: Optimization in Operations Research
15/16
Chapter 2: C Fundamentals
Layout of a C Program
• The statement
pr i nt f ( "Hei ght : %d\ n", hei ght ) ;consists of seven tokens:
pr i nt f Identifier
( Punctuation
"Hei ght : %d\ n" String literal
, Punctuation
hei ght Identifier ) Punctuation
; Punctuation
Copyright © 2008 W. W. Norton & Company. All rights reserved.
57
Chapter 2: C Fundamentals
Layout of a C Program
• The amount of space between tokens usually isn’t critical.
• At one extreme, tokens can be crammed together with nospace between them, except where this would cause two
tokens to merge:
/ * Conver t s a Fahr enhei t t emper at ur e to Cel si us */
#i ncl ude
#def i ne FREEZI NG_PT 32. 0f
#def i ne SCALE_FACTOR ( 5. 0f / 9. 0f )
i nt mai n( voi d) {f l oat f ahr enhei t , cel si us; pr i nt f (
"Ent er Fahr enhei t t emper at ur e: ") ; scanf ( "%f ", &f ahr enhei t ) ;cel si us=( f ahr enhei t - FREEZI NG_PT) *SCALE_FACTOR;
pr i nt f ( "Cel si us equi val ent : %. 1f \ n", cel si us); return 0; }
Copyright © 2008 W. W. Norton & Company. All rights reserved.
58
Chapter 2: C Fundamentals
Layout of a C Program
• The whole program can’t be put on one line,
because each preprocessing directive requires a
separate line.• Compressing programs in this fashion isn’t a good
idea.
• In fact, adding spaces and blank lines to a program
can make it easier to read and understand.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
59
Chapter 2: C Fundamentals
Layout of a C Program
• C allows any amount of space—blanks, tabs, and
new-line characters—between tokens.
• Consequences for program layout: – Statements can be divided over any number of lines.
– Space between tokens (such as before and after each
operator, and after each comma) makes it easier for the
eye to separate them.
– Indentation can make nesting easier to spot.
– Blank lines can divide a program into logical units.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
60
-
8/20/2019 Chapter 2: Optimization in Operations Research
16/16
Chapter 2: C Fundamentals
Layout of a C Program
• Although extra spaces can be added between tokens,
it’s not possible to add space within a token withoutchanging the meaning of the program or causing an
error.
• Writing
f l oat f ahr enhei t , cel si us; / *** WRONG ***/
or
f loat f ahr enhei t , cel si us; / *** WRONG *** /
produces an error when the program is compiled.
Copyright © 2008 W. W. Norton & Company. All rights reserved.
61
Chapter 2: C Fundamentals
Layout of a C Program
• Putting a space inside a string literal is allowed,
although it changes the meaning of the string.• Putting a new-line character in a string (splitting
the string over two lines) is illegal:
pr i nt f ( "To C, or not t o C:t hat i s t he quest i on. \ n") ;
/ *** WRONG ***/
Copyright © 2008 W. W. Norton & Company. All rights reserved.
62