Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C Interview Questions

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

C Programming interview questions :-

Q. What is difference between c and embedded c ?


Ans :-
The following table gives us the summary of the difference between C and Embedded C.

C Language Embedded C
A type of high level language. Nothing but an extension of C.
The source code format depends upon the
Has a free-format of program coding. type of microprocessor/ microcontroller
that is used.
Optimization is normal. High level of optimization.
An Operating system is essential to Can work with or without an Operating
function. system.
Used for microcontroller/ microprocessor
Used for desktop applications.
applications.
Does not bother about memory Uses limited resources.
resources. Hence consider memory allocation of the
i.e., use entire memory available in CPU. embedded processor.
C programs run in console. They run in realtime constraints.
Hence see the output through desktop Hence output is not available in OS
Directly or indirectly support other
Supports only required processor of the
various programming languages during
application.
application.
Input can be given to the program while Only the pre-defined input can be given to
it is running. the running program.
Requires compilers of Embedded C.
C compilers are OS dependent.
These compilers in turn enable access to
i.e, run the ram directly from OS
all resources of the embedded system.
terminal.
Hence OS independent.
Some of the examples of Applications:
Some of the examples of real-time
logical programs, system software
Applications: DVD, TV,Digital camera.
programs.
Q. What is job of preprocessor, compiler, assembler and linker ?
 The preprocessor commands are processed and expanded by the preprocessor before actual
compilation.
 After preprocessing, the compiler takes the output of the preprocessor and the source code, and
generates assembly code.
 Once compiler completes its work, the assembler takes the assembly code and produces an
assembly listing with offsets and generate object files.
 The linker combines object files or libraries and produces a single executable file. It also
resolves references to external symbols, assigns final addresses to functions and variables, and
revises code and data to reflect new addresses.

Q. What is preprocessor ?

Preprocessor is a Program That processes its input data to produce output that is used as input to
another program. The output is said to be a pre processed form of the input data, which is often used by
some subsequent programs like compilers. The amount and kind of processing done depends on the
nature of the pre processor.

Some Pre-processors Are Only Capable Of Performing Relatively Simple Textual Substitutions And
Macro Expansions, while others have the power of full-fledged programming languages.

Q. Can a pointer be volatile?


Ans :- If we see the declaration volatile int *p, it means that the pointer itself is not volatile and points
to an integer that is volatile. This is to inform the compiler that pointer p is pointing to an integer and
the value of that integer may change unexpectedly even if there is no code indicating so in the program.

Q. What Is Pass By Value And Pass By Reference? How Are Structure Passed As Arguments?
Answer :
The parameter to a function can be a copy of a value that is represented by variable or can be a
reference to a memory space that stores value of variable. The former is referred to as pass by value
and the latter is referred to as pass by reference. The difference is that when parameters are passed by
value the changes made to the variable value within the function is not reflected in the caller function,
but when passed as reference changes are reflected outside the called function. The structures are
always passed by reference.
Q. What Is The Volatile Keyword Used For?
Answer :
The volatile keyword is used to represent variables that point to memory in other mapped devices. In
such a case the value of the variable can be changed outside of a program. The compiler does not do
additional optimizations to the code if there is volatile keyword.

Q. What Does Malloc Do? What Will Happen If We Have A Statement Like Malloc(sizeof(0));
Answer :
Malloc is the function that is used for dynamically allocating memory to the different variables. The
malloc returns a memory pointer of void type (void *). The statement malloc(sizeof(0)) returns a valid
integer pointer because sizeof(0) represents the size of memory taken up by the integer value of 0. The
memory allocated by memory is not automatically cleaned up by the compiler after execution of the
functions and should be cleaned up by the programmer using the free() function.

Q. What is void pointer and what is its use?


Ans :- The void pointer means that it points to a variable that can be of any type. Other pointers points
to a specific type of variable while void pointer is a somewhat generic pointer and can be pointed to
any data type, be it standard data type(int, char etc) or user define data type (structure, union etc.). We
can pass any kind of pointer and reference it as a void pointer. But to dereference it, we have to type the
void pointer to correct data type.

Q. Is Count Down_to_Zero Loop better than Count_Up_Loops?


Ans :- Count down to zero loops are better. Reason behind this is that at loop termination, comparison
to zero can be optimized by the compiler. Most processors have instruction for comparing to zero. So
they don't need to load the loop variable and the maximum value, subtract them and then compare to
zero. That is why count down to zero loop is better.

Q. How to write code for an infinite loop in C?


Ans :-
There are several ways to code an infinite loop -

Method 1 :-
while(1)
{

}
Method 2 :-

for(;;)
{

Method 3 :-
Loop:

goto Loop

Q. Why ++n executes faster than n+1?


Ans :- The expression ++n requires a single machine instruction such as INR to carry out the increment
operation. In case of n+1, apart from INR, other instructions are required to load the value of n. That is
why ++n is faster.

Q. Which bit wise operator is suitable for checking whether a particular bit is on or off?
Ans :- "Bitwise And" (&) is used to check if any particular bit is set or not. To check whether 5'th bit is
set we can write like this
bit = (byte >> 4) & 0x01;
Here, shifting byte by 4 position means taking 5'th bit to first position and "Bitwise And" will get the
value in 0 or 1.

Q. Explain What Are The Different Storage Classes In C?


Answer :
Four types of storage classes are there in c,
1.Auto
2.Register
3.Static
4.Extern or Global

Q. When Should We Use The Recursion Function? Mention What Happens When Recursion
Functions Are Declared Inline?
Answer :
Recursion function should be used when we are aware of the number of recursive calls is not excessive.
Inline functions property says whenever it will call, it will copy the full definition of the function.
Recursive functions declared as inline, creates the burden for the compiler's execution.

Q. What Is Pass By Value And Pass By Reference? How Are Structure Passed As Arguments?
Answer :
The parameter to a function can be a copy of a value that is represented by variable or can be a
reference to a memory space that stores value of variable. The former is referred to as pass by value
and the latter is referred to as pass by reference. The difference is that when parameters are passed by
value the changes made to the variable value within the function is not reflected in the caller function,
but when passed as reference changes are reflected outside the called function. The structures are
always passed by reference.

Q. What Is The Volatile Keyword Used For?


Answer :
The volatile keyword is used to represent variables that point to memory in other mapped devices. In
such a case the value of the variable can be changed outside of a program. The compiler does not do
additional optimizations to the code if there is volatile keyword.

Q. What Does Malloc Do? What Will Happen If We Have A Statement Like Malloc(sizeof(0));
Answer :
Malloc is the function that is used for dynamically allocating memory to the different variables. The
malloc returns a memory pointer of void type (void *). The statement malloc(sizeof(0)) returns a valid
integer pointer because sizeof(0) represents the size of memory taken up by the integer value of 0. The
memory allocated by memory is not automatically cleaned up by the compiler after execution of the
functions and should be cleaned up by the programmer using the free() function.

Q. What Is A Memory Leak? What Is A Segmentation Fault?


Answer :
The memory leak refers to the uncleared memory may builds up across lifetime of the process. When it
comes to a huge value system stalls its execution due to unavailability of the memory. The
segmentation fault on the other hand refers to condition when our program tries to access a memory
space that has already been freed up.
Q. What Is Meant By A Forward Reference In C?
Answer :
The forward reference refers to the case when we point an address space of a smaller data type with a
pointer of a bigger data type. This can be pictured as allocating memory in single bytes and accessing it
with integer pointer as chunks of 4.

Q. What Are The Uses Of The Keyword Static?


Answer :
 A variable declared static within the body of a function maintains its value between function
invocations .
 A variable declared static within a module, (but outside the body of a function) is accessible by
all functions within that module. It is not accessible by functions within any other module. That
is, it is a localized global.
 Functions declared static within a module may only be called by other functions within that
module. That is, the scope of the function is localized to the module within which it is declared.

Q. What Is The Use Of Volatile Keyword?


Answer :
 The C's volatile keyword is a qualifier that tells the compiler not to optimize when applied to a
variable. By declaring a variable volatile, we can tell the compiler that the value of the variable
may change any moment from outside of the scope of the program. A variable should be
declared volatile whenever its value could change unexpectedly and beyond the comprehension
of the compiler.
 In those cases it is required not to optimize the code, doing so may lead to erroneous result and
load the variable every time it is used in the program. Volatile keyword is useful for memory-
mapped peripheral registers, global variables modified by an interrupt service routine, global
variables accessed by multiple tasks within a multi-threaded application.

Q. Can A Variable Be Both Const And Volatile?


Answer :
The const keyword make sure that the value of the variable declared as const can't be changed.
This statement holds true in the scope of the program. The value can still be changed by outside
intervention. So, the use of const with volatile keyword makes perfect sense.

Q. Can A Pointer Be Volatile?


Answer :
If we see the declaration volatile int *p, it means that the pointer itself is not volatile and points to
an integer that is volatile. This is to inform the compiler that pointer p is pointing to an integer
and the value of that integer may change unexpectedly even if there is no code indicating so in
the program.

Q. What Is Null Pointer And What Is Its Use?


Answer :
The NULL is a macro defined in C. Null pointer actually means a pointer that does not point to any
valid location. We define a pointer to be null when we want to make sure that the pointer does not point
to any valid location and not to use that pointer to change anything. If we don't use null pointer, then
we can't verify whether this pointer points to any valid location or not.

Q. What Is Void Pointer And What Is Its Use?


Answer :
The void pointer means that it points to a variable that can be of any type. Other pointers points to a
specific type of variable while void pointer is a somewhat generic pointer and can be pointed to any
data type, be it standard data type(int, char etc) or user define data type (structure, union etc.). We can
pass any kind of pointer and reference it as a void pointer. But to dereference it, we have to type the
void pointer to correct data type.

Q. Can Static Variables Be Declared In A Header File?


Answer :
A static variable cannot be declared without defining it. A static variable can be defined in the header
file. But doing so, the result will be having a private copy of that variable in each source file which
includes the header file. So it will be wise not to declare a static variable in header file, unless you are
dealing with a different scenario.

Q. Why Cannot Arrays Be Passed By Values To Functions?


Answer :
In C, the array name itself represents the address of the first element. So, even if we pass the array
name as argument, it will be passed as reference and not its address.

Q. What Is Wild Pointer?


Answer :
A pointer that is not initialized to any valid address or NULL is considered as wild pointer. Consider
the following code fragment -
int *p;
*p = 20;
Here p is not initialized to any valid address and still we are trying to access the address. The p will get
any garbage location and the next statement will corrupt that memory location.

Q. What Is Dangling Pointer?


Answer :
If a pointer is de-allocated and freed and the pointer is not assigned to NULL, then it may still
contain that address and accessing the pointer means that we are trying to access that location
and it will give an error. This type of pointer is called dangling pointer.

You might also like