Sample Programs
The following sample programs (adapted from those provided by Jeff Ondich) will help you explore many useful features in C.
Study them! It is expected that you read them as the term progresses, just as you would the textbook or any other assigned reading.
Here are some things to keep in mind as you explore these programs:
-
Ask questions: You aren’t expected to understand everything the first time you read these sample programs. As you work through them, write down your questions and make sure to get them answered!
-
Experiment: The beauty of programming is that you can (usually) make changes and immediately see the impact when re-running your code. Play around with these samples to help you understand them! If you find yourself thinking I wonder what happens if I make this change…, just try it!
-
Build these programs: For most of these samples, you should use the following compilation approach, assuming the code file is named
prog.c
:gcc -Wall -Werror -g -o prog prog.c
We will talk about the different parts of this command throughout the first few weeks of the term.
- Pedagogical comments vs. production comments: These programs are written to have a lot of explanatory comments, because they are designed as pedagogical programs (to teach you things!), not as production programs. Production code (i.e., that intended for use by end users or even as homework submissions) should following the commenting advice in our style guide. You are welcome to borrow code from these samples, but delete these pedagogical comments before you are ready to submit.
Samples
Getting started in C, char
arrays:
- hello.c -
main
,printf
,%d
- char.c -
char[]
,for
,if
,||
,%c
- args.c - command-line arguments,
&&
,%s
- struct.c -
struct
,%ld
- struct_array.c - array of structs
- character_counter.c - file I/O,
while
- line_counter.c - checking for
\n
,%x
- line_counter_debug.c - the previous sample, but with more (optional) output
Bits and bytes, pointers, integers:
- bitwise.c - bitwise/logical/shift operations
- debug_this.c - simple program with a bug for Lab 1
- pointers.c -
&
and*
pointer operators - sizes.c -
sizeof
,typedef
- strings.c - C strings and
char *
,strlen
,strcmp
, etc. - integer_rep.c - program to display bytes in numbers for Lab 2
- show_bytes.c - byte orderings for common types
- integers.c - store integer values in various ways
- printing_bytes.c -
printf
and its implicit casting toint
for%x
- memblock.c - a big block of memory to make sense of
Program representation and ISA:
- program_memory.c - a program to disassemble and explore
- arrays.c - treating a block of memory as different types of arrays
- switch.c -
switch
statements in C - struct_math.c - structs with arrays to view in assembly
- gdb_test.c - a program to use with
gdb
Buffer overflow attacks:
- attack.c - a program that is susceptible to buffer-overflow attacks
Process-related samples:
- Makefile - a simple Makefile for all process-related samples
- Getting started with processes:
- Redirection:
- Helpful for writing your shell:
- Pipes and signals: