Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Which is better #define or enum?

Answer»
  • If we let it, the COMPILER can build enum values AUTOMATICALLY. However, each of the defined values must be given separately.
  • Because macros are preprocessors, UNLIKE enums, which are compile-time entities, the source code is unaware of these macros. So, if we use a debugger to debug the code, the enum is superior.
  • Some compilers will GIVE a warning if we use enum values in a switch and the default CASE is missing.
  • Enum always generates int-type identifiers. The macro, on the other hand, allowed us to pick between various integral types.
  • Unlike enum, the macro does not have a defined scope constraint.
2.

Suppose a global variable and local variable have the same name. Is it possible to access a global variable from a block where local variables are defined?

Answer»

No. This isn’t possible in C. It’s ALWAYS the most local VARIABLE that GETS preference.

3.

Which structure is used to link the program and the operating system?

Answer»

The file structure is used to link the operating system and a PROGRAM. The header file "stdio.h" (STANDARD input/output header file) defines the file. It contains information about the file being used LIKE its CURRENT size and its memory location. It contains a CHARACTER pointer that points to the character which is currently being opened. When you open a file, it establishes a link between the program and the operating system about which file is to be accessed.

4.

What is a near pointer and a far pointer in C?

Answer»
  • Near Pointer: In general, the near pointer can be considered because it is used to hold the address, which has a maximum SIZE of just 16 bits. We can't store an address with a size larger than 16 bits using the near pointer. All other smaller addresses that are within the 16-bit LIMIT, on the other hand, can be stored. Because we can only access 64kb of DATA at a time, you might assume the 16 bits are insufficient. As a result, it is regarded as one of the near-pointer's biggest drawbacks, which is why it is no longer commonly used.
  • Far Pointer: A far pointer is considered a pointer of size 32 bits. It can, however, use the current segment to access information stored outside the computer's MEMORY. Although, in order to use this type of pointer, we usually NEED to allocate the sector register to store the data address in the current segment.
5.

What is the difference between ‘g’ and “g” in C?

Answer»

In C double-quotes variables are identified as a STRING WHEREAS single-quoted variables are identified as the CHARACTER. Another MAJOR difference being the string (double-quoted) variables end with a null terminator that makes it a 2 character array.

6.

What are dangling pointers? How are dangling pointers different from memory leaks?

Answer»

The dangling pointer points to a memory that has already been freed. The storage is no LONGER allocated. Trying to access it might cause a Segmentation fault. A common way to end up with a dangling pointer:

#include<stdio.h>#include<string.h>char *func(){ char STR[10]; STRCPY(str,"Hello!"); return(str);}

You are returning an address that was a local variable, which would have gone out of scope by the time control was returned to the calling function. (Undefined behavior)

*c = malloc(5izeof(INT));free(c);*c = 3; //writing to freed location!

In the figure shown above writing to a memory that has been freed is an example of the dangling pointer, which makes the program crash.

A memory leak is something where the memory allocated is not freed which causes the program to use an undefined amount of memory from the ram making it unavailable for every other running program(or daemon) which causes the programs to crash. There are various tools like O profile testing which is useful to detect memory leaks on your programs.

