C Language Variables Interview Questions
 
1. What is a variable inward C language?
Variables are retention place inward computer's retention to shop data. To betoken the retention location, each variable should live on given a unique refer called identifier.
Variable names are simply the symbolic representation of a retention location. Examples of variable refer are sum, car_no, count etc.
  
int num;
Here, num is a variable of integer type.
2. Give the rules for variable declaration?
The rules for variable announcement inward C are given below:
• Influenza A virus subtype H5N1 variable refer consists of alphabets, digits as well as the underscore (_) character.
• The length of variable should live on kept upto 8 characters though your arrangement may permit upto twoscore characters.
• They must start amongst an alphabet.
• Some systems besides recognize an underscore equally the commencement character.
• White infinite as well as commas are non allowed.
• Any reserved discussion (keyword) cannot live on used equally a variable name.
3. How to declare a variable inward C language?
All variables must live on declared earlier nosotros utilization them inward C program, although sure enough declarations tin live on made implicitly yesteryear content.
To declare a variable y'all specify its refer as well as information type it tin store. The variable announcement ever ends amongst a semicolon (;)
A announcement specifies a type, as well as contains a listing of i or to a greater extent than variables of that type equally follows:
type variable_list;
Here, type must live on a valid C information type including char, int, float, double, or whatever user defined information type etc., as well as variable_list may consist of i or to a greater extent than identifier names separated yesteryear commas.
A variable tin live on declared multiple times inward a program, but it must live on defined only once.
4. How to assign values to a variable?
A variable tin live on considered equally a box that tin concur a unmarried value. However, initially the content of a variable (or a box) is empty.
Therefore, earlier i tin utilization a variable, it must have a value. Do non assume the compiler or reckoner volition set roughly value, say 0, into a variable.
There are at to the lowest degree 3 ways to set a value into a variable:
• initializing it when the plan is run
• using an assignment statement
• reading a value from keyboard or other device amongst a READ statement.
Variables may conduct hold values assigned to them through the utilization of an assignment statement.
Such a arguing uses the assignment operator =
This operator does non announce equality. It assigns the value of the right-hand side of the arguing (the expression) to the variable on the left-hand side.
Examples:
Diameter = 5.9;
Area = length * width;
Note that only unmarried variables may appear on the left-hand side of the assignment operator
Initializing a variable is only done precisely in i lawsuit when the reckoner loads your plan into retention for execution.
That is, all initializations are done earlier the plan starts its execution. The utilization of un-initialized variables may travail unexpected result.
5. What is the departure betwixt declaring a variable as well as defining a variable?
Declaration of a variable inward C hints the compiler virtually the type as well as size of the variable inward compile time. Similarly, announcement of a component hints virtually type as well as size of component parameters.
No infinite is reserved inward retention for whatever variable inward instance of declaration. Example: int a; Here variable 'a' is declared of information type 'int' Defining a variable agency declaring it as well as besides allocating infinite to concur it. We tin say "Definition = Declaration + Space reservation". Example: int a = 10; Here variable "a" is described equally an int to the compiler as well as retention is allocated to concur value 10.
6. Define the term Scope, Visibility as well as Lifetime of a Variable?
The compass of a variable is the attain of plan statements that tin access that variable.
The lifetime of a variable is the interval of fourth dimension inward which storage is saltation to the variable.
A variable is visible within its compass as well as invisible or hidden exterior it.
7. What are the dissimilar types of variables depending on variable scope?
Depending on the compass of variables inward c language, variables could live on classified equally follows:
Global Variables:
Global variables inward C conduct hold their announcement exterior the component definition of all functions used within the plan as well as remains inward the retention equally long equally the plan is executing.
All global variables inward c could live on accessed from anywhere inward plan which agency that whatever seem inward the plan tin access the global variable regardless of what block that seem is written.
Thus, the global variables conduct hold their compass global.
Local Variables:
Local variables are declared amongst inward a component definition as well as hence could only live on accessed from anywhere inward that component inward which it is declared.
Thus, local variables conduct hold their compass local to the component inward which they are declared as well as are unknown to other functions exterior their ain function.
Local variables are defined as well as initialized every fourth dimension a component containing them is called as well as equally shortly equally the compiler exits from that function, the local variable is destroyed.
8. What is a storage shape inward C language?
 
