]> Pileus Git - ~andy/sfvlug/blobdiff - c/src/data.c
Add C notes.
[~andy/sfvlug] / c / src / data.c
diff --git a/c/src/data.c b/c/src/data.c
new file mode 100644 (file)
index 0000000..331bbb1
--- /dev/null
@@ -0,0 +1,132 @@
+#include <stdio.h>
+#include <stdint.h>
+
+int main()
+{
+       /*
+        * Numeric types.
+        */
+       int the_answer = 42;
+
+       signed negative = -100;
+
+       unsigned positive = 100;
+
+       long long trillion = 1000000000000;
+
+       unsigned long long int neg_trillion = -1000000000000;
+
+       short two_bytes = 65000;
+
+       char one_byte = 123;
+
+       printf("Numbers\n");
+       printf("  the_answer   = %d\n",   the_answer);
+       printf("  positive     = %d\n",   positive);
+       printf("  negative     = %u\n",   negative);
+       printf("  trillion     = %lld\n", trillion);
+       printf("  neg_trillion = %lld\n", neg_trillion);
+       printf("  two_bytes    = %hd\n",  two_bytes);
+       printf("  one_byte     = %hhd\n", one_byte);
+       printf("\n");
+
+       /*
+        * Standard integers.
+        */
+       int8_t signed_8bit = 1;
+
+       uint8_t unsigned_8bit = 2;
+
+       int64_t signed_64bit = 3;
+
+       uint64_t unsigned_64bit = 4;
+
+       printf("Standard Ints\n");
+       printf("  signed_8bit    = %hhd\n", signed_8bit);
+       printf("  unsigned_8bit  = %hhu\n", unsigned_8bit);
+       printf("  signed_6bit_   = %ld\n",  signed_64bit);
+       printf("  unsigned_64bit = %lu\n",  unsigned_64bit);
+       printf("\n");
+
+       /*
+        * Characeters.
+        */
+       char a_char = 'a';
+
+       char a_int = 97;
+
+       char a_hex = 0x61;
+
+       printf("Standard Ints\n");
+       printf("  as integers = %2d %2d %2d\n", a_char, a_int, a_hex);
+       printf("  as chars    = %2c %2c %2c\n", a_char, a_int, a_hex);
+       printf("  as hex      = %2x %2x %2x\n", a_char, a_int, a_hex);
+       printf("\n");
+
+       /*
+        * Arrays.
+        */
+       int fixed_size[100] = {1, 2, 3};
+
+       int auto_sized[] = {1, 2, 3};
+
+       int tables[][32] = {
+               {1, 2, 3},
+               {4, 5, 6},
+       };
+
+       printf("Array sizes\n");
+       printf("  fixed_size  = %zd\n", sizeof(fixed_size));
+       printf("  auto_sized  = %zd\n", sizeof(auto_sized));
+       printf("  table size  = %zd\n", sizeof(tables));
+       printf("  first       = %d\n",  fixed_size[0]);
+       printf("  third       = %d\n",  fixed_size[2]);
+       printf("  last        = %d\n",  fixed_size[99]);
+       printf("  tables      = %d\n",  tables[0][0]);
+       printf("\n");
+
+       /*
+        * Strings.
+        */
+       char hello_char[] = {'h', 'e', 'l', 'l', 'o', '\0'};
+
+       char hello_str[] = "hello";
+
+       char *hello_ptr = "hello";
+
+       printf("Strings\n");
+       printf("  as strings  = %s %s %s\n",
+                       hello_char,
+                       hello_str,
+                       hello_ptr);
+       printf("  as arrays   = %c %c %c\n",
+                       hello_char[0],
+                       hello_str[0],
+                       hello_ptr[0]);
+       printf("  sizes       = %zd %zd %zd\n",
+                       sizeof(hello_char),
+                       sizeof(hello_str),
+                       sizeof(hello_ptr));
+       printf("\n");
+
+       /*
+        * Pointers.
+        */
+       int one = 1;
+
+       int two = 2;
+
+       int *pointer = &one;
+
+       *pointer = 100;
+
+       printf("Strings\n");
+       printf("  one      = %d\n",   one);
+       printf("  two      = %d\n",   two);
+       printf("  pointer  = %llx\n", (long long)pointer);
+       printf("  pointer  = %p\n",   pointer);
+       printf("  value    = %d\n",   *pointer);
+
+
+       return 0;
+}