Tuesday, August 18, 2020

5.1.2 Scope of C Variables

  • Which variable can be used in a place where a variable can be used. 
  • This happens to his scope. 
  • According to Scope, variables have been divided into 2 categories.

  1. Local Variables
  2. Globle variables
❖ Local Variable :-
  • Local variables are those variables that are defined in a small block of the program such as function, control statement block etc. 
  • Such variables are used only by the same block.
  • Like if you have created a variable in any function, then you can not use that variable outside of that function.

#include<stdio.h>
using namespace std;
void myFunction();
int main()
{
int num=10;
myFunction();
printf("Num in main() : %d", num);
return 0;
}
void myFunction()
{
/* local variable num */
int num= 6;
printf("Num in myFunction : %dn",num);
}


OUTPUT

Num in myFuntion() : 6 Num in main() : 10


❖ Global Variables :-
  • Global variables are variables that are scope in the whole program. 
  • These variables allow you to access any of the entire program. 
  • These variables are defined at the beginning of the program.

#include<stdio.h>
/* A global variable */
int num=10;
void myFunction();
int main()
{
myFunction();
printf("Num in main() : %d",num);
return 0;
}
void myFunction()
{
printf("Num in myFunction : %dn",num);
}

OUTPUT

Num in myFunction() : 10 Num in main() : 10

No comments:

Post a Comment

Please do not any spam link in Comment Box