Download presentation
Presentation is loading. Please wait.
1
Function: Declaration
<return type> <identifier>( <parameter>, ... );
2
Function: Declaration
unsigned int square( int number ); Declaration creates prototype (older versions of C didn't require parameter list, no prototype created)
3
Function: Declaration
unsigned int square( int );
4
Function: Declaration
unsigned int square( int number );
5
Function: Definition <return type> <identifier>( <parameter>, ... ) { <statements> }
6
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
7
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)
8
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”.
9
Enumeration enum [<identifier>] {
<enumeration constant> [= <constant expression>], . <enumeration constant> [= <constant expression>] };
10
Enumeration enum MultipleChoice { OPTION_A, OPTION_B, OPTION_C,
OPTION_D, ALL_OF_THE_ABOVE };
11
Enumeration enum MultipleChoice { OPTION_A, // 0 OPTION_B, // 1
OPTION_C, // 2 OPTION_D, // 3 ALL_OF_THE_ABOVE // 4 };
12
Enumeration enum MultipleChoice { OPTION_A = 10, OPTION_B, OPTION_C,
OPTION_D, ALL_OF_THE_ABOVE };
13
Enumeration enum MultipleChoice { OPTION_A = 10, // 10 OPTION_B, // 11
OPTION_C, // 12 OPTION_D, // 13 ALL_OF_THE_ABOVE // 14 };
14
Enumeration enum MultipleChoice { OPTION_A, // 0 OPTION_B, // 1
OPTION_C = 12, // 12 OPTION_D, // 13 ALL_OF_THE_ABOVE // 14 };
15
Enumeration enum MultipleChoice question1 = OPTION_C;
16
Structures struct [<identifier>] { <struct declarator>; .
};
17
Structures struct RGBA { float red; float green; float blue;
float alpha; };
18
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;
19
Typedef typedef struct RGBA RGBA;
20
Typedef typedef struct RGBA RGBA;
21
Typedef typedef struct RGBA RGBA;
22
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
23
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
24
Pointers char some_char = 'c'; char *char_ptr = &some_char;
25
Pointers char some_char = 'c'; char *char_ptr = &some_char;
26
char *char_ptr = &some_char;
Pointers char some_char = 'c'; char *char_ptr = &some_char; base-type specifier
27
char *char_ptr = &some_char;
Pointers char some_char = 'c'; char *char_ptr = &some_char; declarator
28
char *char_ptr = &some_char;
Pointers char some_char = 'c'; char *char_ptr = &some_char; identifier
29
char *char_ptr = &some_char;
Pointers char some_char = 'c'; char *char_ptr = &some_char; derived type
30
char *char_ptr = &some_char;
Pointers char some_char = 'c'; char *char_ptr = &some_char; optional initialiser
31
char *char_ptr = &some_char;
Pointers char some_char = 'c'; char *char_ptr = &some_char; address-of operator
32
Pointers char some_char = 'c'; char *char_ptr = 0xA1;
33
Pointers char some_char = 'c'; char *char_ptr = &some_char;
34
char *char_ptr = (char *)0xA1;
Pointers char *char_ptr = (char *)0xA1; 'a' 12 ... 8 'c' 'Z' ... 0x00 0x01 0x02 0xA0 0xA1 0xA2
35
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
36
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
37
Stack void another_func() { ... } void some_func() { another_func();
int main() { some_func(); return 0;
38
Stack void another_func() { ... } void some_func() { another_func();
int main() { some_func(); return 0; main()
39
Stack void another_func() { ... } void some_func() { another_func();
int main() { some_func(); return 0; main()
40
Stack void another_func() { ... } void some_func() { another_func();
int main() { some_func(); return 0; some_func() main()
41
Stack void another_func() { ... } void some_func() { another_func();
int main() { some_func(); return 0; some_func() main()
42
Stack void another_func() { ... } void some_func() { another_func();
int main() { some_func(); return 0; another_func() some_func() main()
43
Stack void another_func() { ... } void some_func() { another_func();
int main() { some_func(); return 0; another_func() some_func() main()
44
Stack void another_func() { ... } void some_func() { another_func();
int main() { some_func(); return 0; some_func() main()
45
Stack void another_func() { ... } void some_func() { another_func();
int main() { some_func(); return 0; main()
46
Stack void another_func() { ... } void some_func() { another_func();
int main() { some_func(); return 0; main()
47
Stack void another_func() { ... } void some_func() { another_func();
int main() { some_func(); return 0; main()
48
Stack void another_func() { ... } void some_func() { another_func();
int main() { some_func(); return 0; Open source 10
49
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
50
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
51
File Size fseek( file, 0, SEEK_END ); long file_size = ftell( file );
rewind( file );
52
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
53
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( ... ) { ... }
54
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
55
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
56
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
57
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
58
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( ... ) { ... }
59
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( ... ) { ... }
60
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
61
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
62
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( ... ) { ... }
63
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( ... ) { ... }
64
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( ... ) { ... }
65
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( ... ) { ... }
66
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
67
Libraries & Compilation
main.c mylib.c cc cc main.o mylib.o ld binary executable
68
Libraries & Compilation
main.c mylib.c cc cc main.o mylib.o ld binary executable
69
Libraries & Compilation
main.c mylib.c cc cc main.o mylib.o ld binary executable
70
Libraries & Compilation
main.c mylib.c cc cc main.o mylib.o ld binary executable
71
Libraries & Compilation
main.c mylib.c cc cc main.o mylib.o ld binary executable
72
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
73
Libraries & Compilation
main.c cc main.o mylib.a ld binary executable
74
Libraries & Compilation
main.c Static Library cc main.o mylib.a ld binary executable
75
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
76
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
77
Linked Lists head
78
Linked Lists: Append head
79
Linked Lists: Append head
80
Linked Lists: Append head
81
Linked Lists: Insert head
82
Linked Lists: Insert head
83
Linked Lists: Insert head
84
Linked Lists: Insert head
85
Linked Lists: Insert head
86
Linked Lists: Delete head
87
Linked Lists: Delete head
88
Linked Lists: Delete head
89
Linked Lists: Delete head
90
Linked Lists: Delete head
91
Linked Lists: Delete head
92
Linked Lists: Search Search for 8 head 21 60 8 17 31
93
Linked Lists: Search Search for 8 head 21 60 8 17 31
94
Linked Lists: Search Search for 8 head 21 60 8 17 31
95
Linked Lists: Search Search for 8 head 21 60 8 17 31
96
Linked Lists: Search Search for 8 head 21 60 8 17 31
97
Linked Lists: Search Search for 8 head 21 60 8 17 31
98
Linked Lists: Search Search for 8 head 21 60 8 17 31
99
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?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.