Permalink
Browse files

ex24

  • Loading branch information...
Sam Breed
Sam Breed committed Feb 27, 2012
1 parent c72ca93 commit 2d3268b4e7aafff1e252c6762992b1ec0341a116
Showing with 143 additions and 0 deletions.
  1. 0 ex22.h
  2. +9 −0 ex22/Makefile
  3. +30 −0 ex22/dbg.h
  4. +33 −0 ex22/ex22.c
  5. +16 −0 ex22/ex22.h
  6. +55 −0 ex22/ex22_main.c
View
0 ex22.h
No changes.
View
@@ -0,0 +1,9 @@
+CFLAGS=-Wall -g
+
+all: ex22_main
+
+ex22_main: ex22.o
+
+clean:
+ rm -f ex22_main
+ rm -f *.o
View
@@ -0,0 +1,30 @@
+#ifndef __dbg_h__
+#define __dbg_h__
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+
+#ifdef NDEBUG
+#define debug(M, ...)
+#else
+#define debug(M, ...) fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
+#endif
+
+#define clean_errno() (errno == 0 ? "None" : strerror(errno))
+
+#define log_err(M, ...) fprintf(stderr, "[ERROR] (%s:%d: errno %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
+
+#define log_warn(M, ...) fprintf(stderr, "[WARN] (%s:%d: errno %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
+
+#define log_info(M, ...) fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
+
+#define check(A, M, ...) if(!(A)) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }
+
+#define sentinel(M, ...) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }
+
+#define check_mem(A) check((A), "out of memory")
+
+#define check_debug(A, M, ...) if(!(A)) { debug(M, ##__VA_ARGS__); errno=0; goto error; }
+
+#endif
View
@@ -0,0 +1,33 @@
+#include <stdio.h>
+#include "ex22.h"
+#include "dbg.h"
+
+int THE_SIZE = 1000;
+
+static int THE_AGE = 37;
+
+int get_age()
+{
+ return THE_AGE;
+}
+
+void set_age(int age)
+{
+ THE_AGE = age;
+}
+
+
+double update_ratio(double new_ratio)
+{
+ static double ratio = 1.0;
+
+ double old_ratio = ratio;
+ ratio = new_ratio;
+
+ return old_ratio;
+}
+
+void print_size()
+{
+ log_info("I think size is: %d", THE_SIZE);
+}
View
@@ -0,0 +1,16 @@
+#ifndef _ex22_h
+#define _ex22_h
+
+// makes THE_SIZE in ex22.c available to other .c files
+extern int THE_SIZE;
+
+// gets and sets an internal static variable in ex22.c
+int get_age();
+void set_age(int age);
+
+// updates a static variable that's inside update_ratio
+double update_ratio(double ratio);
+
+void print_size();
+
+#endif
View
@@ -0,0 +1,55 @@
+#include "ex22.h"
+#include "dbg.h"
+
+const char *MY_NAME = "wookie";
+
+void scope_demo(int count)
+{
+ log_info("count is: %d", count);
+
+ if(count > 10){
+ int count = 100;
+
+ log_info("count in this scope is %d", count);
+ }
+
+ log_info("count is at exit %d", count);
+
+ count = 3000;
+
+ log_info("count after assign: %d", count);
+}
+
+int main(int argc, char *argv[])
+{
+ // test out THE_AGE accessors
+ log_info("my name: %s, age: %d", MY_NAME, get_age());
+
+ set_age(100);
+
+ log_info("my age is now %d", get_age());
+
+ // test out THE_SIZE extern
+ log_info("THE_SIZE is %d", THE_SIZE);
+ print_size();
+
+ THE_SIZE = 9;
+
+ log_info("THE_SIZE is now %d", THE_SIZE);
+ print_size();
+
+ // test the ratio function static
+ log_info("ratio at first %f", update_ratio(2.0));
+ log_info("ratio again %f", update_ratio(10.0));
+ log_info("ratio once more %f", update_ratio(300.0));
+
+ // test the scope demo
+ int count = 4;
+ scope_demo(count);
+ scope_demo(count * 20);
+
+ log_info("count after calling scope demo %d", count);
+
+ return 0;
+}
+

0 comments on commit 2d3268b

Please sign in to comment.