Last active
November 3, 2018 15:25
-
-
Save TomasDrozdik/2e0f451c561398bf2ac4c4a8389b8654 to your computer and use it in GitHub Desktop.
C_specialities
This file contains hidden or 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
#include <stdio.h> | |
#include <stddef.h> | |
struct foo { | |
unsigned short a : 15; | |
short b : 2; | |
}; | |
int | |
main(void) | |
{ | |
printf("%zu\n", sizeof (struct foo)); | |
int a = 65534; | |
int b = -5; | |
struct foo foo = { .a = 0, .b = 0 }; | |
foo.a = a; | |
foo.b = b; | |
printf("%x -> %x (%d)\n", a, foo.a, foo.a); | |
printf("%x -> %x (%d)\n", b, foo.b, foo.b); | |
return (0); | |
} |
This file contains hidden or 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
#include <stdlib.h> | |
struct item { | |
int header; | |
int len; | |
char payload[]; // must be last member | |
}; | |
static struct item * | |
allocate(size_t payload_len) | |
{ | |
// offsetof(struct item, payload) might be better to use here. | |
struct item *p = malloc(sizeof (struct item) + | |
payload_len * sizeof (p->payload[0])); | |
if (p == NULL) | |
return (p); | |
p->len = payload_len; | |
for (size_t i = 0; i < p->len; i++) | |
p->payload[i] = 'A'; | |
return (p); | |
} | |
int | |
main(void) | |
{ | |
struct item *p = allocate(100); | |
// do sth to make sure the code is not optimized away | |
return (0); | |
} |
This file contains hidden or 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
#define MYOFFSETOF(s, m) \ | |
(size_t)(&((s *)0)->m) | |
#include <stdio.h> | |
int | |
main(void) | |
{ | |
struct X { char a; int b; char c; }; | |
printf("%zu\n", MYOFFSETOF(struct X, c)); | |
return (0); | |
} | |
This file contains hidden or 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
/* | |
* Print a decimal number in argv[1] in binary. Limit the input to a positive | |
* int. | |
* | |
* $ ./a.out 2147483647 | |
* 1111111111111111111111111111111 | |
* $ ./a.out 255 | |
* 11111111 | |
* $ ./a.out 348508345 | |
* 10100110001011101000010111001 | |
* | |
* Use bc(1) with obase=2 to verify the output. | |
*/ | |
#include <assert.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
int | |
main(int argc, char **argv) | |
{ | |
assert(argc == 2); | |
char a[sizeof (int) * 8]; | |
int i = 1, n = atoi(argv[1]); | |
assert(n >= 0); | |
(void) memset(a, '0', sizeof (a)); | |
a[sizeof (a) - 1] = '\0'; | |
while (n > 0) { | |
if (n % 2 == 1) | |
a[sizeof (a) - i - 1] = '1'; | |
if ((n = n / 2) > 0) | |
++i; | |
} | |
printf("%s\n", a + sizeof (a) - i - 1); | |
return (0); | |
} |
This file contains hidden or 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
#include <stdio.h> | |
#define static_assert(p, cnt) \ | |
typedef int __assert ## cnt[(p) ? 1 : -1] | |
static void | |
foo(void *x) | |
{ | |
printf("%p\n", x); | |
} | |
int | |
main(void) | |
{ | |
int i = 0xff; | |
static_assert(sizeof(void *) >= sizeof i, 1); | |
foo((void *) i); | |
} |
This file contains hidden or 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
#include <stdio.h> | |
union bar_u { | |
unsigned short i; // 2 bytes | |
struct { | |
unsigned char low; | |
unsigned char high; | |
} bytes; | |
}; | |
int | |
main(void) | |
{ | |
union bar_u bar = { .i = 256 }; | |
printf("size = %zu bytes\n", sizeof (bar)); | |
printf("low: %x high: %x\n", bar.bytes.low, bar.bytes.high); | |
if (bar.bytes.low == 1) | |
printf("big endian\n"); | |
else | |
printf("little endian\n"); | |
return (0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment