Interview Question and Answers On C1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Interview Questions and Answers on C

www.oureducation.in

1. What is C Language?
C is a programming language used to write a program. Programs are the set of instructions given by
a programmer to the computer in high level language. C uses a compiler to translate the high level
program into machine code before executing any instruction.

2. What does static variable mean?
Static variable is available to a C application, throughout the life time. At the time of starting the
program execution, static variables allocations takes place first. In a scenario where one variable is
to be used by all the functions (which is accessed by main () function), then the variable need to be
declared as static in a C program.
3. What is the difference between calloc() and malloc() ?
 A block of memory may be allocated using the function malloc. The malloc function reserves a block
of memory of specified size and returns a pointer of type void. This means we can assign the base
address of the block to any type of pointer
                                      Syntax –     P = (cast type*)malloc(byte size);
Calloc is also a memory allocation function which is generally used to allocate memory for array snd
structure .malloc is used to allocate a single block of storage space, calloc allocates multiple blocks
of storage, each of same size and initializes them with zero.
                                         Syntax -     P = (cast type*)calloc(n,array size);
4. What is a null pointer?
 A null pointer is a special pointer value that is known not to point anywhere. It means that no other
valid pointer, to any other variable or array cell or anything else, will ever compare equal to a null
pointer.
5. Advantages of a macro over a function?
Actually macro and function are used for different purposes. A macro replaces its expression code
physically in the code at the time of preprocessing. But in case of function the control goes to the
function while executing the code.
So when the code is small then it is better to use macro. But when code is large then function should
be used.

6. What is page thrashing?
 It happens when a high level of paging activity. Thrashing is caused by under allocation of minimum
number of pages required by a process, forcing it to continuously page fault. The system can detect
Interview Questions and Answers on C
www.oureducation.in
thrashing by evaluating the level of CPU utilization as compared to the level of multiprogramming. It
can be eliminated by reducing the level of multiprogramming.
7. How do you override a defined macro?
You can use the #undef preprocessor directive to undefined (override) a previously defined
macro.
8. What are the different storage classes in C?
C has three types of storage: automatic, static and allocated. Variable having block scope and
without static specifier have automatic storage duration. Variables with block scope and with static
specifier have static scope. Global variables (i.e., file scope) with or without the static specifier
also have static scope. Memory obtained from calls to malloc(), alloc() or realloc() belongs to storage
class.
9. When does the compiler not implicitly generate the address of the 
first element of an array?
  Whenever an array name appears in an expression such as
• Array as an operand of the sizeof operator.
• Array as an operand of “&” operator.

• Array as a string literal initializer for a character array.

Then the compiler does not implicitly generate the address of the
address of the first element of an array.

10. Is using exit () the same as using return?
No, The exit () function is used to exit your program and return control to the operating system.
The return statement is used to return from a function and return control to the calling function. If you
issue a return from the main () function, you are essentially returning control to the calling function,
which is the operating system. In this case, the return statement and exit () function are similar.

11.  What is the output of the following program?        
   #include<stdio.h>
            main()
              { Int  x=8;
                  Printf(“x=%d”, ++x);
                  Printf(“x=%d”, –x);
               }
Ans:- x=9, x=8
Interview Questions and Answers on C
www.oureducation.in
12.  What do you mean by dynamic memory allocation? Give an example.
The process of allocating memory at the time of execution is called dynamic memory allocation. The
allocation and release of this memory space can be done with the help of some built in functions
whose prototypes are found in alloc.h and stdlib.h.
Example:-
#include<stdio.h>
#include<alloc.h>
main()
{
int *p, n, i;
printf( “Enter the number of integers to be entered.”);
scanf(“%d”, &n);
p=(int *)malloc(n*sizeof(int));
if(p==NULL)
{
printf(“Memory not available”);
exit(1);
}
for(i=0;i<n;i++)
{
printf(“Enter an integer”);
scanf(“%d”, p+1);
}
for(i=0;i<n;i++)
printf(“%d”, *(p+i));
}
return 0;
}
Interview Questions and Answers on C
www.oureducation.in

You might also like