Mega Code Archive

 
Categories / C / Code Snippets
 

Variable scope in and out a block

#include <stdio.h> void main() { int j = 0; /* Declared in outer block */ do { int j = 0; /* This is another variable called j */ ++j ; /* this applies to inner j */ printf("\n j = %d ", j ); } while( ++j <= 3 ); /* This works with outer j */ /* Inner j is dead, this is outer */ printf("\n j = %d\n", j ); }