Anda di halaman 1dari 23

Syntax

if (expression) statement [else statement]


switch (expression) statement
Description
A selection statement alters a program"s execution flow by selecting one path from a collection based
on a specified controlling expression. The if statement and the switch statement are selection
statements.
Examples
if (expression) statement:
if (x<y) x=y;

if (expression) statement else statement:
if (x<y) x=y; else y=x;

switch (expression) statement:
switch (x)
{ case 1: x=y;
break;
default: y=x;
break;
}
Conditional expression:
Syntax
expression-statement ::=
[expression];
Description
C expressions can be statements. A null statement is simply a semicolon by itself.
You can use any valid expression as an expression statement by terminating it with a semicolon.
xpression statements are evaluated for their side effects such as assignment or function calls. !f the
expression is not specified" but the semicolon is still provided" the statement is treated as a null
statement.
#ull statements are useful for specifying no$operation statements. #o$operation statements are often
used in looping constructs where all of the wor% of the statement is done without an additional
statement.
Example
A program fragment that sums up the contents of an array named x containing &' integers might loo%
li%e this(
for(i=0s=0; i<10; s!=x[i!!]);
The syntax of the for statement re)uires a statement following the closing ) of the for. A null
statement *+, satisfies this syntax re)uirement.
Switch:
Syntax
switch ( exp )
{
case const_exp : [statement]"""
[case const_exp : [statement]"""]
[default : [statement]"""]

}
Arguments
exp
The integer expression that the switch statement evaluates and then compares to the
values in all the cases.
const_exp
An integer expression to which exp is compared. !f const_exp matches exp" the
accompanying statement is executed.
statement
-ero or more simple statements. *!f there is more than one simple statement" you do not need
to enclose the statements in braces.,
Description
The switch statement is a conditional branching statement that selects among several statements
based on constant values.
The expression immediately after the switch %eyword must be enclosed in parentheses and must be
an integral expression.
The expressions following the case %eywords must be integral constant expressions+ that is" they may
not contain variables.
An important feature of the switch statement is that program flow continues from the selected case
label until another control$flow statement is encountered or the end of the switch statement is
reached. That is" the compiler executes any statements following the selected case label until a
break" #oto" or retur$ statement appears. The break statement explicitly exits the switch
construct" passing control to the statement following the switch statement. .ince this is usually what
you want" you should almost always include a break statement at the end of the statement list
following each case label.
The following %ri$t&error() function" for example" prints an error message based on an error
code passed to it.
'( )ri$ts error *essa#e based o$ error&code"
( +u$ctio$ is declared with ,oid because it does$-t
( retur$ a$ythi$#"
('
.i$clude <stdio"h/
.defi$e 011&23)45&678 1
.defi$e 011&9)0173: ;
.defi$e 011&9)017591 <
.defi$e 011&5=)0 >
,oid %ri$t&error(i$t error&code)
{
switch (error&code) {
case 011&23)45&678:
%ri$tf(-0rror: 2lle#al i$%ut ,alue"?$-);
break;
case 011&9)0173::
%ri$tf(-0rror: 2lle#al o%era$d"?$-);
break;
case 011&9)017591:
%ri$tf(-0rror: 4$k$ow$ o%erator"?$-);
break;
case 011&5=)0:
%ri$tf(-0rror: 2$co*%atible data"?$-);
break;
default: %ri$tf(-0rror: 4$k$ow$ error code @d?$-
error&code);
break;
}
}
The break statements are necessary to prevent the function from printing more than one error
message. The last break after the default case is not really necessary" but it is a good idea to
include it anyway for the sa%e of consistency.
Evaluation of switch Statement
The switch expression is evaluated+ if it matches one of the case labels" program flow continues
with the statement that follows the matching case label. !f none of the case labels match the switch
expression" program flow continues at the default label" if it exists. *The default label need not be the
last label" though it is good style to put it last., #o two case labels may have the same value.
Associating Statements with Multiple case Values
.ometimes you want to associate a group of statements with more than one case value. To obtain this
behavior" you can enter consecutive case labels. The following function" for instance" returns & if the
argument is a punctuation character" or ' if it is anything else.
'( 5his fu$ctio$ retur$s 1 if the ar#u*e$t is a
( %u$ctuatio$ character" 9therwise it retur$s 0"
('
is&%u$c(char ar#)
{
switch (ar#) {
case -"-:
case --:
case -:-:
case -;-:
case -A-:
case -B-:
case -(-:
case -)-:
case -C-: retur$ 1;
default : retur$ 0;
}
}
Example
'( 4se the switch state*e$t to decide which co**e$t should be %ri$ted ('
.i$clude <stdio"h/
i$t *ai$(,oid)
{
char a$swer #rade;
a$swer = -y-;
%ri$tf(-?$?$-);
while (a$swer == -y- DD a$swer == -=-) {
%ri$tf(-0$ter stude$t-s #rade: -);
fflush(stdi$);
sca$f(-@c- E#rade);
%ri$tf(-?$Fo**e$ts: -);
switch (#rade) {
case -7-:
case -a-:
%ri$tf(-0xcelle$t?$-);
break;
case -G-:
case -b-:
%ri$tf(-Hood?$-);
break;
case -F-:
case -c-:
%ri$tf(-7,era#e?$-);
break;
case -:-:
case -d-:
%ri$tf(-)oor?$-);
break;
case -0-:
case -e-:
case -+-:
case -f-:
%ri$tf(-+ailure?$-);
break;
default:
%ri$tf(-2$,alid #rade?$-);
break;
} '( e$d switch ('
%ri$tf(-?$7#ai$A -);
fflush(stdi$);
sca$f(-@s- Ea$swer);
}
}
!f you execute this program" you get the following output(
0$ter stude$t-s #rade: B

Fo**e$ts: Hood

7#ai$A y
0$ter stude$t-s #rade: C

Fo**e$ts: 7,era#e

7#ai$A n
For Loop:
Syntax
for ([expression1]; [expression2]; [expression3])
statement;
Arguments
expression1
Expression1 is the initialization expression that typically specifies the initial values of
variables. !t is evaluated only once before the first iteration of the loop.
expression2
Expression2 is the controlling expression that determines whether or not to terminate the
loop. !t is evaluated before each iteration of the loop. !f expression2 evaluates to a
non/ero value" the loop body is executed. !f it evaluates to '" execution of the loop body is
terminated and control passes to the first statement after the loop body. This means that if the
initial value of expression2 evaluates to /ero" the loop body is never executed.
expression3
Expression3 is the increment expression that typically increments the variables initiali/ed
in expression1. !t is evaluated after each iteration of the loop body and before the next
evaluation of the controlling expression.
Description
The for statement executes the statement*s, within a loop as long as expression2 is true. The
for statement is a general$purpose looping construct that allows you to specify the initiali/ation"
termination" and increment of the loop. The for uses three expressions. .emicolons separate the
expressions. ach expression is optional" but you must include the semicolons.
How the for Loop is Executed
The for statement wor%s as follows(
1. 0irst" expression1 is evaluated. This is usually an assignment expression that initiali/es
one or more variables.
2. Then expression2 is evaluated. This is the conditional part of the statement.
3. !f expression2 is false" program control exits the for statement and flows to the next
statement in the program. !f expression2 is true" statement is executed.
4. After statement is executed" expression3 is evaluated. Then the statement loops bac%
to test expression2 again.
for Loop Processing
The for loop continues to execute until expression2 evaluates to ' *false," or until a branch
statement" such as a break or #oto" interrupts loop execution.
!f the loop body executes a co$ti$ue statement" control passes to expression3. xcept for the
special processing of the co$ti$ue statement" the for statement is e)uivalent to the following(
expression1;
while (expression2) {
statement
expression3;
}
You may omit any of the three expressions. !f expression2 *the controlling expression, is omitted" it
is ta%en to be a non/ero constant.
for versus while Loops
#ote that for loops can be written as while loops" and vice versa. 0or example" the for loop
for (I = 0; I < 10; I!!)
{
do&so*ethi$#();
}
is the same as the following while loop(
I = 0;
while (I<10)
{
do&so*ethi$#();
I!!;
}
Example
'( )ro#ra* $a*e is -for&exa*%le-" 5he followi$# co*%utes a
( %er*utatio$ that is )($*) = $C'($B*)C usi$# for
( loo%s to co*%ute $C a$d ($B*)C
('
.i$clude <stdio"h/
.defi$e J2K0 10

i$t *ai$(,oid)
{
i$t $ * $&total *&total %er* i I *id cou$t;

%ri$tf(-0$ter the $u*bers for the %er*utatio$ ($ thi$#s -);
%ri$tf(-take$ * at a ti*e)?$se%arated by a s%ace: -);
sca$f(-@d @d- E$ E*);
$&total = *&total = 1;
for (i = $; i / 0; iBB) '( co*%ute $C ('
$&total (= i;
for (i = $ B *; i / 0; iBB) '( co*%ute ($B*)C ('
*&total (= i;
%er* = $&total'*&total;
%ri$tf(-)(@d@d) = @d?$?$- $ * %er*);

'( 5his series of for loo%s %ri$ts a %atter$ of -K-s- a$d shows
( how loo%s ca$ be $ested a$d how you ca$ either i$cre*e$t or
( decre*e$t your loo% ,ariable" 5he loo%s also show the %ro%er
( %lace*e$t of curly braces to i$dicate that the outer loo%s
( ha,e *ulti%le state*e$ts"
('
%ri$tf(-3ow %ri$t the %atter$ three ti*es:?$?$-);
*id = J2K0';;

'( co$trols how *a$y ti*es %atter$ is %ri$ted ('
for (cou$t = 0; cou$t < <; cou$t!!)
{
for (I = 0; I < *id; I!!)
{
'( loo% for %ri$ti$# a$ i$di,idual li$e ('
for (i = 0; i < J2K0; i!!)
if (i < *id B I DD i / *id ! I)
%ri$tf(- -);
else
%ri$tf(-K-);
%ri$tf(-?$-);
}
for (I = *id; I /= 0; IBB)
{
for (i = 0; i <= J2K0; i!!)
if (i < *id B I DD i / *id ! I)
%ri$tf(- -);
else
%ri$tf(-K-);
%ri$tf(-?$-);
}
}
}
!f you execute this program" you get the following output(
0$ter the $u*bers for the %er*utatio$ ($ thi$#s take$ * at a
ti*e) se%arated by a s%ace: 4 3
)(><) = ;>

3ow %ri$t the %atter$ three ti*es:

K
KKK
KKKKK
KKKKKKK
KKKKKKKKK
KKKKKKKKKKK
KKKKKKKKK
KKKKKKK
KKKKK
KKK
K
K
KKK
KKKKK
KKKKKKK
KKKKKKKKK
KKKKKKKKKKK
KKKKKKKKK
KKKKKKK
KKKKK
KKK
K
K
KKK
KKKKK
KKKKKKK
KKKKKKKKK
KKKKKKKKKKK
KKKKKKKKK
KKKKKKK
KKKKK
KKK
K
Syntax
for ([expression1]; [expression2]; [expression3])
statement;
Arguments
expression1
Expression1 is the initialization expression that typically specifies the initial values of
variables. !t is evaluated only once before the first iteration of the loop.
expression2
Expression2 is the controlling expression that determines whether or not to terminate the
loop. !t is evaluated before each iteration of the loop. !f expression2 evaluates to a
non/ero value" the loop body is executed. !f it evaluates to '" execution of the loop body is
terminated and control passes to the first statement after the loop body. This means that if the
initial value of expression2 evaluates to /ero" the loop body is never executed.
expression3
Expression3 is the increment expression that typically increments the variables initiali/ed
in expression1. !t is evaluated after each iteration of the loop body and before the next
evaluation of the controlling expression.
Description
The for statement executes the statement*s, within a loop as long as expression2 is true. The
for statement is a general$purpose looping construct that allows you to specify the initiali/ation"
termination" and increment of the loop. The for uses three expressions. .emicolons separate the
expressions. ach expression is optional" but you must include the semicolons.
How the for Loop is Executed
The for statement wor%s as follows(
1. 0irst" expression1 is evaluated. This is usually an assignment expression that initiali/es
one or more variables.
2. Then expression2 is evaluated. This is the conditional part of the statement.
3. !f expression2 is false" program control exits the for statement and flows to the next
statement in the program. !f expression2 is true" statement is executed.
4. After statement is executed" expression3 is evaluated. Then the statement loops bac%
to test expression2 again.
for Loop Processing
The for loop continues to execute until expression2 evaluates to ' *false," or until a branch
statement" such as a break or #oto" interrupts loop execution.
!f the loop body executes a co$ti$ue statement" control passes to expression3. xcept for the
special processing of the co$ti$ue statement" the for statement is e)uivalent to the following(
expression1;
while (expression2) {
statement
expression3;
}
You may omit any of the three expressions. !f expression2 *the controlling expression, is omitted" it
is ta%en to be a non/ero constant.
for versus while Loops
#ote that for loops can be written as while loops" and vice versa. 0or example" the for loop
for (I = 0; I < 10; I!!)
{
do&so*ethi$#();
}
is the same as the following while loop(
I = 0;
while (I<10)
{
do&so*ethi$#();
I!!;
}
Example
'( )ro#ra* $a*e is -for&exa*%le-" 5he followi$# co*%utes a
( %er*utatio$ that is )($*) = $C'($B*)C usi$# for
( loo%s to co*%ute $C a$d ($B*)C
('
.i$clude <stdio"h/
.defi$e J2K0 10

i$t *ai$(,oid)
{
i$t $ * $&total *&total %er* i I *id cou$t;

%ri$tf(-0$ter the $u*bers for the %er*utatio$ ($ thi$#s -);
%ri$tf(-take$ * at a ti*e)?$se%arated by a s%ace: -);
sca$f(-@d @d- E$ E*);
$&total = *&total = 1;
for (i = $; i / 0; iBB) '( co*%ute $C ('
$&total (= i;
for (i = $ B *; i / 0; iBB) '( co*%ute ($B*)C ('
*&total (= i;
%er* = $&total'*&total;
%ri$tf(-)(@d@d) = @d?$?$- $ * %er*);

'( 5his series of for loo%s %ri$ts a %atter$ of -K-s- a$d shows
( how loo%s ca$ be $ested a$d how you ca$ either i$cre*e$t or
( decre*e$t your loo% ,ariable" 5he loo%s also show the %ro%er
( %lace*e$t of curly braces to i$dicate that the outer loo%s
( ha,e *ulti%le state*e$ts"
('
%ri$tf(-3ow %ri$t the %atter$ three ti*es:?$?$-);
*id = J2K0';;

'( co$trols how *a$y ti*es %atter$ is %ri$ted ('
for (cou$t = 0; cou$t < <; cou$t!!)
{
for (I = 0; I < *id; I!!)
{
'( loo% for %ri$ti$# a$ i$di,idual li$e ('
for (i = 0; i < J2K0; i!!)
if (i < *id B I DD i / *id ! I)
%ri$tf(- -);
else
%ri$tf(-K-);
%ri$tf(-?$-);
}
for (I = *id; I /= 0; IBB)
{
for (i = 0; i <= J2K0; i!!)
if (i < *id B I DD i / *id ! I)
%ri$tf(- -);
else
%ri$tf(-K-);
%ri$tf(-?$-);
}
}
}
!f you execute this program" you get the following output(
0$ter the $u*bers for the %er*utatio$ ($ thi$#s take$ * at a
ti*e) se%arated by a s%ace: 4 3
)(><) = ;>

3ow %ri$t the %atter$ three ti*es:

K
KKK
KKKKK
KKKKKKK
KKKKKKKKK
KKKKKKKKKKK
KKKKKKKKK
KKKKKKK
KKKKK
KKK
K
K
KKK
KKKKK
KKKKKKK
KKKKKKKKK
KKKKKKKKKKK
KKKKKKKKK
KKKKKKK
KKKKK
KKK
K
K
KKK
KKKKK
KKKKKKK
KKKKKKKKK
KKKKKKKKKKK
KKKKKKKKK
KKKKKKK
KKKKK
KKK
K
While:
Syntax
while ( exp )
statement
Arguments
exp
Any expression.
statement
This statement is executed when the while *exp, is true.
Description
The while statement executes the statements within a loop as long as the specified condition" exp"
is true. This is one of the three looping constructions available in C. 1i%e the for loop" the while
statement tests exp and if it is true *non/ero," statement is executed. 2nce exp becomes false *',"
execution of the loop stops. .ince exp could be false the first time it is tested" statement may not
be performed even once.
The following describes two ways to 3ump out of a while loop prematurely *that is" before exp
becomes false,(
4se brea% to transfer control to the first statement following the while loop.
4se goto to transfer control to some labeled statement outside the loop.
Example
'( )ro#ra* $a*e is -while&exa*%le- ('
.i$clude <stdio"h/

i$t *ai$(,oid)
{
i$t cou$t = 0 cou$t; = 0;
char a&stri$#[L0] (%tr&to&a&stri$# = a&stri$#;

%ri$tf(-0$ter a stri$# BB -);
#ets(a&stri$#);

while ((%tr&to&a&stri$#!!)
cou$t!!; '( 7 si*%le state*e$t loo% ('
%ri$tf(-5he stri$# co$tai$s @d characters"?$- cou$t);
%ri$tf(-5he first word of the stri$# is -);

while (a&stri$#[cou$t;] C= - - EE a&stri$#[cou$t;] C= -?0-)
{
'( 7 co*%ou$d state*e$t loo% ('
%ri$tf (-@c- a&stri$#[cou$t;]);
cou$t;!!;
}
%ri$tf(-?$-);
}
!f you execute this program" you get the following output(
0$ter a stri$# Four score and seven years ago
5he stri$# co$tai$s <0 characters"
5he first word of the stri$# is +our
Do whilw:
Syntax
do
statement; while (expression);
Arguments
statement
A null statement" simple statement" or compound statement.
exp
Any expression.
Description
The do statement executes statements within a loop until a specified condition is satisfied. This is one
of the three looping constructions in C. 4nli%e the for and while loops" dowhile performs
statement first and then tests expression. !f expression evaluates to non/ero *true,"
statement executes again" but when expression evaluates to /ero *false," execution of the loop
stops. This type of loop is always executed at least once.
Two ways to 3ump out of a dowhile loop prematurely *that is" before expression becomes false,
are the following(
4se break to transfer control to the first statement following the dowhile loop.
4se #oto to transfer control to some labeled statement outside of the loop.
Example
'( )ro#ra* $a*e is -do"while&exa*%le-" 5his %ro#ra* fi$ds the
( su**atio$ (that is $(($!1)';) of a$ i$te#er that a user
( su%%lies a$d the su**atio$ of the sMuares of that i$te#er"
( 5he use of the do'while *ea$s that the code i$side the loo%
( is always executed at least o$ce"
('
.i$clude <stdio"h/
i$t *ai$(,oid)
{
i$t $u* su* sMuare&su*;
char a$swer;

%ri$tf(-?$-);
do
{
%ri$tf(-0$ter a$ i$te#er: -);
sca$f(-@d- E$u*);
su* = ($u*(($u*!1))';;
sMuare&su* = ($u*(($u*!1)((;($u*!1))'N;
%ri$tf(-5he su**atio$ of @d is: @d?$- $u* su*);
%ri$tf(-5he su**atio$ of its sMuares is: @d?$-
sMuare&su*);
%ri$tf(-?$7#ai$A -);
fflush(stdi$);
sca$f(-@c- Ea$swer);
} while ((a$swer C= -$-) EE (a$swer C= -3-));
}
!f you execute this program" you get the following output(
0$ter a$ i$te#er: 10
5he su**atio$ of 10 is: OO
5he su**atio$ of its sMuares is: <LO

7#ai$A y
0$ter a$ i$te#er: 25
5he su**atio$ of ;O is: <;O
5he su**atio$ of its sMuares is: OO;O

7#ai$A n
Goto And label:
Syntax
#oto label;
#oto (expression;
co$ti$ue;
break;
retur$ [expression];
Description
5ranch statements transfer control unconditionally to another place in the executing program. The
branch statements are goto " continue " brea% " and return .
Examples
These four fragments all accomplish the same thing *they print out the multiples of 6 between & and
&'',(
i = 0;
while (i < 100)
{
if (!!i @ O)
co$ti$ue; '( u$co$ditio$al Iu*% to to% of while loo% ('
%ri$tf (-@;d - i);
}
%ri$tf (-?$-);

i = 0;
8: while (i < 100)
{
if (!!i @ O)
#oto 8: '( u$co$ditio$al Iu*% to to% of while loo% ('
%ri$tf (-@;d -i);
}
%ri$tf (-?$-);


i = 0;
while (1)
{
if ((!!i @ O) == 0)
%ri$tf (-@;d - i);
if (i / 100)
break; '( u$co$ditio$al Iu*% %ast the while loo% ('
}
%ri$tf (-?$-);
i = 0;
while (1)
{
if ((!!i @ O) == 0)
%ri$tf (-@;d - i);
if (i / 100) {
%ri$tf (-?$-);
retur$; '( u$co$ditio$al Iu*% to calli$# fu$ctio$ ('
}
}
Compound statement or block:
Syntax
compound-statement ::=
{[declaration-list][statement-list]}

declaration-list ::=
declaration
declaration-list
declaration

statement-list ::=
statement
statement-list statement
Description
A compound statement allows you to group statements together in a bloc% of code and use them as
if they were a single statement.
7ariables and constants declared in the bloc% are local to the bloc% and to any inner bloc%s unless
declared exter$. !f the ob3ects are initiali/ed" the initiali/ation is performed each time the compound
statement is entered from the top through the left brace *{, character. !f the statement is entered via a
#oto statement or in a switch statement" the initiali/ation is not performed.
Any ob3ect declared with static storage duration is created and initiali/ed when the program is loaded
for execution. This is true even if the ob3ect is declared in an inner bloc%.
Example
if (x / y)
{
i$t te*%;
te*% = x;
x = y;
y = te*%;
}
!n this example" variable te*% is local to the compound statement. !t can only be accessed within the
compound statement.
Label statements:
Syntax
labeled-statement ::=
identifier : statement
case
constant-expression : statement
default: statement
Description
Laeled statements are those preceded by a name or tag. You can prefix any statement using a label
so at some point you can reference it using goto statements. Any statement can have one or more
labels.
The case and default labels can only be used inside a switch statement.
Example
if (fatal&error)
#oto #et&out;
" " "
#et&out: retur$(+7578&F93:25293);
Break:
Syntax
break;
Description
A break statement terminates the execution of the most tightly enclosing switch statement or for "
while " do...while loop.
Control passes to the statement following the switch or iteration statement. You cannot use a
break statement unless it is enclosed in a switch or loop statement. 0urther" a break only exits out
of one level of switch or loop statement. To exit from more than one level" you must use a #oto
statement.
8hen used in the switch statement" break normally terminates each case statement. !f you use no
break *or other unconditional transfer of control," each statement labeled with case flows into the
next. Although not re)uired" a break is usually placed at the end of the last case statement. This
reduces the possibility of errors when inserting additional cases at a later time.
Example
The following example uses break to exit from the for loop after executing the loop three times(
for (i=0; i<=N; i!!)
if(i==<) break;
else %ri$tf (-@d?$-i);
This example prints(
0
1
;
Continue:
Syntax
co$ti$ue;
Description
The co$ti$ue statement halts execution of its enclosing for" while" or do'while loop and s%ips
to the next iteration of the loop. !n the while and do'while" this means the expression is tested
immediately" and in the for loop" the third expression *if present, is evaluated.
Example
'( )ro#ra* $a*e is -co$ti$ue&exa*%le-" 5his %ro#ra*
( reads a file of stude$t $a*es a$d test scores" 2t
( a,era#es each stude$t-s #rade" 5he for loo% uses
( a co$ti$ue state*e$t so that the third test score
( is $ot i$cluded"
('
.i$clude <stdio"h/

i$t *ai$(,oid)
{
i$t test&score tot&score i;
float a,era#e;
+280 (f%;
char f$a*e[10] l$a*e[1O];

f% = fo%e$(-#rades&data- -r-);
while (Cfeof(f%)) '( while $ot e$d of file ('
{
tot&score = 0;
fsca$f(f% -@s @s- f$a*e l$a*e);
%ri$tf(-?$Jtude$t-s $a*e: @s @s?$Hrades: - f$a*e l$a*e);
for (i = 0; i < O; i!!)
{
fsca$f(f% -@d- Etest&score);
%ri$tf(-@d - test&score);
if (i == ;) '( lea,e out this test score ('
co$ti$ue;
tot&score != test&score;
} '( e$d for i ('
fsca$f(f% -?$-); '( read e$dBofBli$e at e$d of ('
'( each stude$t-s data ('
a,era#e = tot&score'>"0;
%ri$tf(-?$7,era#e test score: @>"1f?$- a,era#e);
} '( e$d while ('
fclose(f%);
}
!f you execute this program" you get the following output(
Jtude$t-s $a*e: Garry Pui#ley
Hrades: LO Q1 LL 100 RO
7,era#e test score: LR"L

Jtude$t-s $a*e: )e%%er 1ose$ber#
Hrades: Q1 RN LL Q; LL
7,era#e test score: LN"L

Jtude$t-s $a*e: Jue Fo$$ell
Hrades: QO Q< Q1 Q; LQ
7,era#e test score: Q;";
Return!
Syntax
retur$; '( first for* ('
retur$ exp; '( seco$d for* ('
Arguments
exp
Any valid C expression.
Description
The retur$ statement causes a C program to exit from the function containing the retur$ and go
bac% to the calling bloc%. !t may or may not have an accompanying exp to evaluate. !f there is no exp"
the function returns an unpredictable value.
A function may contain any number of return statements. The first one encountered in the normal flow
of control is executed" and causes program control to be returned to the calling routine. !f there is no
retur$ statement" program control returns to the calling routine when the right brace of the function
is reached. !n this case" the value returned is undefined.
!eturn "ypes
The return value must be assignment$compatible with the type of the function. This means that the
compiler uses the same rules for allowable types on either side of an assignment operator to
determine allowable return types. 0or example" if f() is declared as a function returning an i$t" it is
legal to return any arithmetic type" since they can all be converted to an i$t. !t would be illegal"
however" to return an aggregate type or a pointer" since these are incompatible types.
The following example shows a function that returns a float" and some legal return values.
float f(,oid)
{
float f;;
i$t a;
char c;
f; = a; '( 9S Muietly co$,erts a to float ('
retur$ a; '( 9S Muietly co$,erts a to float ('
f; = c; '( 9S Muietly co$,erts c to float ('
retur$ c; '( 9S Muietly co$,erts c to float ('
}
Pointer !eturn "ypes
The C language is stricter about matching pointers. !n the following example" f() is declared as a
function returning a pointer to a char. .ome legal and illegal return statements are shown.
char (f(,oid)
{
char ((c%% (c%1 (c%; ca[10];
i$t (i%1 (i%;;

c%1 = c%;; '( 9S ty%es *atch ('
retur$ c%;; '( 9S ty%es *atch ('
c%1 = (c%%; '( 9S ty%es *atch ('
retur$ (c%%; '( 9S ty%es *atch ('

'( 7$ array $a*e without a subscri%t is co$,erted
( to a %oi$ter to the first ele*e$t"
('
c%1 = ca; '( 9S ty%es *atch ('
retur$ ca; '( 9S ty%es *atch ('

c%1 = (c%;; '( 0rror *is*atched ty%es ('
'( (%oi$ter to char ,s" char) ('
retur$ (c%;; '( 0rror *is*atched ty%es ('
'( (%oi$ter to char ,s" char) ('
c%1 = i%1; '( 0rror *is*atched %oi$ter ty%es ('
retur$ i%1; '( 0rror *is*atched %oi$ter ty%es ('
retur$; '( )roduces u$defi$ed beha,ior ('
'( should retur$ (char () ('
}
#ote in the last statement that the behavior is undefined if you return nothing. The only time you can
safely use retur$ without an expression is when the function type is ,oid. Conversely" if you return
an expression for a function that is declared as returning ,oid" you will receive a compile$time error.
0unctions can return only a single value directly via the retur$ statement. The retur$ value can be
any type except an array or function. This means that it is possible to return more than a single value
indirectly by passing a pointer to an aggregate type. !t is also possible to return a structure or union
directly. 9: C implements this by passing the structure or union by reference if the structure or union is
greater than eight bytes.
Example
'( )ro#ra* $a*e is -retur$&exa*%le-"
( 5his %ro#ra* fi$ds the le$#th of a word that is e$tered"
('
.i$clude <stdio"h/

i$t fi$d&le$#th( char (stri$# )
{
i$t i;
for (i =0; stri$#[i] C= T?0T; i!!);
retur$ i;
}

i$t *ai$( ,oid )
{
char stri$#[1<;];
i$t result;
i$t a#ai$ = 1;
char a$swer;

%ri$tf( -5his %ro#ra* fi$ds the le$#th of a$y word you -);
%ri$tf( -e$ter"?$- );
do
{
%ri$tf( -0$ter the word: -);
fflush(stdi$);
#ets( stri$# );
result = fi$d&le$#th( stri$# );
%ri$tf( -5his word co$tai$s @d characters" ?$- result);
%ri$tf(-7#ai$A -);
sca$f(-@c- Ea$swer);
} while (a$swer == T=T DD a$swer == TyT);
}
!f you execute this program" you get the following output(
5his %ro#ra* fi$ds the le$#th of a$y stri$# you e$ter"

0$ter the stri$#: Copenhagen
5he stri$# is 10 characters"
7#ai$A y

0$ter the stri$#: galaxy
5he stri$# is N characters"
7#ai$A n
exit#$
Syntax:
.i$clude <stdlib"h/
,oid exit( i$t exit&code );
Description:
The exit() function stops the program. exit_code is passed on to be the return value of the
program, where usuall !ero indicates success and non"!ero indicates an error.
atexit#$
Syntax:
.i$clude <stdlib"h/
i$t atexit( ,oid ((fu$c)(,oid) );
Description:
The function atexit() causes the function pointed to b func to be called when the
program terminates. #ou can ma$e multiple calls to atexit() (at least 32, depending on
our compiler) and those functions will be called in reverse order of their establishment.
The return value of atexit() is !ero upon success, and non!ero on failure.