Global and Local Variable Program
#include <stdio.h>
// Global variable declaration
int globalVar = 10;
// Function that uses a global variable
void functionWithGlobalVariable() {
printf("Inside functionWithGlobalVariable:\n");
printf("Global variable globalVar: %d\n", globalVar);
}
// Function that uses local variables
void functionWithLocalVariables() {
int localVar = 20; // Local variable declaration
printf("\nInside functionWithLocalVariables:\n");
printf("Local variable localVar: %d\n", localVar);
// Accessing the global variable from within a function
printf("Global variable globalVar: %d\n", globalVar);
}
int main() {
// Accessing the global variable from the main function
printf("Inside main:\n");
printf("Global variable globalVar: %d\n", globalVar);
functionWithGlobalVariable();
functionWithLocalVariables();
return 0;
}
Comments
Post a Comment