Thursday, September 5, 2019

C Interview Question


1) What is the difference between the local variable and global variable in C?
Following are the differences between a local variable and global variable:
Basis for comparison
Local variable
Global variable
Declaration
A variable which is declared inside function or block is known as a local variable.
A variable which is declared outside function or block is known as a global variable.
Scope
The scope of a variable is available within a function in which they are declared.
The scope of a variable is available throughout the program.
Access
Variables can be accessed only by those statements inside a function in which they are declared.
Any statement in the entire program can access variables.
Life
Life of a variable is created when the function block is entered and destroyed on its exit.
Life of a variable exists until the program is executing.
Storage
Variables are stored in a stack unless specified.
The compiler decides the storage location of a variable.


2) What is the use of a static variable in C?
Following are the uses of a static variable:
  • A variable which is declared as static is known as a static variable. The static variable retains its value between multiple function calls.
  • Static variables are used because the scope of the static variable is available in the entire program. So, we can access a static variable anywhere in the program.
  • The static variable is initially initialized to zero. If we update the value of a variable, then the updated value is assigned.
  • The static variable is used as a common value which is shared by all the methods.
  • The static variable is initialized only once in the memory heap to reduce the memory usage.



3) What is the use of the function in C?
Uses of C function are:
  • C functions are used to avoid the rewriting the same code again and again in our program.
  • C functions can be called any number of times from any place of our program.
  • When a program is divided into functions, then any part of our program can easily be tracked.
  • C functions provide the reusability concept, i.e., it breaks the big task into smaller tasks so that it makes the C program more understandable.

4) What is the difference between call by value and call by reference in C?
Following are the differences between a call by value and call by reference are:
Call by value
Call by reference
Description
When a copy of the value is passed to the function, then the original value is not modified.
When a copy of the value is passed to the function, then the original value is modified.
Memory location
Actual arguments and formal arguments are created in separate memory locations.
Actual arguments and formal arguments are created in the same memory location.
Safety
In this case, actual arguments remain safe as they cannot be modified.
In this case, actual arguments are not reliable, as they are modified.
Arguments
The copies of the actual arguments are passed to the formal arguments.
The addresses of actual arguments are passed to their respective formal arguments.


No comments:

Post a Comment

Which Python course is best for beginners?

Level Up Your Python Prowess: Newbie Ninjas: Don't fret, little grasshoppers! Courses like "Learn Python 3" on Codecade...