C Interview question and answers

May 8, 2021 0 Comments

C Interview question and answers

C Interview Question and Answers

1. What are the memory segments in C?

Ans: There are different types of memory segments in userspace.

    1. Text segment: text segment will store all the instructions which are part of the program.

    2. Stack: stack segment is used to store all local variables and is used for passing arguments to the functions along with the return address of the instruction which is to be executed after the function call over. The stack and heap grow in opposite directions.

    3. Initialized Data: Initialized data stores all global, static and const that are initialized beforehand.

char c[] = "Blog";
const char c[] = "Blog";
static int a = 10;

    4. Uninitialized Data: uninitialized data stores global and static that are uninitialized beforehand.

int a;
static int b;

    5. Heap: Heap memory used to store the dynamic memory allocation.

char *a = malloc(sizeof(char));

2. What are the Storage classes in C?

Ans: There are four different storage classes

    1. auto: This is default storage class for all local variables.

int a;
auto int b;

 the above both variables with in the same storage class.

    2. register: The register storage class is used to define local variables that should be stored in a register instead of RAM. The variable size equal to the register size and can’t have the unary ‘&’ operator.

register int a;

    3. static: The static storage class instructs to the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope.

static int a;  /*global variable*/
void func() {
    static int b; /*local static variable*/
}

    4. extern: The extern storage class is used to give a reference of a global variable that is visible to all program files. if you extern, “this variable is declared here, but is defined elsewhere”.

main.c
int a;

fun.c
extern int a;

3. What is Volatile Qualifier?

Ans: Volatile specifies a variable whose value may be changed by processes outside the current program.

4. what is const keyword?

Ans: if  you define const keyword infront of varbile name that varible is read-only.

const int a = 10;

5. Write a Program to set bit given position in MSB?

Ans:

void bitset(int n, int p) {
    int i = 1;
    i = i << (pos-1);
    n = n|i;
}

6. Write a Program delete a number in double linked list?

Ans:

int dlete_ele(int num) {
    struct node *tmp,*prv;
    tmp = start;
	while(tmp && (num != tmp->data)) {
          prv = tmp;
          tmp = tmp->next;
    }
    if(tmp == start) {
        start = tmp->next;
        start->prev = NULL;
        free(tmp);
        return 0;
    }
    if(tmp->next == NULL) {
        prv->next = tmp->next;
        free(tmp);
    }
    else {
        prv->next = tmp->next;
        tmp->next->prev = prv;
        free(tmp);
    }
    return 0;
}

7. Write a program reverse single linked list?

Ans:

void reverse_itr(struct list **ptr) {
     struct list *tmp = NULL, *q=*ptr;
     while(q) {
         q= q->next;
         (*ptr)->next = tmp;
         tmp= *ptr;
         if(q)
            *ptr=q;
     }
}

8. Write a Program to set bit in given position?

Ans:

void bitset(int n, int pos) {
        int i = 1;
        i = i << (pos-1);
        n = n|i;
}

9. Write a program to check the PC is little endian or big endian?

Ans:

int find_lsb_msb() {
        int a = 1;
        char *p = &a;
        if(*p) {
            printf(“little endian\n”);
        } else {
            printf(“big endian\n”);
        }
        return 0;
}

10. Difference between structure and union?

Ans: A structure is a collection of one or more variables, possibly of different data types, grouped together under a single name to represent single entity.

A union is a collection of one or more variables, possibly of different data types, grouped together under a single name to represent single entity.

structure size is greater than or equal to sum of all member size. union size is equal to the largest member size.

structure members can be accessed any time. in union only one member accessed at a time. 

11. Write a Program to reverse a string?

Ans:

char *reverse(char *str) {
        char *s;
        int len = strlen(str);
        s = (char *)malloc(len);
        int count=0, i=0;
        for(;*str !=’\0′;){
            count++;
            str++;
        }
        for(i=0; i<=count;i++){
            s[i] = *str;
            str--;
        }
        s[i] = ‘\0’;
        return s;
}

int main() {

    char *s = reverse(“sathi”);
    int i;
    for(i=0;i<10;i++){
        printf(“%c”, s[i]);
    }
    free(s);
    return 0;
}

12. Which functions allocate dynamic memory allocation?

Ans: malloc, calloc and realloc functions used to allocate dynamic memory.

13. What is the sizeof structure?

        struct x{

            int a;

            char y;

            int x;

            };

Ans: 12 bytes.

14. what are different stages of compilation process?

Ans: There are 4 stages of compilation process.

1. pre-processor: The job of pre-processor is take source files(example.c) as an input and pre-processor file example.i produced. If we go through the example.i file we will notice that stdio.h header file will be replaced with its full code.

gcc -E example.c -o example.i

2. compiler: The job of compiler is take pre-processor output as an input and assembler file example.s produced. The output present in example.s is assembly level instructions.

gcc -S example.i -o example.s

3. assembler: The job of assembler is take compiler output as an input and an intermediate file example.o produced. this file also known as relocatable file. the output of this stage is a machine level file (examle.o). So we cannot view the content of it through editor. This can be viewed through a special tool called objdump.

gcc -c example.s -o example.o
objdump -D example.o | more

4. linker: The job of linker is take assembler output as an input and an executable file example produced. This is the final stage at which all the linking of function calls with their definitions is done. The linker also does some extra work; it adds some extra code to our program that is required when the program starts and when the program ends. We call this code as runtime code.

gcc example.o -o example

15. what use of the linker?

Ans: This is the final stage at which all the linking of function calls with their definitions is done. The linker also does some extra work; it adds some extra code to our program that is required when the program starts and when the program ends. We call this code as runtime code.

16. what is structure padding?

Ans: Structure padding is done by the compiler by pushing unused bytes in between the members of the structure to make sure that every member is aligned in the memory.

