C Interview Questions – Set 03

What is a pointer in C?

A pointer is a variable that refers to the address of a value. It makes the code optimized and makes the performance fast. Whenever a variable is declared inside a program, then the system allocates some memory to a variable. The memory contains some address number. The variables that hold this address number is known as the pointer variable.

What is the newline escape sequence?

The new line escape sequence is represented by “n”. It inserts a new line on the output screen.

What is static memory allocation?

  • In case of static memory allocation, memory is allocated at compile time, and memory can’t be increased while executing the program. It is used in the array.
  • The lifetime of a variable in static memory is the lifetime of a program.
  • The static memory is allocated using static keyword.
  • The static memory is implemented using stacks or heap.
  • The pointer is required to access the variable present in the static memory.
  • The static memory is faster than dynamic memory.
  • In static memory, more memory space is required to store the variable.

Can we access the array using a pointer in C language?

Yes, by holding the base address of array into a pointer, we can access the array using a pointer.

Write a program to check prime number in C Programming?

#include
#include
void main()
{
int n,i,m=0,flag=0; //declaration of variables.
clrscr(); //It clears the screen.
printf(“Enter the number to check prime:”);
scanf(“%d”,&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf(“Number is not prime”);
flag=1;
break; //break keyword used to terminate from the loop.
}
}
if(flag==0)
printf(“Number is prime”);
getch(); //It reads a character from the keyword.
}

Can we compile a program without main() function?

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

What is the usage of the pointer in C?

Accessing array elements: Pointers are used in traversing through an array of integers and strings. The string is an array of characters which is terminated by a null character ‘’.
Dynamic memory allocation: Pointers are used in allocation and deallocation of memory during the execution of a program.
Call by Reference: The pointers are used to pass a reference of a variable to other function.
Data Structures like a tree, graph, linked list, etc.: The pointers are used to construct different data structures like tree, graph, linked list, etc.

What is the use of printf() and scanf() functions?

printf(): The printf() function is used to print the integer, character, float and string values on to the screen.

Following are the format specifier:

  • %d: It is a format specifier used to print an integer value.
  • %s: It is a format specifier used to print a string.
  • %c: It is a format specifier used to display a character value.
  • %f: It is a format specifier used to display a floating point value.
    scanf(): The scanf() function is used to take input from the user.

Who is the main contributor in designing the C language after Dennis Ritchie?

Brain Kernighan.

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.