Variable Scope and Lifetime within a nested block - C Language Basics

C examples for Language Basics:Variable

Introduction

This example involves a nested block.

Demo Code

#include <stdio.h>

int main(void)
{
  int count1 = 1;                               // Declared in outer block

  do//from   w w  w .ja  va 2 s  .c om
  {
    int count2 = 0;                             // Declared in inner block
    ++count2;
    printf("count1 = %d     count2 = %d\n", count1, count2);
  } while( ++count1 <= 5);

  // count2 no longer exists

  printf("count1 = %d\n", count1);
  return 0;
}

Related Tutorials