Module 1 - Programming Basics
Variables & Data Types
Once again, we'll be focusing on the example code in the file module1.tar.
We'll focus more on actual program compilation this time; to create an executable C program, you'll need to compile it:
- As restricted and rigorous as the C programming syntax is to write, the computer still can't understand anything that high level; digital computers understand ON or OFF, and need programs written for it to be reduced to a series of 1's (ON) and 0's (OFF).
- A compiler does the job of reducing a syntactically correct C program into a machine readable executable file.
- We invoke the C compiler via the command cc, in the following form:
cc -o <file-name> <c-program-name>
passing the following arguments to the cc command:
- the compiler option -o (To indicate that the executable should be placed in a named file rather than the default a.out),
- the name of the file to put the executable machine code (<file-name>),
- and the name of the C program file (<c-program-name>) to compile.
- For now, that's about all you'll need to know about compiling; we'll discuss compilation in greater detail during class.
Let's take a closer look at the program sumi.c (We'll remove the comments in the version displayed below);
#include <stdio.h>
main() {
int variable1;
int variable2;
int variable3;
variable1 = 2;
variable2 = 3;
variable3 = variable1 + variable2;
printf("The sum is %d\n", variable3);
}
Once again, we include the standard input/output library, stdio.h...
#include <stdio.h>
...so that we can print information to the screen (And in another version of this program, accept user input via the function scanf).
The first actual instruction (After the main () { that sets up the main function) is
int variable1;
which is a variable declaration. It declares that we will be using a variable called variable1, and, via the int reserved word, indicates that it will always contain an integer value.
In C, when a variable is declared, computer memory is assigned to hold whatever value we decide to put into that variable. We do the same thing for the other 2 variables we'll use in this example program.
Next, we assign values to our variables using a C assignment statement;
variable1 = 2;
variable2 = 3;
The above uses the equal sign = to indicate that the value on the right side of the equation should be assigned to the variable on the left side. (Note: In an upcoming module, when we want to test for equality as part of a conditional statement, we'll use consecutive equal signs ==).
The third assignment statement puts the value of the sum of variable1 and variable2 into the variable3.
variable3 = variable1 + variable2;
Finally, we print using the formatting print statement printf:
printf("The sum is %d\n", variable3);
Note that this time, instead of printing static text from the previous "Hello Class" example, we use format identifiers to indicate how the dynamic variables should be printed.
The printf statement is passed a set of arguments; the first is the string to be printed. Printf looks inside the string for format identifiers (which tell it what to expect and how to print it); if there are any, the following argument passed to printf is placed in that spot, formatted based on the type indicated by the format identifier.
So in the above printf statement, the format identifer %d indicates that an integer value should be placed in the text output at the spot in the string (%d indicates decimal signed integer, and %i can also be used), and printf places the next argument in that spot.
Later, you'll become acquainted with the format identifier %f; in addition, you can click here for a complete list of C printf format identifiers.
After you test your knowledge of what we've learned in the apply section, you'll have a chance to reflect on what you've learned by seeing it applied in sumf.c (For floating point variables) and in producti.c and productf.c for assignment of the product of two variables.
The extend section will show how we can extend the formatting concept we learned above with printf to scanning in data from user input via scanf, which will allow the program to assign dynamic values to variables.
Continue to the Apply section->