├── .gitignore ├── 00_operators ├── add_2_integers.c ├── arithmetic.c ├── bitwise.c ├── comma_operator.c └── unary_operators.c ├── 01_if statement ├── if_conditions.c └── if_else_block.c ├── 02_input_output ├── infinite_read_and_write.c └── print_EOF.c ├── 03_switch statement └── count_grades.c ├── 04_loops ├── compound_interest.c ├── do_while.c ├── for_loop.c ├── test.c └── while_loop.c ├── 05_functions ├── exits │ └── exit.c ├── maths_func.c ├── power_function.c ├── random_numbers.c └── return_statement.c ├── 06_arrays ├── array_init.c ├── binary_search.c ├── bubble_sort.c ├── char_array.c ├── histogram.c ├── intro.c ├── linear_search.c ├── multi_dimensional_arrays.c ├── simple_array_sort.c ├── static_arrays.c ├── statistics.c └── strings.c ├── 07_recursion ├── factorial.c └── fibonnaci.c ├── 08_malloc ├── cisfun.c ├── free_mem.c ├── malloc_examples.c ├── segf.c └── while_malloc.c ├── 10_pointers ├── arithmetic.c ├── copy.c ├── intro.c └── size_of_array.c ├── README.md ├── __introduction ├── 0_welcome.c └── hello_world.c ├── alx-low_level_programming └── 0x0C-more_malloc_free │ ├── 0-malloc_checked.c │ └── 1-string_nconcat.c ├── c-programming.cms └── programs examples ├── Exercise1-9.c ├── celsius_to_farenheit_converter.c ├── count_blanks.c ├── crap_game.c ├── execises.c ├── final_velocity.c ├── shape_with_asterisks.c └── test_addition_of_int_to_char.c /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe -------------------------------------------------------------------------------- /00_operators/add_2_integers.c: -------------------------------------------------------------------------------- 1 | // program to add 2 integers in the C programming language 2 | #include 3 | 4 | // start of the main function 5 | int main(void) 6 | { 7 | printf("Starting the program here: \n"); 8 | 9 | int integer1, integer2; // first and second number to be entered by the user 10 | // int integer2; // second number to be entered by the user 11 | 12 | printf("Enter the first Integer:\n"); //prompt 13 | scanf("%d", &integer1); // read the integer 14 | 15 | printf("Enter the second Integer:\n"); //prompt 16 | scanf("%d", &integer2); // read the second integer 17 | 18 | int sum = integer1 + integer2; // assign the value of the total expression to sum 19 | printf("Sum is %d\n", sum); 20 | 21 | }// end of the main function -------------------------------------------------------------------------------- /00_operators/arithmetic.c: -------------------------------------------------------------------------------- 1 | /* a program that asks the user to enter two numbers, obtains them from 2 | the user and prints their sum, product, difference, quotient and remainder. */ 3 | #include 4 | 5 | /* function main starts program execution */ 6 | int main(void) { 7 | int num1, num2; // variables to store the 2 number for the arithmetic 8 | 9 | printf("Enter two numbers: "); // prompt 10 | scanf("%d%d", &num1, &num2); // read the two values into num1 and num2 respectively 11 | 12 | printf("Sum: %d\n", num1 + num2); 13 | printf("Product: %d\n", num1 * num2); // prints their sum 14 | printf("Difference: %d\n", num1 - num2); // prints their product 15 | printf("Quotient: %d\n", num1 / num2); // prints their quotient 16 | printf("Remainder: %d\n", num1 % num2); // prints their remainder 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /00_operators/bitwise.c: -------------------------------------------------------------------------------- 1 | /* program to study the bitwise operation of the c programming language */ 2 | #include 3 | 4 | /* function main begin program execution */ 5 | int main(void) 6 | { 7 | char b1 = 0b00000010; 8 | char b2 = 0b00000011; 9 | char b30, b31, b32; 10 | 11 | b30 = 0 & b2; /* bitwise OR*/ 12 | printf("the value of b30 is %d\n", b30); 13 | 14 | b31 = (b2 >> 2) & 0b00000001; 15 | printf("the value of b31 is %d\n", b31); 16 | 17 | b32 = (b2 >> 1) & 0b00000001; 18 | printf("the value of b32 is %d\n", b32); 19 | 20 | }/* end of the main function */ -------------------------------------------------------------------------------- /00_operators/comma_operator.c: -------------------------------------------------------------------------------- 1 | /* program to test the operation of comma in assignment expression */ 2 | #include 3 | 4 | /* function main starts program execution */ 5 | int main(void) 6 | { 7 | int a; 8 | a = 89, 1336; 9 | printf("%d\n", a); 10 | } -------------------------------------------------------------------------------- /00_operators/unary_operators.c: -------------------------------------------------------------------------------- 1 | /* demonstrate use of a unary operator to check the size of an type or varaible in memory */ 2 | #include 3 | 4 | /* function main starts program execution */ 5 | int main(void) 6 | { 7 | int a; /* declare a integer variable a*/ 8 | int size; /* declare a integer variable size */ 9 | 10 | a = 10000; 11 | size = sizeof(a); 12 | printf("The size of variable 'a' is %d Byte(s)\n", size); 13 | 14 | int int_size = sizeof(int); /* checks the size of the int type*/ 15 | int char_size = sizeof(char); /* checks the size of the char type*/ 16 | 17 | printf("The size of the 'int' type is %d Byte(s)\nThe size of the 'char' type is %d\n", int_size, char_size); 18 | 19 | }/* end of the main function */ -------------------------------------------------------------------------------- /01_if statement/if_conditions.c: -------------------------------------------------------------------------------- 1 | /* this program would make decision from a condition to execute part of the program */ 2 | #include 3 | 4 | /* function main begin program execution */ 5 | int main(void) 6 | { 7 | puts("Enter a value for the two integers and i will tell you"); 8 | printf("%s", "the relation they satisfy: "); 9 | 10 | int num1, num2; /* first and second number to be read from the user */ 11 | 12 | scanf("%d %d", &num1, &num2); /* read the two numbers from the user */ 13 | 14 | if (num1 == num2){ 15 | printf("%d is equal to %d\n", num1, num2); 16 | } /* end if */ 17 | 18 | if (num1 != num2) { 19 | printf( "%d is not equal to %d\n", num1, num2 ); 20 | } /* end if */ 21 | 22 | if (num1 < num2) { 23 | printf( "%d is less than %d\n", num1, num2 ); 24 | } /* end if */ 25 | 26 | if (num1 > num2) { 27 | printf( "%d is greater than %d\n", num1, num2 ); 28 | } /* end if */ 29 | 30 | if (num1 <= num2) { 31 | printf( "%d is less than or equal to %d\n", num1, num2 ); 32 | } /* end if */ 33 | 34 | if (num1 >= num2) { 35 | printf( "%d is greater than or equal to %d\n", num1, num2 ); 36 | } /* end if */ 37 | 38 | } /* end function main */ -------------------------------------------------------------------------------- /01_if statement/if_else_block.c: -------------------------------------------------------------------------------- 1 | /* program to display the decision making process with a if-else blocks and conditions to check against */ 2 | #include 3 | 4 | /* function main starts program execution */ 5 | int main(void) 6 | { 7 | int age; /*declare a variable to hold the age value */ 8 | 9 | printf("Enter your age: "); /* prompt */ 10 | scanf("%d", &age); /* gets the age value from the user*/ 11 | 12 | if (age > 18){ 13 | puts("Hurray! you are an Adults"); /* block to be executed if the if block condition is met*/ 14 | } 15 | else { 16 | puts("Awwwn! you are still a child"); /* block to be executed if the if block's condition is not satistied /*/ 17 | } 18 | 19 | 20 | }/* end of main function */ -------------------------------------------------------------------------------- /02_input_output/infinite_read_and_write.c: -------------------------------------------------------------------------------- 1 | /** continue to read and write a character to the standard input or output 2 | * provided the char is the the E.O.F char 3 | */ 4 | #include 5 | 6 | void main() 7 | { 8 | int c; 9 | 10 | c = getchar(); 11 | while (c != EOF) 12 | { 13 | putchar(c); 14 | c = getchar(); 15 | } 16 | } -------------------------------------------------------------------------------- /02_input_output/print_EOF.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Program to print the end of the filee 3 | * 4 | */ 5 | #include 6 | 7 | int main(void) 8 | { 9 | putchar(EOF); 10 | } -------------------------------------------------------------------------------- /03_switch statement/count_grades.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | unsigned int aCount = 0; 6 | unsigned int bCount = 0; 7 | unsigned int cCount = 0; 8 | unsigned int dCount = 0; 9 | unsigned int eCount = 0; 10 | unsigned int fCount = 0; 11 | 12 | puts("Enter the letter grades."); 13 | puts("Enter the EOF character to end input"); 14 | 15 | int grade; 16 | 17 | while ((grade = getchar()) != EOF) 18 | { 19 | // determine which grade was input 20 | switch (grade) 21 | { // switch nested in while 22 | case 'A': 23 | case 'a': 24 | ++aCount; 25 | break; // if the break is not added at the end of each case block , once a block is executed all the other blocks below will all be executed 26 | // the feature is called a fall through and is rarely ever useful, 27 | case 'B': 28 | case 'b': 29 | ++bCount; 30 | break; 31 | case 'C': 32 | case 'c': 33 | ++cCount; 34 | break; 35 | case 'D': 36 | case 'd': 37 | ++dCount; 38 | break; 39 | case 'E': 40 | case 'e': 41 | ++eCount; 42 | break; 43 | case 'F': 44 | case 'f': 45 | ++fCount; 46 | break; 47 | // Remember to provide processing capabilities for newline (and possibly other white-space) 48 | // characters in the input when processing characters one at a time. 49 | case '\n': 50 | case '\t': 51 | case ' ': 52 | break; 53 | default: 54 | printf("%s", "Incorrect letter entered!\n"); 55 | puts("Enter a new grade."); 56 | break; // optional, will exit switch anyway 57 | } 58 | } 59 | // output summary of results 60 | puts("\nTotals for each letter grade are:"); 61 | printf("A: %u\n", aCount); 62 | printf("B: %u\n", bCount); 63 | printf("C: %u\n", cCount); 64 | printf("D: %u\n", dCount); 65 | printf("F: %u\n", fCount); 66 | } -------------------------------------------------------------------------------- /04_loops/compound_interest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * Name: main 6 | * 7 | * Return: int 8 | */ 9 | int main(void) 10 | { 11 | double principal = 1000.0; 12 | double rate = .05; 13 | 14 | printf("%4s%21s\n", "Year", "Amount on deposit"); 15 | 16 | for (unsigned int year = 1; year <= 10; ++year) 17 | { 18 | double amount = principal * pow(1.0 + rate, year); 19 | 20 | printf("%4u%21.2f\n", year, amount); 21 | } 22 | } -------------------------------------------------------------------------------- /04_loops/do_while.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int counter = 0; 6 | 7 | do { 8 | printf("The value of the counter is %d\n", counter); 9 | } while (++counter <= 10); 10 | } -------------------------------------------------------------------------------- /04_loops/for_loop.c: -------------------------------------------------------------------------------- 1 | /* program to demonstrate the operation of the for loop in C programming language */ 2 | #include 3 | 4 | unsigned int even_sum_to(unsigned int limit); // declare a function that sum even numbers from 2 to a upper limit 5 | 6 | /* function main starts program execution */ 7 | int main(void) 8 | { 9 | int my_array[100]; // declare a array variable of the type int with a size of 100 10 | 11 | /* for statement contains 3 sections as outlined below 12 | * ( initialization section ; termination section ; increment section ) 13 | * 14 | * the initialization section initialzes the control variable (the variable that controls the execution of the for loop) 15 | * the termination section states an condition involving the control variable that the loop should be terminate 16 | * the increment section states an expression used to derive the next value for the control variable 17 | * (the step needed to proceed to the next iteration) 18 | */ 19 | 20 | for (unsigned int i = 1; i <= 100; ++i){ 21 | my_array[i] = 1337; 22 | } 23 | 24 | printf("The value of my_array is %s", my_array); 25 | 26 | }/* end of the main function */ 27 | 28 | 29 | 30 | unsigned int even_sum_to(unsigned int limit) 31 | { 32 | unsigned int sum; 33 | 34 | sum = 0; 35 | for (unsigned int i = 2; i <= limit; i += 2) 36 | { 37 | sum += i; 38 | } 39 | 40 | printf("The Sum of all even numbers between 2 and %u is %u", limit, sum); 41 | } -------------------------------------------------------------------------------- /04_loops/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n = 98; 6 | int *p = &n; 7 | 8 | printf("The value of n = %d\n", n); 9 | printf("The value of p = %d\n", p); 10 | 11 | *p++; 12 | 13 | printf("The value of n = %d\n", n); 14 | printf("The value of p = %d\n", p); 15 | 16 | } -------------------------------------------------------------------------------- /04_loops/while_loop.c: -------------------------------------------------------------------------------- 1 | /* program to display the syntax and use of the while loop in the c programming language */ 2 | #include 3 | 4 | /* function main starts program execution */ 5 | int main(void) 6 | { 7 | int a = 0; 8 | 9 | while (a < 100){ 10 | printf("A is currently = %d\n", a); 11 | a += 1; 12 | } 13 | }/* end of the main function */ -------------------------------------------------------------------------------- /05_functions/exits/exit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * tets_function - function used to explain exit process of the exit standard function 6 | * 7 | * Return: nothing 8 | */ 9 | void test_function(void) 10 | { 11 | printf("Starting the execution of the test function ...\n"); 12 | exit(1); // optionally the constant provided by the standard library can be used as the argument to the exit function 13 | // EXIT_SUCCESS and EXIT_FAILURE 14 | printf("This line should never be reached because of the exit function above\n"); 15 | } 16 | 17 | 18 | int main(void) 19 | { 20 | printf("Starting the program execution with the main function \n"); 21 | printf("About to call the test function \n"); 22 | 23 | test_function(); 24 | 25 | return (0); 26 | } -------------------------------------------------------------------------------- /05_functions/maths_func.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | 7 | int number; 8 | double power; 9 | double square_root; 10 | double cube_root; 11 | double exponential; 12 | double natural_log; 13 | double logarithm; 14 | double floating_abs; 15 | double float_mod; 16 | double sine; 17 | double cosine; 18 | double tangent; 19 | 20 | printf("Please Enter a number: "); 21 | scanf("%d", &number); 22 | 23 | power = pow(number, 2.0); 24 | square_root = sqrt(number); 25 | cube_root = cbrt(number); 26 | exponential = exp(number); 27 | natural_log = log(number); 28 | logarithm = log10(number); 29 | floating_abs = fabs(number); 30 | float_mod = fmod(number, 2.3); 31 | sine = sin(number); 32 | cosine = cos(number); 33 | tangent = tan(number); 34 | 35 | printf("The number entered is %d\n", number); 36 | printf("it square is %.0f\n", power); 37 | printf("it square root is %.2f\n", square_root); 38 | printf("it Cube square root is %.2f\n", cube_root); 39 | printf("it exponential is %.2f\n", exponential); 40 | printf("it Natural log is %.2f\n", natural_log); 41 | printf("it Logarithm is %.2f\n", logarithm); 42 | printf("it Floating absolute value is %.2f\n", floating_abs); 43 | printf("it Floating Mod of %d and %f is %.2f\n", number, 2.3, float_mod); 44 | printf("it sine is %.2f\n", sine); 45 | printf("it cosine is %.2f\n", cosine); 46 | printf("it tangent is %.2f\n", tangent); 47 | 48 | 49 | return (0); 50 | } -------------------------------------------------------------------------------- /05_functions/power_function.c: -------------------------------------------------------------------------------- 1 | /* program to compute the power of an integer */ 2 | 3 | #include 4 | 5 | int power(int base, int exp); 6 | 7 | int main() 8 | { 9 | int number, exp; 10 | 11 | printf("Enter a value for number and exponent: \n"); 12 | scanf("%d%d", &number, &exp); 13 | printf("%d to the power of %d is %d\n", number, exp, power(number, exp)); 14 | } 15 | 16 | int power(int base, int exp) 17 | { 18 | int i, p; 19 | 20 | p = 1; 21 | for (i = 1; i <= exp; ++i) 22 | { 23 | p = p * base; 24 | } 25 | return (p); 26 | } -------------------------------------------------------------------------------- /05_functions/random_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | unsigned int i; 7 | 8 | for (i = 1; i <= 20; ++i) 9 | { 10 | // pick random number from 1 to 6 and output it 11 | printf("%10d", 1 + (rand() % 6)); 12 | 13 | // if counter is divisible by 5, begin new line of output 14 | if (i % 5 == 0) 15 | { 16 | puts(""); 17 | } 18 | 19 | } 20 | 21 | return (0); 22 | } -------------------------------------------------------------------------------- /05_functions/return_statement.c: -------------------------------------------------------------------------------- 1 | /* c program to show the usage of the return statement in c functions */ 2 | #include 3 | 4 | /* user defined function to carry out addition on 3 integers and return the result */ 5 | int add(int a, int b, int c){ 6 | int result; /* declare the result variable */ 7 | 8 | result = a + b + c; /* perform the addition in a single expression */ 9 | return (result); /* return the result to the caller of the function */ 10 | } 11 | 12 | /* function main starts program execution */ 13 | int main(void){ 14 | int a; 15 | int b; 16 | 17 | b = 98; 18 | a = 1 + 2 + 3 + 4; 19 | b = 23 + add(32, a, a + b); /* calls the function add */ 20 | return (0); /* using return in the main function ends the program */ 21 | 22 | a = 999999999; /* anythinh after the return is not executed */ 23 | } -------------------------------------------------------------------------------- /06_arrays/array_init.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | int array[5]; // declare an array of 5 integers, its values are undefined currently 7 | int second_array[7] = {0} // declares an array and initializes it first element, this also initializes the rest of it content to 0 8 | int another_array[5] = {12, 23, 34, 45, 45}; // initializing an array with a list of values 9 | unsigned int index; // declare an index to be used to control the loop for the array manipulation 10 | 11 | for (index = 0; index < 5; ++index) 12 | { 13 | array[index] = 0; 14 | } 15 | 16 | printf("%3s %10s %20s\n", "index", "First Array Values", "Second Array values"); 17 | for (int i = 0; i < 5; ++i) 18 | { 19 | printf("%3d%10d%20d\n", i+1, array[i], another_array[i]); 20 | } 21 | 22 | return (0); 23 | } -------------------------------------------------------------------------------- /06_arrays/binary_search.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // to be completed later 5 | 6 | size_t binarySearch(const int b[], int searchKey, size_t low, size_t high); 7 | 8 | int main(void) 9 | { 10 | 11 | 12 | return (0); 13 | } 14 | 15 | 16 | size_t binarySearch(const int b[], int searchKey, size_t low, size_t high) 17 | { 18 | 19 | return (); 20 | } -------------------------------------------------------------------------------- /06_arrays/bubble_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define SIZE 12 3 | 4 | 5 | int main(void) 6 | { 7 | int array[SIZE] = {32, 56, 43, 43, 67, 564, 34, 65, 32, 56, 45}; 8 | int i; 9 | 10 | // original array 11 | for (i = 0; i < SIZE; ++i) 12 | { 13 | printf("%4d", array[i]); 14 | } 15 | 16 | puts(""); 17 | 18 | for (i = 0; i < SIZE; ++i) // loop over every array element from 0 to (SIZE - 1) 19 | { 20 | for (size_t pass = 0; pass < SIZE; ++pass) 21 | { 22 | if (array[pass] > array[pass + 1]) 23 | { 24 | int hold = array[pass]; 25 | array[pass] = array[pass + 1]; 26 | array[pass + 1] = hold; 27 | } 28 | } 29 | } 30 | 31 | puts("Data in ascending order \n"); 32 | 33 | for (size_t index = 0; index < SIZE; ++index) 34 | { 35 | printf("%4d", array[index]); 36 | } 37 | 38 | puts(" "); 39 | 40 | return (0); 41 | } -------------------------------------------------------------------------------- /06_arrays/char_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define SIZE 6 // size of array to contain the string BRIAN i.e = BRIAN + '\0' 4 | 5 | int main(void) 6 | { 7 | char array[] = "Brian is the Programmer that wrote this program!"; 8 | int index = 0; 9 | char c; 10 | 11 | while ((c = array[index]) != '\0') 12 | { 13 | printf("%c", c); 14 | ++index; 15 | } 16 | 17 | return (0); 18 | } -------------------------------------------------------------------------------- /06_arrays/histogram.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define SIZE 5 // size of the array to be used in the program 4 | 5 | /** 6 | * main - main function to start program execution 7 | * 8 | * 9 | */ 10 | int main(void) 11 | { 12 | int array[SIZE] = {3, 15, 8, 7, 9,}; 13 | int outer_index; // index to control the outter for loop 14 | int inner_index; // index to control the inner for loop 15 | 16 | printf("----------------------------------------------\n"); 17 | printf("%s%13s%17s\n", "Element", "Value", "Histogram"); 18 | printf("----------------------------------------------\n"); 19 | 20 | for (outer_index = 0; outer_index < SIZE; ++outer_index) 21 | { 22 | printf("%7d%13d ", outer_index, array[outer_index]); 23 | 24 | for (inner_index = 0; inner_index < array[outer_index]; ++inner_index) 25 | { 26 | printf("%c", '*'); 27 | } 28 | 29 | puts(""); 30 | } 31 | return (0); 32 | } -------------------------------------------------------------------------------- /06_arrays/intro.c: -------------------------------------------------------------------------------- 1 | // c program to introduce the concept of arrays in the c programming language 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | int arr[3]; // [] [] [] allocates 3 memory spaces for 3 integers in contagion memery addresses 8 | int array[10]; // declares an array of 10 integers 9 | char another_array[100]; // declares an array of 100 chars 10 | float float_array[30]; // declares an array of 30 floats 11 | 12 | printf("Array Address = %p\n", array); 13 | printf("First Element in the Array Address = %p\n", &array[0]); 14 | printf("Second Element in the Array Address = %p\n", &array[1]); 15 | printf("Third Element in the Array Address = %p\n", &array[2]); 16 | printf("Last Element in the Array Address = %p\n", &array[9]); 17 | 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /06_arrays/linear_search.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define SIZE 100 3 | 4 | size_t linearSearch(const int array[], int key, size_t size); 5 | 6 | 7 | int main(void) 8 | { 9 | int a[SIZE]; 10 | int search_key; 11 | size_t index; 12 | 13 | for (size_t i = 0; i < SIZE; ++i) 14 | { 15 | a[i] = 2 * i; 16 | } 17 | 18 | printf("Enter a search key: "); 19 | scanf("%2d", &search_key); 20 | 21 | index = linearSearch(a, search_key, SIZE); 22 | 23 | if (index != -1) 24 | printf("Found value at index = %d\n", index); 25 | else 26 | puts("Value not found!"); 27 | 28 | return (0); 29 | } 30 | 31 | size_t linearSearch(const int array[], int key, size_t size) 32 | { 33 | size_t index; 34 | 35 | for (index = 0; index < size; ++index) 36 | { 37 | if (key == array[index]) 38 | return (index); 39 | } 40 | 41 | return (-1); 42 | } -------------------------------------------------------------------------------- /06_arrays/multi_dimensional_arrays.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define ROW 2 4 | #define COL 2 5 | 6 | 7 | int main(void) 8 | { 9 | int mul_arr[][COL] = {{1,2}, {3,4}}; 10 | 11 | for (int i = 0; i < ROW; ++i) 12 | { 13 | for (int j = 0; j < COL; ++j) 14 | { 15 | printf("%4d", mul_arr[i][j]); 16 | } 17 | puts(" "); 18 | } 19 | 20 | return (0); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /06_arrays/simple_array_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main(void) 5 | { 6 | 7 | return (0); 8 | } -------------------------------------------------------------------------------- /06_arrays/static_arrays.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | static int array[] = {12, 23, 54, 65, 23, 45, 65, 23, 54, 23, 56, 23, 66, 7, 23, 56, 23, 45, 6, 23, 56, 23, 56, 46}; 7 | // static arrays are created once when the program starts and are not destroyed when they are out of scope 8 | // nevertheless the can not be accessed outside thier local scope except declared in the global scope 9 | int i = 0; 10 | 11 | printf("%s%10s\n", "Element", "Value"); 12 | for (i; i < (sizeof(array) / sizeof(int)); ++i) 13 | { 14 | printf("%7d%10d\n", i, array[i]); 15 | } 16 | return (0); 17 | } -------------------------------------------------------------------------------- /06_arrays/statistics.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define SIZE 99 // defines a macro which would be size of the array to be used in the program 3 | 4 | /** 5 | * This source file contains statistc functions used to demonstrate the uses of arrays in 6 | * in the c programming language ecosystem 7 | */ 8 | 9 | void mean(const unsigned int answer[]); 10 | void median(unsigned int answer[]); 11 | void mode(unsigned int freq[], unsigned const int answer[]); 12 | void bubbleSort(unsigned int a[]); 13 | void printArray(unsigned const int a[]); 14 | 15 | int main(void) 16 | { 17 | unsigned int frequency[10] = {0}; // declare and initialize the frequency array 18 | 19 | // initial reponse array 20 | unsigned int response[SIZE] = 21 | { 6, 7, 8, 9, 8, 7, 8, 9, 8, 9, 22 | 7, 8, 9, 5, 9, 8, 7, 8, 7, 8, 23 | 6, 7, 8, 9, 3, 9, 8, 7, 8, 7, 24 | 7, 8, 9, 8, 9, 8, 9, 7, 8, 9, 25 | 6, 7, 8, 7, 8, 7, 9, 8, 9, 2, 26 | 7, 8, 9, 8, 9, 8, 9, 7, 5, 3, 27 | 5, 6, 7, 2, 5, 3, 9, 4, 6, 4, 28 | 7, 8, 9, 6, 8, 7, 8, 9, 7, 8, 29 | 7, 4, 4, 2, 5, 3, 8, 7, 5, 6, 30 | 4, 5, 6, 1, 6, 5, 7, 8, 7}; 31 | 32 | // process response 33 | mean(response); 34 | median(response); 35 | mode(frequency, response); 36 | 37 | return (0); 38 | } 39 | 40 | void mean(const unsigned int answer[]) 41 | { 42 | printf("%s\n%s\n%s\n", "********", " MEAN", "*******"); 43 | 44 | unsigned int total = 0; 45 | 46 | for (size_t i = 0; i < SIZE; ++i) 47 | { 48 | total += answer[i]; 49 | } 50 | 51 | printf("The mean is the average value of the data\n" 52 | "items. The mean is equal to the total of\n" 53 | "all the data items divided by the number\n" 54 | "of data items (%u). The mean value for\n" 55 | "this run is: %u / %u = %.4f\n\n", 56 | SIZE, total, SIZE, (double) total / SIZE); 57 | } 58 | 59 | void median(unsigned int answer[]) 60 | { 61 | unsigned int number; 62 | 63 | printf("\n%s\n%s\n%s\n%s", 64 | "********", " Median", "********", 65 | "The unsorted array of responses is"); 66 | 67 | // print the original array 68 | printArray(answer); 69 | 70 | // sort the array 71 | bubbleSort(answer); 72 | 73 | // print the sorted array 74 | printf("%s", "\n\nThe Sorted array is: "); 75 | printArray(answer); 76 | 77 | printf("\n\nThe median is element %u of\n" 78 | "the sorted %u element array.\n" 79 | "For this run the median is %u\n\n", 80 | SIZE / 2, SIZE, answer[SIZE / 2]); 81 | 82 | } 83 | 84 | // determine most frequent response 85 | void mode(unsigned int freq[], const unsigned int answer[]) 86 | { 87 | unsigned int rating; 88 | 89 | printf("\n%s\n%s\n%s\n","********", " Mode", "********"); 90 | 91 | // initialize frequencies to 0 92 | for (size_t rating = 1; rating <= 9; ++rating) 93 | { 94 | freq[rating] = 0; 95 | } 96 | 97 | // summarize frequencies 98 | for (size_t j = 0; j < SIZE; ++j) { 99 | ++freq[answer[j]]; 100 | } 101 | 102 | // output headers for result columns 103 | printf("%s%11s%19s\n\n%54s\n%54s\n\n", 104 | "Response", "Frequency", "Histogram", 105 | "1 1 2 2", "5 0 5 0 5"); 106 | 107 | // output results 108 | unsigned int largest = 0; // represents largest frequency 109 | unsigned int modeValue = 0; // represents most frequent response 110 | 111 | for (rating = 1; rating <= 9; ++rating) 112 | { 113 | printf("%8u%11u ", rating, freq[rating]); 114 | if (freq[rating] > largest) 115 | { 116 | largest = freq[rating]; 117 | modeValue = rating; 118 | } 119 | 120 | // output histogram bar representing frequency value 121 | for (unsigned int h = 1; h <= freq[rating]; ++h) { 122 | printf("%s", "*"); 123 | } 124 | 125 | puts(""); // being new line of output 126 | } 127 | 128 | // display the mode value 129 | printf("\nThe mode is the most frequent value.\n" 130 | "For this run the mode is %u which occurred" 131 | " %u times.\n", modeValue, largest); 132 | } 133 | 134 | void bubbleSort(unsigned int array[]) 135 | { 136 | unsigned int i; 137 | 138 | for (i = 0; i < SIZE; ++i) // loop over every array element from 0 to (SIZE - 1) 139 | { 140 | for (size_t pass = 0; pass < SIZE; ++pass) 141 | { 142 | if (array[pass] > array[pass + 1]) 143 | { 144 | int hold = array[pass]; 145 | array[pass] = array[pass + 1]; 146 | array[pass + 1] = hold; 147 | } 148 | } 149 | } 150 | } 151 | 152 | void printArray(const unsigned int a[]) 153 | { 154 | // output array contents 155 | for (size_t j = 0; j < SIZE; ++j) { 156 | 157 | if (j % 20 == 0) 158 | { // begin new line every 20 values 159 | puts(""); 160 | } 161 | 162 | printf("%2u", a[j]); 163 | } 164 | } -------------------------------------------------------------------------------- /06_arrays/strings.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main(void) 5 | { 6 | char name[] = "Brian"; 7 | 8 | printf("Name is %s\n", name); 9 | 10 | name[0] = 'R'; 11 | 12 | printf("Name is now %s\n", name); 13 | 14 | return (0); 15 | } -------------------------------------------------------------------------------- /07_recursion/factorial.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * factorial - function to calculate the factorial of a given integer 6 | * @x : integer argument 7 | * 8 | * Return: factorial of the integer argument 9 | */ 10 | 11 | 12 | unsigned long long int factorial(unsigned int x); 13 | 14 | int main(void) 15 | { 16 | unsigned int number; 17 | unsigned int result; 18 | 19 | printf("please a value to compute it factorial: "); 20 | scanf("%u", &number); 21 | 22 | result = factorial(number); 23 | printf("The factorial of %d is %llu", number , result); 24 | 25 | return (0); 26 | } 27 | 28 | 29 | unsigned long long int factorial(unsigned int x) 30 | { 31 | if (x <= 1) 32 | { 33 | return x; 34 | } 35 | return x * factorial(x-1); 36 | } -------------------------------------------------------------------------------- /07_recursion/fibonnaci.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | unsigned long long int fib(unsigned int number); 5 | 6 | int main(void) 7 | { 8 | unsigned int x; 9 | unsigned long long int result; 10 | 11 | printf("Please enter a value to compute the fibonacci series for: "); 12 | scanf("%u", &x); 13 | 14 | result = fib(x); 15 | printf("The fibonnaci of %d is %llu", x, result); 16 | 17 | return (0); 18 | } 19 | 20 | unsigned long long int fib(unsigned int number) 21 | { 22 | if (number == 1 || number == 0) 23 | return number; 24 | else 25 | return fib(number - 1) + fib(number - 2); 26 | } -------------------------------------------------------------------------------- /08_malloc/cisfun.c: -------------------------------------------------------------------------------- 1 | // As presented in the ALX concept materials 2 | 3 | /** 4 | * cisfun - function used for concept introduction 5 | * @n1: number of the project 6 | * @n2: number of tasks 7 | * 8 | * Return: nothing 9 | */ 10 | 11 | void cisfun(unsigned int n1, unsigned int n2) 12 | { 13 | int n; 14 | char c; 15 | int *ptr; 16 | char array[3]; 17 | } 18 | 19 | // when the above function is called. the program would allocate 20 | // 4 bytes each for n1, n2 and n since they are all integers and that's the size of integers 21 | // the program will allocate 1 byte for the variable c, since char are single byte datatype 22 | // the program will also allocate 1 bytes successively in 3 places for the array variable 23 | // which is meant to hold 3 char (1 Byte for each char datatype) 24 | 25 | /** 26 | * main- main introduction 27 | * 28 | * Return: always 0 29 | */ 30 | int main(void) 31 | { 32 | cisfun(3, 4); // when the function is called the spaces are allocated as stated above 33 | // when the function call is over, the space allocated is released for future use which means 34 | // the space and data it held can not be accessed outside the function call 35 | } -------------------------------------------------------------------------------- /08_malloc/free_mem.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * m - stores 3 int in a new allocated space in memory and prints the sum 6 | * @n0: integer to store and print 7 | * @n1: integer to store and print 8 | * @n2: integer to store and print 9 | * 10 | * Return: nothing 11 | */ 12 | void m(int n0, int n1, int n2) 13 | { 14 | int *t; 15 | int sum; 16 | 17 | t = malloc(sizeof(*t) * 3); 18 | printf("The value of the pointer t = %p\n", t); 19 | printf("The value the pointer is pointing at is %d\n", *t); 20 | t[0] = n0; 21 | t[1] = n1; 22 | t[2] = n2; 23 | sum = t[0] + t[1] + t[2]; 24 | printf("%d + %d + %d = %d\n", t[0], t[1], t[2], sum); 25 | printf("Memory address of the first element is %p\n", &t[0]); 26 | printf("Memory address of the second element is %p\n", &t[1]); 27 | printf("Memory address of the third element is %p\n", &t[2]); 28 | free(t); 29 | } 30 | 31 | /** 32 | * main - introduction to malloc and free 33 | * 34 | * Return: 0. 35 | */ 36 | int main(void) 37 | { 38 | m(98, 402, -1024); 39 | return (0); 40 | } -------------------------------------------------------------------------------- /08_malloc/malloc_examples.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - introduction to malloc and free 6 | * 7 | * Return: 0. 8 | */ 9 | int main(void) 10 | { 11 | char *str; 12 | 13 | str = malloc(sizeof(char) * 3); 14 | str[0] = 'O'; 15 | str[1] = 'K'; 16 | str[2] = '\0'; 17 | printf("%s\n", str); 18 | return (0); 19 | } 20 | 21 | /** 22 | * It is very important because as you know, the size of the different types will be different 23 | * on different machines: we want 3 times the size of a char (which happens to be 3 times 1 byte on our 64-bit machine). 24 | * Always use sizeof for a better portability. 25 | */ -------------------------------------------------------------------------------- /08_malloc/segf.c: -------------------------------------------------------------------------------- 1 | /** 2 | * segf - Let's segfault \o/ 3 | * 4 | * Return: nothing. 5 | */ 6 | void segf(void) 7 | { 8 | char *str; 9 | 10 | str = "Holberton"; 11 | str[0] = 's'; 12 | } 13 | 14 | // this is a special case that shows that string literals memory are not deallocated when they are out of scope 15 | 16 | /** 17 | * main - concept introduction 18 | * 19 | * Return: 0. 20 | */ 21 | int main(void) 22 | { 23 | segf(); 24 | return (0); 25 | } -------------------------------------------------------------------------------- /08_malloc/while_malloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - introduction to malloc and free 6 | * 7 | * Return: 0. 8 | */ 9 | int main(void) 10 | { 11 | while (1) 12 | { 13 | malloc(sizeof(char) * 1024); 14 | } 15 | return (0); 16 | } -------------------------------------------------------------------------------- /10_pointers/arithmetic.c: -------------------------------------------------------------------------------- 1 | // arithmetic involving pointer objects 2 | #include 3 | 4 | int main(void) 5 | { 6 | // declare an array of 5 elements and 2 pointers 7 | // po5;inting to the array first element 8 | int arr[5]; 9 | int *Ptr = arr; 10 | int *Ptr2 = &arr[0]; 11 | 12 | // the two pointer above are valid and equavalent to each other 13 | 14 | // when an integer is added to a pointer, its doesn't evaluate to normal arithmetic 15 | // assume that a pointer Sptr has a value of 3000 (which is the address of the obj the pointer is pointing to) 16 | // in normal sense 3000 + 2 == 3002, But with Pointers!! 17 | // Sptr += 3, would evaluate to 3000 plus 3 times the size of the element the pointer is pointing to on that machine 18 | // therefore, for machine whose integer size is 4 bytes, if the pointer is pointing to a int, this would evaluate to 3000 + (3 * 4) == 3012 19 | 20 | // Example of Pointer Addition 21 | int random_numbers[100] = {0}; 22 | int *Sptr = random_numbers; 23 | 24 | printf("The value of the Pointer before arithmetic is %d\n", Sptr); 25 | 26 | Sptr += 3; // increment the pointer by 3 27 | 28 | printf("The value of the Pointer after the arithmetic is %d\n", Sptr); 29 | 30 | // NOTE: it a common programming error to use arithmetic operation on a NULL pointer (pointer that doesn't point to any object) 31 | // NOTE: be careful not to run off the ends of an array when using pointer arithmetic to move across the array 32 | // Example: 33 | /** 34 | int ar[5] = {0}; // declare and the initialze an array to zeroes 35 | int *ptr = arr; // assume address of arr is 3000 36 | 37 | ptr += 12; // ptr = 3000 + (12 * 4) == 3048 > (3000 + 5*4) which is the last element in the array 38 | */ 39 | 40 | // when incrementing an array by 1 , the ++ increment can be used and vice verse 41 | ++Sptr; // increments the pointer by 1 42 | Sptr++; // increments the pointer by 1 43 | --Sptr; // decrements the pointer by 1 44 | Sptr--; // decrements the pointer by 1 45 | 46 | // Subtracting pointer from pointer 47 | // x = V2ptr - vptr; // assume V2ptr value == 3008 and vptr value == 3000 and they both point to int obj 48 | // x == 2, since 8 == 4 * 2, which is 2 integer objs (Funny? i Know, but you will get use to it) 49 | 50 | // Note: Pointer arithmetic is undefined unless performed on an array 51 | // NOTE: it is an error to perform subtraction on pointers that are not the same type and array 52 | 53 | // a pointer can be assigned to another pointer if the both have the same type 54 | int i = 12; 55 | int *Mptr = &i; 56 | int *Nptr = Mptr; 57 | 58 | printf("The Value of Mptr is %d and\nThe value of Nptr is %d\n", Mptr, Nptr); 59 | 60 | // the pointer to void, i.e void * is a generic pointer can be assigned to any other pointer 61 | // a pointer to void, can not be deferenced 62 | 63 | // Note: assigning pointer to another pointer with differenct data type is a syntax error 64 | /* Note: deferencing a pointer to void is a syntax error, this is because when the deferencing a pointer 65 | the compiler needs to know the size of the obj being referenced, and with void pointers, the is no way the 66 | the compiler can know the size of the referenced ob 67 | */ 68 | 69 | /** 70 | pointer can be compared with equality operators, but it is meaningless to compare pointers that do not pointer to the same array 71 | */ 72 | 73 | /* In C Arrays and Pointers are closely related, an array name might be thought of as a constant pointer */ 74 | int list[5] = {0}; 75 | int *lPtr = list; 76 | 77 | printf("The Array name return the memory address of the array == memory address of the first element\n"); 78 | printf("The value of list is %p\n", list); 79 | printf("The value of list[0] is %p\n", lPtr); 80 | printf("The value of lPtr is %p\n", lPtr); 81 | 82 | /* Pointer/Offset Notation 83 | since the name of an array returns the memory address of the first element 84 | (treating it as constant pointer) deferencing the name of the array would be equivalent to index 0 85 | */ 86 | 87 | int ratings[10] = {4, 5, 7}; // partially initialized array would be automatically completed with zeros 88 | 89 | printf("Using Index to get first value = %d\n", ratings[0]); 90 | printf("Deferencing Array Name to get first value = %d\n", *ratings); 91 | 92 | /* therefore from the above 93 | it can be seen that the index operation is equivalent to deferencing an offset 94 | 95 | array[2] == *(array + 2) // this statement does not modify the array name, it still points to the first element 96 | array[0] == *(array + 0) == *array 97 | array[4] == *(array + 4) 98 | 99 | IN general all index expression can be written in terms of pointer offet and deferencing 100 | 101 | Pointers and Arrays Can be used interchangeably 102 | */ 103 | 104 | 105 | return (0); 106 | } -------------------------------------------------------------------------------- /10_pointers/copy.c: -------------------------------------------------------------------------------- 1 | // Copy function implement to copy the content on one string or array into another 2 | #include 3 | 4 | void copy(char * const s1, char * const s2); 5 | 6 | int main(void) 7 | { 8 | char string1[] = "Hello! From the Matrix"; 9 | char string2[10]; 10 | 11 | copy(string1, string2); 12 | 13 | // printf("String2 is now = %s", string2); 14 | 15 | return (0); 16 | } 17 | 18 | 19 | void copy(char * const s1, char * const s2) 20 | { 21 | for (size_t i = 0; (s2[i] = s1[1]) != '\0'; ++i) 22 | { 23 | ; 24 | } 25 | } -------------------------------------------------------------------------------- /10_pointers/intro.c: -------------------------------------------------------------------------------- 1 | // pointers are variables that hold the address of another variable 2 | 3 | #include 4 | 5 | 6 | int main(void) 7 | { 8 | int number = 98; 9 | int *ptr = NULL; // a pointer is declared and initialized to NULL (this is safe practice for pointers) 10 | // that are not assigned a value on declaration 11 | 12 | ptr = &number; // the address operation is used on the number obj to get its address and 13 | // the ptr obj is assinged to the address value 14 | 15 | printf("The Value of ptr is %p\n", ptr); 16 | printf("The Value of number is %d\n", number); 17 | printf("______________________________________\n"); 18 | printf("The memory address of number is %p\n", &number); 19 | printf("The memory address of ptr is %p\n", &ptr); 20 | 21 | return (0); 22 | } -------------------------------------------------------------------------------- /10_pointers/size_of_array.c: -------------------------------------------------------------------------------- 1 | // This program examplifies the usage and rules around the size 2 | // of arrays and other related details 3 | 4 | #include 5 | #define SIZE 20 // define a macro that would be used to declare an array size 6 | /** 7 | * C provides a sizeof unary operator (can be used on a unit obj), that returns the size 8 | of an array or any other object (region of memory) in bytes. 9 | 10 | it is used as though it is a function, with the operand placed within parenthesis 11 | it returns the size_t data type, which is a numeric data type 12 | 13 | */ 14 | 15 | // declaration for a custom function that is defined to wrap the sizeof operator 16 | size_t getSize(float *arr); 17 | 18 | int main(void) 19 | { 20 | float array[SIZE]; // declare an array of 20 float elements 21 | 22 | printf("This size returned by the getSize function is %lu\n", getSize(array)); 23 | printf("This size returned by the sizeof operator is %lu\n", sizeof(array)); 24 | return (0); 25 | } 26 | 27 | size_t getSize(float *arr) 28 | { 29 | return sizeof(arr); 30 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Learning C 2 | 3 | 4 | 5 | # Description 6 | This repository contains structured and curated code snippets and well documented C source codes written to explain basic c programming concepts. This is a memo of my journey learning and developing with the C programming language. 7 | 8 | The choice of the Repo curated structure is highly opinated, i structured them this way because intuitively it felt proper to learn in this progression, with subsequent concepts building more on previous ones. if you do intend to use this repo and or any of it content as a reference material, it is highly likely that the structure would not matter much to you,as you could just move into folders containing the concept you wish to look at. but for people looking to use it as learning material, following the assumed progression would better aid your learning process and progress. 9 | 10 | ## Usage 11 | - Any and all code improvements are whole-heartedly accepted 12 | - The codes provided in this repo are under no restriction in terms of usage 13 | - Use the codes here wisely, as there were written by a budding c programmer 14 | - Any Attribution to the repo is also welcomed 15 | 16 | ## NOTE: 17 | Since most of the codes here were written as a result of learning and praticing with concepts and ideas in the c programming language ecosystem, some code are not so-efficient and may not be an appropriate option for practical applications and solutions. 18 | 19 | Constructive approach are welcomed to request edition and suggest changes to the code found in this repo. 20 | 21 | ## Maintainer 22 | Brian Obot 23 | 24 | -------------------------------------------------------------------------------- /__introduction/0_welcome.c: -------------------------------------------------------------------------------- 1 | // a first program written in the C programming language 2 | #include // every line starting with a # is called a preprocessor directives and would be explained later 3 | 4 | // lines starting with double forward lash are known as comments (single line comments), 5 | // they are completely ignore by the computer when the program is being compiled 6 | // they can be used to better explain some piece of code or make notes for other when the read your source code (source code 7 | // refers to the file containig your program) it is also called source file 8 | 9 | /** 10 | * this are also comments, but they can span multiple lines and are called multi-line comments 11 | * they are used when the information to be embedded in a comment is too long for a single line 12 | * 13 | */ 14 | 15 | // the function 'main' begins program execution (every C program must have a main function i.e a function called 'main' ) 16 | // functions are explained later on, but for now it's enough to know that every function follow this structure below 17 | // datatype function_name(list of arguments) 18 | int main(void) 19 | { 20 | printf("Welcome to C!\n"); // this is a function that displays the text "Welcome to C! on the screen" 21 | } // end of the main function 22 | 23 | 24 | // if you have installed a compiler on your personal computer, 25 | // you can try this on the command line to compile the source code into a executeable program 26 | 27 | // $ gcc welcome.c -o welcome 28 | // after which you can run the 'welcome' program the command line to see the output 29 | // for windows users 30 | // > welcome.exe 31 | // for linux users 32 | // $ ./welcome -------------------------------------------------------------------------------- /__introduction/hello_world.c: -------------------------------------------------------------------------------- 1 | /* Hello world program in C*/ 2 | #include 3 | 4 | // function main begins program execution 5 | int main(void) 6 | { 7 | printf("%s", "Hello, World!\n"); 8 | }// end of the main function -------------------------------------------------------------------------------- /alx-low_level_programming/0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * malloc_checked - function to check a malloc call 7 | * @b: first argument to the function 8 | * 9 | * Return: a pointer 10 | */ 11 | void *malloc_checked(unsigned int b) 12 | { 13 | int *ptr; 14 | 15 | ptr = malloc(b); 16 | 17 | if (ptr == NULL) 18 | { 19 | free(ptr); 20 | exit(98); 21 | } 22 | 23 | return (ptr); 24 | } 25 | 26 | int main(void) 27 | { 28 | char *c; 29 | int *i; 30 | float *f; 31 | double *d; 32 | 33 | c = malloc_checked(sizeof(char) * 1024); 34 | printf("%p\n", (void *)c); 35 | i = malloc_checked(sizeof(int) * 402); 36 | printf("%p\n", (void *)i); 37 | f = malloc_checked(sizeof(float) * 100000000); 38 | printf("%p\n", (void *)f); 39 | d = malloc_checked(INT_MAX); 40 | printf("%p\n", (void *)d); 41 | free(c); 42 | free(i); 43 | free(f); 44 | free(d); 45 | return (0); 46 | } -------------------------------------------------------------------------------- /alx-low_level_programming/0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * string_nconcat - function to check a malloc call 7 | * @s1: first argument to the function 8 | * @s2: second argument to the function 9 | * 10 | * Return: a pointer 11 | */ 12 | 13 | char *string_nconcat(char *s1, char *s2, unsigned int n) 14 | { 15 | char *ptr; 16 | 17 | // reallocate s1 to s2 and only realloc the first n chars 18 | ptr = realloc(s1, ); 19 | } -------------------------------------------------------------------------------- /c-programming.cms: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/brianobot/C_learning/2081ef5637ce6952ef8dcdbd64196996fe36c09b/c-programming.cms -------------------------------------------------------------------------------- /programs examples/Exercise1-9.c: -------------------------------------------------------------------------------- 1 | /* Write a program to copy its input to its output, replacing each 2 | * string of one or more blanks by a single blank.*/ 3 | 4 | #include 5 | 6 | void main(void) 7 | { 8 | int c; 9 | while ((c = getchar()) != EOF) 10 | { 11 | if (c == ' ') 12 | { 13 | putchar(' '); 14 | } 15 | else 16 | { 17 | putchar(c); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /programs examples/celsius_to_farenheit_converter.c: -------------------------------------------------------------------------------- 1 | /* converts a range of Celcieus values to Farenheit values */ 2 | #include 3 | 4 | #define UPPER 300 // Symbolic constants 5 | #define LOWER 0 // 6 | #define STEP 20 // 7 | 8 | int convert_temp_from_fahr_to_cel(int fahr); 9 | 10 | // function main starts program executio 11 | int main(void) 12 | { 13 | int fahr ; // declare a variable to hold the farenheit values 14 | 15 | for (fahr=LOWER; fahr <= UPPER; fahr += STEP) 16 | { 17 | printf("%d\t%d \n", fahr, convert_temp_from_fahr_to_cel(fahr)); 18 | } 19 | } 20 | 21 | int convert_temp_from_fahr_to_cel(int fahr) 22 | { 23 | int cel = (5 * (fahr - 32) / 9); 24 | return (cel); 25 | } -------------------------------------------------------------------------------- /programs examples/count_blanks.c: -------------------------------------------------------------------------------- 1 | /* program to count blanks, spaces and newline */ 2 | #include 3 | 4 | int main(void) 5 | { 6 | unsigned int count = 0; 7 | int c; 8 | 9 | while ((c = getchar()) != EOF) 10 | { 11 | if (c == '\n'){ 12 | ++count; 13 | } 14 | else if (c == ' '){ 15 | ++count; 16 | } 17 | else if (c == '\b'){ 18 | ++count; 19 | } 20 | } 21 | printf("Count of blanks, tabs and newlines = %d", count); 22 | } -------------------------------------------------------------------------------- /programs examples/crap_game.c: -------------------------------------------------------------------------------- 1 | // dice game implemented in c 2 | #include 3 | #include 4 | #include 5 | 6 | enum Status {CONTINUE, WON, LOST}; 7 | 8 | int rollDice(void); 9 | 10 | int main(void) 11 | { 12 | srand(time(NULL)); 13 | 14 | int sum; 15 | int myPoint; 16 | enum Status gameStatus; 17 | 18 | sum = rollDice(); 19 | 20 | switch(sum) 21 | { 22 | 23 | case 7: 24 | case 11: 25 | gameStatus = WON; 26 | break; 27 | 28 | case 2: 29 | case 3: 30 | case 12: 31 | gameStatus = LOST; 32 | break; 33 | 34 | default: 35 | gameStatus = CONTINUE; 36 | myPoint = sum; 37 | printf("Point is %d\n", myPoint); 38 | break; 39 | } 40 | 41 | while (gameStatus == CONTINUE) 42 | { 43 | sum = rollDice(); 44 | 45 | if (sum == myPoint) 46 | { 47 | gameStatus = WON; 48 | } 49 | else 50 | { 51 | if (sum == 7) 52 | gameStatus = LOST; 53 | } 54 | } 55 | 56 | if (gameStatus == WON) 57 | printf("The Player has Won!\n"); 58 | else 59 | printf("The Player has lost!\n"); 60 | 61 | } 62 | 63 | int rollDice(void) 64 | { 65 | int dice1; 66 | int dice2; 67 | int sum; 68 | 69 | dice1 = 1 + (rand() % 6); 70 | dice2 = 1 + (rand() % 6); 71 | 72 | sum = dice1 + dice2; 73 | printf("Player rolled %d and %d, sum is %d\n", dice1, dice2, sum); 74 | 75 | return (sum); 76 | } -------------------------------------------------------------------------------- /programs examples/execises.c: -------------------------------------------------------------------------------- 1 | /* program to complete exercises given in the C How to Program book */ 2 | #include 3 | 4 | /* function main starts program execution */ 5 | int main(void) 6 | { 7 | int c, thisVariable, q76354, number; 8 | int a; 9 | 10 | printf("Enter a number: "); // prompt 11 | scanf("%d", &number); // store the user input into variable a 12 | 13 | if (number != 7){ 14 | printf("%s", "The variable number is not equal to 7\n"); 15 | } 16 | 17 | printf("This is a C program\n"); 18 | printf("This is a C\nprogram\n"); 19 | printf("This\nis\na\nC\nprogram\n"); 20 | printf("This\t is\t a\t C\t program\t\n"); 21 | 22 | }/* end of the main function */ -------------------------------------------------------------------------------- /programs examples/final_velocity.c: -------------------------------------------------------------------------------- 1 | /* program than asks the user to enter the initial velocity and acceleration 2 | of an object, and the time that has elapsed, places them in the variables u, a, and t, and prints 3 | the final velocity, v, and distance traversed, s, using the following equations. 4 | 5 | a) v = u + at 6 | b) s = ut + (1/2)at**2 7 | 8 | */ 9 | 10 | #include 11 | 12 | // function main starts program execution 13 | int main() 14 | { 15 | int u; // initial velocity variable 16 | int a; // acceleration of the body variable 17 | int t; // time elasped variable 18 | int v; // final velocity 19 | int s; // distance traversed 20 | 21 | printf("Enter a value for the initial velocity, accleration, and time elasped: \n"); // prompt 22 | scanf("%d%d%d", &u, &a, &t); // read the value into u, a and t 23 | 24 | v = u + (a * t); 25 | s = u * t + ((1/2)*(a * t * t)); 26 | 27 | printf("Final Velocity is %d \nDistance traversed is %d \n", v, s); 28 | 29 | 30 | }// end of the main function -------------------------------------------------------------------------------- /programs examples/shape_with_asterisks.c: -------------------------------------------------------------------------------- 1 | /* Write a program that prints the following shapes with asterisks. 2 | 3 | ********* *** * * 4 | * * * * *** * * 5 | * * * * ***** * * 6 | * * * * * * * 7 | * * * * * * * 8 | * * * * * * * 9 | * * * * * * * 10 | * * * * * * * 11 | ********* *** * * 12 | 13 | 14 | */ 15 | #include 16 | 17 | // function main starts program execution 18 | int main() 19 | { 20 | printf("*********\t\t***\t\t*\t\t*\n"); 21 | printf("*\t*\t**"); 22 | /* program to be completed later ....*/ 23 | 24 | 25 | }// end of main function -------------------------------------------------------------------------------- /programs examples/test_addition_of_int_to_char.c: -------------------------------------------------------------------------------- 1 | /* program to test the addition of a integer type to char type */ 2 | #include 3 | 4 | /* function main starts program execution */ 5 | int main(void) 6 | { 7 | int a = 15; 8 | char b = 'a'; 9 | 10 | printf("The sum of a and b is %d\n", a+b); /* displat the result of the summation as a formated string */ 11 | 12 | printf("The value of a is %d\n", a); 13 | printf("The value of b is %c\n", b); /* note that the %c converter is used to display character */ 14 | 15 | }/* end of main function */ --------------------------------------------------------------------------------