Variable Local Scope - C Language Basics

C examples for Language Basics:Variable

Introduction

Local variables are defined in functions, such as the main() function.

Demo Code

#include <stdio.h> 

int main() { //from  www  .j a v a2 s.  c  o  m
   int num1; 
   printf("\nEnter a number: "); 
   scanf("%d", &num1); 
   printf("\nYou entered %d\n ", num1); 
   return 0;
}  

Local scope variables are tied to their originating functions, you can reuse variable names in other functions.

Demo Code

#include <stdio.h> 
int getSecondNumber();  //function prototype 
int main()/*from   w  w  w  . j a v a  2s  . c o  m*/
{ 
   int num1; 
   printf("\nEnter a number: "); 
   scanf("%d", &num1); 
   printf("\nYou entered %d and %d\n ", num1, getSecondNumber()); 
} 

int getSecondNumber () 
{ 
   int num1; 
   printf("\nEnter a second number: "); 
   scanf("%d", &num1);   
   return num1; 
}

Related Tutorials