Skip to content

Commit 6f7f25c

Browse files
committed
c basics and other
1 parent c922aa6 commit 6f7f25c

30 files changed

+728
-1
lines changed

c/basics/arithmetics.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <stdlib.h>
2+
#include <math.h>
3+
4+
10 + 20 // 30
5+
20 - 10 // 10
6+
10 * 20 // 200
7+
20 / 10 // 2
8+
9+
// power require math.h
10+
pow(10, 2) // 100.00000
11+
12+
// floating point conversion
13+
10.0 + (10 + 20) // 40.000000
14+
15+
// NOTE: char is promoted to int value
16+
'c' + 10
17+
// 109 (ascii value of c is 99)
18+
19+
// floating point conversion using type casting
20+
(double) 10.0 + (10 + 20) // 40.000000
21+
(double) 20.0 - (10 + 10) // 0.000000
22+
(double) 10.0 * (10 * 2) // 200.000000
23+
24+
// common division error
25+
30 / 20 // 1
26+
(double) 30 / 20 // 1.500000
27+
28+
// integer divison, quotient
29+
20 / 20 // 1
30+
30 / 20 // 1
31+
40 / 20 // 2
32+
33+
// modulo, remainder
34+
10 % 20 // 10
35+
36+
// module, remainder on floating point
37+
fmod(12.5, 10.0) // 2.500000
38+
39+
// built-ins
40+
abs(-20) // 20
41+
42+
// rounding value
43+
round(2.945) // 3.000000
44+
round(2.495) // 2.000000
45+
46+
// NOTE: no built-ins for round to decimal places
47+
48+
// increment and decrement
49+
int a = 10;
50+
51+
a++ // 11
52+
a-- // 9

c/basics/arrays.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#define MAX_STRING_SIZE 40
2+
3+
// arrays are mutable
4+
double array_one[] = { 200.0, -2.2, 1.0, 0.0 };
5+
char array_two[][MAX_STRING_SIZE] = { "REMOVE", "RANDOM" };
6+
7+
array_one[0] // 200.000000
8+
array_two[1] // RANDOM
9+
10+
array_one[0] = 50.0;
11+
array_one[0] // 50.000000
12+
13+
// multi-dimensional array
14+
int array_multi[2][10] = {
15+
{ 0, 1, 2, 3, 4 },
16+
{ 5, 6, 7, 8, 9 }
17+
};
18+
19+
array_multi[1][2]
20+
// 7
21+
22+
// NOTE: no built-ins for length
23+
24+
// calculate length based on type
25+
int length = sizeof(array_one) / sizeof(double);
26+
length
27+
// 4

c/basics/constants.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// OUTSIDE SCOPE: define constants
2+
#define LENGTH 10
3+
#define WIDTH 5
4+
5+
// INSIDE SCOPE: define constants
6+
const int LENGTH = 10;
7+
const int WIDTH = 5;

c/basics/error-handling.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <errno.h>
2+
3+
// OUTSIDE SCOPE: define error variable
4+
extern int errno;
5+
6+
// INSIDE SCOPE: handle error
7+
FILE *file;
8+
9+
int errnum;
10+
file = fopen("file.txt", "rb");
11+
12+
// error returns -1 or NULL
13+
if (file == NULL) {
14+
// error code is stored in errno
15+
errnum = errno;
16+
17+
// print error code
18+
fprintf(stderr, "Error code: %d\n", errno);
19+
20+
// perror: print string representation of error code
21+
perror("Error as printed by perror");
22+
23+
// strerror: print pointer to string representation of error code
24+
fprintf(stderr, "Error opening file: %s\n", strerror(errnum));
25+
} else {
26+
fclose(file);
27+
}
28+
29+
// EXAMPLE: divide by zero
30+
int dividend = 10;
31+
int divisor = 2;
32+
int quotient;
33+
34+
// check if divisor is zero
35+
if (divisor == 0) {
36+
fprintf(stderr, "%s\n", "Division by zero, exiting...");
37+
38+
// exit as failure
39+
exit(EXIT_FAILURE);
40+
}
41+
42+
quotient = dividend / divisor;
43+
fprintf(stderr, "Quotient: %d\n", quotient);
44+
// 5
45+
46+
// exit as success
47+
exit(EXIT_SUCCESS);

