Hardware Reference
In-Depth Information
5.2 Introduction to C
This chapter is not intended to provide a complete coverage of C language. Instead, it pro-
vides a summary of those C language constructs that will be used in this topic. You will be able
to deal with the basic HCS12 interface programming if you fully understand the contents of this
chapter. In addition to providing a tutorial to C language, this chapter will also provide tutorials
on using CodeWarrior IDE and ImageCraft C compiler to enter, compile, and debug C programs.
C language is gradually replacing assembly language in many embedded applications be-
cause it has several advantages over assembly language. The most important one is that it al-
lows the user to work on program logic at a level higher than assembly language, and thus
programming productivity is greatly improved.
A C program, whatever its size, consists of functions and variables. A function contains
statements that specify the operations to be performed. The types of statements in a function
could be a declaration , assignment , function call , control , or null . A variable stores a value to
be used during the computation. The main( ) function is required in every C program and is
the one to which control is passed when the program is executed. A simple C program is as
follows:
(1)
#include <stdio.h>
include HCS12 header file
(2)
/* this is where program execution begins */
(3)
void main (void)
defines a function named main that receives
no argument values and returns no value
(4)
{
statements of main are enclosed in braces
(5)
int a, b, c;
defines three variables of type int
(6)
a 5 3;
assigns 3 to variable a
(7)
b 5 5;
assigns 5 to variable b
(8)
c 5 a 1 b;
adds a and b together and assigns it to c
(9)
printf(“a 1 b 5 %d \n”, c);
calls library function printf to print the result
(10)
return 0;
returns 0 to the caller of main
(11)
}
the end of main function
The first line of the program
#include <stdio.h>
causes the fi le stdio.h to be included in the program. This line appears at the beginning of many
C programs. The header fi le stdio.h contains the prototype declarations of all I/O routines that
can be called by the user program and the constant declarations that can be used by the user
program. C language requires that a function prototype be declared before that function can be
called if a function is not defi ned when it is called. The inclusion of the stdio.h fi le allows the
function printf() be invoked in the program.
The second line is a comment. A comment explains what will be performed and will be
ignored by the compiler. A comment in C language starts with / * and ends with * / . Everything
in between is ignored. Comments provide documentation to the program and enhance readabil-
ity. Comments affect only the size of the text file and do not increase the size of the executable
code. Many commercial C compilers also allow the use of two slashes ( // ) for commenting out
a single line.
The third line main() is where program execution begins. The opening brace on the fourth
line marks the start of main() function's code. Every C program must have one and only one main()
function. Program execution is also ended with the main function. The fifth line declares three
integer variables a , b , and c . In C, all variables must be declared before they can be used.
 
Search WWH ::




Custom Search