Sie sind auf Seite 1von 13

Summary: Variables and Operations

The C anguage factates a structured and dscpned approach to computer


program desgn.
Comments begn wth /* and end wth */. Comments document programs and
mprove program readabty. C99 aso supports C++s snge-ne comments that
begn wth //.
Comments do not cause the computer to perform any acton when the
program s run. Theyre gnored by the C comper and do not cause any
machne-anguage ob|ect code to be generated.
Lnes begnnng wth # are processed by the preprocessor before the program s
comped. The
#ncude drectve tes the preprocessor to ncude the contents of another fe
(typcay a header fe such as <stdo.h>).
The <stdo.h> header contans nformaton used by the comper when
compng cas to standard nput/output brary functons such as prntf.
The functon man s a part of every C program. The parentheses after man
ndcate that man s a program budng bock caed a functon. C programs
contan one or more functons, one of
whch must be man. Every program n C begns executng at the functon man.
Functons can return nformaton. The keyword nt to the eft of man ndcates
that man "returns"
an nteger (whoe number) vaue.
Functons can receve nformaton when theyre caed upon to execute. The
vod n parentheses after man ndcates that man does not receve any
nformaton.
A eft brace, {, begns the body of every functon. A correspondng rght brace, },
ends each functon. Ths par of braces and the porton of the program between the
braces s caed a bock.
The prntf functon nstructs the computer to dspay nformaton on the screen.
A strng s sometmes caed a character strng, a message or a tera.
Every statement must end wth a semcoon (aso known as the statement
termnator).
The characters \n do not dspay characters on the screen. The backsash (\) s
caed an escape character. When encounterng a backsash n a strng, the
comper ooks ahead at the next character and combnes t wth the backsash to
form an escape sequence. The escape sequence \n
means new ne.
When a newne appears n the strng output by a prntf, the new ne causes
the cursor to poston to the begnnng of the next ne on the screen.
The doube backsash (\\) escape sequence can be used to pace a snge
backsash n a strng.
The escape sequence \" represents a tera doube-quote character.
The keyword return s one of severa means to ext a functon. When the
return statement s used at the end of man, the vaue 0 ndcates that the
program has termnated successfuy.
A varabe s a ocaton n memory where a vaue can be stored for use by a
program.
Varabes of type nt hod nteger vaues, .e., whoe numbers such as 7, -11, 0,
31914.
A varabes must be defned wth a name and a data type mmedatey after
the eft brace that begns the body of man before they can be used n a
program.
A varabe name n C s any vad dentfer. An dentfer s a seres of
characters consstng of etters, dgts and underscores ( _ ) that does not
begn wth a dgt. An dentfer can be any ength, but ony the frst 31
characters are requred to be recognzed by C compers accordng to the C
standard.
C s case senstve-uppercase and owercase etters are dfferent n C.
Defntons must be paced after the eft brace of a functon and before any
executabe statements.
A syntax error s caused when the comper cannot recognze a statement. The
comper normay ssues an error message to hep you ocate and fx the ncorrect
statement. Syntax errors are voatons of the anguage. Syntax errors are aso
caed compe errors, or compe-tme errors.
Standard Lbrary functon scanf can be used to obtan nput from the
standard nput, whch s usuay the keyboard.
The scanf format contro strng ndcates the type(s) of data that shoud be nput.
The %d converson specfer ndcates that the data shoud be an nteger (the
etter d stands for
"decma nteger"). The % n ths context s treated by scanf (and prntf) as
a speca character that begns a converson specfer.
The other arguments of scanf begn wth an ampersand (&)-caed the address
operator n C-
foowed by a varabe name. The ampersand, when combned wth a varabe
name, tes scanf the ocaton n memory at whch the varabe s ocated. The
computer then stores the vaue for the varabe at that ocaton.
Most cacuatons are performed n assgnment statements.
The = operator and the + operator are bnary operators-each has two
operands.
Functon prntf aso can use a format contro strng as ts frst argument.
Ths strng contans some tera characters to be dspayed and the
converson specfers that ndcate pace hoders for data to output.
Varabe names correspond to ocatons n the computers memory. Every
varabe has a name, a type and a vaue.
Whenever a vaue s paced n a memory ocaton, the vaue repaces the
prevous vaue n that ocaton; thus, pacng a new vaue nto a memory
ocaton s sad to be destructve.
When a vaue s read out of a memory ocaton, the process s sad to
be nondestructve. Secton 2.5 Arthmetc n C
In agebra, f we want to mutpy a tmes b, we can smpy pace these snge-
etter varabe names sde by sde as n ab. In C, however, f we were to do ths, ab
woud be nterpreted as a snge,
two-etter name (or dentfer). Therefore, C (ke other programmng anguages,
n genera) requres that mutpcaton be expcty denoted by usng the *
operator, as n a * b.
The arthmetc operators are a bnary operators.
Integer dvson yeds an nteger resut. For exampe, the expresson 7 / 4
evauates to 1 and the expresson 17 / 5 evauates to 3.
C provdes the remander operator, %, whch yeds the remander after nteger
dvson. The remander operator s an nteger operator that can be used ony wth
nteger operands. The expresson
x % y yeds the remander after x s dvded by y. Thus, 7 % 4 yeds 3 and 17 % 5
yeds 2.
An attempt to dvde by zero s normay undefned on computer systems and
generay resuts n a fata error that causes the program to termnate
mmedatey. Nonfata errors aow programs
to run to competon, often producng ncorrect resuts.
Arthmetc expressons n C must be wrtten n straght-ne form to factate
enterng programs nto the computer. Thus, expressons such as "a dvded by
b" must be wrtten as a/b so that a operators and operands appear n a
straght ne.
Parentheses are used to group terms n C expressons n much the same
manner as n agebrac expressons.
C evauates arthmetc expressons n a precse sequence determned by the
foowng rues of operator precedence, whch are generay the same as those
foowed n agebra.
Mutpcaton, dvson and remander operatons are apped frst. If an
expresson contans severa mutpcaton, dvson and remander operatons,
evauaton proceeds from eft to rght. Mutpcaton, dvson and remander are
sad to be on the same eve of precedence.
Addton and subtracton operatons are evauated next. If an expresson
contans severa addton and subtracton operatons, evauaton proceeds from
eft to rght. Addton and subtracton aso
have the same eve of precedence, whch s ower than the precedence of the
mutpcaton, dvson and remander operators.
The rues of operator precedence specfy the order C uses to evauate
expressons. When we say evauaton proceeds from eft to rght, were referrng
to the assocatvty of the operators. Some operators assocate from rght to eft.
Executabe C statements ether perform actons or make decsons.
Cs f statement aows a program to make a decson based on the truth or
fasty of a statement of fact caed a condton. If the condton s met (.e., the
condton s true) the statement n the body of the f statement executes. If the
condton s not met (.e., the condton s fase) the
body statement does not execute. Whether the body statement s executed
or not, after the f statement competes, executon proceeds wth the next
statement after the f statement.
Condtons n f statements are formed by usng the equaty operators and reatona
operators.
The reatona operators a have the same eve of precedence and assocate eft to
rght. The
equaty operators have a ower eve of precedence than the reatona operators and
they aso assocate eft to rght.
To avod confusng assgnment (=) and equaty (==), the assgnment operator
shoud be read
"gets" and the equaty operator shoud be read "doube equas."
In C programs, whte-space characters such as tabs, newnes and spaces
are normay gnored. So, statements and comments may be spt over severa
nes. It s not correct to spt dentfers.
Some of the words n C programs-such as nt, return and f-are keywords
or reserved words of the anguage. These words have speca meanng to the C
comper, so you cannot use them
as dentfers such as varabe names.
SelfReview Exercises
Fill in the blanks in each of the following.
a) Every C program begns executon at the functon .
b) The begns the body of every functon and the ends the
body of every functon.
c) Every statement ends wth a(n) .
d) The standard brary functon dspays nformaton on the screen.
e) The escape sequence \n represents the character, whch causes the
cursor to poston to the begnnng of the next ne on the screen.
f) The Standard Lbrary functon s used to obtan data from the keyboard.
g) The converson specfer s used n a scanf format contro strng
to ndcate that an nteger w be nput and n a prntf format contro
strng to ndcate that an nteger w be output.
h) Whenever a new vaue s paced n a memory ocaton, that vaue
overrdes the prevous vaue n that ocaton. Ths process s sad to be
.
i) When a vaue s read out of a memory ocaton, the vaue n that ocaton s
preserved;
ths process s sad to be .
j) The statement s used to make decsons.
State whether each of the following is true or false. If false, explain why.
a) Functon prntf aways begns prntng at the begnnng of a new ne.
b) Comments cause the computer to prnt the text encosed between
/* and */ on the screen when the program s executed.
c) The escape sequence \n when used n a prntf format contro strng
causes the cursor to poston to the begnnng of the next ne on the
screen.
d) A varabes must be defned before theyre used.
e) A varabes must be gven a type when theyre defned.
f) C consders the varabes number and NuMbEr to be dentca.
g) Defntons can appear anywhere n the body of a functon.
h) A arguments foowng the format contro strng n a prntf functon
must be preceded by an ampersand (&).
i) The remander operator (%) can be used ony wth nteger operands.
j) The arthmetc operators *, /, %, + and - a have the same
eve of precedence. k) The foowng varabe names are dentca
on a Standard C systems. thssasuperduperongname1234567
thssasuperduperongname1234568
l) A program that prnts three nes of output must contan three prntf statements.
Write a single C stateent to accoplish each of the following!
a) Defne the varabes c, thsVarabe, q76354 and number to be of type nt.
b) Prompt the user to enter an nteger. End your promptng message wth a
coon (:) foowed by a space and eave the cursor postoned after the
space.
c) Read an nteger from the keyboard and store the vaue entered n nteger
varabe a.
d) If number s not equa to 7, prnt "The varabe number s not equa to 7."
e) Prnt the message "Ths s a C program." on one ne.
f) Prnt the message "Ths s a C program." on two nes so that the frst ne ends
wth C.
g) Prnt the message "Ths s a C program." wth each word on a separate ne.
h) Prnt the message "Ths s a C program." wth the words separated by tabs.
Write a stateent "or coent) to accoplish each of the following!
a) State that a program w cacuate the product of three ntegers.
b) Defne the varabes x, y, z and resut to be of type nt.
c) Prompt the user to enter three ntegers.
d) Read three ntegers from the keyboard and store them n the varabes x, y and
z.
e) Compute the product of the three ntegers contaned n varabes x, y and z, and
assgn
the resut to the varabe resut.
f) Prnt "The product s" foowed by the vaue of the nteger varabe resut.
Identify and correct the errors in each of the following stateents!
a) prntf( "The vaue s %d\n", &number );
b) scanf( "%d%d", &number1, number2 );
c) f ( c < 7 );{
prntf( "C s ess than 7\n" );
}
d) f ( c => 7 ) {
prntf( "C s equa to or ess than 7\n" );
}
#xercises
$.% Identfy and correct the errors n each of the foowng statements.
(Note: There may be more than one error per statement.)
a) scanf( "d", vaue );
b) prntf( "The product of %d and %d s %d"\n, x, y );
c) frstNumber + secondNumber = sumOfNumbers
d) f ( number => argest )
argest == number;
e) */ Program to determne the argest of three ntegers /*
f) Scanf( "%d", anInteger );
g) prntf( "Remander of %d dvded by %d s\n", x, y, x % y );
h) f ( x = y );
prntf( %d s equa to %d\n", x, y );
i) prnt( "The sum s %d\n," x + y );
j) Prntf( "The vaue you entered s: %d\n, &vaue );
&.% F n the banks n each of the foowng:
a) are used to document a program and mprove ts readabty.
b) The functon used to dspay nformaton on the screen s .
c) A C statement that makes a decson s .
d) Cacuatons are normay performed by statements.
e) The functon nputs vaues from the keyboard.
'.% Wrte a snge C statement or ne that accompshes each of the foowng:
a) Prnt the message "Enter two numbers."
b) Assgn the product of varabes b and c to varabe a.
c) State that a program performs a sampe payro cacuaton (.e., use
text that heps to document a program).
d) Input three nteger vaues from the keyboard and pace these vaues
n nteger varabes a, b and c.
(.% State whch of the foowng are true and whch are fase. If fase, expan your
answer.
a) C operators are evauated from eft to rght.
b) The foowng are a vad varabe names: _under_bar_, m928134,
t5, |7, her_saes, hs_account_tota, a, b, c, z, z2.
c) The statement prntf("a = 5;"); s a typca exampe of an assgnment statement.
d) A vad arthmetc expresson contanng no parentheses s evauated from eft
to rght.
e) The foowng are a nvad varabe names: 3g, 87, 67h2, h22, 2h.
).% What, f anythng, prnts when each of the foowng statements s
performed? If nothng prnts, then answer "Nothng." Assume x = 2 and y
= 3.
a) prntf( "%d", x );
b) prntf( "%d", x + x );
c) prntf( "x=" );
d) prntf( "x=%d", x );
e) prntf( "%d = %d", x + y, y + x );
f) z = x + y;
g) scanf( "%d%d", &x, &y );
h) /* prntf( "x + y = %d", x + y ); */
i) prntf( "\n" );
*.% Gven the equaton y = ax3 + 7, whch of the foowng, f any, are
correct C statements for ths equaton?
a) y = a * x * x * x + 7;
b) y = a * x * x * ( x + 7 );
c) y = ( a * x ) * x * ( x + 7 );
d) y = ( a * x ) * x * x + 7;
e) y = a * ( x * x * x ) + 7;
f) y = a * x * ( x * x + 7 );
+.% State the order of evauaton of the operators n each of the foowng
C statements and show the vaue of x after each statement s performed.
a) x = 7 + 3 * 6 / 2 - 1;
b) x = 2 % 2 + 2 * 2 - 2 / 2;
c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );
,.% (Arthmetc) Wrte a program that asks the user to enter two numbers,
obtans them from the user and prnts ther sum, product, dfference,
quotent and remander.
-.% (Prntng Vaues wth prntf) Wrte a program that prnts the numbers 1
to 4 on the same ne. Wrte the program usng the foowng methods.
a) Usng one prntf statement wth no converson specfers.
b) Usng one prntf statement wth four converson
specfers. c) Usng four prntf statements.
$..% (Comparng Integers) Wrte a program that asks the user to enter two
ntegers, obtans the numbers from the user, then prnts the arger number
foowed by the words "s arger." If the numbers are equa, prnt the message
"These numbers are equa." Use ony the snge-seecton form of the f
statement you earned n ths chapter.
$$.% (Arthmetc, Largest Vaue and Smaest Vaue) Wrte a program that nputs
three dfferent
ntegers from the keyboard, then prnts the sum, the average, the product, the
smaest and the argest of these numbers. Use ony the snge-seecton form of the
f statement you earned n ths chapter. The screen daogue shoud appear as
foows:
Input three dfferent ntegers: 13 27 14
Sum s 54
Average s 18
Product s 4914
Smaest s 13
Largest s 27
$&.% What does the foowng code prnt?
prntf( "*\n**\n***\n****\n*****\n" );
$'.% (Largest and Smaest Integers) Wrte a program that reads n fve ntegers
and then determnes and prnts the argest and the smaest ntegers n the
group. Use ony the programmng technques you have earned n ths chapter.
$(.% (Odd or Even) Wrte a program that reads an nteger and determnes
and prnts whether t
s odd or even. |Hnt: Use the remander operator. An even number s a mutpe of
two. Any mutpe of two eaves a remander of zero when dvded by 2.|
$).% Prnt your ntas n bock etters down the page. Construct each bock
etter out of the etter t represents as shown beow.
$*.% (Mutpes) Wrte a program that reads n two ntegers and determnes
and prnts f the frst s a mutpe of the second. |Hnt: Use the remander
operator.|
$+.% (Separatng Dgts n an Integer) Wrte a program that nputs one fve-dgt
number, separates the number nto ts ndvdua dgts and prnts the dgts
separated from one another by three
spaces each. |Hnt: Use combnatons of nteger dvson and the remander
operaton.| For exampe, f the user types n 42139, the program shoud prnt
$,.% (Tabe of Squares and Cubes) Usng ony the technques you earned n
ths chapter, wrte
a program that cacuates the squares and cubes of the numbers from 0 to 10
and uses tabs to prnt the foowng tabe of vaues:
Makng a
Dfference
$-.% (Car-Poo Savngs Cacuator) Research severa car-poong webstes.
Create an appcaton
that cacuates your day drvng cost, so that you can estmate how much
money coud be saved by car poong, whch aso has other advantages such as
reducng carbon emssons and reducng traffc congeston. The appcaton
shoud nput the foowng nformaton and dspay the users cost per day of
drvng to work:
a) Tota mes drven per
day. b) Cost per gaon
of gasone. c) Average
mes per gaon.
d) Parkng fees per
day. e) Tos per
day.

Das könnte Ihnen auch gefallen