Home / Interview / C :: General Questions

Interview :: C

21) What is dynamic memory allocation?
  • In case of dynamic memory allocation, memory is allocated at runtime and memory can be increased while executing the program. It is used in the linked list.
  • The malloc() or calloc() function is required to allocate the memory at the runtime.
  • An allocation or deallocation of memory is done at the execution time of a program.
  • No dynamic pointers are required to access the memory.
  • The dynamic memory is implemented using data segments.
  • Less memory space is required to store the variable.

The above example allocates the memory at runtime.

22) What functions are used for dynamic memory allocation in C language?
  1. malloc()
    • The malloc() function is used to allocate the memory during the execution of the program.
    • It does not initialize the memory but carries the garbage value.
    • It returns a null pointer if it could not be able to allocate the requested space.

    Syntax

  2. calloc()
    • The calloc() is same as malloc() function, but the difference only is that it initializes the memory with zero value.

    Syntax

  3. realloc()
    • The realloc() function is used to reallocate the memory to the new size.
    • If sufficient space is not available in the memory, then the new block is allocated to accommodate the existing data.

    Syntax

    In the above syntax, ptr is allocated to a new size.

  4. free():The free() function releases the memory allocated by either calloc() or malloc() function.
  5. Syntax

    The above syntax releases the memory from a pointer variable ptr.

23) What is the difference between malloc() and calloc()?
calloc() malloc()
Description The malloc() function allocates a single block of requested memory. The calloc() function allocates multiple blocks of requested memory.
Initialization It initializes the content of the memory to zero. It does not initialize the content of memory, so it carries the garbage value.
Number of arguments It consists of two arguments. It consists of only one argument.
Return value It returns a pointer pointing to the allocated memory. It returns a pointer pointing to the allocated memory.

24) What is the structure?
  • The structure is a user-defined data type that allows storing multiple types of data in a single unit. It occupies the sum of the memory of all members.
  • The structure members can be accessed only through structure variables.
  • Structure variables accessing the same structure but the memory allocated for each variable will be different.

Syntax of structure

Let's see a simple example.

Output:

 Enter the name shikha Enter the age 26 Name and age of a student: shikha,26   

25) What is a union?
  • The union is a user-defined data type that allows storing multiple types of data in a single unit. However, it doesn't occupy the sum of the memory of all members. It holds the memory of the largest member only.
  • In union, we can access only one variable at a time as it allocates one common space for all the members of a union.

Syntax of union

Let's see a simple example

Output:

 value of a is 1085485921 value of b is 5.600022 value of ch is a 

In the above example, the value of a and b gets corrupted, and only variable ch shows the actual output. This is because all the members of a union share the common memory space. Hence, the variable ch whose value is currently updated.

26) What is an auto keyword in C?

In C, every local variable of a function is known as an automatic (auto) variable. Variables which are declared inside the function block are known as a local variable. The local variables are also known as an auto variable. It is optional to use an auto keyword before the data type of a variable. If no value is stored in the local variable, then it consists of a garbage value.

27) What is the purpose of sprintf() function?

The sprintf() stands for "string print." The sprintf() function does not print the output on the console screen. It transfers the data to the buffer. It returns the total number of characters present in the string.

Syntax

Let's see a simple example

Output:

 value of n is 9 
28) Can we compile a program without main() function?

Yes, we can compile, but it can't be executed.

But, if we use #define, we can compile and run a C program without using the main() function. For example:

29) What is a token?

The Token is an identifier. It can be constant, keyword, string literal, etc. A token is the smallest individual unit in a program. C has the following tokens:

  1. Identifiers: Identifiers refer to the name of the variables.
  2. Keywords: Keywords are the predefined words that are explained by the compiler.
  3. Constants: Constants are the fixed values that cannot be changed during the execution of a program.
  4. Operators: An operator is a symbol that performs the particular operation.
  5. Special characters: All the characters except alphabets and digits are treated as special characters.
30) What is command line argument?

The argument passed to the main() function while executing the program is known as command line argument. For example: