79
Chapter 8: C Programming
STRUCTURE
Below are some examples that will help demonstrate the C language structure. Refer to
following pages for explanation of the keywords, functions, and commands used within the
sample programs.
1. All programs must begin with the main() function. It must be followed by an open brace {
and closed with an end brace }.
This program is valid, but will do nothing.
main()
{
}
2. A statement is a line of programming code. All statements are placed inside the braces {}
and end with a semicolon (;). For example:
main()
{
puts(“Hello there\n”);
puts(“I am ISACC.”);
}
This program will print the words “Hello there” and “I am ISACC.” The \n following “Hello
there” means start a new line. When this program is run, the screen would look like this:
Hello there
I am ISACC.
3. In the C language, numbers are stored in what are called variables. A variable must be
defined before the main() function.
int x;
main()
{
x=12;
}
In this program we defined a variable called “x” and set its value equal to 12. Here, x is de-
fined as an integer. ISACC’s C language can define variables as integers or characters. An
integer can hold a value from -32768 to 32767, but a character can only hold a value from -
128 to 127. Both characters and integers must be whole numbers. NOTE: If a value exceeds
these limits, the variable will still contain a number, but it will not be the correct number. For
example, var x contains the value 32767. Adding one to x will cause x to equal -32768.
The following program will set y to 47 and x equal to 57:
char y;
int x;
main()
{
y=47;
x=y+10;
}