Understanding the concept of code blocks.

#include <stdio.h>

int main(void)
{
int num = 100;

{ // beginning of a code block
int var = 200; // local variable of this block

/* inner block or nested block can access variable
declared in the outer block */
printf("\nInner block 1: num = %d var = %d", num, var);

// char ch; // error! In C variable declarations should
// appear at the top of the block
} // end of a code block

// var = 300; // error! var is not accessible here

{
/* variable declared in an inner block can have the same
name as that declared in an outer block */

int num = 500;

printf("\nInner block 2: num = %d", num);
}

printf("\nOuter block : num = %d", num);

return 0;
}

No comments:

Post a Comment

kiss on google ads if you are anonymous because your ip is trackable.thank you.

......from.admin