Advertisement
cd62131

Static Variable

Mar 15th, 2017
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.76 KB | None | 0 0
  1. sv_inner.h:
  2. static int sv;
  3. static int sv_changed;
  4. static void init_sv(void);
  5.  
  6. sv.h:
  7. int get_sv(void);
  8. void set_sv(int);
  9. void update_sv(void);
  10.  
  11. sv.c:
  12. #include <stdbool.h>
  13. #include "sv.h"
  14. #include "sv_inner.h"
  15. static void init_sv(void) { static bool already = false; if (already) { return; } sv = sv_changed = 0; already = true; }
  16. int get_sv(void) { init_sv(); return sv; }
  17. void set_sv(int val) { init_sv(); sv_changed = val; }
  18. void update_sv(void) { init_sv(); sv = sv_changed; }
  19.  
  20. main.c:
  21. #include <stdio.h>
  22. #include "sv.h"
  23. int main(void) {
  24.   printf("sv = %d\n", get_sv());
  25.   set_sv(100); printf("set_sv(100); sv = %d\n", get_sv());
  26.   set_sv(10); printf("set_sv(10); sv = %d\n", get_sv());
  27.   update_sv(); printf("update_sv(); sv = %d\n", get_sv());
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement