├── 10 └── 1_fast_array │ └── main.c ├── 11 ├── 1_hash_function │ ├── hash_function.c │ ├── hash_function.h │ └── main.c ├── 2_tuple │ └── main.c └── 3_getter │ └── main.c ├── 12 └── 1_static_inline │ ├── adder.c │ ├── adder.h │ ├── inline_math.c │ ├── inline_math.h │ ├── main.c │ └── static_math.h ├── .gitignore ├── 01 ├── 1_hello_world │ └── main.c ├── 2_primitive_types │ └── main.c ├── 3_enum │ └── main.c ├── 4_switch_statement │ └── main.c └── 5_statements │ └── main.c ├── 02 ├── 1_global_variable │ └── main.c ├── 2_goto_statement │ └── main.c ├── 3_header_and_source_file │ ├── main.c │ └── wallet.h ├── 4_array │ └── main.c ├── 5_multi_dimensional_array │ └── main.c ├── 6_extern_keyword │ ├── main.c │ ├── minion.c │ └── minion.h └── 7_static_keyword │ ├── bank_account.c │ ├── bank_account.h │ └── main.c ├── 03 ├── 1_pointer │ ├── algorithm.c │ ├── algorithm.h │ └── main.c ├── 2_memory_viewer │ └── main.c ├── 3_is_array_overlap │ ├── main.c │ ├── memory.c │ └── memory.h ├── 4_const_pointer │ ├── main.c │ ├── vector.c │ └── vector.h └── 5_pointer_array │ └── main.c ├── 04 ├── 1_string_case_insensitive_compare │ ├── main.c │ ├── string_utils.c │ └── string_utils.h ├── 2_print_string_buffer │ ├── buffered_print.c │ ├── buffered_print.h │ └── main.c ├── 3_string_toupper_tolower │ ├── main.c │ ├── string_utils.c │ └── string_utils.h ├── 4_draw_ascii │ ├── main.c │ ├── print_ascii_table.c │ └── print_ascii_table.h └── 5_formatting │ ├── byte_conversion_chart.c │ ├── byte_conversion_chart.h │ └── main.c ├── 05 ├── 1_whitespace_counter │ ├── main.c │ ├── whitespace_counter.c │ └── whitespace_counter.h ├── 2_match_history │ ├── main.c │ ├── match_history_io.c │ └── match_history_io.h ├── 3_file_copy │ ├── file_utils.c │ ├── file_utils.h │ ├── main.c │ └── src.txt ├── 4_repeat_mark │ ├── file_utils.c │ ├── file_utils.h │ ├── main.c │ └── text.txt └── 5_real_file_copy │ ├── file_utils.c │ ├── file_utils.h │ ├── main.c │ └── src.txt ├── 06 ├── 1_point_line_rectangle │ ├── main.c │ ├── shape.c │ └── shape.h ├── 2_color │ ├── color.h │ └── main.c ├── 3_callback_function │ ├── error_handler.c │ ├── error_handler.h │ └── main.c ├── 4_quicksort_with_struct │ ├── main.c │ ├── user_sorter.c │ ├── user_sorter.h │ └── userdata.h └── 5_radixsort │ ├── main.c │ └── userdata.h ├── 07 └── 1_simple_printf │ ├── main.c │ ├── simpleio.c │ └── simpleio.h ├── 09 └── 1_sort_words │ ├── main.c │ ├── string_comparer.c │ └── string_comparer.h ├── CODEOWNERS └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /01/1_hello_world/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | printf("Hello, world!\n"); 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /01/2_primitive_types/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int int_value = 10; 6 | long long_value = 100; 7 | float float_value = 1.5f; 8 | char char_value = 'A'; 9 | 10 | printf("int_value: %d\n", int_value); 11 | printf("long_value: %ld\n", long_value); 12 | printf("float_value: %f\n", float_value); 13 | printf("char_value: %c\n", char_value); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /01/3_enum/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | enum champ { 4 | CHAMP_ZED, 5 | CHAMP_JAX, 6 | CHAMP_VAYNE, 7 | CHAMP_LULU, 8 | CHAMP_LEESIN 9 | }; 10 | 11 | enum role { 12 | ROLE_TOP, 13 | ROLE_MID, 14 | ROLE_JUNGLE, 15 | ROLE_BOTTOM, 16 | ROLE_SUPPORTER 17 | }; 18 | 19 | int main(void) 20 | { 21 | enum champ my_champ = CHAMP_VAYNE; 22 | enum role my_role = ROLE_BOTTOM; 23 | 24 | printf("my_champ: %d\n", my_champ); 25 | printf("my_role: %d\n", my_role); 26 | printf("\n"); 27 | 28 | my_role = CHAMP_LEESIN; 29 | printf("my_role: %d\n", my_role); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /01/4_switch_statement/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int x = 3; 6 | switch (x) { 7 | case 0: 8 | printf("Case 0\n"); 9 | break; 10 | case 1: 11 | printf("Case 1\n"); 12 | break; 13 | case 2: 14 | printf("Case 2\n"); 15 | break; 16 | case 3: 17 | printf("Case 3\n"); 18 | /* intentional fallthrough */ 19 | default: 20 | printf("Default case\n"); 21 | break; 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /01/5_statements/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int counter = 5; 6 | while (counter >= 0) { 7 | if (counter) { 8 | printf("Hello beautiful world\n"); 9 | } else { 10 | printf("Goodbye cruel world\n"); 11 | } 12 | --counter; 13 | } 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /02/1_global_variable/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int g_x = 100; 4 | 5 | void increase_number(void) 6 | { 7 | int y = 200; 8 | y += 100; 9 | g_x += 100; 10 | } 11 | 12 | int main(void) 13 | { 14 | printf("g_x: %d\n", g_x); /* 100 */ 15 | increase_number(); 16 | printf("g_x: %d\n", g_x); /* 200 */ 17 | /* below does not compile */ 18 | /*printf("y: %d\n", y);*/ 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /02/2_goto_statement/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void calculate_factorial(const unsigned int n) 4 | { 5 | unsigned int result = 1; 6 | unsigned int i = 1; 7 | 8 | goto begin_loop; 9 | 10 | /* this is a bad practice */ 11 | exit_loop: 12 | printf("%d factorial: %d\n", n, result); 13 | return; 14 | 15 | begin_loop: 16 | while (i <= n) { 17 | result *= i; 18 | ++i; 19 | } 20 | 21 | goto exit_loop; 22 | } 23 | 24 | void find_number(int arr[3][3], const int n) 25 | { 26 | size_t i; 27 | size_t j; 28 | int default_matrix[4][4] = { 29 | { 4, 1, 3, 1 }, 30 | { 9, 2, -1, 6 }, 31 | { 6, 0, 10, 5 }, 32 | { 1, 2, 3, 3 }, 33 | }; 34 | 35 | for (i = 0; i < 3; ++i) { 36 | for (j = 0; j < 3; ++j) { 37 | if (arr[i][j] == n) { 38 | printf("%d found in arr.", n); 39 | goto found; 40 | } 41 | } 42 | } 43 | 44 | for (i = 0; i < 4; ++i) { 45 | for (j = 0; j < 4; ++j) { 46 | if (default_matrix[i][j] == n) { 47 | printf("%d found in default_matrix.", n); 48 | goto found; 49 | } 50 | } 51 | } 52 | 53 | found: 54 | printf("Number: %d\n", n); 55 | printf("Index positions: (%zu, %zu)\n", i, j); 56 | } 57 | 58 | int main(void) 59 | { 60 | int square_matrix[3][3] = { 61 | { 0, 1, 2 }, 62 | { 3, 4, 5 }, 63 | { 6, 7, 8 } 64 | }; 65 | 66 | calculate_factorial(4); 67 | find_number(square_matrix, 0); 68 | find_number(square_matrix, -1); 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /02/3_header_and_source_file/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "wallet.h" 4 | 5 | unsigned int g_dollars = 0u; 6 | unsigned int g_cents = 0u; 7 | 8 | int main(void) 9 | { 10 | printf("g_dollars: %d\n", g_dollars); /* 0 */ 11 | printf("g_cents: %d\n", g_cents); /* 0 */ 12 | 13 | deposit(10u, 25u); 14 | 15 | printf("g_dollars: %d\n", g_dollars); /* 10 */ 16 | printf("g_cents: %d\n", g_cents); /* 25 */ 17 | 18 | withdraw(10u, 25u); 19 | 20 | printf("g_dollars: %d\n", g_dollars); /* 0 */ 21 | printf("g_cents: %d\n", g_cents); /* 0 */ 22 | 23 | return 0; 24 | } 25 | 26 | void deposit(unsigned int dollars, unsigned int cents) 27 | { 28 | g_dollars += dollars; 29 | g_cents += cents; 30 | } 31 | 32 | void withdraw(unsigned int dollars, unsigned int cents) 33 | { 34 | if (g_dollars >= dollars) { 35 | g_dollars -= dollars; 36 | } 37 | if (g_cents >= cents) { 38 | g_cents -= cents; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /02/3_header_and_source_file/wallet.h: -------------------------------------------------------------------------------- 1 | #ifndef WALLET_H 2 | #define WALLET_H 3 | 4 | void deposit(unsigned int dollars, unsigned int cents); 5 | void withdraw(unsigned int dollars, unsigned int cents); 6 | 7 | #endif /* WALLET_H */ 8 | -------------------------------------------------------------------------------- /02/4_array/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int grades[5] = { 70, 60, 85, 55, 90 }; 6 | int average = 0; 7 | int i; 8 | 9 | for (i = 0; i < 5; ++i) { 10 | average += grades[i]; 11 | } 12 | average /= i; 13 | 14 | printf("Average: %d\n", average); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /02/5_multi_dimensional_array/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int grades[2][3] = { 6 | { 65, 90, 75 }, 7 | { 70, 40, 50 } 8 | }; 9 | int average = 0; 10 | int i; 11 | int j; 12 | 13 | for (i = 0; i < 2; ++i) { 14 | for (j = 0; j < 3; ++j) { 15 | average += grades[i][j]; 16 | } 17 | } 18 | average /= (i * j); 19 | 20 | printf("Average: %d\n", average); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /02/6_extern_keyword/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "minion.h" 4 | 5 | extern unsigned int g_hp; 6 | extern unsigned int g_strength; 7 | 8 | int main(void) 9 | { 10 | printf("Normal minion:\n"); 11 | printf("hp: %u\n", g_hp); /* 100 */ 12 | printf("strength: %u\n", g_strength); /* 10 */ 13 | /* this does not compile */ 14 | /*printf("gold: %u\n", g_gold);*/ /* 15 */ 15 | 16 | printf("\n"); 17 | 18 | go_berserk(); 19 | add_gold(10u); 20 | 21 | printf("Berserk minion:\n"); 22 | printf("hp: %u\n", g_hp); /* 150 */ 23 | printf("strength: %u\n", g_strength); /* 30 */ 24 | /* this does not compile */ 25 | /*printf("gold: %u\n", g_gold);*/ /* 25 */ 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /02/6_extern_keyword/minion.c: -------------------------------------------------------------------------------- 1 | #include "minion.h" 2 | 3 | unsigned int g_hp = 100u; 4 | unsigned int g_strength = 10u; 5 | unsigned int g_gold = 15u; 6 | 7 | void go_berserk(void) 8 | { 9 | g_hp += 50u; 10 | g_strength += 20u; 11 | } 12 | 13 | void add_gold(unsigned int gold) 14 | { 15 | g_gold += gold; 16 | } 17 | -------------------------------------------------------------------------------- /02/6_extern_keyword/minion.h: -------------------------------------------------------------------------------- 1 | #ifndef MINION_H 2 | #define MINION_H 3 | 4 | void go_berserk(void); 5 | void add_gold(unsigned int gold); 6 | 7 | #endif /* MINION_H */ 8 | -------------------------------------------------------------------------------- /02/7_static_keyword/bank_account.c: -------------------------------------------------------------------------------- 1 | #include "bank_account.h" 2 | 3 | unsigned int g_chequing = 0u; 4 | unsigned int g_saving = 0u; 5 | static unsigned int s_fee = 20u; 6 | 7 | void deposit_into_chequing(const unsigned int amount) 8 | { 9 | /* this could underflow */ 10 | g_chequing += (amount - s_fee); 11 | } 12 | 13 | void deposit_into_saving(const unsigned int amount) 14 | { 15 | /* this could underflow */ 16 | g_saving += (amount - s_fee); 17 | } 18 | 19 | static void set_fee(const unsigned int fee) 20 | { 21 | s_fee = fee; 22 | } 23 | -------------------------------------------------------------------------------- /02/7_static_keyword/bank_account.h: -------------------------------------------------------------------------------- 1 | #ifndef BANK_ACCOUNT_H 2 | #define BANK_ACCOUNT_H 3 | 4 | void deposit_into_chequing(const unsigned int amount); 5 | void deposit_into_saving(const unsigned int amount); 6 | 7 | #endif /* BANK_ACCOUNT_H */ 8 | -------------------------------------------------------------------------------- /02/7_static_keyword/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "bank_account.h" 4 | 5 | extern unsigned int g_chequing; 6 | extern unsigned int g_saving; 7 | extern unsigned int s_fee; 8 | extern void set_fee(const unsigned int fee); 9 | 10 | int main(void) 11 | { 12 | printf("g_chequing: %u\n", g_chequing); /* 0 */ 13 | printf("s_saving: %u\n", g_saving); /* 0 */ 14 | /* this is a linker error */ 15 | /*printf("s_fee: %u", s_fee);*/ /* 20 */ 16 | 17 | deposit_into_chequing(300u); 18 | deposit_into_saving(700u); 19 | /* this is a linker error */ 20 | /*set_fee(100);*/ 21 | 22 | printf("g_chequing: %u\n", g_chequing); /* 280 */ 23 | printf("s_saving: %u\n", g_saving); /* 680 */ 24 | /* this a linker error */ 25 | /*printf("s_fee: %u", s_fee);*/ /* 100 */ 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /03/1_pointer/algorithm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "algorithm.h" 4 | 5 | /* pre-condition: length >= 1 */ 6 | void get_min_max(const int nums[], const size_t length, int* out_min, int* out_max) 7 | { 8 | size_t i; 9 | 10 | assert(length >= 1); 11 | 12 | *out_min = nums[0]; 13 | *out_max = nums[0]; 14 | for (i = 1; i < length; ++i) { 15 | if (*out_min > nums[i]) { 16 | *out_min = nums[i]; 17 | } 18 | if (*out_max < nums[i]) { 19 | *out_max = nums[i]; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /03/1_pointer/algorithm.h: -------------------------------------------------------------------------------- 1 | #ifndef ALGORITHM_H 2 | #define ALGORITHM_H 3 | 4 | void get_min_max(const int nums[], const size_t length, int* out_min, int* out_max); 5 | 6 | #endif /* ALGORITHM_H */ 7 | -------------------------------------------------------------------------------- /03/1_pointer/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "algorithm.h" 4 | 5 | #define LENGTH (5) 6 | 7 | int main(void) 8 | { 9 | const int nums[LENGTH] = { 3, 10, 9, 8, 7 }; 10 | int min; 11 | int max; 12 | 13 | get_min_max(nums, LENGTH, &min, &max); 14 | 15 | printf("min: %d\n", min); 16 | printf("max: %d\n", max); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /03/2_memory_viewer/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | const int NUM = 0x12345678; 6 | const char* NUM_ADDRESS = (char*)&NUM; 7 | size_t i; 8 | 9 | for (i = 0; i < sizeof(NUM); ++i) { 10 | printf("%hhx ", *(NUM_ADDRESS + i)); 11 | } 12 | printf("\n"); 13 | 14 | printf("NUM in hex form: 0x%x", NUM); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /03/3_is_array_overlap/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "memory.h" 4 | 5 | int main(void) 6 | { 7 | int nums1[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 8 | int nums2[5] = { 1, 2, 8, 9, 10 }; 9 | int* nums3 = nums1 + 2; 10 | const size_t NUMS3_LENGTH = 5u; 11 | char* result = NULL; 12 | 13 | result = is_overlap(nums1, ARRAY_LENGTH(nums1), nums2, ARRAY_LENGTH(nums2)) 14 | ? "Yes" : "No"; 15 | printf("Are nums1 and nums2 overlapped?: %s\n", result); 16 | 17 | result = is_overlap(nums1, ARRAY_LENGTH(nums1), nums3, NUMS3_LENGTH) 18 | ? "Yes" : "No"; 19 | printf("Are nums1 and nums3 overlapped?: %s\n", result); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /03/3_is_array_overlap/memory.c: -------------------------------------------------------------------------------- 1 | #include "memory.h" 2 | 3 | int is_overlap(int nums1[], const size_t length1, int nums2[], const size_t length2) 4 | { 5 | return (nums1 <= nums2 6 | ? nums1 + length1 > nums2 7 | : nums2 + length2 > nums1); 8 | } 9 | -------------------------------------------------------------------------------- /03/3_is_array_overlap/memory.h: -------------------------------------------------------------------------------- 1 | #ifndef MEMORY_H 2 | #define MEMORY_H 3 | 4 | #define TRUE (1) 5 | #define FALSE (0) 6 | 7 | #define ARRAY_LENGTH(arr) (sizeof(arr) / sizeof(arr[0])) 8 | 9 | int is_overlap(int nums1[], const size_t length1, int nums2[], const size_t length2); 10 | 11 | #endif /* MEMORY_H */ 12 | -------------------------------------------------------------------------------- /03/4_const_pointer/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "vector.h" 4 | 5 | int main(void) 6 | { 7 | const int v1[VECTOR_LENGTH] = {1, 2, 3}; 8 | const int v2[VECTOR_LENGTH] = {1, 2, 8}; 9 | int v3[VECTOR_LENGTH]; 10 | 11 | add_vec3(v1, v2, v3); 12 | 13 | printf("v3: %d, %d, %d", v3[0], v3[1], v3[2]); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /03/4_const_pointer/vector.c: -------------------------------------------------------------------------------- 1 | #include "vector.h" 2 | 3 | void add_vec3(const int* v1, const int* v2, int* out_v3) 4 | { 5 | size_t i = 0; 6 | for (i = 0 ; i < VECTOR_LENGTH; ++i) { 7 | *out_v3++ = *v1++ + *v2++; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /03/4_const_pointer/vector.h: -------------------------------------------------------------------------------- 1 | #ifndef VECTOR_H 2 | #define VECTOR_H 3 | 4 | #define VECTOR_LENGTH (3) 5 | 6 | void add_vec3(const int* v1, const int* v2, int* out_v3); 7 | 8 | #endif /* VECTOR_H */ 9 | -------------------------------------------------------------------------------- /03/5_pointer_array/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | const int nums[3][5] = { 6 | { 35, 50, 65, 24, 71 }, 7 | { 52, 93, 30, 18, 83 }, 8 | { 72, 53, 41, 88, 69 } 9 | }; 10 | const int* nums2[3]; 11 | 12 | nums2[0] = nums[0]; 13 | nums2[1] = nums[1]; 14 | nums2[2] = nums[2]; 15 | 16 | printf("nums[0] address: %p\n", (void*)nums[0]); 17 | printf("nums[1] address: %p\n", (void*)nums[1]); 18 | printf("nums[2] address: %p\n", (void*)nums[2]); 19 | printf("nums[2]'s offset from nums[0]: %d\n", nums[2] - nums[0]); /* 10 */ 20 | printf("nums[1]'s offset from nums[0]: %d\n", nums[1] - nums[0]); /* 5 */ 21 | 22 | printf("\n"); 23 | 24 | printf("nums2[0] address: %p\n", (void*)&nums2[0]); 25 | printf("nums2[1] address: %p\n", (void*)&nums2[1]); 26 | printf("nums2[2] address: %p\n", (void*)&nums2[2]); 27 | printf("nums2[2]'s offset from nums2[0]: %d\n", &nums2[2] - &nums2[0]); /* 2 */ 28 | printf("nums2[1]'s offset from nums2[0]: %d\n", &nums2[1] - &nums2[0]); /* 1 */ 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /04/1_string_case_insensitive_compare/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "string_utils.h" 4 | 5 | int main(void) 6 | { 7 | int diff; 8 | 9 | diff = string_case_insensitive_compare("hello", "HELL"); 10 | printf("hello to HELL: %d\n", diff); /* 1 */ 11 | 12 | diff = string_case_insensitive_compare("hello", "yellow"); 13 | printf("hello to yellow: %d\n", diff); /* -1 */ 14 | 15 | diff = string_case_insensitive_compare("hello", "HELLO"); 16 | printf("hello to HELLO: %d\n", diff); /* 0 */ 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /04/1_string_case_insensitive_compare/string_utils.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "string_utils.h" 4 | 5 | int string_case_insensitive_compare(const char* str0, const char* str1) 6 | { 7 | int c1; 8 | int c2; 9 | 10 | c1 = tolower(*str0); 11 | c2 = tolower(*str1); 12 | 13 | while (c1 != '\0' && c1 == c2) { 14 | c1 = tolower(*++str0); 15 | c2 = tolower(*++str1); 16 | } 17 | 18 | if (c1 == c2) { 19 | return 0; 20 | } 21 | 22 | return c1 > c2 ? 1 : -1; 23 | } 24 | -------------------------------------------------------------------------------- /04/1_string_case_insensitive_compare/string_utils.h: -------------------------------------------------------------------------------- 1 | #ifndef STRING_UTILS_H 2 | #define STRING_UTILS_H 3 | 4 | int string_case_insensitive_compare(const char* str0, const char* str1); 5 | 6 | #endif /* STRING_UTILS_H */ 7 | -------------------------------------------------------------------------------- /04/2_print_string_buffer/buffered_print.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | 6 | #include "buffered_print.h" 7 | 8 | #define BUFFER_LENGTH (32) 9 | 10 | static size_t s_buffer_index = 0u; 11 | static char s_buffer[BUFFER_LENGTH]; 12 | 13 | void buffered_print(const char* src) 14 | { 15 | size_t num_left; 16 | const char* p = src; 17 | 18 | num_left = strlen(src); 19 | 20 | while (num_left > 0) { 21 | size_t copy_count = BUFFER_LENGTH - 1 - s_buffer_index; 22 | 23 | const int buffer_empty = s_buffer_index == 0; 24 | 25 | if (num_left < copy_count) { 26 | copy_count = num_left; 27 | } 28 | 29 | s_buffer_index += copy_count; 30 | num_left -= copy_count; 31 | 32 | if (buffer_empty) { 33 | strncpy(s_buffer, p, copy_count); 34 | s_buffer[s_buffer_index] = '\0'; 35 | } else { 36 | strncat(s_buffer, p, copy_count); 37 | } 38 | 39 | p += copy_count; 40 | 41 | if (s_buffer_index == BUFFER_LENGTH - 1) { 42 | printf("%s\n", s_buffer); 43 | s_buffer_index = 0; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /04/2_print_string_buffer/buffered_print.h: -------------------------------------------------------------------------------- 1 | #ifndef BUFFERED_PRINT_H 2 | #define BUFFERED_PRINT_H 3 | 4 | void buffered_print(const char* src); 5 | 6 | #endif /* BUFFERED_PRINT_H */ 7 | -------------------------------------------------------------------------------- /04/2_print_string_buffer/main.c: -------------------------------------------------------------------------------- 1 | #include "buffered_print.h" 2 | 3 | int main(void) 4 | { 5 | buffered_print("Hello, "); /* Hello, */ 6 | buffered_print("World. "); /* Hello, World. */ 7 | buffered_print("C is awesome! "); /* Hello, World. C is awesome! */ 8 | buffered_print("C# is awesome too! "); /* Hello, World. C is awesome! C# */ 9 | buffered_print("Is Java better? "); /* is awesome too! Is Java better? */ 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /04/3_string_toupper_tolower/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "string_utils.h" 4 | 5 | int main(void) 6 | { 7 | char str[15] = "Welcome to C"; 8 | 9 | printf("Is space alphabet?: %s\n", 10 | is_alpha(' ') ? "Yes" : "No"); /* No */ 11 | 12 | printf("m in uppercase: %c\n", to_upper('m')); /* M */ 13 | printf("W in lowercase: %c\n", to_lower('W')); /* w */ 14 | 15 | string_toupper(str); 16 | printf("Uppercase: %s\n", str); /* WELCOME TO C */ 17 | 18 | string_tolower(str); 19 | printf("Lowercase: %s\n", str); /* welcome to c */ 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /04/3_string_toupper_tolower/string_utils.c: -------------------------------------------------------------------------------- 1 | #include "string_utils.h" 2 | 3 | int is_alpha(int c) 4 | { 5 | return (c >= 'A' && c <= 'Z') 6 | || (c >= 'a' && c <= 'z'); 7 | } 8 | 9 | int to_upper(int c) 10 | { 11 | if (is_alpha(c)) { 12 | return c & ~0x20; 13 | } 14 | 15 | return c; 16 | } 17 | 18 | int to_lower(int c) 19 | { 20 | if (is_alpha(c)) { 21 | return c | 0x20; 22 | } 23 | 24 | return c; 25 | } 26 | 27 | void string_toupper(char* str) 28 | { 29 | while (*str != '\0') { 30 | *str = to_upper(*str); 31 | ++str; 32 | } 33 | } 34 | 35 | void string_tolower(char* str) 36 | { 37 | while (*str != '\0') { 38 | *str = to_lower(*str); 39 | ++str; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /04/3_string_toupper_tolower/string_utils.h: -------------------------------------------------------------------------------- 1 | #ifndef STRING_UTILS_H 2 | #define STRING_UTILS_H 3 | 4 | int is_alpha(int c); 5 | 6 | int to_upper(int c); 7 | int to_lower(int c); 8 | 9 | void string_toupper(char* str); 10 | void string_tolower(char* str); 11 | 12 | #endif /* STRING_UTILS_H */ 13 | -------------------------------------------------------------------------------- /04/4_draw_ascii/main.c: -------------------------------------------------------------------------------- 1 | #include "print_ascii_table.h" 2 | 3 | int main(void) 4 | { 5 | print_ascii_table(); 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /04/4_draw_ascii/print_ascii_table.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "print_ascii_table.h" 4 | 5 | void print_ascii_table(void) 6 | { 7 | const int MIN_ASCII = 32; 8 | const int MAX_ASCII = 126; 9 | const int NUM_CHARS = MAX_ASCII - MIN_ASCII + 1; 10 | const int NUM_COLS = 3; 11 | const int NUM_ROWS = (NUM_CHARS + NUM_COLS - 1) / NUM_COLS; 12 | 13 | int r; 14 | int ch; 15 | 16 | printf("Dec Hex Char\tDec Hex Char\tDec Hex Char\n"); 17 | 18 | for (r = 0; r < NUM_ROWS - 1; ++r) { 19 | ch = MIN_ASCII + r; 20 | printf("%03d %#X %c\t", ch, ch, ch); 21 | 22 | ch += NUM_ROWS; 23 | printf("%03d %#X %c\t", ch, ch, ch); 24 | 25 | ch += NUM_ROWS; 26 | printf("%03d %#X %c\n", ch, ch, ch); 27 | } 28 | 29 | /* last row doesn't have all columns */ 30 | for (ch = MIN_ASCII + r; ch <= MAX_ASCII; ch += NUM_ROWS) { 31 | printf("%03d %#X %c\t", ch, ch, ch); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /04/4_draw_ascii/print_ascii_table.h: -------------------------------------------------------------------------------- 1 | #ifndef PRINT_ASCII_TABLE_H 2 | #define PRINT_ASCII_TABLE_H 3 | 4 | void print_ascii_table(void); 5 | 6 | #endif /* PRINT_ASCII_TABLE_H */ 7 | -------------------------------------------------------------------------------- /04/5_formatting/byte_conversion_chart.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "byte_conversion_chart.h" 4 | 5 | #define SCALE (1024.0f) 6 | 7 | #define BYTE (1.0f) 8 | #define KB (SCALE * BYTE) 9 | #define MB (SCALE * KB) 10 | #define GB (SCALE * MB) 11 | #define TB (SCALE * GB) 12 | #define PB (SCALE * TB) 13 | 14 | #define LENGTH (6) 15 | 16 | static const char* const DATA_STORAGE_NAMES[LENGTH] = 17 | { "Byte", "Kilobyte", "Megabyte", "Gigabyte", "Terabyte", "Petabyte" }; 18 | 19 | void print_byte_conversion_chart(void) 20 | { 21 | double divisor = 1.0; 22 | size_t i; 23 | 24 | printf("%9s",""); 25 | 26 | for (i = 0; i < LENGTH; ++i) { 27 | printf("%17s ", DATA_STORAGE_NAMES[i]); 28 | } 29 | 30 | printf("\n%4s", ""); 31 | 32 | for (i = 0; i < LENGTH; ++i) { 33 | printf("%s %17.15f %17.12f %17.9f %17.6f %17.3f %17.0f\n", 34 | DATA_STORAGE_NAMES[i], BYTE / divisor, KB / divisor, 35 | MB / divisor, GB / divisor, TB / divisor, PB / divisor); 36 | 37 | divisor *= SCALE; 38 | } 39 | } 40 | 41 | void print_byte_conversion_chart_scientific(void) 42 | { 43 | double divisor = 1.0; 44 | size_t i; 45 | 46 | printf("%9s", ""); 47 | 48 | for (i = 0; i < LENGTH; ++i) { 49 | printf("%17s ", DATA_STORAGE_NAMES[i]); 50 | } 51 | 52 | printf("\n%4s", ""); 53 | 54 | for (i = 0; i < LENGTH; ++i) { 55 | printf("%s %17.2e %17.2e %17.2e %17.2e %17.2e %17.2e\n", 56 | DATA_STORAGE_NAMES[i], BYTE / divisor, KB / divisor, 57 | MB / divisor, GB / divisor, TB / divisor, PB / divisor); 58 | 59 | divisor *= SCALE; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /04/5_formatting/byte_conversion_chart.h: -------------------------------------------------------------------------------- 1 | #ifndef PRINT_DATA_UNITS_H 2 | #define PRINT_DATA_UNITS_H 3 | 4 | void print_byte_conversion_chart(void); 5 | void print_byte_conversion_chart_scientific(void); 6 | 7 | #endif /* PRINT_DATA_UNITS_H */ 8 | -------------------------------------------------------------------------------- /04/5_formatting/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "byte_conversion_chart.h" 4 | 5 | int main(void) 6 | { 7 | print_byte_conversion_chart(); 8 | 9 | printf("\n"); 10 | 11 | print_byte_conversion_chart_scientific(); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /05/1_whitespace_counter/main.c: -------------------------------------------------------------------------------- 1 | #include "whitespace_counter.h" 2 | 3 | int main(void) 4 | { 5 | print_whitespace_stat(); 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /05/1_whitespace_counter/whitespace_counter.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "whitespace_counter.h" 5 | 6 | void print_whitespace_stat(void) 7 | { 8 | int c; 9 | size_t num_whitespaces = 0u; 10 | size_t num_newlines = 0u; 11 | 12 | c = getchar(); 13 | while (c != EOF) { 14 | if (isspace(c)) { 15 | ++num_whitespaces; 16 | 17 | if (c == '\n') { 18 | ++num_newlines; 19 | } 20 | } 21 | 22 | c = getchar(); 23 | } 24 | 25 | printf("# whitespaces: %5d\n", num_whitespaces); 26 | printf("# new lines: %5d\n", num_newlines); 27 | } 28 | -------------------------------------------------------------------------------- /05/1_whitespace_counter/whitespace_counter.h: -------------------------------------------------------------------------------- 1 | #ifndef WHITESPACE_COUNTER_H 2 | #define WHITESPACE_COUNTER_H 3 | 4 | void print_whitespace_stat(void); 5 | 6 | #endif /* WHITESPACE_COUNTER_H */ 7 | -------------------------------------------------------------------------------- /05/2_match_history/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "match_history_io.h" 4 | 5 | #define MATCH_COUNT (5) 6 | #define NUM_CHAMPS (5) 7 | 8 | #define BUFFER_LENGTH (4096) 9 | 10 | int main(void) 11 | { 12 | const char* names[NUM_CHAMPS] = { 13 | "Akali", 14 | "Sylas", 15 | "Yasuo", 16 | "Leblanc", 17 | "Aatrox" 18 | }; 19 | const int wins[NUM_CHAMPS] = { 20 | 52, 55, 28, 34, 32 21 | }; 22 | const int losses[NUM_CHAMPS] = { 23 | 62, 38, 31, 21, 21 24 | }; 25 | const float average_kills[NUM_CHAMPS] = { 26 | 6.11f, 6.62f, 4.81f, 5.95f, 5.19f 27 | }; 28 | const float average_deaths[NUM_CHAMPS] = { 29 | 3.65f, 3.87f, 3.97f, 3.05f, 3.23f 30 | }; 31 | const float average_assists[NUM_CHAMPS] = { 32 | 4.63f, 6.68f, 4.47f, 5.25f, 6.02f 33 | }; 34 | 35 | char buffer[BUFFER_LENGTH]; 36 | 37 | write_match_history(buffer, BUFFER_LENGTH, 38 | names, wins, losses, 39 | average_kills, average_deaths, 40 | average_assists, 41 | NUM_CHAMPS); 42 | 43 | read_match_history(buffer); 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /05/2_match_history/match_history_io.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | 6 | #include "match_history_io.h" 7 | 8 | #define LINE_LENGTH (4096) 9 | 10 | void write_match_history(char* buffer, 11 | size_t buffer_size, const char* names[], 12 | const int wins[], const int losses[], 13 | const float kills[], const float deaths[], 14 | const float assists[], 15 | size_t count) 16 | { 17 | char line[LINE_LENGTH]; 18 | size_t i; 19 | size_t remaining_buffer_size; 20 | 21 | remaining_buffer_size = buffer_size - 1; 22 | buffer[0] = '\0'; 23 | 24 | for (i = 0; i < count; ++i) { 25 | size_t num_written; 26 | 27 | sprintf(line, "%8s %.2f %.2f %.2f %d %d\n", 28 | names[i], 29 | kills[i], deaths[i], assists[i], 30 | wins[i], losses[i]); 31 | 32 | num_written = strlen(line); 33 | if (num_written > remaining_buffer_size) { 34 | break; 35 | } 36 | 37 | strcpy(buffer, line); 38 | buffer += num_written; 39 | remaining_buffer_size -= num_written; 40 | } 41 | } 42 | 43 | void read_match_history(char* buffer) 44 | { 45 | const char* DELIM = "\n"; 46 | char* tokenizer; 47 | 48 | printf("%8s %7s %7s %7s %7s %6s %6s %9s\n", 49 | "Champ", "Kills", "Deaths", "Assists", 50 | "KDA", "Wins", "Losses", "Win Ratio"); 51 | 52 | tokenizer = strtok(buffer, DELIM); 53 | while (tokenizer != NULL) { 54 | char name[LINE_LENGTH]; 55 | float kills; 56 | float deaths; 57 | float assists; 58 | int wins; 59 | int losses; 60 | 61 | float kda; 62 | float win_ratio; 63 | 64 | if (sscanf(tokenizer, "%s %f %f %f %d %d", 65 | name, &kills, &deaths, &assists, 66 | &wins, &losses) != 6) { 67 | 68 | continue; 69 | }; 70 | 71 | kda = (kills + assists) / deaths; 72 | win_ratio = wins * 100.0f / (wins + losses); 73 | 74 | printf("%8s %7.2f %7.2f %7.2f %7.2f %6d %6d %8.2f%%\n", 75 | name, kills, deaths, assists, 76 | kda, wins, losses, win_ratio); 77 | 78 | tokenizer = strtok(NULL, DELIM); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /05/2_match_history/match_history_io.h: -------------------------------------------------------------------------------- 1 | #ifndef MATCH_HISTORY_IO_H 2 | #define MATCH_HISTORY_IO_H 3 | 4 | void write_match_history(char* buffer, size_t buffer_size, 5 | const char* names[], const int wins[], const int losses[], 6 | const float kills[], const float deaths[], const float assists[], 7 | size_t count); 8 | 9 | void read_match_history(char* buffer); 10 | 11 | #endif /* MATCH_HISTORY_IO_H */ 12 | -------------------------------------------------------------------------------- /05/3_file_copy/file_utils.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | 5 | #include "file_utils.h" 6 | 7 | void copy_file(const char* src, const char* dst) 8 | { 9 | FILE* src_file; 10 | FILE* dst_file; 11 | int c; 12 | 13 | src_file = fopen(src, "rb"); 14 | if (src_file == NULL) { 15 | perror("error while opening source file"); 16 | return; 17 | } 18 | 19 | dst_file = fopen(dst, "wb"); 20 | if (dst_file == NULL) { 21 | perror("error while creating target file"); 22 | goto close_source; 23 | } 24 | 25 | c = fgetc(src_file); 26 | while (c != EOF) { 27 | fputc(c, dst_file); 28 | c = fgetc(src_file); 29 | } 30 | 31 | if (fclose(dst_file) == EOF) { 32 | perror("error while closing target file"); 33 | } 34 | 35 | close_source: 36 | if (fclose(src_file) == EOF) { 37 | perror("error while closing source file"); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /05/3_file_copy/file_utils.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_UTILS_H 2 | #define FILE_UTILS_H 3 | 4 | void copy_file(const char* src, const char* dst); 5 | 6 | #endif /* FILE_UTILS_H */ 7 | -------------------------------------------------------------------------------- /05/3_file_copy/main.c: -------------------------------------------------------------------------------- 1 | #include "file_utils.h" 2 | 3 | int main(void) 4 | { 5 | copy_file("src.txt", "copy.txt"); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /05/3_file_copy/src.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/POCU/COMP2200CodeSamples/9dbe3d25deda0a8f3dd104316d88c4472f67cf3c/05/3_file_copy/src.txt -------------------------------------------------------------------------------- /05/4_repeat_mark/file_utils.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | 5 | #include "file_utils.h" 6 | 7 | #define TRUE (1) 8 | #define FALSE (0) 9 | 10 | void print_with_repeats(const char* filename) 11 | { 12 | long pos = -1L; 13 | int repeating = FALSE; 14 | int c; 15 | FILE* file; 16 | 17 | file = fopen(filename, "r"); 18 | if (file == NULL) { 19 | perror("error while opening"); 20 | return; 21 | } 22 | 23 | c = fgetc(file); 24 | while (c != EOF) { 25 | if (c != ':') { 26 | putchar(c); 27 | goto next_char; 28 | } 29 | 30 | if (!repeating) { 31 | if (pos < 0) { 32 | /* start mark */ 33 | pos = ftell(file); 34 | if (pos < 0) { 35 | perror("error while getting start position"); 36 | break; 37 | } 38 | } else { 39 | /* end mark */ 40 | repeating = TRUE; 41 | 42 | if (fseek(file, pos, SEEK_SET) != 0) { 43 | perror("error while fseek() to start position"); 44 | break; 45 | } 46 | } 47 | 48 | goto next_char; 49 | } 50 | 51 | /* revisited end mark */ 52 | repeating = FALSE; 53 | pos = -1; 54 | 55 | next_char: 56 | c = fgetc(file); 57 | } 58 | 59 | if (fclose(file) == EOF) { 60 | perror("error while closing"); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /05/4_repeat_mark/file_utils.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_UTILS_H 2 | #define FILE_UTILS_H 3 | 4 | void print_with_repeats(const char* filename); 5 | 6 | #endif /* FILE_UTILS_H */ 7 | -------------------------------------------------------------------------------- /05/4_repeat_mark/main.c: -------------------------------------------------------------------------------- 1 | #include "file_utils.h" 2 | 3 | int main(void) 4 | { 5 | print_with_repeats("text.txt"); 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /05/4_repeat_mark/text.txt: -------------------------------------------------------------------------------- 1 | Things you should study 2 | 1. Hello World! 3 | 2. C# 4 | 3. Java: 5 | 4. Math 6 | 5. C: 7 | 6. :Pointer: 8 | 7. How to pat a cat 9 | -------------------------------------------------------------------------------- /05/5_real_file_copy/file_utils.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | 5 | #include "file_utils.h" 6 | 7 | void copy_file(const char* src, const char* dst) 8 | { 9 | FILE* src_file; 10 | FILE* dst_file; 11 | int c; 12 | 13 | src_file = fopen(src, "rb"); 14 | if (src_file == NULL) { 15 | perror("error while opening source file"); 16 | return; 17 | } 18 | 19 | dst_file = fopen(dst, "wb"); 20 | if (dst_file == NULL) { 21 | perror("error while creating target file"); 22 | goto close_source; 23 | } 24 | 25 | c = fgetc(src_file); 26 | while (c != EOF) { 27 | fputc(c, dst_file); 28 | c = fgetc(src_file); 29 | } 30 | 31 | if (fclose(dst_file) == EOF) { 32 | perror("error while closing target file"); 33 | } 34 | 35 | close_source: 36 | if (fclose(src_file) == EOF) { 37 | perror("error while closing source file"); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /05/5_real_file_copy/file_utils.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_UTILS_H 2 | #define FILE_UTILS_H 3 | 4 | void copy_file(const char* src, const char* dst); 5 | 6 | #endif /* FILE_UTILS_H */ 7 | -------------------------------------------------------------------------------- /05/5_real_file_copy/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "file_utils.h" 5 | 6 | int main(int argc, const char* argv[]) 7 | { 8 | if (argc != 3) { 9 | FILE* out; 10 | 11 | if (argc == 2 && strcmp(argv[1], "--help") == 0) { 12 | out = stdout; 13 | } else { 14 | out = stderr; 15 | } 16 | 17 | fprintf(out, "+--------------------------------------+\n"); 18 | fprintf(out, "| New File Copier v1 +\n"); 19 | fprintf(out, "+--------------------------------------+\n"); 20 | fprintf(out, " usage: copy \n"); 21 | fprintf(out, " usage: copy --help to see help page"); 22 | 23 | if (out == stderr) { 24 | fprintf(out, "\nerror: invalid arguments\n"); 25 | } 26 | 27 | return 1; 28 | } 29 | 30 | copy_file(argv[1], argv[2]); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /05/5_real_file_copy/src.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/POCU/COMP2200CodeSamples/9dbe3d25deda0a8f3dd104316d88c4472f67cf3c/05/5_real_file_copy/src.txt -------------------------------------------------------------------------------- /06/1_point_line_rectangle/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "shape.h" 4 | 5 | int main(void) 6 | { 7 | point_t p0; 8 | point_t p1; 9 | line_t line0; 10 | line_t line1; 11 | rectangle_t rect; 12 | 13 | /* init points */ 14 | p0.x = 5; 15 | p0.y = 1; 16 | 17 | p1.x = -3; 18 | p1.y = 5; 19 | 20 | /* these 2 lines are reversed */ 21 | line0.start = p0; 22 | line0.end = p1; 23 | 24 | line1.start = p1; 25 | line1.end = p0; 26 | 27 | printf("line0 length^2: %d\n", 28 | get_line_length_sq(line0)); /* 80 */ 29 | 30 | printf("line1 length^2: %d\n", 31 | get_line_length_sq(line1)); /* 80 */ 32 | 33 | /* properly build rect by calling util func */ 34 | rect = build_rectangle(p0, p1); 35 | 36 | /* (-3, 1) (5, 5) */ 37 | printf("rect0: (%d, %d) (%d, %d)\n", 38 | rect.top_left.x, rect.top_left.y, 39 | rect.bottom_right.x, rect.bottom_right.y); 40 | 41 | rect = build_rectangle(p1, p0); 42 | printf("rect1: (%d, %d) (%d, %d)\n", 43 | rect.top_left.x, rect.top_left.y, 44 | rect.bottom_right.x, rect.bottom_right.y); 45 | 46 | printf("rect area: %d\n", 47 | get_rectangle_area(rect)); /* 32 */ 48 | 49 | /* improper rect build */ 50 | rect.top_left = p0; 51 | rect.bottom_right = p1; 52 | printf("rect area: %d\n", 53 | get_rectangle_area(rect)); /* 32 */ 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /06/1_point_line_rectangle/shape.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "shape.h" 4 | 5 | rectangle_t build_rectangle(point_t p0, point_t p1) 6 | { 7 | rectangle_t rect; 8 | 9 | if (p0.x < p1.x) { 10 | rect.top_left.x = p0.x; 11 | rect.bottom_right.x = p1.x; 12 | } else { 13 | rect.top_left.x = p1.x; 14 | rect.bottom_right.x = p0.x; 15 | } 16 | 17 | if (p0.y < p1.y) { 18 | rect.top_left.y = p0.y; 19 | rect.bottom_right.y = p1.y; 20 | } else { 21 | rect.top_left.y = p1.y; 22 | rect.bottom_right.y = p0.y; 23 | } 24 | 25 | return rect; 26 | } 27 | 28 | int get_line_length_sq(line_t line) 29 | { 30 | int x_diff; 31 | int y_diff; 32 | 33 | x_diff = line.end.x - line.start.x; 34 | y_diff = line.end.y - line.start.y; 35 | 36 | return x_diff * x_diff + y_diff * y_diff; 37 | } 38 | 39 | int get_rectangle_area(rectangle_t rect) 40 | { 41 | int w; 42 | int h; 43 | 44 | w = abs(rect.bottom_right.x - rect.top_left.x); 45 | h = abs(rect.bottom_right.y - rect.top_left.y); 46 | 47 | return w * h; 48 | } 49 | -------------------------------------------------------------------------------- /06/1_point_line_rectangle/shape.h: -------------------------------------------------------------------------------- 1 | #ifndef SHAPE_H 2 | #define SHAPE_H 3 | 4 | typedef struct { 5 | int x; 6 | int y; 7 | } point_t; 8 | 9 | typedef struct { 10 | point_t start; 11 | point_t end; 12 | } line_t; 13 | 14 | typedef struct { 15 | point_t top_left; 16 | point_t bottom_right; 17 | } rectangle_t; 18 | 19 | rectangle_t build_rectangle(point_t p0, point_t p1); 20 | int get_line_length_sq(line_t line); 21 | int get_rectangle_area(rectangle_t rect); 22 | 23 | #endif /* SHAPE_H */ 24 | -------------------------------------------------------------------------------- /06/2_color/color.h: -------------------------------------------------------------------------------- 1 | #ifndef COLOR_H 2 | #define COLOR_H 3 | 4 | typedef union { 5 | unsigned int val; 6 | 7 | struct { 8 | unsigned char r; 9 | unsigned char g; 10 | unsigned char b; 11 | unsigned char a; 12 | } rgba; 13 | } color_t; 14 | 15 | #endif /* COLOR_H */ 16 | -------------------------------------------------------------------------------- /06/2_color/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "color.h" 4 | 5 | int main(void) 6 | { 7 | color_t trans_black; 8 | color_t red; 9 | color_t yellow; 10 | 11 | trans_black.val = 0x0; 12 | 13 | red.val = 0; 14 | red.rgba.r = 255; 15 | red.rgba.a = 255; 16 | 17 | yellow = red; 18 | yellow.rgba.g = 255; 19 | 20 | printf("size: %d\n", sizeof(color_t)); 21 | printf(" black: 0x%08x(%3d, %3d, %3d, %3d)\n", 22 | trans_black.val, 23 | trans_black.rgba.r, trans_black.rgba.g, 24 | trans_black.rgba.b, trans_black.rgba.a); 25 | 26 | printf(" red: 0x%08x(%3d, %3d, %3d, %3d)\n", 27 | red.val, 28 | red.rgba.r, red.rgba.g, 29 | red.rgba.b, red.rgba.a); 30 | 31 | printf("yellow: 0x%08x(%3d, %3d, %3d, %3d)\n", 32 | yellow.val, 33 | yellow.rgba.r, yellow.rgba.g, 34 | yellow.rgba.b, yellow.rgba.a); 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /06/3_callback_function/error_handler.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | 6 | #include "error_handler.h" 7 | 8 | static void (*s_handler)(const char*) = NULL; 9 | 10 | void register_error_handler(void (*handler)(const char* msg)) 11 | { 12 | s_handler = handler; 13 | } 14 | 15 | void log_error(const char* msg) 16 | { 17 | if (s_handler != NULL) { 18 | s_handler(msg); 19 | } 20 | } 21 | 22 | void default_error_handler(const char* msg) 23 | { 24 | time_t now; 25 | struct tm* local; 26 | 27 | now = time(NULL); 28 | 29 | local = localtime(&now); 30 | 31 | fprintf(stderr, "[%02d:%02d:%02d] %s\n", 32 | local->tm_hour, local->tm_min, local->tm_sec, 33 | msg); 34 | } 35 | -------------------------------------------------------------------------------- /06/3_callback_function/error_handler.h: -------------------------------------------------------------------------------- 1 | #ifndef ERROR_HANDLER_H 2 | #define ERROR_HANDLER_H 3 | 4 | void register_error_handler(void (*handler)(const char* msg)); 5 | void log_error(const char* msg); 6 | 7 | void default_error_handler(const char* msg); 8 | 9 | #endif /* ERROR_HANDLER_H */ 10 | -------------------------------------------------------------------------------- /06/3_callback_function/main.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | 5 | #include "error_handler.h" 6 | 7 | #define TRUE (1) 8 | #define FALSE (0) 9 | 10 | void simple_stderr_print(const char* msg); 11 | int run(void); 12 | 13 | int main(void) 14 | { 15 | int success; 16 | 17 | success = run(); 18 | 19 | register_error_handler(default_error_handler); 20 | if (run() == FALSE) { 21 | success = FALSE; 22 | } 23 | 24 | register_error_handler(simple_stderr_print); 25 | if (run() == FALSE) { 26 | success = FALSE; 27 | } 28 | 29 | return success ? 0 : 1; 30 | } 31 | 32 | int run(void) 33 | { 34 | int numerator; 35 | int denominator; 36 | 37 | while (TRUE) { 38 | printf("enter numerator: "); 39 | if (scanf("%d", &numerator) == 1) { 40 | break; 41 | } 42 | } 43 | 44 | while (TRUE) { 45 | printf("enter denominator: "); 46 | if (scanf("%d", &denominator) == 1 ) { 47 | break; 48 | } 49 | } 50 | 51 | if (denominator == 0) { 52 | log_error("cannot divide by zero"); 53 | return FALSE; 54 | } 55 | 56 | printf("%d / %d = %.2f\n", 57 | numerator, denominator, 58 | numerator / (float)denominator ); 59 | 60 | return TRUE; 61 | } 62 | 63 | void simple_stderr_print(const char* msg) 64 | { 65 | fputs(msg, stderr); 66 | } 67 | -------------------------------------------------------------------------------- /06/4_quicksort_with_struct/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "userdata.h" 6 | #include "user_sorter.h" 7 | 8 | enum { NUM_USERS = 7 }; 9 | 10 | int main(void) 11 | { 12 | userdata_t users[NUM_USERS]; 13 | size_t i; 14 | 15 | i = 0u; 16 | users[i].id = 482; 17 | users[i].sex = SEX_FEMALE; 18 | users[i++].age = 102; 19 | 20 | users[i].id = 510; 21 | users[i].sex = SEX_MALE; 22 | users[i++].age = 22; 23 | 24 | users[i].id = 32; 25 | users[i].sex = SEX_UNKNOWN; 26 | users[i++].age = 1; 27 | 28 | users[i].id = 221; 29 | users[i].sex = SEX_FEMALE; 30 | users[i++].age = 38; 31 | 32 | users[i].id = 15; 33 | users[i].sex = SEX_FEMALE; 34 | users[i++].age = 22; 35 | 36 | users[i].id = 333; 37 | users[i].sex = SEX_MALE; 38 | users[i++].age = 1; 39 | 40 | users[i].id = 1024; 41 | users[i].sex = SEX_UNKNOWN; 42 | users[i++].age = 52; 43 | 44 | assert(i == NUM_USERS); 45 | 46 | puts("== sort by age, id =="); 47 | 48 | qsort(users, NUM_USERS, sizeof(userdata_t), compare_age_id); 49 | for (i = 0; i < NUM_USERS; ++i) { 50 | printf("age: %3d, id: %5d, sex: %d\n", 51 | users[i].age, users[i].id, users[i].sex); 52 | } 53 | 54 | puts("\n== sort by age(desc), sex =="); 55 | 56 | qsort(users, NUM_USERS, sizeof(userdata_t), compare_age_desc_sex); 57 | for (i = 0; i < NUM_USERS; ++i) { 58 | printf("age: %3d, sex: %d, id: %d\n", 59 | users[i].age, users[i].sex, users[i].id); 60 | } 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /06/4_quicksort_with_struct/user_sorter.c: -------------------------------------------------------------------------------- 1 | #include "userdata.h" 2 | #include "user_sorter.h" 3 | 4 | int compare_age_id(const void* p0, const void* p1) 5 | { 6 | const userdata_t* user0 = p0; 7 | const userdata_t* user1 = p1; 8 | 9 | if (user0->age == user1->age) { 10 | return user0->id - user1->id; 11 | } 12 | 13 | return user0->age - user1->age; 14 | } 15 | 16 | int compare_age_desc_sex(const void* p0, const void* p1) 17 | { 18 | const userdata_t* user0 = p0; 19 | const userdata_t* user1 = p1; 20 | 21 | if (user0->age == user1->age) { 22 | return user0->sex - user1->sex; 23 | } 24 | 25 | return user1->age - user0->age; 26 | } 27 | -------------------------------------------------------------------------------- /06/4_quicksort_with_struct/user_sorter.h: -------------------------------------------------------------------------------- 1 | #ifndef USER_SORTER_H 2 | #define USER_SORTER_H 3 | 4 | int compare_age_id(const void* p0, const void* p1); 5 | int compare_age_desc_sex(const void* p0, const void* p1); 6 | 7 | #endif /* USER_SORTER_H */ 8 | -------------------------------------------------------------------------------- /06/4_quicksort_with_struct/userdata.h: -------------------------------------------------------------------------------- 1 | #ifndef USERDATA_H 2 | #define USERDATA_H 3 | 4 | typedef enum { 5 | SEX_MALE, 6 | SEX_FEMALE, 7 | SEX_UNKNOWN 8 | } sex_t; 9 | 10 | typedef struct { 11 | unsigned short id; 12 | unsigned char age; 13 | sex_t sex; 14 | } userdata_t; 15 | 16 | #endif /* USERDATA_H */ 17 | -------------------------------------------------------------------------------- /06/5_radixsort/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "userdata.h" 6 | 7 | enum { NUM_USERS = 7 }; 8 | 9 | int compare_uint(const void* p0, const void* p1); 10 | 11 | int main(void) 12 | { 13 | radix_userdata_t users[NUM_USERS]; 14 | size_t i; 15 | 16 | i = 0u; 17 | users[i].user.id = 482; 18 | users[i].user.sex = SEX_FEMALE; 19 | users[i++].user.age = 102; 20 | 21 | users[i].user.id = 510; 22 | users[i].user.sex = SEX_MALE; 23 | users[i++].user.age = 22; 24 | 25 | users[i].user.id = 32; 26 | users[i].user.sex = SEX_UNKNOWN; 27 | users[i++].user.age = 1; 28 | 29 | users[i].user.id = 221; 30 | users[i].user.sex = SEX_FEMALE; 31 | users[i++].user.age = 38; 32 | 33 | users[i].user.id = 15; 34 | users[i].user.sex = SEX_FEMALE; 35 | users[i++].user.age = 22; 36 | 37 | users[i].user.id = 333; 38 | users[i].user.sex = SEX_MALE; 39 | users[i++].user.age = 1; 40 | 41 | users[i].user.id = 1024; 42 | users[i].user.sex = SEX_UNKNOWN; 43 | users[i++].user.age = 52; 44 | 45 | assert(i == NUM_USERS); 46 | 47 | puts("== sort by age, id =="); 48 | for (i = 0; i < NUM_USERS; ++i) { 49 | unsigned char age = users[i].user.age; 50 | unsigned short id = users[i].user.id; 51 | 52 | users[i].sort_key = age << 16 | id; 53 | } 54 | 55 | qsort(users, NUM_USERS, sizeof(radix_userdata_t), compare_uint); 56 | for (i = 0; i < NUM_USERS; ++i) { 57 | printf("age: %3d, id: %5d, sex: %d\n", 58 | users[i].user.age, 59 | users[i].user.id, 60 | users[i].user.sex); 61 | } 62 | 63 | puts("\n== sort by age(desc), sex =="); 64 | assert(SEX_UNKNOWN < (1 << 2)); 65 | 66 | for (i = 0; i < NUM_USERS; ++i) { 67 | unsigned char inv_age = 255 - users[i].user.age; 68 | sex_t sex = users[i].user.sex; 69 | 70 | users[i].sort_key = inv_age << 2 | sex; 71 | } 72 | 73 | qsort(users, NUM_USERS, sizeof(radix_userdata_t), compare_uint); 74 | for (i = 0; i < NUM_USERS; ++i) { 75 | printf("age: %3d, sex: %d, id: %d\n", 76 | users[i].user.age, 77 | users[i].user.sex, 78 | users[i].user.id); 79 | } 80 | 81 | return 0; 82 | } 83 | 84 | int compare_uint(const void* p0, const void* p1) 85 | { 86 | const unsigned int* i0 = p0; 87 | const unsigned int* i1 = p1; 88 | 89 | return *i0 - *i1; 90 | } 91 | -------------------------------------------------------------------------------- /06/5_radixsort/userdata.h: -------------------------------------------------------------------------------- 1 | #ifndef USERDATA_H 2 | #define USERDATA_H 3 | 4 | typedef enum { 5 | SEX_MALE, 6 | SEX_FEMALE, 7 | SEX_UNKNOWN 8 | } sex_t; 9 | 10 | typedef struct { 11 | unsigned short id; 12 | unsigned char age; 13 | sex_t sex; 14 | } userdata_t; 15 | 16 | typedef struct { 17 | unsigned int sort_key; 18 | userdata_t user; 19 | } radix_userdata_t; 20 | 21 | #endif /* USERDATA_H */ 22 | -------------------------------------------------------------------------------- /07/1_simple_printf/main.c: -------------------------------------------------------------------------------- 1 | #include "simpleio.h" 2 | 3 | int main(void) 4 | { 5 | printf_simple("c\n", 'A'); 6 | printf_simple("d\n", 10); 7 | printf_simple("s\n", "Hello"); 8 | 9 | printf_simple("\n"); 10 | printf_simple("c d s\n", 'A', 10, "Hello"); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /07/1_simple_printf/simpleio.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "simpleio.h" 6 | 7 | static void print_int_recursive(unsigned int val) 8 | { 9 | if (val == 0) { 10 | return; 11 | } 12 | 13 | print_int_recursive(val / 10); 14 | putchar('0' + val % 10); 15 | } 16 | 17 | static void print_int(unsigned int val) { 18 | if (val == 0) { 19 | putchar('0'); 20 | return; 21 | } 22 | 23 | print_int_recursive(val); 24 | } 25 | 26 | void printf_simple(const char* format, ...) 27 | { 28 | va_list ap; 29 | 30 | va_start(ap, format); 31 | { 32 | while (*format != '\0') { 33 | unsigned val; 34 | const char* str; 35 | 36 | switch (*format) { 37 | case 's': 38 | str = va_arg(ap, const char*); 39 | while (*str != '\0') { 40 | putchar(*str++); 41 | } 42 | break; 43 | case 'c': 44 | val = va_arg(ap, unsigned int); 45 | putchar(val); 46 | break; 47 | case 'd': 48 | val = va_arg(ap, unsigned int); 49 | print_int(val); 50 | break; 51 | default: 52 | putchar(*format); 53 | break; 54 | } 55 | 56 | ++format; 57 | } 58 | } 59 | va_end(ap); 60 | } 61 | -------------------------------------------------------------------------------- /07/1_simple_printf/simpleio.h: -------------------------------------------------------------------------------- 1 | #ifndef SIMPLEIO_H 2 | #define SIMPLEIO_H 3 | 4 | void printf_simple(const char* format, ...); 5 | 6 | #endif /* SIMPLEIO_H */ 7 | -------------------------------------------------------------------------------- /09/1_sort_words/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "string_comparer.h" 5 | 6 | enum { NUM_WORDS = 6 }; 7 | 8 | int main(void) 9 | { 10 | size_t i; 11 | const char* words[NUM_WORDS] = { 12 | "premium", "level", "cultured", 13 | "moaning", "skinny", "curve" 14 | }; 15 | 16 | puts("\n== sort ascending =="); 17 | 18 | qsort(words, NUM_WORDS, sizeof(const char*), compare_string); 19 | for (i = 0; i < NUM_WORDS; ++i) { 20 | printf("%s\n", words[i]); 21 | } 22 | 23 | puts("\n== sort descending =="); 24 | 25 | qsort(words, NUM_WORDS, sizeof(const char*), compare_string_desc); 26 | for (i = 0; i < NUM_WORDS; ++i) { 27 | printf("%s\n", words[i]); 28 | } 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /09/1_sort_words/string_comparer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "string_comparer.h" 3 | 4 | int compare_string(const void* a, const void* b) 5 | { 6 | const char** w0 = (const char**)a; 7 | const char** w1 = (const char**)b; 8 | 9 | return strcmp(*w0, *w1); 10 | } 11 | 12 | int compare_string_desc(const void* a, const void* b) 13 | { 14 | const char** w0 = (const char**)a; 15 | const char** w1 = (const char**)b; 16 | 17 | return strcmp(*w1, *w0); 18 | } 19 | -------------------------------------------------------------------------------- /09/1_sort_words/string_comparer.h: -------------------------------------------------------------------------------- 1 | #ifndef STRING_COMPARER_H 2 | #define STRING_COMPARER_H 3 | 4 | int compare_string(const void* a, const void* b); 5 | int compare_string_desc(const void* a, const void* b); 6 | 7 | #endif /* STRING_COMPARER_H */ 8 | -------------------------------------------------------------------------------- /10/1_fast_array/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | enum { MAX_NUMS = 8 }; 5 | enum { INVALID_INDEX = -1 }; 6 | 7 | int s_nums[MAX_NUMS]; 8 | size_t s_num_count = 0; 9 | 10 | void print_array(void) 11 | { 12 | size_t i; 13 | 14 | for (i = 0; i < s_num_count; ++i) { 15 | printf("%d ", s_nums[i]); 16 | } 17 | 18 | puts(""); 19 | } 20 | 21 | void insert_at(size_t index, int n) 22 | { 23 | size_t i; 24 | 25 | assert(index <= s_num_count); 26 | assert(s_num_count < MAX_NUMS); 27 | 28 | for (i = s_num_count; i > index; --i) { 29 | s_nums[i] = s_nums[i - 1]; 30 | } 31 | 32 | s_nums[index] = n; 33 | ++s_num_count; 34 | } 35 | 36 | size_t find_index(int n) 37 | { 38 | size_t i; 39 | 40 | for (i = 0; i < s_num_count; ++i) { 41 | if (s_nums[i] == n) { 42 | return i; 43 | } 44 | } 45 | 46 | return INVALID_INDEX; 47 | } 48 | 49 | void remove_at(size_t index) 50 | { 51 | size_t i; 52 | 53 | assert(index < s_num_count); 54 | 55 | --s_num_count; 56 | for (i = index; i < s_num_count; ++i) { 57 | s_nums[i] = s_nums[i + 1]; 58 | } 59 | } 60 | 61 | void remove_at_unordered(size_t index) 62 | { 63 | assert(index < s_num_count); 64 | 65 | s_nums[index] = s_nums[--s_num_count]; 66 | } 67 | 68 | int main(void) 69 | { 70 | insert_at(0, 0); 71 | insert_at(1, 1); 72 | insert_at(2, 2); 73 | insert_at(3, 3); 74 | insert_at(4, 4); 75 | insert_at(5, 5); 76 | 77 | /* 0 1 2 3 4 5 */ 78 | print_array(); 79 | 80 | remove_at(1); 81 | /* 0 2 3 4 5 */ 82 | print_array(); 83 | 84 | remove_at_unordered(1); 85 | /* 0 5 3 4 */ 86 | print_array(); 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /11/1_hash_function/hash_function.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "hash_function.h" 4 | 5 | size_t hash_int(int value) 6 | { 7 | return value; 8 | } 9 | 10 | size_t hash_float(float value) 11 | { 12 | return *((size_t*)&value); 13 | } 14 | 15 | size_t hash_data(const void* data, size_t length) 16 | { 17 | const char* p; 18 | size_t i; 19 | size_t hash; 20 | 21 | p = data; 22 | hash = 0; 23 | for (i = 0; i < length; ++i) { 24 | hash = 65599 * hash + *p++; 25 | } 26 | 27 | return hash ^ (hash >> 16); 28 | } 29 | -------------------------------------------------------------------------------- /11/1_hash_function/hash_function.h: -------------------------------------------------------------------------------- 1 | #ifndef HASH_FUNCTION_H 2 | #define HASH_FUNCTION_H 3 | 4 | size_t hash_int(int value); 5 | size_t hash_float(float value); 6 | size_t hash_data(const void* data, size_t length); 7 | 8 | #endif /* HASH_FUNCTION_H */ 9 | -------------------------------------------------------------------------------- /11/1_hash_function/main.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | 6 | #include "hash_function.h" 7 | 8 | typedef struct { 9 | unsigned char age; 10 | unsigned int id; 11 | char name[64]; 12 | } employee_t; 13 | 14 | int main(void) 15 | { 16 | employee_t person; 17 | size_t hash; 18 | float fvalue; 19 | 20 | hash = hash_int(10); 21 | printf("int %u\n", hash); 22 | 23 | hash = hash_int(-10); 24 | printf("int %u\n", hash); 25 | 26 | hash = hash_int('A'); 27 | printf("char %u\n", hash); 28 | 29 | hash = hash_float(3.2f); 30 | printf("float %u\n", hash); 31 | 32 | hash = hash_data("Pope Kim", strlen("Pope Kim")); 33 | printf("string %u\n", hash); 34 | 35 | fvalue = 3.2f; 36 | hash = hash_data(&fvalue, sizeof(float)); 37 | printf("float %u\n", hash); 38 | 39 | memset(&person, 0, sizeof(employee_t)); 40 | person.age = 23; 41 | person.id = 18274192; 42 | strcpy(person.name, "Pope Kim"); 43 | 44 | hash = hash_data(&person, sizeof(employee_t)); 45 | printf("struct %u\n", hash); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /11/2_tuple/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* id(int), "name"(const char*), hp(int) */ 4 | #define MONSTER_DATA \ 5 | MONSTER_ENTRY(0, "pope", 100) \ 6 | MONSTER_ENTRY(1, "big rat", 30) \ 7 | MONSTER_ENTRY(2, "mama", 255) \ 8 | MONSTER_ENTRY(3, "dragon", 300000)\ 9 | 10 | int main(void) 11 | { 12 | size_t i; 13 | 14 | int ids[] = { 15 | #define MONSTER_ENTRY(id, name, hp) id, 16 | MONSTER_DATA 17 | #undef MONSTER_ENTRY 18 | }; 19 | 20 | const char* names[] = { 21 | #define MONSTER_ENTRY(id, name, hp) name, 22 | MONSTER_DATA 23 | #undef MONSTER_ENTRY 24 | }; 25 | 26 | int healths[] = { 27 | #define MONSTER_ENTRY(id, name, hp) hp, 28 | MONSTER_DATA 29 | #undef MONSTER_ENTRY 30 | }; 31 | 32 | for (i = 0; i < sizeof(ids) / sizeof(int); ++i) { 33 | printf("%3d %6d %s\n", 34 | ids[i], healths[i], names[i]); 35 | } 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /11/3_getter/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* (type, name) */ 4 | #define MONSTER_STRUCT \ 5 | MONSTER_MEMBER(int, id) \ 6 | MONSTER_MEMBER(const char*, name) \ 7 | MONSTER_MEMBER(int, hp) \ 8 | 9 | typedef struct { 10 | #define MONSTER_MEMBER(type, name) type name; 11 | MONSTER_STRUCT 12 | #undef MONSTER_MEMBER 13 | } monster_t; 14 | 15 | #define MONSTER_MEMBER(type, name) \ 16 | type get_mob_##name(const monster_t* mob)\ 17 | { \ 18 | return mob->name; \ 19 | } \ 20 | 21 | MONSTER_STRUCT 22 | 23 | #undef MONSTER_MEMBER 24 | 25 | int main(void) 26 | { 27 | monster_t mob; 28 | mob.id = 0; 29 | mob.name = "Pope Mob"; 30 | mob.hp = 10001; 31 | 32 | printf("%3d %6d %s\n", 33 | get_mob_id(&mob), 34 | get_mob_hp(&mob), 35 | get_mob_name(&mob)); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /12/1_static_inline/adder.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "adder.h" 4 | #include "inline_math.h" 5 | #include "static_math.h" 6 | 7 | void run_adder(void) 8 | { 9 | printf("1 + 2 = %d\n", static_add(1, 2)); 10 | printf("addr of static_add: %p\n", 11 | (void*)static_add); 12 | 13 | printf("1 + 2 = %d\n", inline_add(1, 2)); 14 | printf("addr of inline_add: %p\n", 15 | (void*)inline_add); 16 | } 17 | -------------------------------------------------------------------------------- /12/1_static_inline/adder.h: -------------------------------------------------------------------------------- 1 | #ifndef ADDER_H 2 | #define ADDER_H 3 | 4 | void run_adder(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /12/1_static_inline/inline_math.c: -------------------------------------------------------------------------------- 1 | #include "inline_math.h" 2 | 3 | extern inline int inline_add(int a, int b); 4 | -------------------------------------------------------------------------------- /12/1_static_inline/inline_math.h: -------------------------------------------------------------------------------- 1 | #ifndef INLINE_MATH_H 2 | #define INLINE_MATH_H 3 | 4 | inline int inline_add(int a, int b) 5 | { 6 | return a + b; 7 | } 8 | 9 | #endif /* INLINE_MATH_H */ 10 | -------------------------------------------------------------------------------- /12/1_static_inline/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "adder.h" 4 | #include "inline_math.h" 5 | #include "static_math.h" 6 | 7 | int main(void) 8 | { 9 | printf("1 + 2 = %d\n", static_add(1, 2)); 10 | printf("addr of static_add: %p\n", 11 | (void*)static_add); 12 | 13 | printf("1 + 2 = %d\n", inline_add(1, 2)); 14 | printf("addr of inline_add: %p\n", 15 | (void*)inline_add); 16 | 17 | run_adder(); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /12/1_static_inline/static_math.h: -------------------------------------------------------------------------------- 1 | #ifndef STATIC_MATH_H 2 | #define STATIC_MATH_H 3 | 4 | static inline int static_add(int a, int b) 5 | { 6 | return a + b; 7 | } 8 | 9 | #endif /* STATIC_MATH_H */ 10 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @POCU/reviewers 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # COMP2200 C Code Samples 2 | C sample codes for COMP2200 course offered at POCU(https://pocu.academy) 3 | 4 | Web-based presentation with annotation is also available for enrolled users as well 5 | --------------------------------------------------------------------------------