Compare local and global variables. Write a program with function to find area of rectangle in C with and without global variable.
Question
Compare local and global variables. Write a program with function to find area of rectangle in C with and without global variable.
Solution
Local and Global Variables:
-
Scope: The scope of a local variable is limited to the function or block in which it is declared while a global variable is accessible throughout the program.
-
Lifetime: The lifetime of a local variable lasts until the function or block in which it is declared is running. On the other hand, a global variable exists for the entire duration of the program.
-
Declaration: Local variables are declared inside a function or block, whereas global variables are declared outside all functions.
-
Default Value: Uninitialized local variables have garbage value. However, uninitialized global variables are automatically initialized to zero.
Now, let's write a program to find the area of a rectangle in C with and without global variables.
Program with global variables:
#include<stdio.h>
int length, breadth; // global variables
void findArea() {
int area = length * breadth;
printf("Area of rectangle is: %d", area);
}
int main() {
printf("Enter length and breadth of rectangle: ");
scanf("%d%d", &length, &breadth);
findArea();
return 0;
}
Program without global variables:
#include<stdio.h>
void findArea(int length, int breadth) {
int area = length * breadth;
printf("Area of rectangle is: %d", area);
}
int main() {
int length, breadth;
printf("Enter length and breadth of rectangle: ");
scanf("%d%d", &length, &breadth);
findArea(length, breadth);
return 0;
}
In the first program, length and breadth are global variables, so they can be accessed directly inside the findArea function. In the second program, length and breadth are local to the main function and are passed to the findArea function as arguments.
Similar Questions
Compare local and global variables. Write a program with function to find area of rectangle in C with and without global variable.
Write a program to calculate the area of circle, triangle, rectangle and square using function overloading.
Which of the following lists the main steps of a program which calculates and prints the area of a square?
Write down the C code for the following questions.a) Write a program in C to check if a given number is even or odd using the function.b) Write a program in C to swap two numbers using a function.c) Write a program in C to find the square of a number using the function.d) Write a program in C to find the factorial of a number using the function.e) Write a program in C to calculate the area of circle using the function.f) Write a C program to compute the sum of the first “N” natural numbers using thefunction.g) Write a program in C to print the Fibonacci series using the function.
Write a program to print the area of a rectangle by creating a class named 'Area' having two methods. First method named as 'setDim' takes length and breadth of rectangle as parameters and the second method named as 'getArea' returns the area of the rectangle. Length and breadth of rectangle are entered through keyboard.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.