Skip to content

Commit 1563691

Browse files
committed
c ignore
1 parent 616499a commit 1563691

24 files changed

+759
-0
lines changed

c/_ignore/_hello-c.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// 2020-11
2+
#include <stdio.h>
3+
4+
int main(int argc, char *argv[]) {
5+
// filename
6+
printf("filename: %s\n", argv[0]);
7+
// arguments
8+
if (argc > 1) {
9+
printf("%s\n", "argv: ");
10+
for (int i = 1; i < argc; i++) {
11+
printf("\t%s\n", argv[i]);
12+
}
13+
}
14+
}
15+
16+
// $ gcc hello-c.c -o _hello ; ./_hello
17+
// filename: ./_hello
18+
19+
// $ gcc hello-c.c -o _hello-c.s -S -O0 -masm=intel -fno-stack-protector; cat _hello-c.s
20+
// <asm>

c/_ignore/_hello-c.s

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
.section __TEXT,__text,regular,pure_instructions
2+
.build_version macos, 13, 0 sdk_version 13, 3
3+
.intel_syntax noprefix
4+
.globl _main ## -- Begin function main
5+
.p2align 4, 0x90
6+
_main: ## @main
7+
.cfi_startproc
8+
## %bb.0:
9+
push rbp
10+
.cfi_def_cfa_offset 16
11+
.cfi_offset rbp, -16
12+
mov rbp, rsp
13+
.cfi_def_cfa_register rbp
14+
sub rsp, 32
15+
mov dword ptr [rbp - 4], 0
16+
mov dword ptr [rbp - 8], edi
17+
mov qword ptr [rbp - 16], rsi
18+
mov rax, qword ptr [rbp - 16]
19+
mov rsi, qword ptr [rax]
20+
lea rdi, [rip + L_.str]
21+
mov al, 0
22+
call _printf
23+
cmp dword ptr [rbp - 8], 1
24+
jle LBB0_6
25+
## %bb.1:
26+
lea rdi, [rip + L_.str.1]
27+
lea rsi, [rip + L_.str.2]
28+
mov al, 0
29+
call _printf
30+
mov dword ptr [rbp - 20], 1
31+
LBB0_2: ## =>This Inner Loop Header: Depth=1
32+
mov eax, dword ptr [rbp - 20]
33+
cmp eax, dword ptr [rbp - 8]
34+
jge LBB0_5
35+
## %bb.3: ## in Loop: Header=BB0_2 Depth=1
36+
mov rax, qword ptr [rbp - 16]
37+
movsxd rcx, dword ptr [rbp - 20]
38+
mov rsi, qword ptr [rax + 8*rcx]
39+
lea rdi, [rip + L_.str.3]
40+
mov al, 0
41+
call _printf
42+
## %bb.4: ## in Loop: Header=BB0_2 Depth=1
43+
mov eax, dword ptr [rbp - 20]
44+
add eax, 1
45+
mov dword ptr [rbp - 20], eax
46+
jmp LBB0_2
47+
LBB0_5:
48+
jmp LBB0_6
49+
LBB0_6:
50+
xor eax, eax
51+
add rsp, 32
52+
pop rbp
53+
ret
54+
.cfi_endproc
55+
## -- End function
56+
.section __TEXT,__cstring,cstring_literals
57+
L_.str: ## @.str
58+
.asciz "filename: %s\n"
59+
60+
L_.str.1: ## @.str.1
61+
.asciz "%s\n"
62+
63+
L_.str.2: ## @.str.2
64+
.asciz "argv: "
65+
66+
L_.str.3: ## @.str.3
67+
.asciz "\t%s\n"
68+
69+
.subsections_via_symbols
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <stdio.h>
2+
#include <math.h>
3+
4+
int main(void) {
5+
int base;
6+
7+
scanf("%d", &base);
8+
if (base > 1) {
9+
printf("%.8f\n", sqrt(base));
10+
11+
} else {
12+
printf("%s\n", "Invalid base.");
13+
}
14+
}
15+
16+
// ./main
17+
// 2
18+
// 1.41421356
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdio.h>
2+
#include <assert.h>
3+
4+
// declare functions
5+
double c_to_f(double temp);
6+
double f_to_c(double temp);
7+
8+
int main(void) {
9+
double temp;
10+
char unit;
11+
12+
scanf("%lf %c", &temp, &unit);
13+
if (unit == 'C') {
14+
printf("%.1f F", c_to_f(temp));
15+
16+
} else if (unit == 'F') {
17+
printf("%.1f C", f_to_c(temp));
18+
}
19+
}
20+
21+
// define functions
22+
23+
// convert C to F
24+
double c_to_f(double temp) {
25+
return ((9.0 / 5) * temp + 32);
26+
}
27+
28+
// convert F to C
29+
double f_to_c(double temp) {
30+
return ((5.0 / 9) * (temp - 32));
31+
}
32+
33+
// ./main
34+
// 40 C
35+
// 104.0 F

c/_ignore/_primer/_header.h

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef FILENAME_H
2+
#define FILENAME_H
3+
4+
const int number = 10;
5+
6+
#endif

c/_ignore/_primer/_test.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a string.

