]> Pileus Git - ~andy/sfvlug/blob - c/src/data.c
Add C notes.
[~andy/sfvlug] / c / src / data.c
1 #include <stdio.h>
2 #include <stdint.h>
3
4 int main()
5 {
6         /*
7          * Numeric types.
8          */
9         int the_answer = 42;
10
11         signed negative = -100;
12
13         unsigned positive = 100;
14
15         long long trillion = 1000000000000;
16
17         unsigned long long int neg_trillion = -1000000000000;
18
19         short two_bytes = 65000;
20
21         char one_byte = 123;
22
23         printf("Numbers\n");
24         printf("  the_answer   = %d\n",   the_answer);
25         printf("  positive     = %d\n",   positive);
26         printf("  negative     = %u\n",   negative);
27         printf("  trillion     = %lld\n", trillion);
28         printf("  neg_trillion = %lld\n", neg_trillion);
29         printf("  two_bytes    = %hd\n",  two_bytes);
30         printf("  one_byte     = %hhd\n", one_byte);
31         printf("\n");
32
33         /*
34          * Standard integers.
35          */
36         int8_t signed_8bit = 1;
37
38         uint8_t unsigned_8bit = 2;
39
40         int64_t signed_64bit = 3;
41
42         uint64_t unsigned_64bit = 4;
43
44         printf("Standard Ints\n");
45         printf("  signed_8bit    = %hhd\n", signed_8bit);
46         printf("  unsigned_8bit  = %hhu\n", unsigned_8bit);
47         printf("  signed_6bit_   = %ld\n",  signed_64bit);
48         printf("  unsigned_64bit = %lu\n",  unsigned_64bit);
49         printf("\n");
50
51         /*
52          * Characeters.
53          */
54         char a_char = 'a';
55
56         char a_int = 97;
57
58         char a_hex = 0x61;
59
60         printf("Standard Ints\n");
61         printf("  as integers = %2d %2d %2d\n", a_char, a_int, a_hex);
62         printf("  as chars    = %2c %2c %2c\n", a_char, a_int, a_hex);
63         printf("  as hex      = %2x %2x %2x\n", a_char, a_int, a_hex);
64         printf("\n");
65
66         /*
67          * Arrays.
68          */
69         int fixed_size[100] = {1, 2, 3};
70
71         int auto_sized[] = {1, 2, 3};
72
73         int tables[][32] = {
74                 {1, 2, 3},
75                 {4, 5, 6},
76         };
77
78         printf("Array sizes\n");
79         printf("  fixed_size  = %zd\n", sizeof(fixed_size));
80         printf("  auto_sized  = %zd\n", sizeof(auto_sized));
81         printf("  table size  = %zd\n", sizeof(tables));
82         printf("  first       = %d\n",  fixed_size[0]);
83         printf("  third       = %d\n",  fixed_size[2]);
84         printf("  last        = %d\n",  fixed_size[99]);
85         printf("  tables      = %d\n",  tables[0][0]);
86         printf("\n");
87
88         /*
89          * Strings.
90          */
91         char hello_char[] = {'h', 'e', 'l', 'l', 'o', '\0'};
92
93         char hello_str[] = "hello";
94
95         char *hello_ptr = "hello";
96
97         printf("Strings\n");
98         printf("  as strings  = %s %s %s\n",
99                         hello_char,
100                         hello_str,
101                         hello_ptr);
102         printf("  as arrays   = %c %c %c\n",
103                         hello_char[0],
104                         hello_str[0],
105                         hello_ptr[0]);
106         printf("  sizes       = %zd %zd %zd\n",
107                         sizeof(hello_char),
108                         sizeof(hello_str),
109                         sizeof(hello_ptr));
110         printf("\n");
111
112         /*
113          * Pointers.
114          */
115         int one = 1;
116
117         int two = 2;
118
119         int *pointer = &one;
120
121         *pointer = 100;
122
123         printf("Strings\n");
124         printf("  one      = %d\n",   one);
125         printf("  two      = %d\n",   two);
126         printf("  pointer  = %llx\n", (long long)pointer);
127         printf("  pointer  = %p\n",   pointer);
128         printf("  value    = %d\n",   *pointer);
129
130
131         return 0;
132 }