A storage shape is an attribute that tells us where the variable would live on stored, what volition live on the initial value of the variable if no value is assigned to that variable, life fourth dimension of the variable as well as compass of the variable.
9. What are dissimilar types of storage classes inward C language?
Storage shape tells us:
? Where the variable is stored.
? Initial value of the variable.
? Scope of the variable. Scope specifies the component of the plan which a variable is accessed.
? Life of the variable.
There are iv storage classes inward C:
• Automatic storage class
• Register storage class
• Static storage class
• External storage class
10. What is an automatic storage shape inward C language?
The keyword for automatic storage shape is auto. Variables declared within the component trunk are automatic yesteryear default.
These variables are besides known equally local variables equally they are local to the component as well as don’t conduct hold pregnant exterior that component since, variable within a component is automatic yesteryear default, keyword auto are rarely used.
11. Where is the auto variable stored?
Auto variables tin live on stored anywhere, so long equally recursion works. Practically, they’re stored on the stack. It is non necessary that ever a stack exist. You could theoretically allocate component invocation records from the heap.
12. What are the advantages of auto variables?
• The same auto variable refer tin live on used inward dissimilar blocks.
• There is no side outcome yesteryear changing the values inward the blocks.
• The retention is economically used.
• Auto variables conduct hold protection because of local scope.
13. What are register variables? What are the advantages of using register variables?
If a variable is declared amongst a register storage class, it is known equally register variable.
The register variable is stored inward the CPU register instead of master copy memory. Frequently used variables are declared equally register variable equally its access fourth dimension is faster.
14. What does static variable mean?
Static variables are the variables which retain their values betwixt the component calls. They are initialized only in i lawsuit their compass is within the component inward which they are defined.
15. What is external storage class?
External variable tin live on accessed yesteryear whatever function. They are besides known equally global variables. Variables declared exterior every component are external variables.
In instance of large program, containing to a greater extent than than i file, if the global variable is declared inward file 1 as well as that variable is used inward file 2 then, compiler volition exhibit error.
To solve this problem, keyword extern is used inward file 2 to betoken that, the variable specified is global variable as well as declared inward roughly other file.
16. Can static variables live on declared inward a header file?
You can’t declare a static variable without defining it equally good (this is because the storage shape modifiers static as well as extern are mutually exclusive).
A static variable tin live on defined inward a header file, but this would travail each rootage file that included the header file to conduct hold its ain mortal re-create of a variable, which is in all probability non what was intended.
17. Is it acceptable to declare/define a variable inward a C header?
A global variable that must live on accessed from to a greater extent than than i file tin as well as should live on declared inward a header file. In addition, such a variable must live on defined inward i rootage file.
Variables should non live on defined inward header files, because the header file tin live on included inward multiple rootage files, which would travail multiple definitions of the variable.
The ANSI C touchstone volition permit multiple external definitions, provided that in that place is only i initialization. But because there's actually no wages to using this feature, it's in all probability best to avoid it as well as hold a higher score of portability.
"Global" variables that create non conduct hold to live on accessed from to a greater extent than than i file should live on declared static as well as should non appear inward a header file.
18. What is a pointer variable?
A pointer variable is a variable that may comprise the address of roughly other variable or whatever valid address inward the memory.
19. What are dissimilar types of type Qualifiers inward C language?
The keywords which are used to modify the properties of a variable are called type qualifiers.
There are 2 types of qualifiers available inward C language. They are,
• const
• volatile
20. What is const qualifier inward C language?
• Constants are besides similar normal variables. But, only departure is, their values can’t live on modified yesteryear the plan in i lawsuit they are defined.
• They refer to fixed values. They are besides called equally literals.
• They may live on belonging to whatever of the information type.
Syntax:
const data_type variable_name; (or) const data_type *variable_name;
21. What is volatile qualifier inward C language?
• When a variable is defined equally volatile, the plan may non modify the value of the variable explicitly.
• But, these variable values mightiness croak on on changing without whatever explicit assignment yesteryear the program. These types of qualifiers are called volatile.
• For example, if global variable’s address is passed to clock routine of the operating arrangement to shop the arrangement time, the value inward this address croak on on changing without whatever assignment yesteryear the program. These variables are named equally volatile variable.
Syntax:
volatile data_type variable_name; (or) volatile data_type *variable_name;
22. Can a variable live on both const as well as volatile?
Yes. The const modifier agency that this code cannot modify the value of the variable, but that does non hateful that the value cannot live on changed yesteryear agency exterior this code.
For instance, the timer construction was accessed through a volatile const pointer. The component itself did non modify the value of the timer, so it was declared const.
However, the value was changed yesteryear hardware on the computer, so it was declared volatile. If a variable is both const as well as volatile, the 2 modifiers tin appear inward either order.
23. What is signed as well as unsigned variable inward C language?
A numeric value may conduct hold a positive or a negative sign. In the memory, for a variable, i fighting is used only to hold the sign of the data.
If nosotros don't conduct hold sign, the sign fighting besides may live on used for data. If the value is negative, the sign fighting is 1, as well as if it is positive, it volition live on 0.
  Sumber http://www.gcreddy.com/
