forked from thradams/cake
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
776 changed files
with
17,011 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
struct X { int i; void* p; }; | ||
void f(struct X* p) {} | ||
|
||
int main() | ||
int main(void) | ||
{ | ||
const struct X x = {0}; | ||
f(&x); | ||
} | ||
#pragma cake diagnostic check "-Wdiscarded-qualifiers" | ||
int n = 1; | ||
int a[n]; // re-allocated 10 times, each with a different size | ||
int k = (sizeof a / sizeof * a); | ||
|
||
int b[2]; | ||
int k2 = (sizeof b / sizeof * b); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//en.cppreference.com/w/c/algorithm/bsearch.html | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
struct data { | ||
int nr; | ||
char const *value; | ||
} dat[] = { | ||
{1, "Foo"}, {2, "Bar"}, {3, "Hello"}, {4, "World"} | ||
}; | ||
|
||
int data_cmp(void const *lhs, void const *rhs) | ||
{ | ||
struct data const *const l = lhs; | ||
struct data const *const r = rhs; | ||
|
||
if (l->nr < r->nr) return -1; | ||
else if (l->nr > r->nr) return 1; | ||
else return 0; | ||
|
||
// return (l->nr > r->nr) - (l->nr < r->nr); // possible shortcut | ||
// return l->nr - r->nr; // erroneous shortcut (fails if INT_MIN is present) | ||
} | ||
|
||
int main(void) | ||
{ | ||
struct data key = { .nr = 3 }; | ||
struct data const *res = bsearch(&key, dat, sizeof dat / sizeof dat[0], | ||
sizeof dat[0], data_cmp); | ||
if (res) { | ||
printf("No %d: %s\n", res->nr, res->value); | ||
} else { | ||
printf("No %d not found\n", key.nr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//en.cppreference.com/w/c/algorithm/qsort.html | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <limits.h> | ||
|
||
int compare_ints(const void* a, const void* b) | ||
{ | ||
int arg1 = *(const int*)a; | ||
int arg2 = *(const int*)b; | ||
|
||
if (arg1 < arg2) return -1; | ||
if (arg1 > arg2) return 1; | ||
return 0; | ||
|
||
// return (arg1 > arg2) - (arg1 < arg2); // possible shortcut | ||
// return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present) | ||
} | ||
|
||
int main(void) | ||
{ | ||
int ints[] = { -2, 99, 0, -743, 2, INT_MIN, 4 }; | ||
int size = sizeof ints / sizeof *ints; | ||
|
||
qsort(ints, size, sizeof(int), compare_ints); | ||
|
||
for (int i = 0; i < size; i++) { | ||
printf("%d ", ints[i]); | ||
} | ||
|
||
printf("\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//en.cppreference.com/w/c/chrono/localtime.html | ||
#define __STDC_WANT_LIB_EXT1__ 1 | ||
#define _XOPEN_SOURCE // for putenv | ||
#include <time.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> // for putenv | ||
|
||
int main(void) | ||
{ | ||
time_t t = time(NULL); | ||
printf("UTC: %s", asctime(gmtime(&t))); | ||
printf("local: %s", asctime(localtime(&t))); | ||
// POSIX-specific | ||
putenv("TZ=Asia/Singapore"); | ||
printf("Singapore: %s", asctime(localtime(&t))); | ||
|
||
#ifdef __STDC_LIB_EXT1__ | ||
struct tm buf; | ||
char str[26]; | ||
asctime_s(str,sizeof str,gmtime_s(&t, &buf)); | ||
printf("UTC: %s", str); | ||
asctime_s(str,sizeof str,localtime_s(&t, &buf)); | ||
printf("local: %s", str); | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//en.cppreference.com/w/c/language/character_constant.html | ||
#include <stddef.h> | ||
#include <stdio.h> | ||
#include <uchar.h> | ||
|
||
int main (void) | ||
{ | ||
printf("constant value \n"); | ||
printf("-------- ----------\n"); | ||
|
||
// integer character constants, | ||
int c1='a'; printf("'a':\t %#010x\n", c1); | ||
int c2='🍌'; printf("'🍌':\t %#010x\n\n", c2); // implementation-defined | ||
|
||
// multicharacter constant | ||
int c3='ab'; printf("'ab':\t %#010x\n\n", c3); // implementation-defined | ||
|
||
// 16-bit wide character constants | ||
char16_t uc1 = u'a'; printf("'a':\t %#010x\n", (int)uc1); | ||
char16_t uc2 = u'¢'; printf("'¢':\t %#010x\n", (int)uc2); | ||
char16_t uc3 = u'猫'; printf("'猫':\t %#010x\n", (int)uc3); | ||
// implementation-defined (🍌 maps to two 16-bit characters) | ||
char16_t uc4 = u'🍌'; printf("'🍌':\t %#010x\n\n", (int)uc4); | ||
|
||
// 32-bit wide character constants | ||
char32_t Uc1 = U'a'; printf("'a':\t %#010x\n", (int)Uc1); | ||
char32_t Uc2 = U'¢'; printf("'¢':\t %#010x\n", (int)Uc2); | ||
char32_t Uc3 = U'猫'; printf("'猫':\t %#010x\n", (int)Uc3); | ||
char32_t Uc4 = U'🍌'; printf("'🍌':\t %#010x\n\n", (int)Uc4); | ||
|
||
// wide character constants | ||
wchar_t wc1 = L'a'; printf("'a':\t %#010x\n", (int)wc1); | ||
wchar_t wc2 = L'¢'; printf("'¢':\t %#010x\n", (int)wc2); | ||
wchar_t wc3 = L'猫'; printf("'猫':\t %#010x\n", (int)wc3); | ||
wchar_t wc4 = L'🍌'; printf("'🍌':\t %#010x\n\n", (int)wc4); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//en.cppreference.com/w/c/language/compound_literal.html | ||
#include <stdio.h> | ||
|
||
int *p = (int[]){2, 4}; // creates an unnamed static array of type int[2] | ||
// initializes the array to the values {2, 4} | ||
// creates pointer p to point at the first element of | ||
// the array | ||
const float *pc = (const float []){1e0, 1e1, 1e2}; // read-only compound literal | ||
|
||
struct point {double x,y;}; | ||
|
||
int main(void) | ||
{ | ||
int n = 2, *p = &n; | ||
p = (int [2]){*p}; // creates an unnamed automatic array of type int[2] | ||
// initializes the first element to the value formerly | ||
// held in *p | ||
// initializes the second element to zero | ||
// stores the address of the first element in p | ||
|
||
void drawline1(struct point from, struct point to); | ||
void drawline2(struct point *from, struct point *to); | ||
drawline1( | ||
(struct point){.x=1, .y=1}, // creates two structs with block scope and | ||
(struct point){.x=3, .y=4}); // calls drawline1, passing them by value | ||
drawline2( | ||
&(struct point){.x=1, .y=1}, // creates two structs with block scope and | ||
&(struct point){.x=3, .y=4}); // calls drawline2, passing their addresses | ||
} | ||
|
||
void drawline1(struct point from, struct point to) | ||
{ | ||
printf("drawline1: `from` @ %p {%.2f, %.2f}, `to` @ %p {%.2f, %.2f}\n", | ||
(void*)&from, from.x, from.y, (void*)&to, to.x, to.y); | ||
} | ||
|
||
void drawline2(struct point *from, struct point *to) | ||
{ | ||
printf("drawline2: `from` @ %p {%.2f, %.2f}, `to` @ %p {%.2f, %.2f}\n", | ||
(void*)from, from->x, from->y, (void*)to, to->x, to->y); | ||
} |
Oops, something went wrong.