C language Variables

Interview Questions on C language Variables

1. What is variable in C?

Variable is simply a name used to refer to some location in memory - a location that holds a value with which we are working. These memory location contains constants (Integer, Real and Character). Types of variables depend on types of variables that it can handle.
Particular type of variable can hold only the same type of constants.
Ex: A integer type of variable can hold integer constant.

2.  What are the rules for constructing a variable name?
The rules for constructing a variable name are as follows:

1. First character in the variable name must be an alphabet or underscore.
2. There should be no commas or blank spaces within variable name.
3. There should not be any special symbol within a variable name except an underscore.
4. Variable name should be 31 characters long.
5. Variable name should be meaningful so that user can understand.

3. What are the different Variable types?

The C programming language has three different types of variables.
1. Local Variables.
2. Global Variables.
3. Static Variables.

4. Explain about local variables?

The scope of local variables is confined within the block or function where it is defined. Local variables must always be defined at the top of a block. When a local variable is defined, it is not initialized by the system, you must initialize it yourself. Local variables cease to exist once if the function that created them is completed. Each time a function is executed or called, they are recreated.

5. Explain about Global Variables?

Global variable is defined at the top of the program file and it can be visible and modified by any function that may reference it. When you define them, global variables are initialized automatically by the system. If same variable name is being used for global and local variable then local variable takes preference in its scope. But it is not a good practice to use global variables and local variables with the same name.

6. Explain about static variables?

A static variable is a special variable that is stored in the data segment unlike the default automatic variable that is stored in stack. Static variables are created during program initialization (prior to main ()) and remain in existence until program termination. While these commonly are stored in the data segment, the C standard does not require this to be the case, and in some cases (eg, C interpreters) they may be stored in other locations, such as the heap. Static variable can be initialized by using keyword static before variable name.
Ex:  Static int a=1;

7. In how many places Variable declarations are shown?

The variable declarations show up in three places:
1. Outside of a function. These declarations declare global variables which are visible throughout the program (i.e. they have global scope). Use of global variables is always a big mistake.
2. In the header of a function in the argument list. These variables are the parameters to the function. They are only visible inside the function body and their local scope), exist only from when the function is called to when the function returns (bounded extent---note that this is different from what happens in some garbage-collected languages like Scheme), and get their initial values from the arguments to the function when it is called.
3. At the start of any block delimited by curly braces only. Such variables are exist only when the containing function is active (bounded extent) and visible only within the block (local scope again). The convention in C is generally to declare all such local variables at the top of a function.

8. How to declare variables in C?

To declare a variable, you specify its name and data type it can store. The variable declaration always ends with a semicolon (;).

Data_type variable_name;

Here data_type is valid C data type i.e. int, char, float, etc.
Variable_name is valid user defined C variable.

int i;
char name;
You can declare variables at any point of your program before using it. The best practice suggests that you should declare your variables closest to their first point of use so the source code is easier to maintain. In C programming language, declaring a variable is also means defining it.

9. How to initialize variables in C?

C allows us to initialize a variable when we declare it.
int x=5;
char ch=’ch’;

10. What is the Storage of Variables in C?

Each variable has its own lifetime (the length of time the variable can be accessible) or storage duration. When you declare your variable you implicitly assign it a lifetime.

11. What is the Scope of Variables in C?

You can declare variables inside a block of code which specifies by curly brackets {}. Each variable has its own scope inside block where it is declared.

12. What is a signed variable?

Signed variables can be either negative or positive. When you declare a variable in the normal way it is automatically a signed variable.

13. What is unsigned variable?

Unsigned variables can only be positive. By using an unsigned variable, you can increase the maximum positive range. To declare an unsigned variable, you just put the word unsigned before your variable declaration or signed for a signed variable although there is no reason to declare a variable assigned since they already are.

14. How to declare signed and unsigned variables?

We can declare signed and unsigned variables as follows:
int main()

unsigned int a;
signed int b;
return 0;

15. What is register variable in c language?

Register variables are a special case of automatic variables. Automatic variables are allocated storage in the memory of the computer; however, for most computers, accessing data in memory is considerably slower than processing in the CPU. These computers often have small amounts of storage within the CPU itself where data can be stored and accessed quickly. These storage cells are called registers.
register float a=0;

16. What is volatile variable?

If a variable is declared as volatile, optimization is not done regarding to that variable. If the value is modified many number of times, each time CPU goes and reads from main memory rather than cache memory.

17. How the compiler treats any volatile variable?

A volatile variable is one whose value can change unexpectedly.  Consequently, the compiler can make no assumptions about the value of the variable.  In particular, the optimizer must be careful to RELOAD the variable every time it is used instead of holding a copy in a register. 

18. What is a Block Scope of variable in C?
A block refers to any sets of statements enclosed in braces ({ and }). A variable declared within a block has block scope. Thus, the variable is active and accessible from its declaration point to the end of the block. Sometimes block scope is also called local scope.
Ex: The variable i declared within the block of the following main function has block scope:
int main()
   int i;   /* block scope */
   return 0;
Usually, a variable with block scope is called a local variable.

19. What is Nested Block Scope of variable in C?