1. What is a variable inward C language?
Variables are retention place inward computer's retention to shop data. To betoken the retention location, each variable should live on given a unique refer called identifier.
Variable names are simply the symbolic representation of a retention location. Examples of variable refer are sum, car_no, count etc.
int num;
Here, num is a variable of integer type.
2. Give the rules for variable declaration?
The rules for variable announcement inward C are given below:
• Influenza A virus subtype H5N1 variable refer consists of alphabets, digits as well as the underscore (_) character.
• The length of variable should live on kept upto 8 characters though your arrangement may permit upto twoscore characters.
• They must start amongst an alphabet.
• Some systems besides recognize an underscore equally the commencement character.
• White infinite as well as commas are non allowed.
• Any reserved discussion (keyword) cannot live on used equally a variable name.
3. How to declare a variable inward C language?
All variables must live on declared earlier nosotros utilization them inward C program, although sure enough declarations tin live on made implicitly yesteryear content.
To declare a variable y'all specify its refer as well as information type it tin store. The variable announcement ever ends amongst a semicolon (;)
A announcement specifies a type, as well as contains a listing of i or to a greater extent than variables of that type equally follows:
type variable_list;
Here, type must live on a valid C information type including char, int, float, double, or whatever user defined information type etc., as well as variable_list may consist of i or to a greater extent than identifier names separated yesteryear commas.
A variable tin live on declared multiple times inward a program, but it must live on defined only once.
4. How to assign values to a variable?
A variable tin live on considered equally a box that tin concur a unmarried value. However, initially the content of a variable (or a box) is empty.
Therefore, earlier i tin utilization a variable, it must have a value. Do non assume the compiler or reckoner volition set roughly value, say 0, into a variable.
There are at to the lowest degree 3 ways to set a value into a variable:
• initializing it when the plan is run
• using an assignment statement
• reading a value from keyboard or other device amongst a READ statement.
Variables may conduct hold values assigned to them through the utilization of an assignment statement.
Such a arguing uses the assignment operator =
This operator does non announce equality. It assigns the value of the right-hand side of the arguing (the expression) to the variable on the left-hand side.
Examples:
Diameter = 5.9;
Area = length * width;
Note that only unmarried variables may appear on the left-hand side of the assignment operator
Initializing a variable is only done precisely in i lawsuit when the reckoner loads your plan into retention for execution.
That is, all initializations are done earlier the plan starts its execution. The utilization of un-initialized variables may travail unexpected result.
5. What is the departure betwixt declaring a variable as well as defining a variable?
Declaration of a variable inward C hints the compiler virtually the type as well as size of the variable inward compile time. Similarly, announcement of a component hints virtually type as well as size of component parameters.
No infinite is reserved inward retention for whatever variable inward instance of declaration. Example: int a; Here variable 'a' is declared of information type 'int' Defining a variable agency declaring it as well as besides allocating infinite to concur it. We tin say "Definition = Declaration + Space reservation". Example: int a = 10; Here variable "a" is described equally an int to the compiler as well as retention is allocated to concur value 10.
6. Define the term Scope, Visibility as well as Lifetime of a Variable?
The compass of a variable is the attain of plan statements that tin access that variable.
The lifetime of a variable is the interval of fourth dimension inward which storage is saltation to the variable.
A variable is visible within its compass as well as invisible or hidden exterior it.
7. What are the dissimilar types of variables depending on variable scope?
Depending on the compass of variables inward c language, variables could live on classified equally follows:
Global Variables:
Global variables inward C conduct hold their announcement exterior the component definition of all functions used within the plan as well as remains inward the retention equally long equally the plan is executing.
All global variables inward c could live on accessed from anywhere inward plan which agency that whatever seem inward the plan tin access the global variable regardless of what block that seem is written.
Thus, the global variables conduct hold their compass global.
Local Variables:
Local variables are declared amongst inward a component definition as well as hence could only live on accessed from anywhere inward that component inward which it is declared.
Thus, local variables conduct hold their compass local to the component inward which they are declared as well as are unknown to other functions exterior their ain function.
Local variables are defined as well as initialized every fourth dimension a component containing them is called as well as equally shortly equally the compiler exits from that function, the local variable is destroyed.
8. What is a storage shape inward C language?
A storage shape is an attribute that tells us where the variable would live on stored, what volition live on the initial value of the variable if no value is assigned to that variable, life fourth dimension of the variable as well as compass of the variable.
9. What are dissimilar types of storage classes inward C language?
Storage shape tells us:
? Where the variable is stored.
? Initial value of the variable.
? Scope of the variable. Scope specifies the component of the plan which a variable is accessed.
? Life of the variable.
There are iv storage classes inward C:
• Automatic storage class
• Register storage class
• Static storage class
• External storage class
10. What is an automatic storage shape inward C language?
The keyword for automatic storage shape is auto. Variables declared within the component trunk are automatic yesteryear default.
These variables are besides known equally local variables equally they are local to the component as well as don’t conduct hold pregnant exterior that component since, variable within a component is automatic yesteryear default, keyword auto are rarely used.
11. Where is the auto variable stored?
Auto variables tin live on stored anywhere, so long equally recursion works. Practically, they’re stored on the stack. It is non necessary that ever a stack exist. You could theoretically allocate component invocation records from the heap.
12. What are the advantages of auto variables?
• The same auto variable refer tin live on used inward dissimilar blocks.
• There is no side outcome yesteryear changing the values inward the blocks.
• The retention is economically used.
• Auto variables conduct hold protection because of local scope.
13. What are register variables? What are the advantages of using register variables?
If a variable is declared amongst a register storage class, it is known equally register variable.
The register variable is stored inward the CPU register instead of master copy memory. Frequently used variables are declared equally register variable equally its access fourth dimension is faster.
14. What does static variable mean?
Static variables are the variables which retain their values betwixt the component calls. They are initialized only in i lawsuit their compass is within the component inward which they are defined.
15. What is external storage class?
External variable tin live on accessed yesteryear whatever function. They are besides known equally global variables. Variables declared exterior every component are external variables.
In instance of large program, containing to a greater extent than than i file, if the global variable is declared inward file 1 as well as that variable is used inward file 2 then, compiler volition exhibit error.
To solve this problem, keyword extern is used inward file 2 to betoken that, the variable specified is global variable as well as declared inward roughly other file.
16. Can static variables live on declared inward a header file?
You can’t declare a static variable without defining it equally good (this is because the storage shape modifiers static as well as extern are mutually exclusive).
A static variable tin live on defined inward a header file, but this would travail each rootage file that included the header file to conduct hold its ain mortal re-create of a variable, which is in all probability non what was intended.
17. Is it acceptable to declare/define a variable inward a C header?
A global variable that must live on accessed from to a greater extent than than i file tin as well as should live on declared inward a header file. In addition, such a variable must live on defined inward i rootage file.
Variables should non live on defined inward header files, because the header file tin live on included inward multiple rootage files, which would travail multiple definitions of the variable.
The ANSI C touchstone volition permit multiple external definitions, provided that in that place is only i initialization. But because there's actually no wages to using this feature, it's in all probability best to avoid it as well as hold a higher score of portability.
"Global" variables that create non conduct hold to live on accessed from to a greater extent than than i file should live on declared static as well as should non appear inward a header file.
18. What is a pointer variable?
A pointer variable is a variable that may comprise the address of roughly other variable or whatever valid address inward the memory.
19. What are dissimilar types of type Qualifiers inward C language?
The keywords which are used to modify the properties of a variable are called type qualifiers.
There are 2 types of qualifiers available inward C language. They are,
• const
• volatile
20. What is const qualifier inward C language?
• Constants are besides similar normal variables. But, only departure is, their values can’t live on modified yesteryear the plan in i lawsuit they are defined.
• They refer to fixed values. They are besides called equally literals.
• They may live on belonging to whatever of the information type.
Syntax:
const data_type variable_name; (or) const data_type *variable_name;
21. What is volatile qualifier inward C language?
• When a variable is defined equally volatile, the plan may non modify the value of the variable explicitly.
• But, these variable values mightiness croak on on changing without whatever explicit assignment yesteryear the program. These types of qualifiers are called volatile.
• For example, if global variable’s address is passed to clock routine of the operating arrangement to shop the arrangement time, the value inward this address croak on on changing without whatever assignment yesteryear the program. These variables are named equally volatile variable.
Syntax:
volatile data_type variable_name; (or) volatile data_type *variable_name;
22. Can a variable live on both const as well as volatile?
Yes. The const modifier agency that this code cannot modify the value of the variable, but that does non hateful that the value cannot live on changed yesteryear agency exterior this code.
For instance, the timer construction was accessed through a volatile const pointer. The component itself did non modify the value of the timer, so it was declared const.
However, the value was changed yesteryear hardware on the computer, so it was declared volatile. If a variable is both const as well as volatile, the 2 modifiers tin appear inward either order.
23. What is signed as well as unsigned variable inward C language?
A numeric value may conduct hold a positive or a negative sign. In the memory, for a variable, i fighting is used only to hold the sign of the data.
If nosotros don't conduct hold sign, the sign fighting besides may live on used for data. If the value is negative, the sign fighting is 1, as well as if it is positive, it volition live on 0.