Page 21 - Programming Microcontrollers in C
P. 21
6 Chapter 1 Introduction to C
The result obtained when running this program is as follows.
a * b * c = 100
a * b + c = 52
a + b * c = 20
Here is another example that demonstrates a primitive looping
construct:
#include <stdio.h>
int main(void)
{
int i;
i=1;
printf(“\ti\ti\ti\n”);
printf(“\t\t Squared Cubed\n\n”);
while(i<11)
{
printf(“\t%d\t%d\t%d\n”, i, i*i, i*i*i);
i=i+1;
}
return 0;
}
This example was designed to produce a simple table of the val
ues of the first ten integers, these values squared, and these values
cubed. The lines
printf(“\ti\ti\ti\n”);
printf(“\t\t Squared Cubed\n\n”);
combine to produce a header that identifies the contents of the three
columns generated by the program. The escape character \t is a tab
character that causes the screen cursor to skip to the next tab posi
tion. The default tab value in C is eight spaces.
The command
while(i<11)
.....
causes the argument of the while to be evaluated immediately, and
if the argument is TRUE, the statement following the while will be