Top Common C Programming Interview Question and Answer

0

What is the difference between declaration and definition of a variable or function?

Ans :- Declaration of a variable or function simply declares that the variable or function exists somewhere in the program but the memory is not allocate for them. But the declaration of a variable or function serves an important role. And that is the type of the variable or function. Therefore, when a variable is declared, the program knows the data type of that variable. In case of function declaration  the program knows what are the arguments to that functions  their data types the order of arguments and the return type of the function. So that’s all about declaration. Coming to the definition, when we define a variable or function, apart from the role of declaration, it also allocates memory for that variable or function. visit GOOPHE for Top Common C Programming Interview Question.

 

What are different storage class specifiers in C Programming?

Ans :- auto, register, static, extern

Also Visit : Top 10 Common Interview Questions And Answers For Freshers

What is scope of a variable ? How are variables scoped in C Programming ?

Ans :- Scope of a variable is the part of the program where the variable may directly be accessible. In C, all identifiers are lexically or statically scoped. visit GOOPHE for Top Common C Programming Interview Question.

 

How will you print “Hello World” without semicolon in C Programming ?

Ans :- #include <stdio.h>

int main(void)

{

if (printf(“Hello World”)) {

}

}

Book Tickets : https://www.flattickets.com/

When should we use pointers in a C programming ?

  1. To get address of a variable
  2. For achieving pass by reference in C :- Pointers allow different functions to share and modify their local variables.
  3. To pass large structures so that complete copy of the structure can be avoided.
  4. To implement “linked” data structures like linked lists and binary trees.

visit GOOPHE for Top Common C Programming Interview Question.

 

What is NULL pointer ?

Ans :- NULL is used to indicate that the pointer doesn’t point to a valid location. Ideally, we should initialize pointers as NULL if we don’t know their value at the time of declaration. Also, we should make a pointer NULL when memory pointed by it is deallocated in the middle of a program.

 

What is Dangling pointer?

Ans :- Dangling Pointer is a pointer that doesn’t point to a valid memory location. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory.

Following are examples:- Example 1

int* ptr = (int*)malloc(sizeof(int));

……..free(ptr);

ptr is a dangling pointer now and operations like following are invalid

*ptr = 10; // or printf(“%d”, *ptr);

 

What is memory leak ? Why it should be avoided ?

Ans :- Memory leak occurs when programmers create a memory in heap and forget to delete it. Memory leaks are particularly serious issues for programs like daemons and servers which by definition never terminate.

Function with memory leak

#include <stdlib.h>

void f()

{

int* ptr = (int*)malloc(sizeof(int));

return; /* Return without freeing ptr*/

}

visit GOOPHE for Top Common C Programming Interview Question.

POPULAR POST

Leave a Reply

Your email address will not be published. Required fields are marked *

10 − 6 =