void function(){ char *leak = malloc (10); //leak assigned but not freed}
7.

What is the difference between #include "..." and #include ?

Answer»

In practice, the difference is in the location where the preprocessor searches for the included file.

For #include <filename> the C preprocessor LOOKS for the filename in the predefined list of system directories first and then to the directories told by the user(we can USE -I option to ADD directories to the mentioned predefined list). 

For #include "filename" the preprocessor searches first in the same DIRECTORY as the file CONTAINING the directive, and then follows the search path used for the #include <filename> form. This method is normally used to include programmer-defined header files.

8.

Why is it usually a bad idea to use gets()? Suggest a workaround.

Answer»

The standard INPUT library gets() reads USER input till it encounters a new line character. However, it does not check on the size of the variable being provided by the user is under the maximum size of the DATA type which makes the system vulnerable to buffer overflow and the input being WRITTEN into memory where it isn’t supposed to.

We, THEREFORE, use gets() to achieve the same with a restricted range of input

Bonus: It remained an official part of the language up to the 1999 ISO C standard, but it was officially removed by the 2011 standard. Most C implementations still support it, but at least GCC issues a warning for any code that uses it.

9.

What is typedef?

Answer»

typedef is a C keyword, used to define ALIAS/synonyms for an existing type in C language. In most cases, we use typedef's to simplify the existing type declaration syntax. Or to provide specific DESCRIPTIVE names to a type.

typedef <existing-type> <new-type-identifiers>;

typedef provides an alias name to the existing complex type definition. With typedef, you can simply create an alias for any type. WHETHER it is a simple INTEGER to complex function pointer or structure declaration, typedef will SHORTEN your code.

10.

What is Dynamic memory allocation in C? Name the dynamic allocation functions.

Answer»

C is a language known for its low-level control over the memory allocation of variables in DMA there are two major standard library malloc() and FREE. The malloc() function takes a single INPUT parameter which tells the size of the memory requested It returns a pointer to the allocated memory. If the allocation fails, it returns NULL. The prototype for the standard library function is like this:

void *malloc(size_t size);
The free() function takes the pointer returned by malloc() and de-allocates the memory. No indication of success or failure is returned. The function prototype is like this: 

void free(void *pointer);
There are 4 library functions provided by C defined under <stdlib.h> header file to facilitate DYNAMIC memory allocation in C programming. They are:

11.

What is a memory leak? How to avoid it?

Answer»

When we assign a variable it takes space of our RAM (either heap or RAM)dependent on the size of data type, however, if a PROGRAMMER uses a memory available on the heap and FORGETS to a delta it, at some point all the memory available on the ram will be occupied with no memory left this can lead to a memory leak.

int main(){ char * PTR = malloc(sizeof(int)); /* Do some work */ /*Not freeing the allocated memory*/ RETURN 0;}

To avoid memory leaks, you can trace all your memory allocations and think forward, where you WANT to destroy (in a good sense) that memory and place delete there. Another way is to use C++ smart pointer in C linking it to GNU compilers.

12.

What is pass by reference in functions?

Answer»

In Pass by reference, the CALLEE receives the address and makes a copy of the address of an ARGUMENT into the formal PARAMETER. Callee FUNCTION uses the address to access the actual argument (to do some manipulation). If the callee function changes the value ADDRESSED at the passed address it will be visible to the caller function as well.

Pass By Reference
13.

What is call by reference in functions?

Answer»

When we CALLER FUNCTION makes a function call bypassing the addresses of actual parameters being passed, then this is called call by reference. In incall by reference, the operation PERFORMED on formal parameters affects the value of actual parameters because all the operations performed on the value STORED in the ADDRESS of actual parameters.

14.

What is the difference between struct and union in C?

Answer»

A struct is a group of complex data structures stored in a block of memory where each MEMBER on the block gets a separate memory location to make them accessible at once

Whereas in the union, all the member variables are stored at the same location on the memory as a result to which while assigning a value to a member variable will CHANGE the value of all other members.

/* struct & union definations*/struct bar { int a; // we can use a & B both SIMULTANEOUSLY char b;} bar;union foo { int a; // we can't use both a and b simultaneously char b;} foo;/* using STRUC and union variables*/struct bar y;y.a = 3; // OK to usey.b = 'c'; // OK to useunion foo x;x.a = 3; // OKx.b = 'c'; // NOl this affects the value of x.a!
15.

What is the difference between malloc() and calloc()?

Answer»

calloc() and malloc() are MEMORY dynamic memory ALLOCATING functions. The main difference is that malloc() only takes one argument, which is the number of BYTES, but calloc() takes TWO arguments, which are the number of blocks and the size of each block.

16.

What is an r-value and l-value?

Answer»
  • The term "r-value" refers to a data value stored in memory at a given address. An r-value is an EXPRESSION that cannot have a value assigned to it, hence it can only exist on the right SIDE of an assignment operator(=).
  • The term "l-value" refers to a memory location that is used to IDENTIFY an object. The l-value can be found on EITHER the LEFT or right side of an assignment operator(=). l-value is frequently used as an identifier.
17.

Specify different types of decision control statements?

Answer»

All statements WRITTEN in a PROGRAM are EXECUTED from top to bottom one by one. Control statements are used to execute/transfer the control from one part of the program to another depending on the condition.

  • If-else STATEMENT.
    • normal if-else statement.
    • Else-if statement
    • nested if-else statement.
  • Switch statement.