echo '' ;

C – Interview Questions

These interview questions specifically focus on understanding how C is applied in various domains and industries, highlighting its versatility and relevance in software development. These interview questions explore C’s diverse applications in various industries, showcasing its versatility and relevance in software development. They cover a range of topics to assess candidates’ understanding of fundamental C programming concepts.

Contents

C-Quiz

C Quiz-1
C Quiz-2
C Quiz-3
C Quiz-4
C Quiz-5
C Quiz-6

Basic

What is size of character, integer, integer pointer, character pointer?

  • Character – 1 Byte
  • Integer – 2 byte or 4 byte
  • Integer Pointer (The size of a pointer is platform-dependent)
    • 64 bit computer 8 byte
    • 32 bit computer 4 byte
  • Character Pointer
    • Character pointer is platform dependent, in a 32 bits system it’s 4 bytes.



#define cat(x,y) x##y concatenates x to y. But cat(cat(1,2),3) does not expand but gives preprocessor warning. Why?

cat(cat(1,2),3) will be replaced to 1##2##3, ## could only concatenate two arguments. The problem is that cat(cat(1,2),3) isn’t expanded in a normal way which you expect that cat(1,2) would give 12 and cat(12, 3) would give 123.



What is the output of printf(“\nab\bcd\ref”); ?

  • Output is efd
  • \n  means new line
  • \b  means backspace
  • \r means delete to the beginning of the line, so it will delete ‘acd’



is the output correct?? a+++b  -> (a++)+b



++ip increments what?

it pre increments the ip variable.



Operations involving unsigned and signed

Unsigned will be converted to signed



What is the result of below Program?

#include<stdio.h>
int main()
{
    int k=0;
    k=1,2;
    printf("%d\n",k);
    return 0;
}

Answere: 1

Explanation: Assigment will be always Right to Left, So first k=2 will be assign and next k=1 will be assign



What do you mean by const keyword ?



Which way of writing infinite loops is more efficient than others?

  • there are 3ways.
    • while(1) ;
    • for(;;);
    • goto
  • I think goto  is the most efficient



How do you write a function which takes 2 arguments – a byte and a field in the byte and returns the value of the field in that byte?

We can move the number using shift operator (>>)



Which parameters decide the size of data type for a processor ?

  • Answers will add Soon

Multiply by 15 using bitwise operator (without addition and multiplication)?

#include <stdio.h>

int main()
{
    int a;
    printf("Enter the number : ");
    scanf("%d",&a);
    char b = 15;
    int c = 0;
    while(b--)
    {
        c = c + a ;
    }
    printf("AEW: Multiplication of 15 * %d = %d", a,c);

    return 0;
}


Storage Class

What are static variables?



How to use a variable in a source file that is defined in another source file?

Using extern keyword



Array

Why cannot arrays be passed by values to functions?

The array name represents the first element in the array.



Pointer

What is NULL pointer and what is its use?

NULL pointer assignment : char q=0;  (or) char *q=(char)0;  (or) char *p=NULL;

  • Example
    • What is (void*)0 ? //Representation of NULL pointer
  • NULL Macro is defined in locale.h , stddef.h , stdio.h , stdlib.h , string.h , time.h , and wchar.h

What is void pointer and what is its use?

  • When we do not know what type of the memory address the pointer variable is going to hold, then we declare a void pointer for such.
  • If a function is defined like: int func (void *a);
  • It can take any type of pointer

What is forward reference w.r.t. pointers in c?

  • Forward reference means a reference is used before its declaration.
    There are only two cases:
    • structure and
    • goto statement

Array of pointer to functions?

An array of pointers to functions is a construct in C that allows storing pointers to different functions within an array. Each element of the array is a pointer to a function, enabling flexibility in function invocation and organization.


Structure

How can you define a structure with bit field members?

  • Answere will add soon.

Can structures be passed to the functions by value?

  • Yes, structures can be passed by value.



Find the structure’s base address using one of the member’s addresses?

#include 

struct AEW
{ int Arun;
  int Eworld;
};


int main()
{

  struct AEW base;
  printf("%u\n",&base);
  printf("%u",&base+1);
  return 0;
}



Difference between Structure and Union?

This is very important question for all embedded engineers


Memory

malloc(sizeof(0)) will return

  • valid pointer

Others

Exchange or swap the all bits in the following manner without using 2nd variable?

  • Swap 15th to 0th location
  • Swap 14th to 1th Location
  • Swap 13th to 2nd Location
  • ……………………..
  • ……………………..
  • Swap 10th to 5th Location
  • ……………………….
  • ……………………….
  • Swap 8th to 7th Location
15th Bit0th bit
1010101010101010
  • Answers will add Soon

What is the use of #progma  keyword

  • This is a preprocessor directive that can be used to turn on or off certain features.
  • It is of two types #pragma startup  , #pragma exit  and #pragma warn  .
    • #pragma startup allows us to specify functions called upon program startup.
    • #pragma exit  allows us to specify functions called upon program exit.

How are macros different from inline functions?

Macros :

  • Its used for when the set of instruction/function/task repeatedly run again&  again. Macros is defined by #define macro_name
  • Example : Add the two numbers using macros #define add(a,b) (a+b) , Here the add is the macro name perform the two number addition functionality.
  • The major disadvantage of macros is that they are not really functions and the usual error checking and stepping through of the code does not occur.

Inline :

  • Whenever inline functions are invoked, they expand, avoiding the control going to the place where the function is defined. They also bypass all activities, such as saving the return address when a jump is performed. Saves time in case of short codes.
  • Example : inline float add(float a,float b) { return a+b; }
  • Inline is just a request to the compiler and it is upto to the compiler whether to substitute the code at the place of invocation or perform a jump based on its performance algorithms.

Advantages and disadvantages of using macro and inline functions?

  • Macro does not have type checking. The compiler replaces the entire function with an inline function.

What happens when recursion functions are declared inline?

  • It depends some compiler will expand it up to a limit, others will simply not expand it.

What is the work of compiler?

Compiler – Source code to Binary code

What is job of preprocessor, compiler, assembler and linker ?

  • Answers will add Soon

What is the difference between static linking and dynamic linking ?

  • Answers will add Soon

Memory

What is the segmentation fault error in C?

Segmentation fault is a runtime error. This will occur due to some causes when the program is running time.

  • Cause-1: Trying to access a memory area which is read-only.
  • Cause-2: Using the dereferenced pointer : A pointer may does not have a valid address/memory location to point. It may also happen when you try to free a memory (using a pointer), which is already freed.

What is ‘stack overflow’ error in C?

In C, a “stack overflow” error occurs when the call stack, a region of memory used for function call and local variable storage, exceeds its predefined size. This typically happens due to excessive recursion or allocating too much memory for local variables within functions. When the stack size limit is reached, the program will terminate abnormally, usually resulting in a segmentation fault or a crash.