c/_ignore/_primer/arrays.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// 2019-08
2+
#include <stdio.h>
3+
4+
int main() {
5+
// array of doubles
6+
double doubles[] = { 200.0, -2.2, 1.0, 0.0 };
7+
printf("%f\n", doubles[1]);
8+
// -2.200000
9+
10+
// arrays are mutable
11+
doubles[1] = 42;
12+
printf("%f\n", doubles[1]);
13+
// 42.000000
14+
15+
// array of arrays of char with max size 10 (strings)
16+
char strings[][10] = { "first", "second" };
17+
printf("%s\n", strings[1]);
18+
// second
19+
20+
// nd-array of ints
21+
int matrix[2][10] = {
22+
{ 0, 1, 2, 3, 4 },
23+
{ 5, 6, 7, 8, 9 }
24+
};
25+
printf("%d\n", matrix[1][2]);
26+
// 7
27+
28+
// calculate length based on type (in bytes)
29+
printf("%lu\n", sizeof(doubles) / sizeof(double));
30+
// 4
31+
}

c/_ignore/_primer/conditionals.c

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// 2019-08
2+
#include <stdio.h>
3+
4+
#define ANSWER 42
5+
6+
int main() {
7+
int value = 42;
8+
9+
if (value < 1) {
10+
// no
11+
} else if (value == 1) {
12+
// no
13+
} else if (value == ANSWER) {
14+
// yes!
15+
printf("%d is the answer: %d\n", value, ANSWER);
16+
} else {
17+
// no
18+
}
19+
// 42 is the answer: 42
20+
21+
// single line
22+
printf("%s\n", (value != ANSWER) ? "no" : "this is the answer");
23+
// this is the answer
24+
}

c/_ignore/_primer/consts.c

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// 2019-08
2+
#include <stdio.h>
3+
4+
// #define is a preprocessor directive (substitute text, inlining)
5+
#define A 100
6+
#define B 200
7+
8+
int main() {
9+
// const is an immutable variable
10+
const int C = 300;
11+
const int D = 400;
12+
13+
printf("%d\n", A);
14+
// 100
15+
printf("%d\n", B);
16+
// 200
17+
printf("%d\n", C);
18+
// 300
19+
printf("%d\n", D);
20+
// 400
21+
}

c/_ignore/_primer/errors.c

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// 2019-08
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
#include <errno.h>
6+
7+
extern int errno;
8+
9+
int main() {
10+
// pointer to file
11+
FILE *file;
12+
// error
13+
int errnum;
14+
// open file that does not exist
15+
file = fopen("file_does_not_exist", "rb"); // error returns -1 or NULL
16+
if (file == NULL) {
17+
// store error code
18+
errnum = errno;
19+
20+
// print error code
21+
fprintf(stderr, "error code: %d\n", errno);
22+
// error code: 2
23+
24+
// perror to print string representation of error code
25+
perror("");
26+
// No such file or directory
27+
28+
// strerror to print pointer to string representation of error code
29+
fprintf(stderr, "error opening file: %s\n", strerror(errnum));
30+
// error opening file: No such file or directory
31+
} else {
32+
fclose(file);
33+
}
34+
35+
// exit codes
36+
if (1 == 0) {
37+
// exit as failure (since something must be wrong?)
38+
exit(EXIT_FAILURE);
39+
} else {
40+
exit(EXIT_SUCCESS);
41+
}
42+
}

c/_ignore/_primer/files.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// 2019-08
2+
#include <stdio.h>
3+
4+
int main() {
5+
FILE *file;
6+
7+
// write to file
8+
file = fopen("_file.txt", "w+");
9+
fprintf(file, "%s\n", "Text to write to file.\n Next line.");
10+
// close file
11+
fclose(file);
12+
13+
// read file
14+
file = fopen("_file.txt", "r");
15+
16+
char buffer[255];
17+
18+
// stop read after first space
19+
// note that reading a word removes it from stream
20+
fscanf(file, "%s\n", buffer);
21+
printf("%s\n", buffer);
22+
// Text
23+
24+
// stop read after first newline
25+
fgets(buffer, 255, (FILE*) file);
26+
printf("%s\n", buffer);
27+
// to write to file.
28+
}

c/_ignore/_primer/functions.c

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// 2019-08
2+
#include <stdio.h>
3+
4+
// declare function (normally in header file)
5+
int power(int base, int x);
6+
// define function
7+
int power(int base, int x) {
8+
int result = 1;
9+
for (int i=0; i<x; i++) {
10+
result *= base;
11+
}
12+
// success
13+
return result;
14+
}
15+
16+
int main() {
17+
printf("%d\n", power(2, 8));
18+
// 256
19+
}

c/_ignore/_primer/header-files.c

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// 2020-11
2+
#include <stdio.h>
3+
4+
// filename.h
5+
/*
6+
#ifndef FILENAME_H
7+
#define FILENAME_H
8+
9+
const int number = 10;
10+
11+
#endif
12+
*/
13+
14+
#include "filename.h"
15+
16+
int main() {
17+
printf("%d\n", number);
18+
// 10
19+
}

c/_ignore/_primer/logicals.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// 2019-08
2+
#include <stdio.h>
3+
#include <stdbool.h>
4+
5+
int main() {
6+
const bool YES = true;
7+
const bool NO = false;
8+
9+
printf("%d\n", YES || NO);
10+
// 1
11+
printf("%d\n", YES && (YES && NO));
12+
// 0
13+
printf("%d\n", !YES);
14+
// 0
15+
printf("%d\n", !(!YES));
16+
// 1
17+
18+
// equality
19+
printf("%d\n", 1 == 1);
20+
// 1
21+
printf("%d\n", 1 != 1);
22+
// 0
23+
printf("%d\n", 1 > 0);
24+
// 1
25+
printf("%d\n", 1 < 0);
26+
// 0
27+
printf("%d\n", 1 >= 1);
28+
// 1
29+
printf("%d\n", 1 <= 0);
30+
// 0
31+
}

0 commit comments

Comments
 (0)