Table of Contents
CSci 322 Computer Architecture: Lecture #2
CSci 322 Computer Architecture: Lecture #2
Overall Topic: Introduction to C
#include <stdio.h>
/*
This is a program that sums numbers from 0-9 and prints
out all of the intermediate results as well.
*/
main() {
int i, sum;
sum = 0;
for(i=0;i<10;i++) {
sum += i;
printf("The sum of %d to %d is %d\n", 0, i, sum);
}
return(0);
}
Relevant things:
- #include <stdio.h>: This tells the compiler to include the standard I/O library headers. Any time you use I/O functions like printf/scanf for writing/reading input you will need to include this header. Other relevant headers include <math.h&g
t;, <string.h>, and <stdlib.h>.
- /* ... */: This is a comment. Anything inside the /* */ pair is considered white space by the compiler.
- main(): Every C program must have one function called main(). This is where your program starts. The form of a function is:
<return value> <functionName>(<argument list>) {
<expressions>
}
The function main can take arguments which pass command line information, but it doesn't have to have them.
All functions begin and end with the curly brackets, which denote the boundaries of code blocks.
The function main() by default returns an integer value. In C, a return value of 0 typically means the function executed successfully.
- int i, sum; : This line declares two local variables of type integer for use in this program. In C, all variable declarations must come before any executable expressions (except variable initialization). All individual expressions in C must end with a
semi-colon.
- sum = 0; : This line assigns sum the value of 0. We could also have done this by putting an initializer in the declaration: int i, sum=0;
- for(): This is a for loop, a very powerful statement in C. It contains three parts within the parentheses: expressions to be performed before the loop begins, a test to see if the loop should be executed, and expressions to be performed at the end of
the loop. In this case, we set i to 0, then if i is less than 10 we execute the loop. At the end of the loop, the statement i++ increments the variable i by 1. Since i is not modified by the loop, this block of code will execute 10 times, with i having va
lues from 0 to 9.
Because we are executing more than one expression within the loop the code block for the for loop has to have curly brackets around it. A single expression would not have to have curly brackets (but having them is not wrong!).
- sum += i; : This line increments sum by the value in i. There are a number of these kinds of short-cuts in C, including +=, -=, *=, and /=.
- printf(): This line calls the library function printf(), which takes as its first argument a string (surrounded by double-quotes) which indicates what to print out. As part of this string you can put place-holders for variables. These place-holders ar
e represented by the % sign followed by a letter (and possibly formatting information) indicating the type of variable to be put in that position. %d is integer, %f is floating point, %s is a string, and %c is a character.
- }: Closing bracket for the code block (notice there is no semi-colon after a curly bracket.
- return(0); : This line ends the function and indicates the return value for the function. In this case, a value of 0 indicates success.
- }: Closing bracket for the function. Again, no semi-colon.
The compiler you will want to use is the Gnu C compiler, called gcc. Let's say you put the above code in a file called foo.c (always end your C code files with the .c suffix). Then you can use the command
gcc foo.c
To compile your file. By default, the executable will be put in the file a.out. If you want the compiler to put the executable somewhere else, you can use the -o option to indicate the file.
gcc -o foo foo.c
will put your executable in the file foo. Now, if you decide you want to use the math library (sin, cos, pow, sqrt, etc.) then you will need to link the math library in with your code. You link in libraries using the -l option, followed immediately by the
mnemonic for the library. In the case of the math library, the command:
gcc -o foo foo.c -lm
will compile foo.c, link the math library with your program, and then put the result in foo.
Now let's say you want to split up your program into two parts. If you put one part in foo1.c, and another part in foo2.c you can use the command
gcc -o foo foo1.c foo2.c -lm
to compile foo1.c and foo2.c, link in the math library, and put the result in foo. You could also compile the two .c files separately using
gcc -c foo1.c
which will create the object file foo1.o, which you can then link together using gcc like this:
gcc -o foo foo1.o foo2.o -lm
- Commenting: C programs need to be well-commented. Each function should have a brief description above it indicating the arguments, the function outputs, and the function's purpose. Comments within the function should describe the algorithm, especially
non-intuitive portions of it.
- Variable names: make them descriptive (they can't start with a number, or be one of C's reserved words (like for, if, while...).
- Curly brackets: Where you put them is your style choice, but it's good to pick a standard style and stick to it.
- Indentation: anything inside of a code block should be indented relative to the code outside the code block. This greatly improves readability.
- White space: put white space after your variable declarations; put white space between blocks of code that do different things; be consistent.
- char: character, 1 byte, 0-255, can be unsigned, as in unsigned char
- int: integer, 2 or 4 bytes (be careful), can be unsigned, as in unsigned int
- long: integer, 4-8 bytes (be careful), can be unsigned
- float: IEEE format floating point format, 4 bytes
- double: Native floating point format, 8-12 bytes, sometimes faster than float
- +: addition
- -: subtraction (also unary minus)
- *: multiplication
- /: division
- %: modulus (remainder)
- Precedence: *, /, and % all have higher precedence than + or -. Use parentheses to control precedence otherwise.
- Place arguments between the parentheses, separated by commas: foo(a, b)
- Arguments can be arbitrary expressions: foo(a+b, a-b), foo(a, foo(a, b))
- Many functions return values, which can be used within an expression: a = foo(a, b) + b;
if(<Boolean expression)
<expressions>
<expressions> can be a single expression, or a code block between curly brackets. <Booleans expression> is simply an expression that evaluates to either 0 (FALSE) or not zero (TRUE). You can add on else if statements, and end with an else stat
ement. For example:
if(a < 1)
return(-1);
else if(a == 0)
return(0);
else
return(1);
while(<Boolean expression>)
<expressions>
- break and continue: The break command jumps out of of the current code block. The continue expression is for use in a while or for loop, and simply skips to the end of the code block and starts the loop again.
- I/O from the keyboard and to the screen
- I/O to and from files
- Global variables
- Pointers and arrays