c/basics/files.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FILE *file;
2+
3+
// write to file
4+
file = fopen("test.txt", "w+");
5+
fprintf(file, "%s\n", "This is a string.");
6+
7+
// close file
8+
fclose(file);
9+
10+
// read a file
11+
char buffer[255];
12+
13+
file = fopen("test.txt", "r");
14+
15+
// stops at first space
16+
fscanf(file, "%s\n", buffer);
17+
printf("%s\n", buffer);
18+
// This
19+
20+
// stops at newline
21+
fgets(buffer, 255, (FILE*)file);
22+
printf("%s\n", buffer);
23+
// is a string.

c/basics/for-loop.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
int a;
2+
for (a = 1; a < 5; a++) {
3+
// do something
4+
}
5+
6+
// iterate array
7+
#define ARRAY_SIZE 5
8+
9+
int array[ARRAY_SIZE] = { 1, 2, 3, 4, 5 };
10+
11+
int n;
12+
for (n = 0; n < ARRAY_SIZE; n++) {
13+
// do something
14+
}
15+
16+
// nested
17+
int i;
18+
int j;
19+
20+
for (i = 0; i < 2; i++) {
21+
for (j = 0; j < 2; j++) {
22+
// do something
23+
}
24+
}

c/basics/functions.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// declare function
2+
// NOTE: needs to be BEFORE main() when functions are DEFINED AFTER main()
3+
int function(int arg);
4+
5+
// define function
6+
int function(int arg) {
7+
// do something
8+
return 0;
9+
}
10+
11+
// EXAMPLE: power function
12+
13+
// declare function
14+
int power(int base, int x);
15+
16+
// main() function
17+
int main(void) {
18+
// call function
19+
power(2, 3) // 8
20+
21+
return 0;
22+
}
23+
24+
// define function
25+
int power(int base, int x) {
26+
/* multiply base with itself x times */
27+
28+
int result = 1;
29+
30+
int i;
31+
for (i = 0; i < x; i++) {
32+
result = result * base;
33+
}
34+
35+
return result;
36+
}
37+
38+
// END EXAMPLE
39+
40+
// variable arguments
41+
#include <stdarg.h>
42+
43+
// OUTSIDE SCOPE: define function with variable arguments
44+
double average(int n, ...) {
45+
// require stdarg.h
46+
va_list valist;
47+
48+
double sum = 0.0;
49+
int i;
50+
51+
// initialise valist for n arguments
52+
va_start(valist, n);
53+
54+
// access arguments assigned to valist
55+
for (i = 0; i < n; i++) {
56+
sum += va_arg(valist, int);
57+
}
58+
59+
// clean memory reserved for valist
60+
va_end(valist);
61+
62+
return sum / n;
63+
}
64+
65+
// INSIDE SCOPE: use function
66+
printf("Average of 2, 3, 4, 5: %f\n", average(2, 3, 4, 5));
67+
// Average of 2, 3, 4, 5: 3.500000

c/basics/header.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// FILE: header.h
2+
const int number = 10;
3+
4+
// FILE: header.c
5+
6+
// include header
7+
#include "header.h"
8+
9+
printf("%d\n", number);
10+
// 10

c/basics/header.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// FILE: header.h
2+
const int number = 10;

c/basics/if-else.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
int a = 1;
2+
3+
if (a < 1) {
4+
// do something
5+
} else if (a == 1) {
6+
// do something
7+
} else {
8+
// do something
9+
}
10+
11+
// single line expression
12+
(a < 1) ? "a is smaller" : "a is not smaller";
13+
// a is not smaller

0 commit comments

Comments
 (0)