RicardasSim

Block Scope

Nov 16th, 2019
674
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.67 KB | None | 0 0
  1. //-std=c99 -Wall -Wextra -Wpedantic -Wshadow
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. int main()
  7. {
  8.  
  9.     int a = 1;
  10.  
  11.     printf("(1a): %d\n", a);
  12.  
  13.     {
  14.         // warning: declaration of ‘a’ shadows a previous local
  15.         int a = 2;  //shadowed declaration is here
  16.  
  17.         int b = 2;  // OK
  18.  
  19.         printf("(2a): %d\n", a);
  20.         printf("(2b): %d\n", b);
  21.     }
  22.  
  23.     printf("(3a): %d\n", a);
  24.  
  25.     {
  26.         a = 3;
  27.  
  28.         int b = 4;
  29.  
  30.         printf("(4a): %d\n", a);
  31.         printf("(4b): %d\n", b);
  32.     }
  33.  
  34.     printf("(5a): %d\n", a);
  35.  
  36.     return 0;
  37. }
  38.  
  39. /*
  40. output:
  41.  
  42. (1a): 1
  43. (2a): 2
  44. (2b): 2
  45. (3a): 1
  46. (4a): 3
  47. (4b): 4
  48. (5a): 3
  49.  
  50. */
Advertisement
Add Comment
Please, Sign In to add comment