The program from Overview appears below. The lines are numbered on the left so we can refer to them. The numbers are NOT part of the program.
1. #include<stdio.h> 2. int main() { 3. int num = 1, 4. sum = 0; 5. while (num<10) 6. { 7. sum += num; 8. num += 1; 9. } 10. printf("%d\n", sum); 11. }
main
.
Function definitions consist of a
header,
in our case
int main()and the body of the function. The body is a compound statement. Compound statements start with the open brace ({) and continue until the matching closing brace (}) is found. For the body, the opening brace is the first character visible after the header (line 2) and the matching closing brace is in line 11.
Compound statements contain 0 or more statements between the braces.
Computers execute compound statements by executing each of the statements
contained inside. The statements are executed in the order that they
found inside the compound statement.
What compound statements are good for will become apparent below.
Note the body of
main
contains a compound statement inside of it. This is the
statement that starts with a open brace on line 6 and ends with a
closing brace on line 9.
Our program contains three types of "simple statements": variable definition, assignment, and function call. It also contains
while (num<10)in line 5.
Variable Definitions. A variable is a named storage location in computer memory. As such it has a content and an address. In addition, the program gives it a type and a value. The value is the content of the storage location. The program can change the value in a variable.
The statement in lines 3 and 4 defines two variables to be used by the program:
3. int num = 1, 4. sum = 0;The variable names are
num
and
sum.
Variable
num
is initialized to 1, and variable
sum
is initialized to 0. Variable names are just made up. They
must start with a letter or underbar (_) and the rest of the name can be
more letters, digits and underbars. You should not use keywords of C/C++ as
variable names (list). You should use names that make sense in the
context of what the program is trying to do.
The keyword,
int,
that starts the
statement determines the type of the variables. "int" says that the
content of the variables will be interpreted as integers. Click on
type keywords to see more types.
The C and C++ languages require that all variables be defined before they can be used. Here are a few more variable definitions.
int num=1; int sum=0; float pi=3.14; float rate = 3.5e-2;The first two lines are an alternate way of doing what lines 3 and 4 do in our program. The third and fourth lines set up variables
pi
and rate
to store non-integers. The value
3.5e-2is scientific notation:
-2 3.5 x 10That is, 0.035.
Assignment Statements. The format for assignment statements is:
variable assignOp expressionLines 7 and 8 are the two assignment statements in our program
7. sum += num; 8. num += 1;There are many assignment operators. See Assignment Operators The assignment operator in the statements above is "+=". No spaces between the "+" and the "=". This operator changes the value stored in the variable on its left by adding to that variable the value on its right. So if variable
sum
has the value 36 stored in it and variable
num
has the value 9 stored in it, then line 7 will replace the 36 with 45 (36+9). Afterwards line 8
executes and replaces the 9 in
num
with a 10.
The while prefix. Take a statement, say the compound statement containing the two assignments:
6. { 7. sum += num; 8. num += 1; 9. }Prefix that statement with
while(num<10)
:
5. while (num<10) 6. { 7. sum += num; 8. num += 1; 9. }This makes a NEW statement. We need to talk about how it executes. The first thing that happens is the condition
num<10inside the parentheses is evaluated. If it's true (num is less than 10), the statement after the parentheses is executed, and then the condition is re-evaluated. So long as the condition is true we continue executing the statement after the parentheses and then re-checking the condition. If the condition tests false, we skip the following statement and go to the next statement in the program. In our program that is line 10.
IMPORTANT NOTE: by having both assignments contained inside the compound statement and then putting the while
in front of the compound statement we repeat the combination below
several times
add num to sum make num one largerEach repetition adds a new number into sum since the second statement makes
num
one larger.
Other comparisons are possible as conditions: More Comparisons.
Function Calls. Line 10
10. printf("%d\n", sum);uses or calls the
printf
function. Some functions do some work for the calling function (in this case, main).
Other functions do some work and return a value to the calling function. Function
printf
prints stuff and sets up where the next printf will print.
Lots of functions require that values be plugged into the parentheses that follow the
function name. These values are called the
arguments
of the functions. The arguments are separated by commas. Our call to function
printf
has two arguments plugged into the function:
"%d\n" sumThe first argument is a string constant. The string is the sequence of characters between the quotes. Mostly printf just prints the string. Like the call
printf("Hello out there!");prints
Hello out there!However both
%d
and
\n
are special.
The \n
is the new line character. It is a single character.
Function printf prints it by
moving down one line and all the way back to the left. So the call
printf("cat\ndog");prints
cat dogThe
%d
tells printf to look at the next argument which should
be an int
and print it in place of the %d using decimal digits.
Thus the call
int num = 35; printf("%d\n", num); printf("Bob");prints
35 BobThe new line character is printed. The next print starts all the way at the left of the new line. Finally in the call
printf("age=%d weight=%s\n", 35, "too much");the first string (the format string) tells printf to expect two more arguments, the first should be an
int
and the second should
be a string (%s
). What prints is
age=35 weight=too muchand the next
printf
, if there is one will print on the line
below.