Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
COMP20007 Design of Algorithms
C Programming Refresher
Knowledge of C is a pre-requisite for this subject so this document isn’t meant as an all-encompassing
introduction to C, rather a refresher for those who may not have worked in C for a semester or two.
Students who completed COMP10002 Foundations of Computing should have seen dynamic memory
allocation using malloc.
Also required will be the use of header files (.h), a brief introduction is provided here.
If any of this is new we strongly recommend Programming, Problem Solving and Abstraction with C
by A. Moffat as a good reference for the C programming required for successful completion of this
subject.
1 Data Types
The integer data types in C are int, char, short and long. char, short and long are at least 1, 2
and 4 bytes respectively. The size of an int is platform dependent but is usually 2 or 4 bytes.
There are corresponding unsigned types for each of these integer data types for non-negative numbers.
For example an int may be able to store the integers in the range −32768 to 32767, in which case an
unsigned int can store integers from 0 to 65535.
For floating point numbers C has float and double.
A char can store a single ASCII character. Strings in C are arrays of char’s terminated by a null byte,
for instance "comp20007" is stored as the array of characters [’c’,’o’,’m’,’p’,’2’,’0’,’0’,’0’,’7’,’\0’].
Here’s an example of some variable declarations of different data types:
/∗ I n t e g e r data t y p e s ∗/
int my negative number = −20007;
unsigned int my unsigned number = 20007 ;
/∗ F l o a t i n g p o i n t numbers ∗/
f loat my f loat = 0 . 1 2 3 ;
double my double = 0 . 1 2 3 ;
/∗ Dec lar ing a char us ing a number or a c h a r a c t e r in s i n g l e quo t e s ∗/
char my char = 100 ;
char my other char = ’A ’ ;
/∗ We can d e c l a r e s t r i n g l i t e r a l s l i k e t h i s : ∗/
char my str [ 5 ] = { ’ c ’ , ’ o ’ , ’m’ , ’p ’ , ’ \0 ’ } ;
/∗ But we ’ d u s u a l l y do i t l i k e t h i s : ∗/
1
char my str [ ] = ”comp” ;
Out of the box C doesn’t have a boolean type, and integers can be used. Non-zero values are interpreted
as true while 0 is interpreted as false. For example:
int my true bool = 1 ;
int m y f a l s e b o o l = 0 ;
i f ( my true bool ) {
p r i n t f ( ” Algorithms are fun !\n” ) ;
}
i f ( m y f a l s e b o o l ) {
p r i n t f ( ” Algorithms are not fun\n” ) ;
}
will print “Algorithms are fun!”.
If you’d prefer (and you’re using C99) the stdbool.h header provides the bool data type and the
values true and false.
2 Function Syntax
When we define a function in C we must declare the type of the value returned (if there is one) and
the types of each of the function arguments. To return a value from a function we use the return
keyword. An example of a function which takes an integer and returns an integer (used as a bool) is
given.
int i s f u n ( int s u b j e c t c o d e ) {
i f ( s ub j e c t c o d e == 20007) {
return 1 ;
} else {
return 0 ;
}
}
To declare a function which does not return a value we use the void keyword:
void r e tu rn s no th ing ( int s u b j e c t c o d e ) {
i f ( s ub j e c t c o d e == 20007) {
p r i n t f ( ”Fun !\n” ) ;
}
}
If a function takes no arguments the parentheses after the function name can be left empty:
void takes no arguments ( ) {
p r i n t f ( ”Fun !\n” ) ;
}
In C you must declare a function before it’s used. To get around having to clever about the order of
your function implementations it’s good practice to provide function prototype declarations towards
the top of the file before the function implementations. An example is provided.
/∗ Function Prototype ∗/
int my funct ion ( int n ) ;
2
int main ( int argc , char ∗∗ argv ) {
int n = 100 ;
int r e s u l t = my funct ion (n ) ;
p r i n t f ( ”%d\n” , r e s u l t ) ;
return 0 ;
}
/∗ Function Implementation ∗/
int my funct ion ( int n) {
return n ∗ n ;
}
3 Main Function
When a C program is run from the command line the main function is executed. The main function
takes the number of provided arguments (argc) and an array of argument strings (argv – for “argument
vector”). The return value is used to indicate success or failure of the program. A return value of zero
indicates success and a non-zero return value indicates failure.
Note that it’s standard for the type of argv to be char **, as it is a pointer to an array of strings.
The example below prints the number of arguments and what those arguments are.
int main ( int argc , char ∗∗ argv ) {
int i ;
p r i n t f ( ”Number o f arguments : %d\n” , argc ) ;
for ( i = 0 ; i < argc ; i++) {
p r i n t f ( ”%s \n” , argv [ i ] ) ;
}
return 0 ;
}
If this is compiled into a program called myprogram then an example of this program’s execution might
be:
$ . / myprogram
Number o f arguments : 1
. / myprogram
$ . / myprogram arg1 arg2
Number o f arguments : 3
. / myprogram
arg1
arg2
4 Compilation
The command I use to compile a C program hello.c is
$ gcc -Wall -pedantic -o hello hello.c
• gcc is the name of the C compiler we’re using.
3
• -Wall stands for “Warnings All” and it turns on the highest level compiler warnings. In general
having more compiler warnings is good since catching an error at compile time is better than
having a hard to catch runtime error.
• -pedantic turns on another set of compiler errors.
• -o hello tells the compiler that the output program should be called hello.
• hello.c is the C source code file.
A debugging tip is that if you use the -g flag you’ll be able to access the source code/variable
names/function names etc. from inside debuggers such as gdb and lldb.
5 Library Functions