Advertisement
teknoraver

static

Mar 22nd, 2021
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. $ cat a.c
  2. #include "h.h"
  3.  
  4. void a(void)
  5. {
  6. inc(__func__);
  7. }
  8.  
  9. $ cat b.c
  10. #include "h.h"
  11.  
  12. void b(void)
  13. {
  14. inc(__func__);
  15. }
  16.  
  17. $ cat counter.c
  18. #include <stdio.h>
  19.  
  20. void inc(const char *s)
  21. {
  22. static int counter = 0;
  23.  
  24. printf("%s:\t%d\n", s, counter++);
  25. }
  26.  
  27. $ cat main.c
  28. #include <stdio.h>
  29. #include "h.h"
  30.  
  31. int main()
  32. {
  33. a();
  34. b();
  35. inc(__func__);
  36. a();
  37. b();
  38. inc(__func__);
  39. a();
  40. b();
  41. inc(__func__);
  42. return 0;
  43. }
  44.  
  45. $ cat h.h
  46. void inc(const char *s);
  47. void a(void);
  48. void b(void);
  49.  
  50. $ gcc -Wall -c -o main.o main.c
  51. $ gcc -Wall -c -o a.o a.c
  52. $ gcc -Wall -c -o b.o b.c
  53. $ gcc -Wall -c -o counter.o counter.c
  54. $ gcc main.o a.o b.o counter.o -o main
  55.  
  56. $ ./main
  57. a: 0
  58. b: 1
  59. main: 2
  60. a: 3
  61. b: 4
  62. main: 5
  63. a: 6
  64. b: 7
  65. main: 8
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement