Interview Questions
- What is the difference between declaration and definition of a variable?
- What are different datatypes in C Language?
- What are the sizes of different datatypes in C Language?
- What is an unsigned int?
- Is float an accurate representation?
- What is an automatic variable?
- What are the default values for automatic variables?
- What happens to the value of an automatic variable when you re-enter the function?
- What are external variables?
- A function wants to access the value of an external variable. How does it do it?
- When can the declaration of an external variable be omitted?
- When are external variables initialized?
- What is the default value of external variables?
- Is using an external variable a good practice?
- When are static variables initialized?
- What is the default value of static variables?
- What does the keyword const represent?
- What is scope of a variable?
- What is a static function?
- What is a Register Variable? What are its advantages?
- Will a register variable definitely be stored in registers?
- Is it allowed to take address of a register variable?
- What is a block?
- What happens if a variable in the block has the same name as another variable in the function?
- How are integer constants handled in C Language?
- How are floating point constants handled in C Language?
- How can you mark an integer constant as long?
- How can you mark a floating point constant as float?
What is the difference between declaration and definition of a variable?
In C, all variables must be declared before they are used.
A declaration announces the properties of variables; name & type. No Storage is allocated.
`Definition'' refers to the place where the variable is assigned storage;
For automatic variables Declaration and Definition can happen simultaneously.
Example below show declarations and definitions of various types of variables.
#include <stdio.h> struct Point { int x; int y; }; //Declaration of a Structure int main() { struct Point p;//Definition of an instance of a Structure extern int externalVariable;//declaration of external variable int automaticVariable=5;//both declaration and definition } int externalVariable;//Definition of external variable
What are different datatypes in C Language?
Int. float, char, short, long and double.
What are the sizes of different datatypes in C Language?
The size of datatypes is machine-dependent. Depends on the Compiler. Restrictions : shorts and ints are at least 16 bits. longs are at least 32 bits. short is no longer than int, which is no longer than long.
What is an unsigned int?
unsigned numbers are always positive or zero.
Is float an accurate representation?
No. It is recommended to use double when accurate calculations are required.
What is an automatic variable?
In the example below p and automaticVariable are examples of automatic variables. They exist on for the lifetime of the function where they are declared.
struct Point { int x; int y; }; //Declaration of a Structure int main() { struct Point p;//Definition of an instance of a Structure int automaticVariable=5;//both declaration and definition }
What are the default values for automatic variables?
Garbage.
What happens to the value of an automatic variable when you re-enter the function?
Value is re-initialized. automaticVariable in increment function below is always initialized to 5 everytime it called. In the example below, it is called twice.
#include <stdio.h> void increment(){ int automaticVariable=5; automaticVariable++; printf("automaticVariable:%d\n",automaticVariable);//Output: automaticVariable:6 } int main() { increment(); increment(); }
What are external variables?
In the example below externalVariable is an external variable. It is defined outside a function. External variables are defined outside of any function, and are thus potentionally available to many functions.
int main() { extern int externalVariable; } int externalVariable;//Definition of external variable
A function wants to access the value of an external variable. How does it do it?
The variable must also be declared in each function that wants to access it; this states the type of the variable.
int main() { extern int externalVariable; } int externalVariable;//Definition of external variable
When can the declaration of an external variable be omitted?
If the definition of the external variable occurs before its use in a function extern declaration is not necessary.
int externalVariable;//Definition of external variable
int main() { //extern int externalVariable; // This is not necessary. }
When are external variables initialized?
External Variables are initialized only once before the program starts executing.
What is the default value of external variables?
External variables are initialized to zero by default.
Is using an external variable a good practice?
We should minimize use of external variables as much as possible. They make programs difficult to understand, especially if the programs are very big.
When are static variables initialized?
Static Variables are initialized only once before the program starts executing. Consider the program below: value of staticVariable in increment is initialized to 5 once and from then on, the changed value is retained. So, when the function is called twice, it prints 6 first and then 7.
#include <stdio.h> void increment(){ static int staticVariable=5; staticVariable++; printf("staticVariable:%d\n",staticVariable); } int main() { increment(); increment(); }
Output:
staticVariable:6 staticVariable:7
What is the default value of static variables?
Static variables are initialized to zero (or null) by default.
What does the keyword const represent?
The qualifier const in declaration of a variable specifies that its value will not be changed.
What is scope of a variable?
The scope of a variable is the part of the program within which the name of the variables can be used.
Scope of static/ automatic variable/parameters : In the function where they are declared, from the line where are declared.
Scope of an external variable : From the point it is declared to the end of the file being compiled.
What is a static function?
Function names are usually global except when the function is declared static. Static functions are available only in the file they are defined.
What is a Register Variable? What are its advantages?
A register declaration is an indication to the compiler that the variable in question will be heavily used. The compiler has the option of storing these variables in the machine registers, resulting in more efficient execution. External and Static variables cannot be Register Variables.
Will a register variable definitely be stored in registers?
Register variable is only a request to the compiler. Compilers are free to ignore the advice.
Is it allowed to take address of a register variable?
It is not possible to take the address of a register variable
What is a block?
Declarations of variables are allowed after the left brace that introduces a block statement.
#include <stdio.h> int main() { int i; { //block int j; } }
What happens if a variable in the block has the same name as another variable in the function?
It over shadows the variable in the function. Look at the example below:
#include <stdio.h> int main() { int i=10; printf("BEFORE BLOCK i:%d\n",i);//BEFORE BLOCK i:10 { //block int i=15; printf("IN THE BLOCK i:%d\n",i);//IN THE BLOCK i:15 } printf("AFTER BLOCK i:%d\n",i);//AFTER BLOCK i:10 }
How are integer constants handled in C Language?
Integer constant will be an int by default. If it is too big to fit in an int, it will be a long.
How are floating point constants handled in C Language?
Floating point constant will be an double by default.
How can you mark an integer constant as long?
With a suffix l or L. 200L or 200l.
How can you mark a floating point constant as float?
The suffixes f or F indicate a float constant;