


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



























-^
C has gradually replaced assembly language in many embedded applications.
-^
A summary of C language constructs that will be used in HCS12 programming.
-^
Books on C language^ –
Kernighan & Ritchie, “The C Programming Language”, Prentice Hall, 1988.
-^
Deitel & Deitel, “C: How to Program”, Prentice Hall, 1998.
-^
Kelly & Pohl, “A Book on C: Programming in C”, Addison-Wesley, 1998.
-^
A C program consists of functions and variables.
A function contains statements that specify the operations to be performed
-^
A function contains statements that specify the operations to be performed.
-^
Types of statements:
-^
Declaration
-^
Assignment
-^
Function call
-^
ControlControl
-^
Null
-^
A variable stores a value to be used during the computation.
-^
The
main ()
function is required in every C program.
-^
C has five basic data types: void,
char, int, float,
and
double.
yp
The v
oid
type represents nothing and is mainly used with function.
A variable of type
char
can hold a single byte of data.
A variable of type
int
is an integer that is the natural size for a particular
hi machine.
-^
The type
float
refers to a 32-bit single-precision floating-point number.
The type
double
refers to a 64-bit double-precision floating-point
numbernumber.
-^
Qualifiers
short
and
long
can be applied to integers. For GNU C
compiler,
short
is 16 bits and
long
is 32 bits.
Qualifiers
signed
and
unsigned
may be applied to data types
char
and
i tinteger.
-^
A declaration specifies a
type,
and contains a list of one or more
variables of that typevariables of that type.
-^
int i j k;
-^
int i, j, k;
-^
char cx, cy;
-^
int m = 0;
-^
char echo = ‘y’; /* the ASCII code of letter
y
is assigned to variable
h^
echo. */
-^
Types of constants: integers, characters, floating-point numbers, andstrings.
g
A character constant is written as one character within single quotes,such as ‘x’.
-^
A character constant is represented by the ASCII code of the character.
-^
A string constant is a sequence of zero or more characters surroundedA string constant is a sequence of zero or more characters surroundedby double quotes, as “MC9S12DP256 is made by Motorola” or “”, whichrepresented an empty string. Each individual character in the string isrepresented by its ASCII code.
-^
An integer constant like 1234 is an
int.
A long constant is written with a
tege
co sta t
e^
s a
t^
o g co sta t s
tte
t^
a
terminal
l^
or L, as in 44332211L.
AND
|^
OR
^^
XOR
XOR
~^
NOT
right shift
<<
left shift
-^
&^
is often used to clear one or more bits of an integral variable to 0. PTH = PTH & 0xBD
/* clears bit 6 and bit 1 of PTH to 0 // PTH is of type char */
| is often used to set one or more bits to 1
-^
| is often used to set one or more bits to 1.PTB = PTB | 0x40;
/* sets bit 6 to 1 (PTB is of type char) */
-^
^^
can be used to toggle a bit. abc = abc ^ 0xF0;
/* toggles upper four bits (abc is of type
char) */char) */
can be used to shift the involved operand to the right for the specified
p^
g^
p
number of places.
xyz
= xyz >> 3;
-- shift right 3 places
can be used to shift the involved operand to the left for the specified
number of places.
xyz
= xyz << 4;
-- shift left 4 places
The assignment operator
is often combined with the operator. For example,
&= 0xBD;
&= 0xBD;
|= 0x40;
xyz
xyz
if (!(ATD0STAT0 & 0x80))
statement
;^1
/* if bit 7 is 0, then execute statement
*/ 1
if (i > 0 && i < 10)
(^
)
statement
;^2
/* if 0 < i < 10 then execute statement
*/ 2
if (a1 == a2)
statement
;^3
/* if a1 == a2 then execute statement
*/ 3
-^
if (
expression
statement
1
else
-- The
else
part is optional.
statementstatement
2
Example
if (a != 0)
r = b; else
r = c; A more concise way for this statement is r = (a != 0)? b : c;
switch (expression) {
switch (i) {
case const_expr
case 1: printf(“*”);
statement
break;
break;
case 2: printf(“**”);
break;
case 2: printf(
case const_expr
break;
statement
case 3: printf(“***”);
break;
break;
i tf(“****”)
case 4: printf(“****”);
default:
break;
statement
;n
case 5: printf(“*****”);
break;
for (
expr
;^ expr
;^ expr
statement; where
expr
and
expr
are assignments or function calls and
expr
is a
where,
expr
and
expr
are assignments or function calls and
expr
is a
relational
expression. Example
sum = 0;for (i = 1; i < 10; i++)for (i
1; i < 10; i++) sum += i * i; for (i = 1; i < 20; i++)
if (i % 2)
i tf(“%d “ i)
if (i % 2) printf(“%d “, i);
Not part of the C language itself.
-^
Four I/O functions will be discussed. 1.
int
getchar
char
xch;
-- returns a character when called
xch = getchar ();
i t
t h
(i t)
t h
int
p
utchar
(i
nt).
putchar(‘a’);
outputs a character on a standardoutput device
int
p
uts
(const char *s).
puts (“Welcome to USA! \n”);
p^
p^
-- outputs the string pointed by
s
on
a standard output device
int
printf
( formatting string, arg1, arg2, …).
converts formats and prints its arguments --
converts
, formats, and prints its arguments
on the standard output device
-^
-^
The character group starts with
The simplest form of a character group consists of the percent signfollowed by a
conversion character
indicating the type of the
corresponding data itemcorresponding data item.
-^
Multiple character groups can be contiguous, or they can be separatedby other characters, including whitespace characters. These “
other
characters
” are simply sent to the output device for display.
printf (“this is a challenging course! \n”);printf(%d %d %d”, x1, x2, x3);
/* outputs
x1, x2,
and
x
using minimal
number of digits with one space separating
-^
each value */
printf(“Today’s temperature is %4.1d\n”, temp);
-^
-^
-^
return_type function_name (declarations of arguments){
-^
{
declarations and statements } ExampleExamplechar lower2upper (char cx){
if^
‘ ’ &&
‘ ’)
t^
(^
(‘ ’
‘A’))
if cx > ‘a’ && cx <= ‘z’) return (cx - (‘a’ - ‘A’));else return cx; }
Example 5.1 Write a function to test if an integer is a prime number.Solution: A number is a prime if it is indivisible by any integer between 2 andSolution: A number is a prime if it is indivisible by any integer between 2 andits half./* this function returns a 1 if
a
is prime. Otherwise, it returns a 0. */
char test_prime (int a){{
int i;if (a == 1) return 0;for (i = 2; i < a/2; i++)
if ((a % i) == 0) return 0;if ((a % i) == 0) return 0;
return 1; } • A function must be defined before it can be called• A function must be defined before it can be called.• Function prototype declaration allows us to call a function before it is defined.• Syntax of a function prototype declaration:
return_type function_name (declarations of arguments);