17. what is the use of function pointer?

Ans: Function pointers can be useful when you want to create callback mechanism , and need to pass address of an function to another function. They can also be useful when you want to store an array of functions, to call dynamically.

18. difference between relocatable and executable binary?

Ans:  Instructions of relocatable binary are bound to offset address assigned as per the position of instruction with in the procedure. Executable binary contains instructions bound to platform specific load address.

function call instructions in relocatable object files reffered to called functions offset. call instructions in executable binaries reffered to function base address.

19. Write a program to swap a nibble?

Ans:

int main(void) {
        int a = 11; 
        int c;
        c = a << 4 | a >> 4;
        printf(“%d %d \n”, a,c);
        return 0;
}

20. Write a program to take n as input and print 1 to n in the following order?

    1

    1 2

    1 2 3 4 

    1 2 3 4 n

Ans:

int main(void) {
        int n= 5;
        int i=0,j=0;
        for(i=1;i<=n;i++){
            for(j=1;j<=i;j++){
                printf(“%d”,j);
            }
            printf(“\n”);
        }
    return 0;
}

22. Write a program to print 1 to n without using loop?

Ans:

 int num(int n){
        if(n) {
        num(n-1);
        printf(“%d”, n);
        }
}

23. Given binary array, sort and print such that all 0s are printed first and then all 1s?

        int a[10] = {1,0,1,1,0,1,0,1};

Ans:

int main(void) {
        int len;
        len = sizeof(arr)/sizeof(int);
        int i = 0,j=0,tmp;

        for(i = 0; i<len; i++){
	    tmp = arr[i];
	    for(j=i+1;j<len;j++){
		if(tmp > arr[j]){
	            arr[i] = arr[j];
		    arr[j] = tmp;
		    break;
	         }
	     }
         }

        for(i=0;i<len; i++){
	   printf(“%d “, arr[i]);
        }
    return 0;
}

24. what are the different types of searching and sorting techniques?

Ans: Searching techniques: 1. Linear search 2. binary search 3. interpolation search 4. Hash table

        Sorting Techniques: 1. Bubble sort 2. insertion sort 3. selection sort 4. merge sort 5. shell sort  6. quick sort

25.  Write a program for fibonacci series?

Ans:

#include<stdio.h>
int fibonacci(int b){

    int i=0,c[10];
    c[0] =1;
    c[1] =1;

    for(i=2;i<=b; i++){
        c[i] = c[i-1]+c[i-2];  //fn = fn-1 +fn-2  1 1 2 3 5
    }
    for (i=0;i<b;i++)
        printf("%d ", c[i]);
    printf("\n");

}

int main(void) {
    fibonacci(10);
    return 0;
}

26. Write a program for prime numbers in between n number?

Ans:

void prime_num(int a, int b) {

    int flag = 0,i=0, j=0;

    while(a < b) {
        flag = 0;
        for(i=2;i <= a/2; i++) {        
	    if(a%i == 0){
	        flag = 1;
		break;
	    }
	}

        if(flag == 0) {
	    printf(“%d”, a);
	    flag = 0;
        }
        a++;
        }
}

27. Write a program single linked list insert and delete functions?

Ans:

int insert(strcut list *start, int data) {
	strcut list *new;
	new = malloc(sizeof(strcut list));
	if(new == NULL) {
		printf("malloc failed \n");
		return -1;
	}
	new->data = data;
	new->next = NULL;
	if(start == NULL) {
		start = new;
		return 0;
	} else {
		new->next = start;
		start = new;
	}
	return 0;
}
int delete(struct list *start, int data){
	struct list *tmp;
	tmp = start;
	if(tmp != NULL){
	start = tmp->next;
	free(tmp);
	return 0;
}

28. Write a program for stack and queue?

Ans:

Queue:
	struct manager {
		int data[MAX];
		int curr_ele;
	};
	struct manager obj;
	int enqueue(int new) {
		if(obj.curr_ele > 99)
			printf("queue is full\n");
		obj.data[obj.curr_ele] = new;
		obj.curr_ele++;</p>
	}

	int dequeue(void) {
		int i,temp;</p>
		if(obj.curr_ele == 0)
			printf("queue is empty \n"); 
		temp = obj.data[0];
		for(i = 0 ; i < obj.curr_ele;i++) {
			obj.data[i] = obj.data[i + 1];
		}
		obj.curr_ele--;
		return temp;
	}

Stack:
	int push(stack_t *p,int new) {
		int i;
		i =p->curr_ele;
		if(p->curr_ele > 99)
			printf("stack is full\n");
		p->data[0] = new;
		for(;i>0;i--) {
			p->data[i] = p->data[i-1];
		}
		curr_ele++;
	}
	int pop(stack_t *p) {
		int i,temp;
		if(p->curr_ele < 0)
			printf("stack is empty \n");
			temp = p->data[0];
			for(i = 0 ; i < p->curr_ele;i++) {
				data[i] = p->data[i + 1];
			}
		curr_ele--;
		return temp;
	}

29. Write a program for adding two numbers without using arithmetic operations?

Ans:

int Add(int x, int y) { 
    while (y != 0) { 
        int carry = x & y; 
        x = x ^ y;
        y = carry << 1;
        }
    return x;
}

30. Write a program for set, clear and toggle bits in the given range?

Ans:

unsigned int clearBits(unsigned int n, unsigned int l, unsigned int r) { 
    int num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
    return (n & ~num); 
}
unsigned int setBits(unsigned int n, unsigned int l, unsigned int r) { 
    int num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
    return (n | num); 
}
unsigned int setBits(unsigned int n, unsigned int l, unsigned int r) { 
    int num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
    return (n ^ num); 
}

C Interview question and answers

Share This: