C Language Local Variables

C Language Local Variables

It is the scope (scope) in the defined sub code segment or function.

#include <stdio.h>

int main(void) {
  
  for (int i = 0; i < 5; ++i) { // "i" is a local variable, which can only be used in the for loop
     printf("Local Variable");
  }
  
 // Error: i is not declared at this point
 // An error will be reported here because i does not declare
  printf("%d", i);  
  return 0;
}
Sourc‮www:e‬.lautturi.com
#include <stdio.h>

int main(void) {
  int i;
  for (i = 0; i < 5; ++i) { // The scope of i is in the main function
     printf("Local Variable");
  }

 // output 5
  printf("%d", i);  
  return 0;
}
Created Time:2017-08-29 09:24:21  Author:lautturi