Browse files

Code for the first two chapters

  • Loading branch information...
0 parents commit 2d4d34ab29f866720b9057387030804084e92afd @dogriffiths committed Aug 26, 2012
Showing with 1,248 additions and 0 deletions.
  1. +7 −0 chapter1/page2/c_rocks.c
  2. +28 −0 chapter1/page20/cards.c
  3. +15 −0 chapter1/page23/bethecompiler_a.c
  4. +14 −0 chapter1/page23/bethecompiler_b.c
  5. +14 −0 chapter1/page23/bethecompiler_c.c
  6. +14 −0 chapter1/page23/bethecompiler_d.c
  7. +63 −0 chapter1/page35/finalexercise.c
  8. +19 −0 chapter1/page5/cards.c
  9. +22 −0 chapter1/page7/cards.c
  10. +10 −0 chapter2/array_address.c
  11. +16 −0 chapter2/codeForTestDrivePage13.c
  12. +37 −0 chapter2/comma.c
  13. +15 −0 chapter2/food.c
  14. +33 −0 chapter2/fred.c
  15. +15 −0 chapter2/handsomeness.c
  16. +12 −0 chapter2/mating.c
  17. +11 −0 chapter2/mating2.c
  18. +8 −0 chapter2/memory_address.c
  19. +18 −0 chapter2/new_monte.c
  20. +6 −0 chapter2/no_return.c
  21. +16 −0 chapter2/page45/southeast.c
  22. +16 −0 chapter2/page50/southeast.c
  23. +12 −0 chapter2/page53/fortune_cookie.c
  24. +13 −0 chapter2/page54/fortune_cookie.c
  25. +12 −0 chapter2/page54/fortune_cookie.c~
  26. +11 −0 chapter2/page57/mating.c
  27. +13 −0 chapter2/page62/sharpen.c
  28. +184 −0 chapter2/page62/sharpen.c~
  29. +10 −0 chapter2/page66/food.c
  30. +184 −0 chapter2/page66/food.c~
  31. +10 −0 chapter2/page67/food.c
  32. +10 −0 chapter2/page67/food.c~
  33. +14 −0 chapter2/page69/monte.c
  34. +18 −0 chapter2/page69/monte.c~
  35. +14 −0 chapter2/page72/monte.c
  36. +14 −0 chapter2/page72/monte.c~
  37. +16 −0 chapter2/pirate_game_broken.c
  38. +16 −0 chapter2/pirate_game_fixed.c
  39. +9 −0 chapter2/safe_gets.c
  40. +13 −0 chapter2/stackotrouble.c
  41. +14 −0 chapter2/test.c
  42. +23 −0 chapter2/test_scope.c
  43. +17 −0 chapter2/testwords.c
  44. +28 −0 chapter2/text_search.c
  45. +184 −0 chapter2/whereitlives.c
