Anda di halaman 1dari 39

CHAPTER 3: Functions

1. What is the purpose of main() function?


In C, program execution starts from the main() function.
Every C program must contain a main() function. The main
function may contain any number of statements. These
statements are executed sequentially in the order which
they are written.
The main function can in-turn call other functions. hen
main calls a function, it passes the execution control to that
function. The function returns control to main when a return
statement is executed or when end of function is reached.
In C, the function prototype of the !main! is one of the
following"
int main(); //main with no arguments
int main(int argc, char *argv[]); //main with
arguments
The parameters argc and argv respectively give the number
and value of the program!s command-line arguments.
Eamp!e:
#include <stdio.h>
/* program section egins here */
int main() !
// opening race " program e#ecution starts here
print$(%&elcome to the world o$ '%);
return (;
)/
/ closing race " program terminates here
"utput:
elcome to the world of C
#. Ep!ain comman$ !ine ar%uments of main function?
In C, we can supply arguments to !main! function. The
arguments that we pass to main # $ at command prompt are
1
called command line arguments. These arguments are
supplied at the time of invo%ing the program.
The main( )function can ta%e arguments as"
main(int argc, char *argv[]) ! )
The &rst argument argc is %nown as !argument counter!. It
represents the number of arguments in the command line.
The second argument argv is %nown as 'argument vector'. It
is an array of char type pointers that points to the command
line arguments. 'i(e of this array will be equal to the value of
argc.
Eamp!e:
at the command prompt if we give"
'*+> $ruit.e#e apple mango
Then
argc would contain value )
argv [(]
would contain base address of string * fruit.exe* which is the
command name that invo%es the program.
argv [,] would contain base address of string *apple*
argv [-] would contain base address of string *mango*
here apple and mango are the arguments passed to the
program fruit.exe
Pro%ram:
#include <stdio.h>
int main(int argc, char *argv[]) !
int n;
2
print$(%.ollowing are the arguments entered in the
command line%);
$or (n / (; n < argc; n00) !
print$(%+n 1s%, argv[n]);
)
print$(%+n 2umer o$ arguments entered are+n 1d+n%,
argc);
return (;
)
"utput:
+ollowing are the arguments entered in the command line
'*+testpro3ect.e#e
apple
mango
,umber of arguments entered are )
3. What are hea$er &!es? Are functions $ec!are$ or
$e&ne$ in hea$er &!es ?
+unctions and macros are declared in header &les. -eader
&les would be included in source &les by the compiler at the
time of compilation.
-eader &les are included in source code using #include
directive. #include<some.h> includes all the declarations
present in the header &le 4some.h4.
. header &le may contain declarations of sub-routines,
functions, macros and also variables which we may want to
use in our program. -eader &les help in reduction of
repetitive code.
'(nta of inc!u$e $irecti)e:
#include<stdio.h> //includes the header $ile
stdio.h, standard input output header into the
source code
3
+unctions can be declared as well as de&ned in header &les.
/ut it is recommended only to declare functions and not to
de&ne in the header &les. hen we include a header &le in
our program we actually are including all the functions,
macros and variables declared in it.
In case of pre-de&ned C standard library header &les
ex#stdio.h$, the functions calls are replaced by equivalent
binary code present in the pre-compiled libraries. Code for C
standard functions are lin%ed and then the program is
executed. -eader &les with custom names can also be
created.
Pro%ram: Custom hea$er &!es eamp!e
/****************
5nde#* restaurant.h
****************/
int ill6ll(int $ood7cost, int ta#, int tip);
/****************
5nde#* restaurant.c
****************/
#include<stdio.h>
int ill6ll(int $ood7cost, int ta#, int tip) !
int result;
result / $ood7cost 0 ta# 0 tip;
print$(%8otal ill is 1d+n%,result);
return result;
)
/****************
5nde#* main.c
****************/
#include<stdio.h>
#include%restaurant.h%
int main() !
int $ood7cost, ta#, tip;
$ood7cost / 9(;
ta# / ,(;
tip / 9;
ill6ll($ood7cost,ta#,tip);
return (;
)
4
*. What are the $i+erences ,et-een forma!
ar%uments an$ actua! ar%uments of a function?
.rgument" .n argument is an expression which is passed to
a function by its caller #or macro by its invo%er$ in order for
the function#or macro$ to perform its tas%. It is an expression
in the comma-separated list bound by the parentheses in a
function call expression.
Actua! ar%uments:
The arguments that are passed in a function call are called
actual arguments. These arguments are de&ned in the
calling function.
Forma! ar%uments:
The formal arguments are the parameters0arguments in a
function declaration. The scope of formal arguments is local
to the function de&nition in which they are used. +ormal
arguments belong to the called function. +ormal arguments
are a copy of the actual arguments. . change in formal
arguments would not be re1ected in the actual arguments.
Eamp!e:
#include <stdio.h>
void sum(int i, int 3, int :);
/* calling $unction */
int main() !
int a / 9;
// actual arguments
sum(;, - * a, a);
return (;
)
/* called $unction */
/* $ormal arguments*/
void sum(int i, int 3, int :) !
int s;
s / i 0 3 0 :;
print$(%sum is 1d%, s);
5
)
-ere ),23a,a are actual arguments and i,4,% are formal
arguments.
.. What is pass ,( )a!ue in functions?
5ass by 6alue" In this method, the value of each of the actual
arguments in the calling function is copied into
corresponding formal arguments of the called function. In
pass by value, the changes made to formal arguments in the
called function have no e7ect on the values of actual
arguments in the calling function.
Eamp!e:
#include <stdio.h>
void swap(int #, int <) !
int t;
t / #;
# / <;
< / t;
) int main()
!
int m / ,(, n / -(;
print$(%=e$ore e#ecuting swap m/1d n/1d+n%, m, n);
swap(m, n);
print$(%6$ter e#ecuting swap m/1d n/1d+n%, m, n);
return (;
)
"utput:
/efore executing swap m89: n82:
.fter executing swap m89: n82:
Ep!anation:
In the main function, value of variables m, n are not changed
though they are passed to function !swap!. 'wap function
6
has a copy of m, n and hence it can not manipulate the
actual value of arguments passed to it.
/. What is pass ,( reference in functions?
Pass ,( Reference:
In this method, the addresses of actual arguments in the
calling function are copied into formal arguments of the
called function. This means that using these addresses, we
would have an access to the actual arguments and hence we
would be able to manipulate them. C does not support Call
by reference. /ut it can be simulated using pointers.
Eamp!e:
#include <stdio.h>
/* $unction de$inition */
void swap(int *#, int *<) !
int t;
t / *#; /* assign the value at address # to t */
*# / *<; /* put the value at < into # */
*< / t; /* put the value at to < */
) int main() !
int m / ,(, n / -(;
print$(%=e$ore e#ecuting swap m/1d n/1d+n%, m, n);
swap(>m, >n);
print$(%6$ter e#ecuting swap m/1d n/1d+n%, m, n);
return (;
)
"utput:
/efore executing swap m89: n82:
.fter executing swap m82: n89:
Ep!anation:
In the main function, address of variables m, n are sent as
arguments to the function !swap!. .s swap function has the
access to address of the arguments, manipulation of passed
7
arguments inside swap function would be directly re1ected
in the values of m, n.
;. hat are the di7erences between getchar#$ and scanf#$
functions for reading string
8
0. "ut of the functions f%ets() an$ %ets()1 -hich one is
safer to use an$ -h(?
<ut of functions $gets( ) and gets( ), $gets( ) is safer to
use. gets( ) receives a string from the %eyboard and it is
terminated only when the enter %ey is hit. There is no limit
for the input string. The string can be too long and may lead
to bu7er over1ow.
Eamp!e:
gets(s) /* s is the input string */
hereas $gets( ) reads string with a speci&ed limit, from a
&le and displays it on screen.The function $gets( ) ta%es
three arguments.
First ar%ument " address where the string is stored.
'econ$ ar%ument " maximum length of the string.
Thir$ ar%ument " pointer to a +I=E.
Eamp!e:
$gets(s,-(,$p); /* s* address o$ the string, -(*
ma#imum length o$ string, $p* pointer to a $ile */
The second argument limits the length of string to be read.
Thereby it avoids over1ow of input bu7er. Thus $gets( ) is
preferable to gets( ).
>. What is the $i+erence ,et-een the functions
str$up() an$ strcp(()?
strcp( function:
9
copies a source string to a destination de&ned by user. In
strcpy function both source and destination strings are
passed as arguments. ?ser should ma%e sure that
destination has enough space to accommodate the string to
be copied. !strcpy! sounds li%e short form of *string copy*.
'(nta:
strcp<(char *destination, const char *source);
'ource string is the string to be copied and destination string
is string into which source string is copied. If successful,
strcpy subroutine returns the address of the copied string.
<therwise, a null pointer is returned.
Eamp!e Pro%ram:
#include<stdio.h>
#include<string.h>
int main() !
char m<name[,(];
//cop< contents to m<name
strcp<(m<name, %6?@52A%);
//print the string
puts(m<name);
return (;
)
"utput:
.@6I,A
Ep!anation:
If the string to be copied has more than 9: letters, strcpy
cannot copy this string into the string !myname!. This is
because string !myname! is declared to be of si(e 9:
characters only.
In the above program, string *nodalo* is copied in myname
and is printed on output screen.
10
str$up function:
duplicates a string to a location that will be decided by the
function itself. +unction will copy the contents of string to
certain memory location and returns the address to that
location. 4strdup4 sounds li%e short form of "string
duplicate"
'(nta:
strdup (const char *s);
strdup returns a pointer to a character or base address of an
array.
+unction returns address of the memory location where the
string has been copied. In case free space could not be
created then it returns a null pointer.
/oth strcp< and strdup functions are present in header &le
<string.h>
Pro%ram:
Pro%ram to i!!ustrate str$up().
#include<stdio.h>
#include<string.h>
#include<stdli.h>
int main() !
char m<name[] / %6?@52A%;
//name is pointer variale which can store the
address o$ memor< location o$ string
char* name;
//contents o$ m<name are copied in a memor< address
and are assigned to name
name / strdup(m<name);
//prints the contents o$ 4name4
puts(name);
//prints the contents o$ 4m<name4
puts(m<name);
//memor< allocated to 4name4 is now $reed
11
$ree(name);
return (;
)
"utput:
.@6I,A
.@6I,A
Ep!anation:
'tring myname consists of *.@6I,A* stored in it. Contents of
myname are copied in a memory address and memory is
assigned to name. .t the end of the program, memory can
be freed using $ree(name);
1. What is the difference between declaring a variable and defining a variable?
Ans: Declaration of a variable in C hints the compiler about the type and size of
the variable in compile time. Similarly,
declaration of a function hints about type and size of function parameters. o
space is reserved in memory for any variable in case of declaration.

Example! int a"
#ere variable $a$ is declared of data type $int$

Defining a variable means declaring it and also allocating space to hold it.

We can say %Definition & Declaration ' Space reservation%.

Example: int a & 1("
#ere variable %a% is described as an int to the compiler and memory is allocated
to hold value 1(.

). What is a static variable?
* static variable is a special variable that is stored in the data segment unli+e the
default automatic variable that
is stored in stac+. * static variable can be initialized by using +eyword static
before variable name.

Example:

static int a / 9;
12
* static variable behaves in a different manner depending upon whether it is a
global variable or a local variable.
* static global variable is same as an ordinary global variable e,cept that it
cannot be accessed by other files in the same program - pro.ect even with the
use of +eyword e,tern. * static local variable is different from local variable. /t is
initialized only once no matter how many times that function in which it resides is
called. /t may be used as a count variable.

Example:
#include <stdio.h>
//program in $ile $,.c
void count(void) !
static int count, / (;
int count- / (;
count,00;
count-00;
print$(%+n@alue o$ count, is 1d, @alue o$ count- is 1d%,
count,, count-);
)/
*Bain $unction*/
int main()!
count();
count();
count();
return (;
)

Output:
0alue of count1 is 1, 0alue of count) is 1
0alue of count1 is ), 0alue of count) is 1
0alue of count1 is 1, 0alue of count) is 1
1. What is a register variable?
2egister variables are stored in the C34 registers. /ts default value is a garbage
value. Scope of a register variable is local to the bloc+ in which it is defined.
5ifetime is till control remains within the bloc+ in which the register variable is
defined. 0ariable stored in a C34 register can always be accessed faster than
the one that is stored in memory. 6herefore, if a variable is used at many places
in a program, it is better to declare its storage class as register

Example:
13

register int #/9;
0ariables for loop counters can be declared as register. ote that register
+eyword may be ignored by some compilers.
7. Where is an auto variables stored?
8ain memory and C34 registers are the two memory locations where auto
variables are stored. *uto variables are defined under automatic storage class.
6hey are stored in main memory. 8emory is allocated to an automatic variable
when the bloc+ which contains it is called and it is de9allocated at the completion
of its bloc+ e,ecution.

Auto variables:
Storage : main memory.
Default value : garbage value.
Scope : local to the bloc+ in which the variable is defined.
Lifetime : till the control remains within the bloc+ in which the variable is defined.

:. What is scope ; storage allocation of e,tern and global variables?
<,tern variables! belong to the <,ternal storage class and are stored in the main
memory. e,tern is used when we have to refer a function or variable that is
implemented in other file in the same pro.ect. 6he scope of the e,tern variables is
=lobal.


Example:
/***************
5nde#* $,.c
****************/
#include <stdio.h>
e#tern int #;
int main() !
print$(%value o$ # 1d%, #);
return (;
)
5nde#* $-.c
****************/
int # / ;;

#ere, the program written in file f1.c has the main function and reference to
variable ,. 6he file f).c has the declaration of variable ,. 6he compiler should
14
+now the datatype of , and this is done by e,tern definition.

=lobal variables! are variables which are declared above the main> ? function.
6hese variables are accessible throughout the program. 6hey can be accessed
by all the functions in the program. 6heir default value is zero.

Example:
#include <stdio.h>
int # / (;
/* @ariale # is a gloal variale.
5t can e accessed throughout the program */
void increment(void) !
# / # 0 ,;
print$(%+n value o$ #* 1d%, #);
) int main()!
print$(%+n value o$ #* 1d%, #);
increment();
return (;
)

@. What is scope ; storage allocation of register, static and local variables?
Register variables:
belong to the register storage class and are stored in the C34 registers. 6he
scope of the register variables is local to the bloc+ in which the variables are
defined. 6he variables which are used for more number of times in a program are
declared as register variables for faster access.

Example: loop counter variables.
register int </C;

Static variables:
8emory is allocated at the beginning of the program e,ecution and it is
reallocated only after the program terminates. 6he scope of the static variables is
local to the bloc+ in which the variables are defined.

Example:
#include <stdio.h>
void decrement()!
static int a/9;
a"";
print$(%@alue o$ a*1d+n%, a);
15
) int main()!
decrement();
return (;
)

#ere $a$ is initialized only once. <very time this function is called, $a$ does not get
initialized. so output would be 7 1 ) etc.,

Local variables: are variables which are declared within any function or a bloc+.
6hey can be accessed only by function or bloc+ in which they are declared. 6heir
default value is a garbage value.
A. What are storage memory, default value, scope and life of *utomatic and
2egister storage class?
1. Automatic storage class:
Storage : main memory.
Default value : garbage value.
Scope : local to the bloc+ in which the variable is defined.
Lifetime : till control remains within the bloc+.
2. Register storage class:
Storage : C34 registers.
Default value : garbage value.
Scope : local to the bloc+ in which the variable is defined.
Lifetime : till control remains within the bloc+.
B. What are storage memory, default value, scope and life of Static and <,ternal
storage class?
1. Static storage class:
Storage : main memory.
Default value : zero
Scope : local to the bloc+ in which the variable is defined.
Lifetime : till the value of the variable persists between different function calls.
2. External storage class:
Storage : main memory
Default value : zero
Scope : global
Lifetime : as long as the program e,ecution doesn$t come to an end.
C. What is the difference between $brea+$ and $continue$ statements?
16
1(. What is the difference between $for$ and $while$ loops?
for loop:
When it is desired to do initialization, condition chec+ and increment-decrement
in a single statement of an iterative loop, it is recommended to use $for$ loop.

Syntax:
$or(initialiDation;condition;increment/decrement)
!/
17
/loc: o$ statements
increment or decrement
)
Erogram* Erogram to illustrate $or loop
#include<stdio.h>
int main() !
int i;
$or (i / ,; i </ 9; i00) !
//print the numer
print$(%+n 1d%, i);
)
return (;
)

Output:
1)17:

Explanation:
6he loop repeats for : times and prints value of $i$ each time. $i$ increases by 1 for
every cycle of loop.

wile loop:
When it is not necessary to do initialization, condition chec+ and
increment-decrement in a single statement of an iterative loop, while loop could
be used. /n while loop statement, only condition statement is present.

Syntax:
#include<stdio.h>
int main() !
int i / (, $lag / (;
int a[,(] / ! (, ,, F, C, GH, 9F, IG, -9, C;9, 9(( );
//8his loop is repeated until the condition is $alse.
while ($lag // () !
i$ (a[i] // 9F) !
//as element is $ound, $lag / ,,the loop terminates
$lag / ,;
)
else !
i00;
)
)
print$(%Jlement $ound at 1d th location%, i);
return (;
18
)

Output:

<lement found at :th location

Explanation:
#ere flag is initialized to zero. $while$ loop repeats until the value of flag is zero,
increments i by 1. $if$ condition chec+s whether number :7 is found. /f found,
value of flag is set to 1 and $while$ loop terminates.
Embedded C Tutorial (Chapter-3)
!apter ": #umbers$ !aracters an% Strings
What's in Chapter 3?
#ow are numbers represented on the computer
B9bit unsigned numbers
B9bit signed numbers
1@9bit unsigned numbers
1@9bit signed numbers
Dig and little endian
Doolean >true-false?
Decimal numbers
#e,adecimal numbers
Ectal numbers
Characters
Strings
<scape seFuences
6his chapter defines the various data types supported by the compiler. Since the
ob.ective of most computer systems is to process data, it is important to
understand how data is stored and interpreted by the software. We define a
literal as the direct specification of the number, character, or string. <.g.,
,(( 4a4 %Kello &orld%
are e,amples of a number literal, a character literal and a string literal
respectively. We will discuss the way data are stored on the computer as well as
the C synta, for creating the literals. 6he /magecraft and 8etrower+s compilers
recognize three types of literals >numeric, character, string?. umbers can be
19
written in three bases >decimal, octal, and hexadecimal?. *lthough the
programmer can choose to specify numbers in these three bases, once loaded
into the computer, the all numbers are stored and processed as unsigned or
signed binary. *lthough C does not support the binary literals, if you wanted to
specify a binary number, you should have no trouble using either the octal or
he,adecimal format.
Binary representation
umbers are stored on the computer in binary form. /n other words, information
is encoded as a seFuence of 1Gs and (Gs. En most computers, the memory is
organized into B9bit bytes. 6his means each B9bit byte stored in memory will have
a separate address. Precision is the number of distinct or different values. We
e,press precision in alternatives, decimal digits, bytes, or binary bits.
Alternatives are defined as the total number of possibilities. Hor e,ample, an B9bit
number scheme can represent ):@ different numbers. *n B9bit digital to analog
converter can generate ):@ different analog outputs. *n B9bit analog to digital
converter >*DC? can measure ):@ different analog inputs. We use the
e,pression 7I decimal digits to mean about )(,((( alternatives and the
e,pression 7J decimal digits to mean more than )(,((( alternatives but less
than 1((,((( alternatives. 6he I decimal digit means twice the number of
alternatives or one additional binary bit. Hor e,ample, a voltmeter with a range of
(.(( to C.CC0 has a three decimal digit precision. 5et the operation KK,LL be the
greatest integer of ,. <.g., KK).1LL is rounded up to 1. 6ables 1.1a and 1.1b
illustrate various representations of precision.
&inary bits &ytes Alternatives
B 1 ):@
1( 1()7
1) 7(C@
1@ ) @::1@
)( 1,(7B,:A@
)7 1 1@,AAA,)1@
1( 1,(A1,A71,B)7
1) 7 7,)C7,C@A,)C@
20
n KKn-BLL )
n

Table 3-1a. Relationships between various representations of precision.
'ecimal
%igits
Alternatives
1 1(((
1I )(((
1J 7(((
7 1((((
7I )((((
7J 7((((
: 1(((((
n 1(
n
Table 3-1b. Relationships between various representations of precision.
Observation A good rule of thumb to remember is !
1"#n
is about 1"
3#n
.
Hor large numbers we use abbreviations, as shown in the following table. Hor
e,ample, 1@M means 1@N1()7 which eFuals 1@1B7. Computer engineers use the
same symbols as other scientists, but with slightly different values.
abbreviation pronunciation !omputer Engineering (alue Scientific (alue
M %+ay% )
1(
1()7 1(
1

8 %meg% )
)(
1,(7B,:A@ 1(
@

= %gig% )
1(
1,(A1,A71,B)7 1(
C

6 %tera% )
7(
1,(CC,:11,@)A,AA@ 1(
1)

3 %peta% )
:(
1,1):,BCC,C(@,B71,@)7 1(
1:

< %e,a% )
@(
1,1:),C)1,:(7,@(@,B7@,CA@ 1(
1B

Table 3-!. $ommon abbreviations for large numbers.
8-it unsigne! numers
* byte contains B bits

21
where each bit bA,...,b( is binary and has the value 1 or (. We specify bA as the
most significant bit or 8SD, and b( as the least significant bit or 5SD. /f a byte is
used to represent an unsigned number, then the value of the number is
& 1)BObA ' @7Ob@ ' 1)Ob: ' 1@Ob7 ' BOb1 ' 7Ob) ' )Ob1 ' b(
6here are ):@ different unsigned B9bit numbers. 6he smallest unsigned B9bit
number is ( and the largest is )::. Hor e,ample, ((((1(1(
)
is B') or 1(. Ether
e,amples are shown in the following table.
binary he, Calculation decimal
(((((((( (,(( (
(1(((((1 (,71 @7'1 @:
(((1(11( (,1@ 1@'7') ))
1((((111 (,BA 1)B'7')'1 11:
11111111 (,HH 1)B'@7'1)'1@'B'7')'1 )::
Table 3-3. %xample conversions from unsigned &-bit binar' to hexadecimal and
to decimal.
6he basis of a number system is a subset from which linear combinations of the
basis elements can be used to construct the entire set. Hor the unsigned B9bit
number system, the basis is
P 1, ), 7, B, 1@, 1), @7, 1)BQ
Ene way for us to convert a decimal number into binary is to use the basis
elements. 6he overall approach is to start with the largest basis element and
wor+ towards the smallest. Ene by one we as+ ourselves whether or not we need
that basis element to create our number. /f we do, then we set the corresponding
bit in our binary result and subtract the basis element from our number. /f we do
not need it, then we clear the corresponding bit in our binary result. We will wor+
through the algorithm with the e,ample of converting 1(( to B bit binary. We with
the largest basis element >in this case 1)B? and as+ whether or not we need to
include it to ma+e 1((. Since our number is less than 1)B, we do not need it so
bit A is zero. We go the ne,t largest basis element, @7 and as+ do we need it. We
do need @7 to generate our 1((, so bit @ is one and subtract 1(( minus @7 to get
1@. e,t we go the ne,t basis element, 1) and as+ do we need it. *gain we do
need 1) to generate our 1@, so bit : is one and we subtract 1@ minus 1) to get 7.
Continuing along, we need basis element 7 but not 1@ B ) or 1, so bits 71)1( are
((1(( respectively. 3utting it together we get (11((1(() >which means
@7'1)'7?.
Observation (f the least significant binar' bit is )ero* then the number is
even.
Observation (f the right most n bits +least significant, are )ero* then the
number is divisible b' !
n
.
22
#umber &asis #ee% it bit Operation
1(( 1)B no bitA&( none
1(( @7 yes bit@&1 subtract 1((9@7
1@ 1) yes bit:&1 subtract 1@91)
7 1@ no bit7&( none
7 B no bit1&( none
7 7 yes bit)&1 subtract 797
( ) no bit1&( none
( 1 no bit(&( none
Table 3--. %xample conversion from decimal to unsigned &-bit binar' to
hexadecimal.
We define an unsigned B9bit number using the unsigned char format. When a
number is stored into an unsigned char it is converted to B9bit unsigned value.
Hor e,ample
unsigned char data; // ( to -99
unsigned char $unction(unsigned char input)!
data/input0,;
return data;)
)*bit signe% numbers
/f a byte is used to represent a signed !.s complement number, then the value of
the number is
& 91)BObA ' @7Ob@ ' 1)Ob: ' 1@Ob7 ' BOb1 ' 7Ob) ' )Ob1 ' b(
6here are also ):@ different signed B bit numbers. 6he smallest signed B9bit
number is 91)B and the largest is 1)A. Hor e,ample, 1(((((1(
)
is 91)B') or
91)@. Ether e,amples are shown in the following table.
binary he, Calculation decimal
(((((((( (,(( (
(1(((((1 (,71 @7'1 @:
(((1(11( (,1@ 1@'7') ))
1((((111 (,BA 91)B'7')'1 91)1
11111111 (,HH 91)B'@7'1)'1@'B'7')'1 91
Table 3-/. %xample conversions from signed &-bit binar' to hexadecimal and to
decimal.
Hor the signed B9bit number system the basis is
23
P 1, ), 7, B, 1@, 1), @7, 91)BQ
Observation The most significant bit in a !.s complement signed number
will specif' the sign.
otice that the same binary pattern of 11111111
)
could represent either ):: or 91.
/t is very important for the software developer to +eep trac+ of the number format.
6he computer can not determine whether the B9bit number is signed or unsigned.
Rou, as the programmer, will determine whether the number is signed or
unsigned by the specific assembly instructions you select to operate on the
number. Some operations li+e addition, subtraction, and shift left >multiply by )?
use the same hardware >instructions? for both unsigned and signed operations.
En the other hand, multiply, divide, and shift right >divide by )? reFuire separate
hardware >instruction? for unsigned and signed operations. Hor e,ample, the
@B(:-@B(B-@B11 multiply instruction, mul, operates only on unsigned values. So
if you use the mul instruction, you are implementing unsigned arithmetic. 6he
Hreescale @B1) has both unsigned, mul, and signed, smul, multiply instructions.
So if you use the smul instruction, you are implementing signed arithmetic. 6he
compiler will automatically choose the proper implementation.
It is always good programming practice to have clear understanding o the data type or
each num!er" varia!le" parameter" etc# $or some operations there is a dierence !etween
the signed and unsigned num!ers while or others it does not matter#
signe% %ifferent from unsigne% signe% same as unsigne%
- S division ' addition
N multiplication 9 subtraction
T greater than && is eFual to
U less than V logical or
T& greater than or eFual to ; logical and
U& less than or eFual to W logical e,clusive or
TT right shift UU left shift
Table 3-0. Operations either depend or don1t depend on whether the number is
signed2unsigned.
6he point is that care must be ta+en when dealing with a mi,ture of numbers of
different sizes and types.
Similar to the unsigned algorithm, we can use the basis to convert a decimal
number into signed binary. We will wor+ through the algorithm with the e,ample
of converting 91(( to B9bit binary. We with the largest basis element >in this case
91)B? and decide do we need to include it to ma+e 91((. Res >without 91)B, we
would be unable to add the other basis elements together to get any negative
result?, so we set bit A and subtract the basis element from our value. Eur new
value is 91(( minus 91)B, which is )B. We go the ne,t largest basis element, @7
24
and as+ do we need it. We do not need @7 to generate our )B, so bit@ is zero.
e,t we go the ne,t basis element, 1) and as+ do we need it. We do not need 1)
to generate our )B, so bit: is zero. ow we need the basis element 1@, so we set
bit7, and subtract 1@ from our number )B >)B91@&1)?. Continuing along, we need
basis elements B and 7 but not ) 1, so bits 1)1( are 11((. 3utting it together we
get 1((111(() >which means 91)B'1@'B'7?.
#umber &asis #ee% it bit Operation
91(( 91)B yes bitA&1 subtract 91(( 9 91)B
)B @7 no bit@&( none
)B 1) no bit:&( none
)B 1@ yes bit7&1 subtract )B91@
1) B yes bit1&1 subtract 1)9B
7 7 yes bit)&1 subtract 797
( ) no bit1&( none
( 1 no bit(&( none
Table 3-3. %xample conversion from decimal to signed &-bit binar' to
hexadecimal.
"servation: To ta4e the negative of a !.s complement signed number
we first complement +flip, all the bits* then add 1.
* second way to convert negative numbers into binary is to first convert them into
unsigned binary, then do a )Gs complement negate. Hor e,ample, we earlier
found that '1(( is (11((1((). 6he )Gs complement negate is a two step process.
Hirst we do a logic complement >flip all bits? to get 1((11(11). 6hen add one to
the result to get 1((111(().
* third way to convert negative numbers into binary is to first subtract the number
from ):@, then convert the unsigned result to binary using the unsigned method.
Hor e,ample, to find 91((, we subtract ):@ minus 1(( to get 1:@. 6hen we
convert 1:@ to binary resulting in 1((111((). 6his method wor+s because in B bit
binary math adding ):@ to number does not change the value. <.g., ):@91(( is
the same value as 91((.
$ommon %rror An error will occur if 'ou use signed operations on
unsigned numbers* or use unsigned operations on signed numbers.

5aintenance Tip To improve the clarit' of our software* alwa's specif'
the format of 'our data +signed versus unsigned, when defining or
accessing the data.
25
We define a signed B9bit number using the char format. When a number is stored
into a char it is converted to B9bit signed value. Hor e,ample
char data; // ",-G to ,-I
char $unction(char input)!
data/input0,;
return data;)
#$ it unsigne! numers
* word or double b'te contains 1@ bits
where each bit b1:,...,b( is binary and has the value 1 or (. /f a word is used to
represent an unsigned number, then the value of the number is
& 1)A@BOb1: ' 1@1B7Ob17 ' B1C)Ob11 ' 7(C@Ob1)
' )(7BOb11 ' 1()7Ob1( ' :1)ObC ' ):@ObB
' 1)BObA ' @7Ob@ ' 1)Ob: ' 1@Ob7 ' BOb1 ' 7Ob) ' )Ob1 ' b(
6here are @:,:1@ different unsigned 1@9bit numbers. 6he smallest unsigned 1@9
bit number is ( and the largest is @::1:. Hor e,ample, ((1(,(((1,1(((,(1((
)
or
(,)1B7 is B1C)'):@'1)B'7 or B:B(. Ether e,amples are shown in the following
table.
binary ex !alculation %ecimal
((((,((((,((((,(((( (,(((( (
((((,(1((,((((,(((1 (,(7(1 1()7'1 1():
((((,11((,1(1(,(((( (,(C*( )(7B'1()7'1)B'1) 1)1)
1(((,111(,((((,((1( (,B<() 1)A@B')(7B'1()7':1)') 1@1:7
1111,1111,1111,1111 (,HHHH 1)A@B'1@1B7'B1C)'7(C@')(7B'1()7
':1)'):@'1)B'@7'1)'1@'B'7')'1
@::1:
Table 3-&. %xample conversions from unsigned 10-bit binar' to hexadecimal and
to decimal.
Hor the unsigned 1@9bit number system the basis is
26
P 1, ), 7, B, 1@, 1), @7, 1)B, ):@, :1), 1()7, )(7B, 7(C@, B1C), 1@1B7,
1)A@BQ
/f a word is used to represent a signed )Gs complement number, then the value of
the number is
& 91)A@BOb1: ' 1@1B7Ob17 ' B1C)Ob11 ' 7(C@Ob1)
' )(7BOb11 ' 1()7Ob1( ' :1)ObC ' ):@ObB
' 1)BObA ' @7Ob@ ' 1)Ob: ' 1@Ob7 ' BOb1 ' 7Ob) ' )Ob1 ' b(
We define an unsigned 1@9bit number using the unsigned short format. When a
number is stored into an unsigned short it is converted to 1@9bit unsigned value.
Hor e,ample
unsigned short data; // ( to C99;9
unsigned short $unction(unsigned short input)!
data/input0,;
return data;)
1+*bit signe% numbers
6here are also @:,:1@ different signed 1@9bit numbers. 6he smallest signed 1@9
bit number is 91)A@B and the largest is 1)A@A. Hor e,ample,
11(1,((((,((((,(1((
)
or (,D((7 is 91)A@B'1@1B7'7(C@'7 or 91))B7. Ether
e,amples are shown in the following table.
binary ex !alculation %ecimal
((((,((((,((((,(((( (,(((( (
((((,(1((,((((,(((1 (,(7(1 1()7'1 1():
((((,11((,1(1(,(((( (,(C*( )(7B'1()7'1)B'1) 1)1)
1(((,(1((,((((,((1( (,B7() 91)A@B'1()7') 911A7)
1111,1111,1111,1111 (,HHHH 9
1)A@B'1@1B7'B1C)'7(C@')(7B'1()7
':1)'):@'1)B'@7'1)'1@'B'7')'1
91
Table 3-6. %xample conversions from signed 10-bit binar' to hexadecimal and to
decimal.
Hor the signed 1@9bit number system the basis is
P 1, ), 7, B, 1@, 1), @7, 1)B, ):@, :1), 1()7, )(7B, 7(C@, B1C), 1@1B7,
91)A@BQ
%aintenance &ip To improve the 7ualit' of our software* we should
alwa's specif' the precision of our data when defining or accessing the
data.
27
We define a signed 1@9bit number using the short format. When a number is
stored into a short it is converted to 1@9bit signed value. Hor e,ample
short data; // "-;ICG to ;-ICI
short $unction(short input)!
data/input0,;
return data;)
&ig an% Little En%ian
When we store 1@9bit data into memory it reFuires two bytes. Since the memory
systems on most computers are byte addressable >a uniFue address for each
byte?, there are two possible ways to store in memory the two bytes that
constitute the 1@9bit data. Hreescale microcomputers implement the big endian
approach that stores the most significant part first. /ntel microcomputers
implement the little endian approach that stores the least significant part first. 6he
3ower3C is biendian, because it can be configured to efficiently handle both big
and little endian. Hor e,ample, assume we wish to store the 1@ bit number 1(((
>(,(1<B? at locations (,:(,(,:1, then
We also can use either the big or little endian approach when storing 1)9bit
numbers into memory that is byte >B9bit? addressable. /f we wish to store the 1)9
bit number (,1)17:@AB at locations (,:(9(,:1 then
/n the above two e,amples we normally would not pic+ out individual bytes >e.g.,
the (,1)?, but rather capture the entire multiple byte data as one nondivisable
piece of information. En the other hand, if each byte in a multiple byte data
structure is individually addressable, then both the big and little endian schemes
store the data in first to last seFuence. Hor e,ample, if we wish to store the 7
*SC// characters X@B11G which is (,1@1B1111 at locations (,:(9(,:1, then the
*SC// X@G&(,1@ comes first in both big and little endian schemes.
28
6he term %Dig <ndian% comes from Yonathan SwiftGs satire =ulliverGs 6ravels. /n
SwiftGs boo+, a Dig <ndian refers to a person who crac+s their egg on the big
end. 6he 5illiputians considered the big endians as inferiors. 6he big endians
fought a long and senseless war with the 5illiputians who insisted it was only
proper to brea+ an egg on the little end.
Common 'rror: An error will occur when data is stored in 8ig %ndian b'
one computer and read in 9ittle %ndian format on another.
&oolean information
* boolean number is has two states. 6he two values could represent the logical
true or false. 6he positive logic representation defines true as a 1 or high, and
false as a ( or low. /f you were controlling a motor, light, heater or air conditioner
the boolean could mean on or off. /n communication systems, we represent the
information as a seFuence of booleans! mar+ or space. Hor blac+ or white
graphic displays we use booleans to specify the state of each pi,el. 6he most
efficient storage of booleans on a computer is to map each boolean into one
memory bit. /n this way, we could pac+ B booleans into each byte. /f we have .ust
one boolean to store in memory, out of convenience we allocate an entire byte or
word for it. 8ost C compilers including /CC11-/CC1)-8etrower+s define!
29
Halse be all zeros, and
6rue be any nonzero value.
8any programmers add the following macros
#de$ine 8?LJ ,
#de$ine .6MNJ (
'ecimal #umbers
Decimal numbers are written as a seFuence of decimal digits >( through C?. 6he
number may be preceded by a plus or minus sign or followed by a Lor ,. 5ower
case l or u could also be used. 6he minus sign gives the number a negative
value, otherwise it is positive. 6he plus sign is optional for positive values.
4nsigned 1@9bit numbers between 1)A@B and @::1: should be followed by ,.
Rou can place a Lat the end of the number to signify it to be a 1)9bit signed
number. 6he range of a decimal number depends on the data type as shown in
the following table.
type range precision examples
unsigned char ( to ):: B bits ( 1( 1)1
char 91)A to 1)A B bits 91)1 ( 1( '1(
unsigned int ( to @::1:4 1@ bits ( )((( )(((4
:((((4
int 91)A@A to 1)A@A 1@ bits 91((( ( 1((( ')((((
unsigned short ( to @::1:4 1@ bits ( )((( )(((4
:((((4
short 91)A@A to 1)A@A 1@ bits 91((( ( 1((( ')((((
long 9)17A7B1@7A5 to
)17A7B1@7A5
1) bits 91)17:@A5 (5
1)17:@A5
Table 3-1". The range of decimal numbers.

Decause the @B11 and @B1) microcomputers are most efficient for 1@ bit data
>and not 1) bit data?, the unsigne% int and int data types are 1@ bits. En the
other hand, on a ,B@9based machine, the unsigne% int and int data types are 1)
bits. /n order to ma+e your software more compatible with other machines, it is
preferable to use the sort type when needing 1@ bit data and the long type for
1) bit data.
type +)11-+)12 x)+
unsigned char B bits B bits
char B bits B bits
unsigned int 1@ bits 1) bits
int 1@ bits 1) bits
unsigned short 1@ bits 1@ bits
30
short 1@ bits 1@ bits
long 1) bits 1) bits
Table 3-11. :ifferences between a 0&1120&1! and an x&0

Since the @B11 and @B1) microcomputers do not have direct support of 1)9bit
numbers, the use of long data types should be minimized. En the other hand, a
careful observation of the code generated yields the fact that these compilers are
more efficient with 1@ bit numbers than with B bit numbers.
Decimal numbers are reduced to their two$s complement or unsigned binary
eFuivalent and stored as B-1@-1)9bit binary values.
6he manner in which decimal literals are treated depends on the conte,t. Hor
e,ample
short 5;
unsigned short O;
char P;
unsigned char M;
long B;
void main(void)!
5/HI; /* ,C its (#((C, */
O/HI; /* ,C its (#((C, */
P/HI; /* G its (#C, */
M/HI; /* G its (#C, */
B/HI; /* ;- its (#((((((C, */)
6he @B1) code generated by the /CC1) compiler is as follows
.area te#t
7main**
psh#
t$r s,#
movw #HI,75 ;,C its
movw #HI,7O ;,C its
mov #HI,7P ;G its
mov #HI,7M ;G its
ld< #M-
3sr 77l<-reg ;;- its
ld< #7B
3sr 77lreg-<
t$r #,s
pul#
rts
.area ss
7B** .l: F
7M** .l: ,
7P** .l: ,
7O** .l: -
75** .l: -
.area te#t
M-* .word (,HI
31
6he @B1) code generated by the 8etrower+s compiler is much more efficient
when dealing with 1) bit long integers
MA6= #HI
'M?6
N8A 5
N8A O
N86= P
N86= M
N8A B*-
'M?=
N8A B
?8N
Octal #umbers
/f a seFuence of digits begins with a leading .>zero? it is interpreted as an octal
value. 6here are only eight octal digits, ( through A. *s with decimal numbers,
octal numbers are converted to their binary eFuivalent in B9bit or 1@9bit words.
6he range of an octal number depends on the data type as shown in the
following table.
type range precision examples
unsigned char ( to (1AA B bits ( (1( (1)1
char 9()(( to (1AA B bits 9(1)1 ( (1( '(1(
unsigned int ( to (1AAAAA 1@ bits ( ()((( (1:((((4
int 9(AAAAA to (AAAAA 1@ bits 9(1((( ( (1(((
'()((((
unsigned short ( to (1AAAAA 1@ bits ( ()((( (1:((((4
short 9(AAAAA to (AAAAA 1@ bits 9(1((( ( (1(((
'()((((
long 9(1AAAAAAAAAA5 to
(1AAAAAAAAAA5
1) bits 9(1)17:@A5 (5
(1)17:@A5
Table 3-1!. The range of octal numbers.

otice that the octal values ( through (A are eFuivalent to the decimal values (
through A. Ene of the advantages of this format is that it is very easy to convert
bac+ and forth between octal and binary. <ach octal digit maps directly to-from 1
binary digits.
/exa%ecimal #umbers
6he he,adecimal number system uses base 1@ as opposed to our regular
decimal number system that uses base 1(. 5i+e the octal format, the
he,adecimal format is also a convenient mechanism for us humans to represent
binary information, because it is e,tremely simple for us to convert bac+ and forth
32
between binary and he,adecimal. * nibble is defined as 7 binary bits. <ach value
of the 79bit nibble is mapped into a uniFue he, digit.
/ex 'igit 'ecimal (alue &inary (alue
( ( ((((
1 1 (((1
) ) ((1(
1 1 ((11
7 7 (1((
: : (1(1
@ @ (11(
A A (111
B B 1(((
C C 1((1
* or a 1( 1(1(
D or b 11 1(11
C or c 1) 11((
D or d 11 11(1
< or e 17 111(
H or f 1: 1111
Table 3-13. :efinition of hexadecimal representation.

Computer programming environments use a wide variety of symbolic notations to
specify the numbers in various bases. 6he following table illustrates various
formats for numbers
environment binary format exa%ecimal
format
%ecimal format
Hreescale assembly language S(1111(1( ZA* 1))
/ntel and 6/ assembly language (1111(1(D A*# 1))
C language 9 (,A* 1))
Table 3-1-. ;arious hexadecimal formats.

6o convert from binary to he,adecimal we can!
1? divide the binary number into right .ustified nibbles"
)? convert each nibble into its corresponding he,adecimal digit.
33
6o convert from he,adecimal to binary we can!
1? convert each he,adecimal digit into its corresponding 7 bit binary
nibble"
)? combine the nibbles into a single binary number.
/f a seFuence of digits begins with .x or .0 then it is ta+en as a he,adecimal
value. /n this case the word digits refers to he,adecimal digits >( through H?. *s
with decimal numbers, he,adecimal numbers are converted to their binary
eFuivalent in B9bit bytes or1@9bit words. 6he range of a he,adecimal number
depends on the data type as shown in the following table.
type range
precisio
n
examples
unsigned
char
(,(( to (,HH B bits (,(1 (,1a (,D1
char 9(,AH to (,AH B bits 9(,(1 (,1a 9(,AD
unsigned int (,(((( to (,HHHH 1@ bits (,)) ([abcd (,H(*@
int 9(,AHHH to (,AHHH 1@ bits 9(,)) ([( '(,A(*@
unsigned
short
(,(((( to (,HHHH 1@ bits (,)) ([abcd (,H(*@
short 9(,AHHH to (,AHHH 1@ bits 9(,1)17 (,( '(,Aabc
long
9(,AHHHHHHH to
(,AHHHHHHH
1) bits
9(,1)17:@A
(,*DCD<H
Table 3-1/. The range of hexadecimal numbers.
!aracter Literals
Character literals consist of one or two characters surrounded by apostrophes.
6he manner in which character literals are treated depends on the conte,t. Hor
e,ample
short 5;
unsigned short O;
char P;
unsigned char M;
34
long B;
void main(void)!
5/4a4; /* ,C its (#((C, */
O/4a4; /* ,C its (#((C, */
P/4a4; /* G its (#C, */
M/4a4; /* G its (#C, */
B/4a4; /* ;- its (#((((((C, */)
6he @B1) code generated by the /CC1) compiler is as follows
.area te#t
7main**
psh#
t$r s,#
movw #HI,75 ;,C its
movw #HI,7O ;,C its
mov #HI,7P ;G its
mov #HI,7M ;G its
ld< #M-
3sr 77l<-reg ;;- its
ld< #7B
3sr 77lreg-<
t$r #,s
pul#
rts
.area ss
7B** .l: F
7M** .l: ,
7P** .l: ,
7O** .l: -
75** .l: -
.area te#t
M-* .word (,HI
6he @B1) code generated by the 8etrower+s compiler is as follows
MA6= #HI
'M?6
N8A 5
N8A O
N86= P
N86= M
N8A B*-
'M?=
N8A B
?8N
*ll standard *SC// characters are positive because the high9order bit is zero. /n
most cases it doesn$t matter if we declare character variables as signed or
unsigned. En the other hand, we have seen earlier that the compiler treats
signed and unsigned numbers differently. 4nless a character variable is
specifically declared to be unsigned, its high9order bit will be ta+en as a sign bit.
35
6herefore, we should not e,pect a character variable, which is not declared
unsigned, to compare eFual to the same character literal if the high9order bit is
set. Hor more on this see Chapter 7 on 0ariables.
String Literals
Strictly spea+ing, C does not recognize character strings, but it does recognize
arrays of characters and provides a way to write character arrays, which we call
strings. Surrounding a character seFuence with Fuotation mar+s, e.g., 12on1,
sets up an array of characters and generates the address of the array. /n other
words, at the point in a program where it appears, a string literal produces the
address of the specified array of character literals. 6he array itself is located
elsewhere. /CC11 and /CC1) will place the strings into the te,t area. /.e., the
string literals are considered constant and will be defined in the 2E8 of an
embedded system. 6his is very important to remember. otice that this differs
from a character literal which generates the value of the literal directly. Yust to be
sure that this distinct feature of the C language is not overloo+ed, consider the
following e,ample!
char *pt;
void main(void)!
pt/%Oon%; /* pointer to the string */
print$(pt); /* passes the pointer not the data itsel$ */
)
6he @B1) code generated by the /CC1) compiler is as follows
.area te#t
7main**
movw #M-,7pt
ldd 7pt
3sr 7print$
rts
.area ss
7pt** .l: -
.area te#t
M-* .<te 4O,4o,4n,(
6he @B1) code generated by the 8etrower+s compiler is virtually the same as
/CC1). Doth compilers place the string in memory and use a pointer to it when
calling printf. /CC1) will pass the parameter in 2egD,while 8etrower+s pushes
the parameter on the stac+.
BQ@& #%Oon%,pt
MAA pt
ENKA
36
ON? print$
ELMA
?8N
otice that the pointer, pt, is allocated in 2*8 >.area bss? and the string is stored
in 2E8 >.area te,t?. 6he assignment statement pt/%Oon%; copies the address not
the data. Similarly, the function print$() must receive the address of a string as
its first >in this case, only? argument. Hirst, the address of the string is assigned to
the character pointer pt >/CC11-/CC1) use the 1@ bit 2egister D for the first
parameter?. 4nli+e other languages, the string itself is not assigned to pt, only its
address is. *fter all, pt is a 1@9bit ob.ect and, therefore, cannot hold the string
itself. 6he same program could be written better as
void main(void)!
print$(%Oon%); /* passes the pointer not the data itsel$ */
)
otice again that the program passes a pointer to the string into print$(), and
not the string itself. 6he @B1) code generated by the /CC1) compiler is as follows
.area te#t
7main**
ldd #M-
3sr 7print$
rts
.area te#t
M-* .<te 4O,4o,4n,(
<,cept for the parameter passing, the @B1) code generated by the 8etrower+s
compiler is virtually the same as /CC1).
MAA #%Oon%
ENKA
ON? print$
ELMA
?8N
/n this case, it is tempting to thin+ that the string itself is being passed to
print$()" but, as before, only its address is.
Since strings may contain as few as one or two characters, they provide an
alternative way of writing character literals in situations where the address, rather
than the character itself, is needed.
/t is a convention in C to identify the end of a character string with a null >zero?
character. 6herefore, C compilers automatically suffi, character strings with such
a terminator. 6hus, the string 12on1 sets up an array of four characters >323, 3o3,
3n3, and zero? and generates the address of the first character, for use by the
program.
2emember that $*$ is different from %*%, consider the following e,ample!
37
char letter,*pt;
void main(void)!
pt/%6%; /* pointer to the string */
letter/464; /* the data itsel$ (464 6N'55 C9/RF,) */
)
6he @B1) code generated by the /CC1) compiler is as follows
.area te#t
7main**
movw #M-,7pt
mov #C9,7letter
rts
.area ss
7letter** .l: ,
7pt** .l: -
.area te#t
M-* .<te 46,(
6he @B1) code generated by the 8etrower+s compiler is as follows
BQ@& #%6%,pt
MA6= #C9
N86= letter
?8N
Escape Se4uences
Sometimes it is desirable to code nongraphic characters in a character or string
literal. 6his can be done by using an escape se7uence99a seFuence of two or
more characters in which the first >escape? character changes the meaning of the
following character>s?. When this is done the entire seFuence generates only one
character. C uses the bac+slash >5? for the escape character. 6he following
escape seFuences are recognized by the /CC11-/CC1)-8etrower+s compilers!
seFuence name value
\n newline, linefeed Z(* & 1(
\t tab Z(C & C
\b bac+space Z(B & B
\f form feed Z(C & 1)
\a bell Z(A & A
\r return Z(D & 11
\v vertical tab Z(D & 11
\( null Z(( & (
\% *SC// Fuote Z)) & 17
\\ *SC// bac+ slash Z:C & C)
38
\$ *SC// single Fuote Z)A & 1C

Table 3-10. The escape se7uences supported b' ($$11 ($$1! and 5etrower4s.
Ether nonprinting characters can also be defined using the 5ooo octal format. 6he
digits ooo can define any B9bit octal number. 6he following three lines are
eFuivalent!
print$(%+tOon+n%);
print$(%+,,Oon+,-%);
print$(%+(,,Oon+(,-%);
6he term newline refers to a single character which, when written to an output
device, starts a new line. Some hardware devices use the *SC// carriage return
>11? as the newline character while others use the *SC// line feed >1(?. /t really
doesn$t matter which is the case as long as we write +n in our programs. *void
using the *SC// value directly since that could produce compatibility problems
between different compilers.
6here is one other type of escape seFuence! anything undefined. /f the
bac+slash is followed by any character other than the ones described above,
then the bac+slash is ignored and the following character is ta+en literally. So the
way to code the bac+slash is by writing a pair of bac+slashes and the way to
code an apostrophe or a Fuote is by writing 53 or 51 respectively.
39

Anda mungkin juga menyukai