CS 202 - Notes 2018-09-14
Source to app (completed)
Reminder
Text file written in some programming language (intro.c)
compile (gcc -S
)
Assembly file (intro.s)
assemble (gcc -c
)
Object file in machine language (intro.o)
The .o file still won't run. The final stage is linking. This gathers together all of the executable code needed (including material from libraries and other object files), links it all together and identifies the entry point (the first line of the main
function). Now we have a runnable program.
Adding a file
To illustrate this, we can add another file.
helper.c
/*
This is a fairly pointless function that takes in two integers and returns some function of them.
*/
int f(int x, int y){
return x*2 + y*5;
}
intro.c
#include <stdio.h>
int f(int x, int y); // function declaration
int main(int argc, char* argv[]){
printf("Don't Panic!\n");
printf("x=%d, y=%d, f=%d\n", 8, 3, f(8, 3));
}
Note that we need to include a function declaration for f()
in intro.c
. This is required so the compiler (which works one file at a time) knows that the function call is valid (i.e., that the arguments are appropriate)
We can compile this with
gcc intro.c helper.c -o intro_plus
We seem to have skipped some steps, but as it turns out, gcc
can do the compile, assemble, and link all in one step. The -o
flag is just used to name the output file (otherwise, the output is called a.out
).
Header files
Function declarations are typically stored in header files (.h). We do not include links to the executable code in our source files, that needs to be specified in the call to gcc
. We either include the file with the executable code (as we did with helper.c
), we tell gcc
where to find the code in the library directories, or we assume gcc
knows where it is (as is the case for the code in stdio
and stdlib
).
If we move the declaration out to a header file:
helper.c
/*
This is a fairly pointless function that takes in two integers and returns some function of them.
*/
int f(int x, int y){
return x*2 + y*5;
}
helper.h
int f(int x, int y); // function declaration
intro.c
#include <stdio.h>
#include "helper.h" // quotes indicate the file path is relative to this file
int main(int argc, char* argv[]){
printf("Don't Panic!\n");
printf("x=%d, y=%d, f=%d\n", 8, 3, f(8, 3));
}
Intro to C
Much of C's syntax will feel very familiar coming from Java
Things that are similar: semicolons, {} blocks, function syntax, typed integer declarations, for loops, while loops, if statements
Deconstructing main
every program must have a main -- it is the starting place of your program
int main(inst argc, char * argv[])
The return value is returned to the calling process, and it tells the process if the program completed successfully or not. We typically return 0 to indicate no errors occurred and -1 if there was an error.
The arguments of main give us access to the command line arguments with which the program was called.
argc
tells us how many arguments have been passed to the programargv
is an array of strings- the program name itself is always the first element of the argument list
printf
printf()
is our primary tool for textual output. The arguments are a format string and then a list of substitutions to be included.
%d
for integers%s
for strings%f
for floating point numbers
This just scratches the surface. Wikipedia has a good list of all the various options that you can include.
example
power.c
#include <stdio.h>
#include <stdlib.h>
/*
Returns x raised to the y.
This is a simple example of a for loop.
*/
int power(int x, int y){
int result = 1;
for (int i = 0; i < y; i++){
result *= x;
}
return result;
}
int main(int argc, char * argv[]){
// get x and y from the command line and convert them to integers
int x = atoi(argv[1]);
int y = atoi(argv[2]);
printf("%d^%d = %d\n", x, y, power(x, y));
}