Function: Declaration <return type> <identifier>( <parameter>, ... );
Function: Declaration unsigned int square( int number ); Declaration creates prototype (older versions of C didn't require parameter list, no prototype created)
Function: Declaration unsigned int square( int );
Function: Declaration unsigned int square( int number );
Function: Definition <return type> <identifier>( <parameter>, ... ) { <statements> }
Function: Definition unsigned int square( int number ) { return number * number; } Definition “acts” as declaration if no declaration exists Definition must match prototype if declaration exists
Function: Calling unsigned int two_squared = square( 2 ); All arguments passed by value i.e. copy made Open source 07 (functions and overloading) Explain _Generic (compile time macro)
Exercises Open exercises/ex06/main.c Declare a new function with the following prototype: unsigned int gcd( unsigned int a, unsigned int b ) Define the function so that it implements Euclid's greatest common divisor algorithm as follows: if b equals 0, return a else return gcd( b, a modulo b ) ←- Recursive call to gcd() Build using Makefile Run & test. Output to console should be “GCD = 8”.
Enumeration enum [<identifier>] { <enumeration constant> [= <constant expression>], . <enumeration constant> [= <constant expression>] };
Enumeration enum MultipleChoice { OPTION_A, OPTION_B, OPTION_C, OPTION_D, ALL_OF_THE_ABOVE };
Enumeration enum MultipleChoice { OPTION_A, // 0 OPTION_B, // 1 OPTION_C, // 2 OPTION_D, // 3 ALL_OF_THE_ABOVE // 4 };
Enumeration enum MultipleChoice { OPTION_A = 10, OPTION_B, OPTION_C, OPTION_D, ALL_OF_THE_ABOVE };
Enumeration enum MultipleChoice { OPTION_A = 10, // 10 OPTION_B, // 11 OPTION_C, // 12 OPTION_D, // 13 ALL_OF_THE_ABOVE // 14 };
Enumeration enum MultipleChoice { OPTION_A, // 0 OPTION_B, // 1 OPTION_C = 12, // 12 OPTION_D, // 13 ALL_OF_THE_ABOVE // 14 };
Enumeration enum MultipleChoice question1 = OPTION_C;
Structures struct [<identifier>] { <struct declarator>; . };
Structures struct RGBA { float red; float green; float blue; float alpha; };
Structures struct RGBA polygon_colour; polygon_colour.red = 0.5f; polygon_colour.green = 0.5f; polygon_colour.blue = 0.5f; polygon_colour.alpha = 1.0f;
Typedef typedef struct RGBA RGBA;
Typedef typedef struct RGBA RGBA;
Typedef typedef struct RGBA RGBA;
Typedef typedef struct RGBA RGBA; Open source 08 Add typedef for enum and struct Briefly explain char * as string VLA char[] cannot be used, except at the end of the struct
Exercises Open exercises/ex07/main.c Define a new enum called Genre, with two or three constants for movie genres Define a new struct called BluRay, with the following attributes: char *title char *director unsigned short year enum Genre genre Define a new function that creates and returns a new BluRay, and another that prints a BluRay to console Create a BluRay in main(), and print to console, using the new functions Build using Makefile Run & test
Pointers char some_char = 'c'; char *char_ptr = &some_char;
Pointers char some_char = 'c'; char *char_ptr = &some_char;
char *char_ptr = &some_char; Pointers char some_char = 'c'; char *char_ptr = &some_char; base-type specifier
char *char_ptr = &some_char; Pointers char some_char = 'c'; char *char_ptr = &some_char; declarator
char *char_ptr = &some_char; Pointers char some_char = 'c'; char *char_ptr = &some_char; identifier
char *char_ptr = &some_char; Pointers char some_char = 'c'; char *char_ptr = &some_char; derived type
char *char_ptr = &some_char; Pointers char some_char = 'c'; char *char_ptr = &some_char; optional initialiser
char *char_ptr = &some_char; Pointers char some_char = 'c'; char *char_ptr = &some_char; address-of operator
Pointers char some_char = 'c'; char *char_ptr = 0xA1;
Pointers char some_char = 'c'; char *char_ptr = &some_char;
char *char_ptr = (char *)0xA1; Pointers char *char_ptr = (char *)0xA1; 'a' 12 ... 8 'c' 'Z' ... 0x00 0x01 0x02 0xA0 0xA1 0xA2
Pointers char some_char = 'c'; char *char_ptr = &some_char; printf( "I am pointing to %c", *char_ptr ); Open source 09 Arrays decay to pointer Pointer arithmetic – warn about pointing to other data Show loop using pointer arithmetic (assign back to pointer) Index operator dereference operator
Exercises Open exercises/ex08/main.c Complete the definition for swap(). The function must swap the values of its two arguments. Create two int variables in main() (initialise them to any values) Output the values of the variables before and after calling swap() Build using Makefile Run & test
Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0;
Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; main()
Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; main()
Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; some_func() main()
Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; some_func() main()
Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; another_func() some_func() main()
Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; another_func() some_func() main()
Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; some_func() main()
Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; main()
Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; main()
Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; main()
Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; Open source 10
Exercises Open exercises/ex09/main.c Define the function zero_fill(). The function must fill mem with zeroes for the specified size. Define the function alloc_and_zero(). The function must allocate memory on the heap, of the specified size, fill it with zeroes, and return a pointer to the new heap memory Allocate a new instance of Date using alloc_and_zero(). Output the content of year, month, day to console. Build using Makefile Run & test. Ensure that output is zeroes. Open source 11 Explain function pointers before exercise on next slide
Exercises Open source 12 Open exercises/ex10/main.c Create a typedef alias for the function pointer type of the func parameter in for_each(). Rewrite the func parameter to use your new typedef. Define the function for_each(). The function must call func for each value in the array, passing in the value and its associated index. Define the function print_idx_val(). The function must print the value and index parameters to console e.g. "Value 0 is 100". In main(), call for_each() with the test values supplied and print_idx_val() Build using Makefile Run & test Open source 12
File Size fseek( file, 0, SEEK_END ); long file_size = ftell( file ); rewind( file );
Exercises Open source 12 Open exercises/ex11/main.c Define the function read_in_file(). The function must read in the contents of the file, at file_name, into memory and return it. If an error occurs, return NULL. HINT: retrieve the file size to determine the amount of memory to allocate. Define the function print_buffer(). The function must output the contents of buffer to the console. Read implementation of main(). Ensure you understand. Build using Makefile Run & test using haiku.txt Open source 12
Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... }
Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... } Library header file with single function declaration Explain inclusion guard
Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #pragma once void my_func( ... ); mylib.c #include “mylib.h” void my_func( ... ) { ... } #pragma once inclusion guard
Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... } Library header file with single function declaration Explain inclusion guard
Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... } Library source file #include explanation to follow Contains the definition for the function declared in header
Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... }
Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... }
Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... } Explain #include is preprocessor Point out quotes in #include
Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... } Explain inclusion guard
Libraries & #include Directive main.c mylib.h #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... }
Libraries & #include Directive main.c mylib.h #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... }
Libraries & #include Directive main.c mylib.h #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif void my_func( ... ) { ... }
Libraries & #include Directive main.c mylib.h #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif void my_func( ... ) { ... }
Libraries & #include Directive main.c mylib.h #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif void my_func( ... ) { ... } Compilation Unit
Libraries & Compilation main.c mylib.c cc cc main.o mylib.o ld binary executable
Libraries & Compilation main.c mylib.c cc cc main.o mylib.o ld binary executable
Libraries & Compilation main.c mylib.c cc cc main.o mylib.o ld binary executable
Libraries & Compilation main.c mylib.c cc cc main.o mylib.o ld binary executable
Libraries & Compilation main.c mylib.c cc cc main.o mylib.o ld binary executable
Libraries & Compilation main.c mylib.c cc cc main.o mylib.o ld Not really a distributable library See how to make a reusable static library now binary executable
Libraries & Compilation main.c cc main.o mylib.a ld binary executable
Libraries & Compilation main.c Static Library cc main.o mylib.a ld binary executable
Exercises Open source 12 Open exercises/ex12/src/mystring.c Define the function my_streq() that is declared in exercises/ex12/include/mystring.h. The function must compare the two argument strings to determine if they’re equal. Add a new test to exercises/ex12/tests/main.c to test your new function Build lib and tests using Makefile Run tests Open source 12
Exercises Create the header and implementation files for a new library called vector Define a new struct called Vector in your header file. The Vector struct should have the following attributes float x; float y; float z; Declare and define a function called vec_add(). The function must take two Vector arguments and return a Vector containing their sum. Declare and define a function called vec_dot(). The function must take two Vector arguments and return their dot product, calculated as: ( vec1.x * vec2.x ) + ( vec1.y * vec2.y ) + ( vec1.z * vec2.z ) Create your own tests for the functions Build using Makefile Run tests
Linked Lists head
Linked Lists: Append head
Linked Lists: Append head
Linked Lists: Append head
Linked Lists: Insert head
Linked Lists: Insert head
Linked Lists: Insert head
Linked Lists: Insert head
Linked Lists: Insert head
Linked Lists: Delete head
Linked Lists: Delete head
Linked Lists: Delete head
Linked Lists: Delete head
Linked Lists: Delete head
Linked Lists: Delete head
Linked Lists: Search Search for 8 head 21 60 8 17 31
Linked Lists: Search Search for 8 head 21 60 8 17 31
Linked Lists: Search Search for 8 head 21 60 8 17 31
Linked Lists: Search Search for 8 head 21 60 8 17 31
Linked Lists: Search Search for 8 head 21 60 8 17 31
Linked Lists: Search Search for 8 head 21 60 8 17 31
Linked Lists: Search Search for 8 head 21 60 8 17 31
Linked Lists: Search What would happen if 8 didn’t exist? Search for 8 head 21 60 8 17 31 What would happen if 8 didn’t exist?