We can also declare variables within a nested block. If a variable declared in the outer block shares the same name with one of the variables in the inner block, the variable within the outer block is hidden by the one within the inner block for the scope of the inner block.

20. What is the Function Scope of the variable in C?

Function scope indicates that a variable is active and visible from the beginning to the end of a function. In C, only the goto label has function scope.
Ex: The goto label, start, shown in the following code portion has function scope:
int main()
   int i;   /* block scope */
   start:   /* A goto label has function scope */
   goto  start;  /* the goto statement */
   return 0;
Here the label start is visible from the beginning to the end of the main () function. Therefore, there should not be more than one label having the same name within the main () function.

21. What is the Program Scope of variables in C?

A variable is said to have program scope when it is declared outside a function.
int x = 0;        /* program scope */
float y = 0.0;    /* program scope */
int main()
   int i;   /* block scope */
   return 0;
Here the int variable x and the float variable y have program scope.
Variables with program scope are also called global variables, which are visible among different files. These files are the entire source files that make up an executable program. Note that a global variable is declared with an initializer outside a function.

22. What is the Storage Class Specifier of the variable in C?

In C, the storage class of a variable refers to the combination of its spatial and temporal regions.

23. How we indicate duration of a variable in C?

There are four specifiers and two modifiers that can be used to indicate the duration of a variable.

24. How many types of specifiers are there in C?

There are four specifiers that can be used to indicate the duration of a variable.
1. The auto Specifier.
2. The static Specifier.
3. The register Specifier.
4. The extern Specifier.

25. Explain about the auto Specifier in C?

The auto specifier indicates that the memory location of a variable is temporary. In other words, a variable's reserved space in the memory can be erased or relocated when the variable is out of its scope. Only variables with block scope can be declared with the auto specifier. The auto keyword is rarely used, however, because the duration of a variable with block scope is temporary by default.

26. Explain about the static Specifier in C?

The static specifier, on the other hand, can be applied to variables with either block scope or program scope. When a variable within a function is declared with the static specifier, the variable has a permanent duration. In other words, the memory storage allocated for the variable is not destroyed when the scope of the variable is exited, the value of the variable is maintained outside the scope, and if execution ever returns to the scope of the variable, the last value stored in the variable is still there.

27. Explain about the register Specifier?

The word register is borrowed from the world of computer hardware. Each computer has a certain number of registers to hold data and perform arithmetic or logical calculations. Because registers are located within the CPU (central processing unit) chip, it's much quicker to access a register than a memory location. Therefore, storing variables in registers may help to speed up your program. The C language provides you with the register specifier. You can apply this specifier to variables when you think it's necessary to put the variables into the computer registers.

28. Explain about the extern Specifier?

A variable with program scope is visible through all source files that make up an executable program. A variable with program scope is also called a global variable. Use the extern specifier provided by the C language to allude to a global variable defined elsewhere.

29. What is the Storage Class Modifier?

C provides you with two storage class modifiers (or qualifiers, as they're sometimes called) that you can use to indicate to the C compiler how variables may be accessed.

30. How many types of storage class modifiers are there in C?
There are two types of storage class modifiers are there in C.
1. The const Modifier.
2. The volatile Modifier.

31. Explain about the const Modifier in C?

If you declare a variable with the const modifier, the content of the variable cannot be changed after it is initialized.

32. Explain about the volatile Modifier in C?

If you want to let the compiler know that the value of a variable can be changed without an explicit assignment statement, declare the variable with the volatile modifier so that the compiler will turn off optimizations on expressions involving the variable.

33. Can a global variable be hidden by a local variable with block scope?
Yes. If a local variable shares the same name with a global variable, the global variable can be hidden by the local variable for the scope of the block within which the local variable is defined with block scope. However, outside the block, the local variable cannot be seen, but the global variable becomes visible again.

34. Why do you need the static specifier?

In many cases, the value of a variable is needed, even if the scope of the block, in which the variable is declared, has exited. By default, a variable with block scope has temporary memory storage that is, the lifetime of the variable starts when the block is executed and the variable is declared, and ends when the execution is finished. Therefore, to declare a variable with permanent duration, you have to use the static specifier to indicate to the compiler that the memory location of the variable and the value stored in the memory location should be retained after the execution of the block.

35. Does using the register specifier guarantee to improve the performance of a program?
Not really. Declaring a variable with the register specifier only suggests to the compiler that the variable be stored in a register. But there is no guarantee that the variable will be stored in a register. The compiler can ignore the request based on the availability of registers or other restrictions.

36. When you declare a variable with the extern specifier, do you define the variable or allude to a global variable elsewhere?

When a variable is declared with the extern specifier, the compiler considers the declaration of the variable as an allusion rather than a definition. The compiler will therefore look somewhere else to find a global variable to which the variable with extern alludes.

C Language Interview Questions

Interview Questions on C Language Overview

Interview Questions on C Language Data Types

Interview Questions on C Language Constants

Interview Questions on C Language Operators

Interview Questions on C Language Conditional Statements

Interview Questions on C Language Loop Statements

Interview Questions on C Language Built-in Functions

Interview Questions on C Language User defined Functions

Interview Questions on C Language Pointers

Working with Files

Working with Databases

Interview Questions on C Data Structures


Programming Examples 


Post a Comment