Skip to content

Commit 64ff067

Browse files
committed
Document the source code better for beginners
1 parent 31a4024 commit 64ff067

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+411
-28
lines changed

Diff for: operators/add_2_integers.c renamed to 00_operators/add_2_integers.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ int main(void)
1515
printf("Enter the second Integer:\n"); //prompt
1616
scanf("%d", &integer2); // read the second integer
1717

18-
int sum = integer1 + integer2; // assign total to sum
18+
int sum = integer1 + integer2; // assign the value of the total expression to sum
1919
printf("Sum is %d\n", sum);
2020

2121
}// end of the main function
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: Loops/do_while.c renamed to 04_loops/do_while.c

File renamed without changes.

Diff for: Loops/for_loop.c renamed to 04_loops/for_loop.c

File renamed without changes.

Diff for: Loops/test.c renamed to 04_loops/test.c

File renamed without changes.
File renamed without changes.

Diff for: 05_functions/exits/exit.c

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
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+
}

Diff for: 05_functions/maths_func.c

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <math.h>
2+
#include <stdio.h>
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+
}
File renamed without changes.

Diff for: 05_functions/random_numbers.c

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
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+
}
File renamed without changes.
File renamed without changes.

Diff for: 06_arrays/bubble_sort.c

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <stdio.h>
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+
}
File renamed without changes.
File renamed without changes.

Diff for: arrays/intro.c renamed to 06_arrays/intro.c

File renamed without changes.

Diff for: 06_arrays/simple_array_sort.c

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <stdio.h>
2+
3+
4+
int main(void)
5+
{
6+
7+
return (0);
8+
}

Diff for: 06_arrays/static_arrays.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
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+
}

Diff for: 07_recursion/factorial.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
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+
}

Diff for: 07_recursion/fibonnaci.c

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
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+
}

Diff for: malloc/cisfun.c renamed to 08_malloc/cisfun.c

File renamed without changes.

Diff for: malloc/free_mem.c renamed to 08_malloc/free_mem.c

File renamed without changes.
File renamed without changes.

Diff for: malloc/segf.c renamed to 08_malloc/segf.c

File renamed without changes.
File renamed without changes.

Diff for: 0__introduction/0_welcome.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// a first program written in the C programming language
2+
#include <stdio.h> // 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
File renamed without changes.

Diff for: Introduction/welcome.c

-26
This file was deleted.

Diff for: README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
# Learning C
22

33
# Description
4-
this is repo contains structure and curated code snippets an well documented C source codes written to explain basic c programming concepts.
4+
This repository contains structured and curated code snippets an 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.
5+
6+
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.
57

68
## Usage
79
- Any and all code improvements are whole-heartedly accepted
810
- The codes provided in this repo are under no restriction in terms of usage
911
- Use the codes here wisely, as there were written by a budding c programmer
1012
- Any Attribution to the repo is also welcomed
1113

14+
## NOTE:
15+
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.
16+
17+
Constructive approach are welcomed to request edition and suggest changes to the code found in this repo.
18+
19+
## Maintainer
20+
Brian Obot <[email protected]>
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <limits.h>
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+
}

0 commit comments

Comments
 (0)