Week 01 (Wednesday)
C Program Structure | 1/18 |
Each C program consists of
.c
a.out
Makefiles | 2/18 |
A Makefile helps to organise the compilation of modules of a C program
Example:
seqq: seqq.c gcc -Wall -Werror -o seqq seqq.c
The UNIX command make
Makefile
prompt$ make gcc -Wall -Werror -o seqq seqq.c
prompt$ make make: `seqq' is up to date.
... Makefiles | 3/18 |
Can have multiple targets:
example: a.o b.o c.o gcc -o a.out a.o b.o c.o a.o: a.c gcc -Wall -c a.c b.o: c.c gcc -Wall -c b.c c.o: c.c gcc -Wall -c c.c
make example
make a.o
make b.o
make c.o
make
Module Interfaces | 4/18 |
Names are shared via module interfaces
.h
a.c
a.h
typedef
... Module Interfaces | 5/18 |
Example module interface stdio.h
typedef FILE ...; FILE *stdin, *stdout, *stderr; FILE *fopen(char *fileName, char *mode); int fgetc(FILE *inf); int fputc(int ch, FILE *outf); char *fgets(char *strbuf, int max, FILE *inf); int fputs(char *str, FILE *outf); ...
Many more examples available in .h
/usr/include
Exercise: MyInt | 6/18 |
Design an interface to a module that provides:
int x; ... if (isEven(x)) { ...
int x; ... while (isOdd(x)) { ...
Makefile
... Exercise: MyInt | 7/18 |
Can generalise the Makefile
Can also add a target to clean up directory
Example:
CC=gcc CFLAGS=-Wall -Werror tester: tester.o MyInt.o $(CC) $(CFLAGS) -o tester tester.o MyInt.o tester.o: tester.c MyInt.h $(CC) $(CFLAGS) -c tester.c MyInt.o: MyInt.c MyInt.h $(CC) $(CFLAGS) MyInt.c clean: rm -f tester tester.o MyInt.o core
... Exercise: MyInt | 8/18 |
# General structure of a Makefile definitions target: dependencies commands
definitions
target
.o
dependencies
.c
.h
.o
commands
C Execution: External View | 9/18 |
... C Execution: External View | 10/18 |
Command-line arguments:
argc
argv
stdin
stdout
stderr
char *environ[]
environ[]
NULL
... C Execution: External View | 11/18 |
Example argc
argv
C execution: Startup | 12/18 |
All C programs must have a main()
Executing a C program causes main()
argc
argv
stdin
stdout
stderr
main()
return
EXIT_SUCCESS
EXIT_FAILURE
$?
C execution: Redirect | 13/18 |
The streams stdin
stdout
stderr
stdin
prompt$ myprog < input.data
stdout
prompt$ myprog > output.data
stderr
prompt$ myprog 2> error.data
C execution: Memory | 14/18 |
An executing C program partitions memory into:
malloc()
... C execution: Memory | 15/18 |
Lifetime | 16/18 |
Lifetime: duration that a variable is available in memory.
Variables in the global data ...
malloc()
free()
Exercise: Buggy Function | 17/18 |
Explain what is wrong with this function:
// returns a string of 'x' characters // e.g. xs(5) -> "xxxxx", xs(0) = "" char *xs(int n) { int i; char buffer[100]; // truncate if try to add too many 'x's if (n > 100) { n = 100; } else if (n < 0) { n = 0; } for (i = 0; i < n; i++) { buffer[i] = 'x'; } buffer[i] = '\0'; // terminate string return buffer; }
How could we fix the problem?
Tips for Next Week's Lab | 18/18 |
Command line arguments; Makefiles
argc
argv[0], argv[1], ...
prompt$ ./add3 1 2 a ./add3: found non-numerical argument
Makefile
Produced: 11 Aug 2016