7 chapter1/page2/c_rocks.c
@@ -0,0 +1,7 @@
+#include <stdio.h>
+
+int main()
+{
+ puts("C Rocks!");
+ return 0;
+}
28 chapter1/page20/cards.c
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <stdlib.h>
+int main()
+{
+ char card_name[2];
+ puts("Enter the card_name: ");
+ scanf("%3s", card_name);
+ printf("%s\n", card_name);
+ int val = 0;
+ if (card_name[0] == 'K') {
+ val = 10;
+ } else if (card_name[0] == 'Q') {
+ val = 10;
+ } else if (card_name[0] == 'J') {
+ val = 10;
+ } else if (card_name[0] == 'A') {
+ val = 11;
+ } else {
+ val = atoi(card_name);
+ }
+ /* Check if the value is 3 to 6 */
+ if ((val > 2) && (val < 7))
+ puts("Count has gone up");
+ /* Otherwise check if the card was 10, J, Q or K */
+ else if (val == 10)
+ puts("Count has gone down");
+ return 0;
+}
15 chapter1/page23/bethecompiler_a.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+
+int main()
+{
+ int card = 1;
+ if (card > 1)
+ card = card - 1;
+ if (card < 7)
+ puts("Small card");
+ else {
+ puts("Ace!");
+ }
+
+ return 0;
+}
14 chapter1/page23/bethecompiler_b.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+int main()
+{
+ int card = 1;
+ if (card > 1) {
+ card = card - 1;
+ if (card < 7)
+ puts("Small card");
+ else
+ puts("Ace!");
+ }
+ return 0;
+}
14 chapter1/page23/bethecompiler_c.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+int main()
+{
+ int card = 1;
+ if (card > 1) {
+ card = card - 1;
+ if (card < 7)
+ puts("Small card");
+ } else
+ puts("Ace!");
+
+ return 0;
+}
14 chapter1/page23/bethecompiler_d.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+int main()
+{
+ int card = 1;
+ if (card > 1) {
+ card = card - 1;
+ if (card < 7)
+ puts("Small card");
+ else
+ puts("Ace!");
+
+ return 0;
+}
63 chapter1/page35/finalexercise.c
@@ -0,0 +1,63 @@
+/**
+ * THIS PROGRAM IS DIFFERENT TO THE ONE IN THE BOOK!
+ *
+ * We all make mistakes. After the book was published, one keen-eyed
+ * spotted that there was a problem with the program published on
+ * page 35/37 in the book.
+ *
+ * In the original version of the program the while loop
+ * checked the value of the card_name[0] *before* anything
+ * had been entered into the card_name array. What's the problem
+ * with that? In C, local variables are not set to any specific
+ * value when they're first created. That means that
+ * that theoretically the card_name[0] might have *any* char
+ * value. And that's bad.
+ *
+ * Instead, we've slightly re-written the code so that instead of
+ * using a while{} loop, it now uses a do-while{} loop.
+ *
+ * A do-while{} loop checks the lopp condition at the *end* of the
+ * loop, and by the time we get to the end of the loop the
+ * card_name array has been set to whatever the typed in at the keyboard.
+ *
+ * The coder responsible for this error has been warned that a
+ * terrible vengeance that will be wrought against them.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+int main()
+{
+ char card_name[2];
+ int count = 0;
+ do {
+ puts("Enter the card_name: ");
+ scanf("%s", card_name);
+ int val = 0;
+ switch(card_name[0]) {
+ case 'K':
+ case 'Q':
+ case 'J':
+ val = 10;
+ break;
+ case 'A':
+ val = 11;
+ break;
+ case 'X':
+ continue;
+ default:
+ val = atoi(card_name);
+ if ((val < 1) || (val > 10)) {
+ puts("I don't understand that value!");
+ continue;
+ }
+ }
+ if ((val > 2) && (val < 7)) {
+ count++;
+ } else if (val == 10) {
+ count--;
+ }
+ printf("Current count: %i\n", count);
+ } while (card_name[0] != 'X');
+ return 0;
+}
19 chapter1/page5/cards.c
@@ -0,0 +1,19 @@
+/* @(#)page5.c
+ */
+
+#include <stdio.h>
+
+int main()
+{
+ int decks;
+ puts("Enter a number of decks");
+ scanf("%i", &decks);
+ if (decks < 1) {
+ puts("That is not a valid number of decks");
+ return 1;
+ }
+ printf("There are %i cards\n", (decks * 52));
+ return 0;
+}
+
+
22 chapter1/page7/cards.c
@@ -0,0 +1,22 @@
+#include <stdio.h>
+#include <stdlib.h>
+int main()
+{
+ char card_name[2];
+ puts("Enter the card_name: ");
+ scanf("%s", card_name);
+ int val = 0;
+ if (card_name[0] == 'K') {
+ val = 10;
+ } else if (card_name[0] == 'Q') {
+ val = 10;
+ } else if (card_name[0] == 'J') {
+ val = 10;
+ } else if (card_name[0] == 'A') {
+ val = 11;
+ } else {
+ val = atoi(card_name);
+ }
+ printf("The card value is: %i\n", val);
+ return 0;
+}
10 chapter2/array_address.c
@@ -0,0 +1,10 @@
+#include <stdio.h>
+
+int main()
+{
+ char c[18] = "Bow ties are cool";
+ printf("c is %p\n", c);
+ printf("c[0] is at %p\n", &c[0]);
+ printf("c[1] is at %p\n", &c[1]);
+ return 0;
+}
16 chapter2/codeForTestDrivePage13.c
@@ -0,0 +1,16 @@
+#include <stdio.h>
+#include <stdlib.h>
+int main()
+{
+int stock = 180;
+int order;
+ while(stock > 0) {
+ printf("%i glasses left\n", stock);
+ scanf("%i", &order);
+
+ stock = stock - order;
+ printf("You ordered %i glasses\n", order);
+ }
+ puts("We're out of stock, baby!");
+ return 0;
+}
37 chapter2/comma.c
@@ -0,0 +1,37 @@
+#include <stdio.h>
+#include <limits.h>
+
+#define DAYS_OF_THE_WEEK 7
+
+#define ADD_ONE(x) ((x) + 1)
+
+#define ADD_ITSELF(a) ((a) + (a))
+#ifdef SPANISH
+char *greeting = "Hola";
+#else
+char *greeting = "Hello";
+#endif
+
+int counter()
+{
+ static int count = 0;
+ return ++count;
+}
+
+int main()
+{
+ char *s, t;
+ s = "HELLO";
+ t = 'a';
+ printf("There are %i days of the week\n", DAYS_OF_THE_WEEK);
+ printf("The answer is %i\n", ADD_ONE(3));
+ int i = 2;
+ printf("The answer is %i\n", ADD_ITSELF(++i));
+ printf("Say '%s'\n", greeting);
+ printf("Count = %i\n", counter());
+ printf("Count = %i\n", counter());
+ printf("On this machine an int takes up %lu bytes\n", sizeof(int));
+ printf("And ints can store values from %i to %i\n", INT_MIN, INT_MAX);
+ printf("And shorts can store values from %i to %i\n", SHRT_MIN, SHRT_MAX);
+ return 0;
+}
15 chapter2/food.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+
+int main()
+{
+ char food[50];
+ char food2[50];
+ printf("Enter favorite food: ");
+ scanf("%s", food);
+ scanf("%s", food2);
+ /* fgets(food, sizeof(food), stdin);*/
+ printf("Favorite food: %s and length = %i\n", food, sizeof(food));
+ printf("Favorite food2: %s and length = %i\n", food2, sizeof(food2));
+ printf("And the mad value is %li\n", food * 2);
+ return 0;
+}
33 chapter2/fred.c
@@ -0,0 +1,33 @@
+/* @(#)fred.c
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+int main()
+{
+ char s[][3] = {"AB", "CD"};
+ char* t[3] = {"AB", "CD"};
+ char **fred = s;
+ /* s[0] = s[1];*/
+ if (s == s[0])
+ puts("Equal????");
+ printf("s at %p\n", s);
+ printf("sizeof(s) is %i\n", sizeof(s));
+ printf("s[0] at %p\n", s[0]);
+ printf("s[1] at %p\n", s[1]);
+ printf("s[0] is %s\n", s[0]);
+ printf("t at %p\n", t);
+ printf("sizeof(t) is %i\n", sizeof(t));
+ printf("t[0] at %p\n", t[0]);
+ printf("t[1] at %p\n", t[1]);
+ printf("t[0] is %s\n", t[0]);
+ printf("fred at %p\n", fred);
+ printf("sizeof(fred) is %i\n", sizeof(fred));
+ printf("fred[0] at %p\n", fred[0]);
+ printf("fred[1] at %p\n", fred[1]);
+ printf("s[0] char is %s\n", s[0]);
+ printf("s[1] char is %s\n", s[1]);
+ printf("strlen(s[0]) is %i\n", strlen(s[0]));
+ return 0;
+}
15 chapter2/handsomeness.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+
+int larger(int a, int b)
+{
+ if (a > b)
+ return a;
+ return b;
+}
+
+int main()
+{
+ int greatest = larger(100, 1000);
+ printf("%i is the greatest!\n", greatest);
+ return 0;
+}
12 chapter2/mating.c
@@ -0,0 +1,12 @@
+#include <stdio.h>
+
+int main()
+{
+ int contestants[] = {1, 2, 3};
+ int* choice = contestants;
+ contestants[0] = 2;
+ contestants[1] = contestants[2];
+ contestants[2] = *choice;
+ printf("I'm going to pick contestant number %i\n", contestants[2]);
+ return 0;
+}
11 chapter2/mating2.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+int main()
+{
+ int contestants[] = {1, 2, 3};
+ int* choice = contestants;
+ contestants[0] = 2;
+ contestants[1] = contestants[2];
+ contestants[2] = *choice;
+ printf("I’m going to pick contestant number %i\n", contestants[2]);
+ return 0;
+}
8 chapter2/memory_address.c
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+int main()
+{
+ int x = 0;
+ printf("x is stored at %p\n", &x);
+ return 0;
+}
18 chapter2/new_monte.c
@@ -0,0 +1,18 @@
+ #include <stdio.h>
+
+ int main()
+ {
+ /* const int x = 1;
+ x = 2;
+ const char * s = "HERE";
+ s = "THERE";*/
+ char cards[] = "JQK";
+ char a_card = cards[2];
+ cards[2] = cards[1];
+ cards[1] = cards[0];
+ cards[0] = cards[2];
+ cards[2] = cards[1];
+ cards[1] = a_card;
+ puts(cards);
+ return 0;
+ }
6 chapter2/no_return.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+int main()
+{
+ printf("Hello World\n");
+}
16 chapter2/page45/southeast.c
@@ -0,0 +1,16 @@
+#include <stdio.h>
+
+void go_south_east(int lat, int lon)
+{
+ lat = lat - 1;
+ lon = lon + 1;
+}
+
+int main()
+{
+ int latitude = 32;
+ int longitude = -64;
+ go_south_east(latitude, longitude);
+ printf("Avast! Now at: [%i, %i]\n", latitude, longitude);
+ return 0;
+}
16 chapter2/page50/southeast.c
@@ -0,0 +1,16 @@
+#include <stdio.h>
+
+void go_south_east(int* lat, int* lon)
+{
+ *lat = *lat - 1;
+ *lon = *lon + 1;
+}
+
+int main()
+{
+ int latitude = 32;
+ int longitude = -64;
+ go_south_east(&latitude, &longitude);
+ printf("Avast! Now at: [%i, %i]\n", latitude, longitude);
+ return 0;
+}
12 chapter2/page53/fortune_cookie.c
@@ -0,0 +1,12 @@
+#include <stdio.h>
+
+void fortune_cookie(char msg[])
+{
+ printf("Message reads: %s\n", msg);
+}
+
+int main()
+{
+ char quote[] = "Cookies make you fat";
+ fortune_cookie(quote);
+}
13 chapter2/page54/fortune_cookie.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+void fortune_cookie(char msg[])
+{
+ printf("Message reads: %s\n", msg);
+ printf("msg occupies %i bytes\n", sizeof(msg));
+}
+
+int main()
+{
+ char quote[] = "Cookies make you fat";
+ fortune_cookie(quote);
+}
12 chapter2/page54/fortune_cookie.c~
@@ -0,0 +1,12 @@
+#include <stdio.h>
+
+void fortune_cookie(char msg[])
+{
+ printf("Message reads: %s\n", msg);
+}
+
+int main()
+{
+ char quote[] = "Cookies make you fat";
+ fortune_cookie(quote);
+}
11 chapter2/page57/mating.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+int main()
+{
+ int contestants[] = {1, 2, 3};
+ int* choice = contestants;
+ contestants[0] = 2;
+ contestants[1] = contestants[2];
+ contestants[2] = *choice;
+ printf("I’m going to pick contestant number %i\n", contestants[2]);
+ return 0;
+}
13 chapter2/page62/sharpen.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+void skip(char* msg)
+{
+ puts(msg + 6);
+}
+
+int main()
+{
+ char* msg_from_amy = "Don't call me";
+ skip(msg_from_amy);
+ return 0;
+}
184 chapter2/page62/sharpen.c~
@@ -0,0 +1,184 @@
+#include <stdio.h>
+
+
+void print_reverse(char* s)
+{
+ size_t len = strlen(s);
+ char* t = s + len - 1;
+ while (t >= s) {
+ printf("%c", *t);
+ t = t - 1;
+ }
+ puts("");
+}
+
+void fred()
+{
+ int x = 2;
+ /* return 1;*/
+}
+
+void dummy0(int* b)
+{
+ printf("dummy0 sizeof(b) = %li\n", sizeof(b));
+}
+
+void dummy1(int b[])
+{
+ printf("dummy1 sizeof(b) = %li\n", sizeof(b));
+}
+
+void print_me(char s[13])
+{
+ char* t;
+ printf("I've been told: %s\nsize = %i\nt size = %i", s, sizeof(s), sizeof(t));
+ printf("Location = %p\n", s);
+}
+
+void fortune_cookie(char msg[])
+{
+ printf("Message reads: %s\n", msg);
+ printf("msg occupies %i bytes\n", sizeof(msg));
+}
+
+void dosizeof(void* p)
+{
+ printf("p has size %i\n", sizeof(p));
+}
+
+void skip(char* msg)
+{
+ puts(msg + 6);
+}
+
+char food[10];
+
+
+int global0 = 1;
+int main()
+{
+ int local0 = 4;
+ float local1 = 2.0f;
+ int local2 = 4;
+ float local3 = 2.0f;
+ printf("local0 lives at %p, global0 lives at %p, local1 lives at %p, local2 lives at %p, local3 lives at %p\n",
+ &local0, &global0, &local1, &local2, &local3);
+ int int0;
+ int* int_pointer = &int0;
+ double double0;
+ double* double_pointer = &double0;
+ char char0;
+ char* char_pointer = &char0;
+ void* void_pointer = &int0;
+ printf("int0 is at %p\n", int_pointer);
+ printf("double0 is at %p\n", double_pointer);
+ printf("char0 is at %p\n", char_pointer);
+ printf("void_pointer is at %p\n", void_pointer);
+ int_pointer = int_pointer + 1;
+ double_pointer = double_pointer + 1;
+ char_pointer = char_pointer + 1;
+ void_pointer = void_pointer + 1;
+ printf("int0 is at %p\n", int_pointer);
+ printf("double0 is at %p\n", double_pointer);
+ printf("char0 is at %p\n", char_pointer);
+ printf("void_pointer is at %p\n", void_pointer);
+ int arr[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
+ int* a = arr;
+ printf("Item 2 = %i\n", arr[2]);
+ printf("Item 2 = %i\n", a[2]);
+ printf("Item 2 = %i\n", *(arr + 2));
+ printf("Item 2 = %i\n", *(a + 2));
+ arr[2] = 99;
+ printf("Item 2 = %i\n", arr[2]);
+ printf("Item 2 = %i\n", a[2]);
+ printf("Item 2 = %i\n", *(arr + 2));
+ printf("Item 2 = %i\n", *(a + 2));
+ *(arr + 1) = 98;
+ printf("Item 1 = %i\n", arr[1]);
+ printf("Item 1 = %i\n", a[1]);
+ printf("Item 1 = %i\n", *(arr + 1));
+ printf("Item 1 = %i\n", *(a + 1));
+ *(a + 1) = 97;
+ printf("Item 1 = %i\n", 1[arr]);
+ printf("Item 1 = %i\n", 1[a]);
+ printf("Item 1 = %i\n", *(arr + 1));
+ printf("Item 1 = %i\n", *(a + 1));
+ printf("a = %p\n", a);
+ printf("a + 1 = %p\n", (a + 1));
+ printf("a = %li\n", a);
+ printf("a + 1 = %li\n", (a + 1));
+ printf("sizeof(a) = %li\n", sizeof(a));
+ printf("sizeof(arr) = %li\n", sizeof(arr));
+ printf("a = %li, &a = %li\n", a, &a);
+ printf("arr = %li, &arr = %li\n", arr, &arr);
+ long fred = (long)a;
+ printf("fred = %li\n", fred);
+ dummy0(arr);
+ dummy1(arr);
+ printf("Location of original string: %p\n", "Hello world!");
+ printf("Location of original string: %p\n", "Hello world!1");
+ printf("A long has size %i\n", sizeof(long));
+ printf("A int has size %i\n", sizeof(int));
+ printf("Turtles has size %i\n", sizeof("Turtles!"));
+ print_me("Hello world!");
+ fortune_cookie("To have a friend, learn to be a friend");
+ char shoes[] = "sneakers";
+ printf("The shoes string is stored at: %p\n", shoes);
+ dosizeof(&fred);
+ dosizeof(shoes);
+ char s[] ="How big is it?";
+ char* t = s;
+ printf("s is %i\n", sizeof(s));
+ printf("t is %i\n", sizeof(t));
+ /* char anarray[] = "I am some array";
+ char achar = 'a';
+ char* p1;
+ p1 = &achar;*/
+ /* anarray = &achar;*/
+
+
+ char* msg_from_amy = "Don't call me";
+ skip(msg_from_amy);
+
+ int nums[] = {1, 2, 3};
+ printf("nums is at %li\n", nums);
+ printf("nums + 1 is at %li\n", nums + 1);
+
+ /* char name[40];
+ printf("Enter your name: ");
+ scanf("%39s", name);
+
+ char surname[40];
+ printf("\nEnter your surname: ");
+ scanf("%39s", surname);
+ printf("\nYour name: %s %s\n", name, surname);
+
+ char first_name[20];
+ char last_name[20];
+ printf("Enter first and last name: ");
+ scanf("%19s %19s", first_name, last_name);
+
+ printf("%s-%s\n", first_name, last_name);*/
+ /* int courses;
+ printf("Enter favorite food: ");
+ scanf("%s", food);
+ printf("Enter meal courses: ");
+ scanf("%i", &courses);
+ printf("Favorite food: %s", food);
+ printf("\nCourses: %i", courses);*/
+
+ char s0 = "Hello";
+ char * s1 = s0;
+ // char s2[];
+ printf("s0 is at %p\n", s0);
+ printf("s1 is at %p\n", s1);
+ // printf("s2 is at %p\n", s2);
+
+ print_reverse("Never odd or even");
+
+ char *s1000, t1000;
+ printf("size of s1000 is %i\n", sizeof(s1000));
+ printf("size of t1000 is %i\n", sizeof(t1000));
+
+ return 0;
+}
10 chapter2/page66/food.c
@@ -0,0 +1,10 @@
+#include <stdio.h>
+
+int main()
+{
+ char food[5];
+ printf("Enter favorite food: ");
+ scanf("%s", food);
+ printf("Favorite food: %s", food);
+ return 0;
+}
184 chapter2/page66/food.c~
@@ -0,0 +1,184 @@
+#include <stdio.h>
+
+
+void print_reverse(char* s)
+{
+ size_t len = strlen(s);
+ char* t = s + len - 1;
+ while (t >= s) {
+ printf("%c", *t);
+ t = t - 1;
+ }
+ puts("");
+}
+
+void fred()
+{
+ int x = 2;
+ /* return 1;*/
+}
+
+void dummy0(int* b)
+{
+ printf("dummy0 sizeof(b) = %li\n", sizeof(b));
+}
+
+void dummy1(int b[])
+{
+ printf("dummy1 sizeof(b) = %li\n", sizeof(b));
+}
+
+void print_me(char s[13])
+{
+ char* t;
+ printf("I've been told: %s\nsize = %i\nt size = %i", s, sizeof(s), sizeof(t));
+ printf("Location = %p\n", s);
+}
+
+void fortune_cookie(char msg[])
+{
+ printf("Message reads: %s\n", msg);
+ printf("msg occupies %i bytes\n", sizeof(msg));
+}
+
+void dosizeof(void* p)
+{
+ printf("p has size %i\n", sizeof(p));
+}
+
+void skip(char* msg)
+{
+ puts(msg + 6);
+}
+
+char food[10];
+
+
+int global0 = 1;
+int main()
+{
+ int local0 = 4;
+ float local1 = 2.0f;
+ int local2 = 4;
+ float local3 = 2.0f;
+ printf("local0 lives at %p, global0 lives at %p, local1 lives at %p, local2 lives at %p, local3 lives at %p\n",
+ &local0, &global0, &local1, &local2, &local3);
+ int int0;
+ int* int_pointer = &int0;
+ double double0;
+ double* double_pointer = &double0;
+ char char0;
+ char* char_pointer = &char0;
+ void* void_pointer = &int0;
+ printf("int0 is at %p\n", int_pointer);
+ printf("double0 is at %p\n", double_pointer);
+ printf("char0 is at %p\n", char_pointer);
+ printf("void_pointer is at %p\n", void_pointer);
+ int_pointer = int_pointer + 1;
+ double_pointer = double_pointer + 1;
+ char_pointer = char_pointer + 1;
+ void_pointer = void_pointer + 1;
+ printf("int0 is at %p\n", int_pointer);
+ printf("double0 is at %p\n", double_pointer);
+ printf("char0 is at %p\n", char_pointer);
+ printf("void_pointer is at %p\n", void_pointer);
+ int arr[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
+ int* a = arr;
+ printf("Item 2 = %i\n", arr[2]);
+ printf("Item 2 = %i\n", a[2]);
+ printf("Item 2 = %i\n", *(arr + 2));
+ printf("Item 2 = %i\n", *(a + 2));
+ arr[2] = 99;
+ printf("Item 2 = %i\n", arr[2]);
+ printf("Item 2 = %i\n", a[2]);
+ printf("Item 2 = %i\n", *(arr + 2));
+ printf("Item 2 = %i\n", *(a + 2));
+ *(arr + 1) = 98;
+ printf("Item 1 = %i\n", arr[1]);
+ printf("Item 1 = %i\n", a[1]);
+ printf("Item 1 = %i\n", *(arr + 1));
+ printf("Item 1 = %i\n", *(a + 1));
+ *(a + 1) = 97;
+ printf("Item 1 = %i\n", 1[arr]);
+ printf("Item 1 = %i\n", 1[a]);
+ printf("Item 1 = %i\n", *(arr + 1));
+ printf("Item 1 = %i\n", *(a + 1));
+ printf("a = %p\n", a);
+ printf("a + 1 = %p\n", (a + 1));
+ printf("a = %li\n", a);
+ printf("a + 1 = %li\n", (a + 1));
+ printf("sizeof(a) = %li\n", sizeof(a));
+ printf("sizeof(arr) = %li\n", sizeof(arr));
+ printf("a = %li, &a = %li\n", a, &a);
+ printf("arr = %li, &arr = %li\n", arr, &arr);
+ long fred = (long)a;
+ printf("fred = %li\n", fred);
+ dummy0(arr);
+ dummy1(arr);
+ printf("Location of original string: %p\n", "Hello world!");
+ printf("Location of original string: %p\n", "Hello world!1");
+ printf("A long has size %i\n", sizeof(long));
+ printf("A int has size %i\n", sizeof(int));
+ printf("Turtles has size %i\n", sizeof("Turtles!"));
+ print_me("Hello world!");
+ fortune_cookie("To have a friend, learn to be a friend");
+ char shoes[] = "sneakers";
+ printf("The shoes string is stored at: %p\n", shoes);
+ dosizeof(&fred);
+ dosizeof(shoes);
+ char s[] ="How big is it?";
+ char* t = s;
+ printf("s is %i\n", sizeof(s));
+ printf("t is %i\n", sizeof(t));
+ /* char anarray[] = "I am some array";
+ char achar = 'a';
+ char* p1;
+ p1 = &achar;*/
+ /* anarray = &achar;*/
+
+
+ char* msg_from_amy = "Don't call me";
+ skip(msg_from_amy);
+
+ int nums[] = {1, 2, 3};
+ printf("nums is at %li\n", nums);
+ printf("nums + 1 is at %li\n", nums + 1);
+
+ /* char name[40];
+ printf("Enter your name: ");
+ scanf("%39s", name);
+
+ char surname[40];
+ printf("\nEnter your surname: ");
+ scanf("%39s", surname);
+ printf("\nYour name: %s %s\n", name, surname);
+
+ char first_name[20];
+ char last_name[20];
+ printf("Enter first and last name: ");
+ scanf("%19s %19s", first_name, last_name);
+
+ printf("%s-%s\n", first_name, last_name);*/
+ /* int courses;
+ printf("Enter favorite food: ");
+ scanf("%s", food);
+ printf("Enter meal courses: ");
+ scanf("%i", &courses);
+ printf("Favorite food: %s", food);
+ printf("\nCourses: %i", courses);*/
+
+ char s0 = "Hello";
+ char * s1 = s0;
+ // char s2[];
+ printf("s0 is at %p\n", s0);
+ printf("s1 is at %p\n", s1);
+ // printf("s2 is at %p\n", s2);
+
+ print_reverse("Never odd or even");
+
+ char *s1000, t1000;
+ printf("size of s1000 is %i\n", sizeof(s1000));
+ printf("size of t1000 is %i\n", sizeof(t1000));
+
+ return 0;
+}
10 chapter2/page67/food.c
@@ -0,0 +1,10 @@
+#include <stdio.h>
+
+int main()
+{
+ char food[5];
+ printf("Enter favorite food: ");
+ fgets(food, sizeof(food), stdin);
+ printf("Favorite food: %s", food);
+ return 0;
+}
10 chapter2/page67/food.c~
@@ -0,0 +1,10 @@
+#include <stdio.h>
+
+int main()
+{
+ char food[5];
+ printf("Enter favorite food: ");
+ scanf("%s", food);
+ printf("Favorite food: %s", food);
+ return 0;
+}
14 chapter2/page69/monte.c
@@ -0,0 +1,14 @@
+ #include <stdio.h>
+
+ int main()
+ {
+ char *cards = "JQK";
+ char a_card = cards[2];
+ cards[2] = cards[1];
+ cards[1] = cards[0];
+ cards[0] = cards[2];
+ cards[2] = cards[1];
+ cards[1] = a_card;
+ puts(cards);
+ return 0;
+ }
18 chapter2/page69/monte.c~
@@ -0,0 +1,18 @@
+ #include <stdio.h>
+
+ int main()
+ {
+ /* const int x = 1;
+ x = 2;
+ const char * s = "HERE";
+ s = "THERE";*/
+ char cards[] = "JQK";
+ char a_card = cards[2];
+ cards[2] = cards[1];
+ cards[1] = cards[0];
+ cards[0] = cards[2];
+ cards[2] = cards[1];
+ cards[1] = a_card;
+ puts(cards);
+ return 0;
+ }
14 chapter2/page72/monte.c
@@ -0,0 +1,14 @@
+ #include <stdio.h>
+
+ int main()
+ {
+ char cards[] = "JQK";
+ char a_card = cards[2];
+ cards[2] = cards[1];
+ cards[1] = cards[0];
+ cards[0] = cards[2];
+ cards[2] = cards[1];
+ cards[1] = a_card;
+ puts(cards);
+ return 0;
+ }
14 chapter2/page72/monte.c~
@@ -0,0 +1,14 @@
+ #include <stdio.h>
+
+ int main()
+ {
+ char *cards = "JQK";
+ char a_card = cards[2];
+ cards[2] = cards[1];
+ cards[1] = cards[0];
+ cards[0] = cards[2];
+ cards[2] = cards[1];
+ cards[1] = a_card;
+ puts(cards);
+ return 0;
+ }
16 chapter2/pirate_game_broken.c
@@ -0,0 +1,16 @@
+#include <stdio.h>
+
+void go_south_east(int lat, int lon)
+{
+ lat = lat - 1;
+ lon = lon + 1;
+}
+
+int main()
+{
+ int latitude = 32;
+ int longitude = -64;
+ go_south_east(latitude, longitude);
+ printf("Avast! Now at: [%i, %i]\n", latitude, longitude);
+ return 0;
+}
16 chapter2/pirate_game_fixed.c
@@ -0,0 +1,16 @@
+#include <stdio.h>
+
+void go_south_east(int* lat, int* lon)
+{
+ *lat = *lat - 1;
+ *lon = *lon + 1;
+}
+
+int main()
+{
+ int latitude = 32;
+ int longitude = -64;
+ go_south_east(&latitude, &longitude);
+ printf("Avast! Now at: [%i, %i]\n", latitude, longitude);
+ return 0;
+}
9 chapter2/safe_gets.c
@@ -0,0 +1,9 @@
+#include <stdio.h>
+
+int main()
+{
+ char line[80];
+ fgets(line, 80, stdin);
+ printf("Your quote was: %s", line);
+ return 0;
+}
13 chapter2/stackotrouble.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+int main()
+{
+ int contestants[] = {1, 2, 3};
+ int choice = contestants[0];
+ contestants[0] = 2;
+ contestants[1] = contestants[2];
+ contestants[2] = choice;
+ choice = contestants[1];
+ printf("I'm going to pick contestant number %i\n", choice);
+ return 0;
+}
14 chapter2/test.c
@@ -0,0 +1,14 @@
+/* @(#)test.c
+ */
+
+#include <stdio.h>
+#include <limits.h>
+#include <inttypes.h>
+
+int main()
+{
+ printf("On this machine an int takes up %lu bytes\n", sizeof(int8_t));
+ printf("On this machine an int takes up %lu bytes\n", sizeof(int));
+ printf("And ints can store values from %i to %i\n", INT_MIN, INT_MAX);
+ return 0;
+}
23 chapter2/test_scope.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+
+char * get_message() {
+ char * msg = "Aren't pointers fun?";
+ printf("msg address = %p\n", msg);
+ return msg;
+}
+
+int main() {
+ char * string = get_message();
+ printf("Address = %p\n", string);
+ printf("Address = %p\n", "Aren't pointers fun?");
+ char * s0 = "Aren't pointers fun?";
+ char s1[] = "Aren't pointers fun?";
+ if (s0 == "Aren't pointers fun?") {
+ puts("The C compiler reused the string");
+ }
+ if (s1 == "Aren't pointers fun?") {
+ puts("The C compiler reused the string in an array - I DON'T BELIEVE IT!");
+ }
+ puts(string);
+ return 0;
+}
17 chapter2/testwords.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+
+int main()
+{
+ char line[80];
+ char line2[80];
+ printf("Big text: ");
+ scanf("%79[^\n]%*\n", line);
+ /* scanf("%79[^\n]", line);*/
+ printf("Line: %s\n", line);
+ printf("Big text2: ");
+ scanf("%79[^\n]%*[\n]", line2);
+ /* scanf("%*[ \n\t]%79[^\n]", line2);*/
+ /* scanf("%79[^\n]", line2);*/
+ printf("Line: %s\n", line2);
+ return 0;
+}
28 chapter2/text_search.c
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <string.h>
+
+char tracks[][80] = {
+ "I left my heart in Harvard Med School",
+ "Newark, Newark - a wonderful town",
+ "Dancing with a Dork",
+ "From here to maternity",
+ "The girl from Iwo Jima",
+};
+
+int main()
+{
+ char search_for[80];
+ printf("Search for: ");
+ scanf("%79s", search_for);
+ find_track(search_for);
+ return 0;
+}
+void find_track(char search_for[])
+{
+ int i;
+ for (i = 0; i < 5; i++) {
+ if (strstr(tracks[i], search_for))
+ printf("Track %i: '%s'\n", i, tracks[i]);
+ }
+}
+
184 chapter2/whereitlives.c
@@ -0,0 +1,184 @@
+#include <stdio.h>
+
+
+void print_reverse(char* s)
+{
+ size_t len = strlen(s);
+ char* t = s + len - 1;
+ while (t >= s) {
+ printf("%c", *t);
+ t = t - 1;
+ }
+ puts("");
+}
+
+void fred()
+{
+ int x = 2;
+ /* return 1;*/
+}
+
+void dummy0(int* b)
+{
+ printf("dummy0 sizeof(b) = %li\n", sizeof(b));
+}
+
+void dummy1(int b[])
+{
+ printf("dummy1 sizeof(b) = %li\n", sizeof(b));
+}
+
+void print_me(char s[13])
+{
+ char* t;
+ printf("I've been told: %s\nsize = %i\nt size = %i", s, sizeof(s), sizeof(t));
+ printf("Location = %p\n", s);
+}
+
+void fortune_cookie(char msg[])
+{
+ printf("Message reads: %s\n", msg);
+ printf("msg occupies %i bytes\n", sizeof(msg));
+}
+
+void dosizeof(void* p)
+{
+ printf("p has size %i\n", sizeof(p));
+}
+
+void skip(char* msg)
+{
+ puts(msg + 6);
+}
+
+char food[10];
+
+
+int global0 = 1;
+int main()
+{
+ int local0 = 4;
+ float local1 = 2.0f;
+ int local2 = 4;
+ float local3 = 2.0f;
+ printf("local0 lives at %p, global0 lives at %p, local1 lives at %p, local2 lives at %p, local3 lives at %p\n",
+ &local0, &global0, &local1, &local2, &local3);
+ int int0;
+ int* int_pointer = &int0;
+ double double0;
+ double* double_pointer = &double0;
+ char char0;
+ char* char_pointer = &char0;
+ void* void_pointer = &int0;
+ printf("int0 is at %p\n", int_pointer);
+ printf("double0 is at %p\n", double_pointer);
+ printf("char0 is at %p\n", char_pointer);
+ printf("void_pointer is at %p\n", void_pointer);
+ int_pointer = int_pointer + 1;
+ double_pointer = double_pointer + 1;
+ char_pointer = char_pointer + 1;
+ void_pointer = void_pointer + 1;
+ printf("int0 is at %p\n", int_pointer);
+ printf("double0 is at %p\n", double_pointer);
+ printf("char0 is at %p\n", char_pointer);
+ printf("void_pointer is at %p\n", void_pointer);
+ int arr[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
+ int* a = arr;
+ printf("Item 2 = %i\n", arr[2]);
+ printf("Item 2 = %i\n", a[2]);
+ printf("Item 2 = %i\n", *(arr + 2));
+ printf("Item 2 = %i\n", *(a + 2));
+ arr[2] = 99;
+ printf("Item 2 = %i\n", arr[2]);
+ printf("Item 2 = %i\n", a[2]);
+ printf("Item 2 = %i\n", *(arr + 2));
+ printf("Item 2 = %i\n", *(a + 2));
+ *(arr + 1) = 98;
+ printf("Item 1 = %i\n", arr[1]);
+ printf("Item 1 = %i\n", a[1]);
+ printf("Item 1 = %i\n", *(arr + 1));
+ printf("Item 1 = %i\n", *(a + 1));
+ *(a + 1) = 97;
+ printf("Item 1 = %i\n", 1[arr]);
+ printf("Item 1 = %i\n", 1[a]);
+ printf("Item 1 = %i\n", *(arr + 1));
+ printf("Item 1 = %i\n", *(a + 1));
+ printf("a = %p\n", a);
+ printf("a + 1 = %p\n", (a + 1));
+ printf("a = %li\n", a);
+ printf("a + 1 = %li\n", (a + 1));
+ printf("sizeof(a) = %li\n", sizeof(a));
+ printf("sizeof(arr) = %li\n", sizeof(arr));
+ printf("a = %li, &a = %li\n", a, &a);
+ printf("arr = %li, &arr = %li\n", arr, &arr);
+ long fred = (long)a;
+ printf("fred = %li\n", fred);
+ dummy0(arr);
+ dummy1(arr);
+ printf("Location of original string: %p\n", "Hello world!");
+ printf("Location of original string: %p\n", "Hello world!1");
+ printf("A long has size %i\n", sizeof(long));
+ printf("A int has size %i\n", sizeof(int));
+ printf("Turtles has size %i\n", sizeof("Turtles!"));
+ print_me("Hello world!");
+ fortune_cookie("To have a friend, learn to be a friend");
+ char shoes[] = "sneakers";
+ printf("The shoes string is stored at: %p\n", shoes);
+ dosizeof(&fred);
+ dosizeof(shoes);
+ char s[] ="How big is it?";
+ char* t = s;
+ printf("s is %i\n", sizeof(s));
+ printf("t is %i\n", sizeof(t));
+ /* char anarray[] = "I am some array";
+ char achar = 'a';
+ char* p1;
+ p1 = &achar;*/
+ /* anarray = &achar;*/
+
+
+ char* msg_from_amy = "Don't call me";
+ skip(msg_from_amy);
+
+ int nums[] = {1, 2, 3};
+ printf("nums is at %li\n", nums);
+ printf("nums + 1 is at %li\n", nums + 1);
+
+ /* char name[40];
+ printf("Enter your name: ");
+ scanf("%39s", name);
+
+ char surname[40];
+ printf("\nEnter your surname: ");
+ scanf("%39s", surname);
+ printf("\nYour name: %s %s\n", name, surname);
+
+ char first_name[20];
+ char last_name[20];
+ printf("Enter first and last name: ");
+ scanf("%19s %19s", first_name, last_name);
+
+ printf("%s-%s\n", first_name, last_name);*/
+ /* int courses;
+ printf("Enter favorite food: ");
+ scanf("%s", food);
+ printf("Enter meal courses: ");
+ scanf("%i", &courses);
+ printf("Favorite food: %s", food);
+ printf("\nCourses: %i", courses);*/
+
+ char s0 = "Hello";
+ char * s1 = s0;
+ // char s2[];
+ printf("s0 is at %p\n", s0);
+ printf("s1 is at %p\n", s1);
+ // printf("s2 is at %p\n", s2);
+
+ print_reverse("Never odd or even");
+
+ char *s1000, t1000;
+ printf("size of s1000 is %i\n", sizeof(s1000));
+ printf("size of t1000 is %i\n", sizeof(t1000));
+
+ return 0;
+}

0 comments on commit 2d4d34a

Please sign in to comment.