C Interview Questions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

1.What is a header file ?

A header file is a file with extension ​.h​ which contains C function declarations and macro
definitions. You request to use a header file in your program by including it with the C
preprocessing directive ​#include​, like you have seen inclusion of ​stdio.h​ header file, which
comes along with your compiler.

2.Difference between structure and union.

3. Write a factorial program using recursion.

#include <stdio.h>

long int fact(int n);

int main()

int n;

scanf("%d", &n);

printf(" %ld", fact(n));

return 0;

long int fact(int n)

if (n >= 1)

return n*multiplyNumbers(n-1);

else

return 1;
}

4.​What is void in C?

‘void’ is used as a function return type, it indicates that the function does not return a
value.

5.What is void pointer ?


Void pointer is a specific pointer type – void * – a pointer that points to some data location
in storage, which doesn’t have any specific type. Void refers to the type.

Basically the type of data that it points to is can be any. If we assign address of char data
type to void pointer it will become char Pointer, if int data type then int pointer and so on.
Any pointer type is convertible to a void pointer hence it can point to any value.

#include<stdio.h>

int main() {

int a = 7;

float b = 7.6;

void *p;

p = &a;

printf("Integer variable is = %d", *( (int*) p) );

p = &b;

printf("\nFloat variable is = %f", *( (float*) p) );

return 0;

6. What is Dangling pointer?


Dangling pointers arise when an object is deleted or de-allocated, without modifying the
value of the pointer, so that the pointer still points to the memory location of the
de-allocated memory.(In short pointer pointing to non-existing memory location is called
dangling pointer.)

7. Different types of operators in C

​ n operator is a symbol that tells the compiler to perform specific mathematical or


A
logical functions. C language is rich in built-in operators and provides the following types
of operators −

● Arithmetic Operators
● Relational Operators
● Logical Operators
● Bitwise Operators
● Assignment Operators
● Unary Operators
● Tenary or conditional operator(?:)…​Syntax​ : condition ​?​ (true_statement) ​:
(​false_statement​);

8. Difference between & and &&.

‘&’ is bitwise operator used to perform ‘AND’ operation bitwise between two numbers,
whereas ‘&&’ is logical operator used to become true, both of the statements must be
true. If one of them is false, it becomes false.

9. Difference between break and continue.

break​ causes the innermost enclosing loop or switch to be exited immediately where as
the ​continue​ statement is used when we want to skip one or more statements in loop's
body and to transfer the control to the next iteration.

10. What is the difference between exit() and return() in c?

● exit() is a system call which terminates current process, Whereas, return() is a C


language statement and it returns from the current function

11. What is global variable in c?

● The variables which are having scope/life throughout the program are called global
variables.
● Global variable is defined outside the main function. So, this variable is visible to
main function and all other sub functions.

12. What is the difference C and JAVA ?

● C is structure/procedure oriented programming language whereas Java is object


oriented programming language.
● C language program design is top down approach whereas Java is using bottom up
approach.
● Polymorphism, inheritance, encapsulation are not available in C programming
language. Whereas Java supports all these concepts and features.

13. ​What is data type in c?

● Data types in C language are defined as the data storage format that a variable can
store a data to perform a specific operation.
● Data types are used to define a variable before to use in a program.
● Size of variable, constant and array are determined by data types.

14.What is compiler?
● Compiler is a program that converts high level language into machine readable
code. This process is called compilation. Eg. gcc, Microsoft Visual studio

15. What is macro?

● Macro is a name which is given to a value or to a piece of code/block in a program.


Instead of using the value, we can use macro which will replace the value in a
program.

16.​ ​What is file pointer in c?


File pointer is a pointer which is used to handle and keep track on the files being accessed.
A new data type called “FILE” is used to declare file pointer. This data type is defined
in stdio.h file. File pointer is declared as FILE *fp. Where, ‘fp’ is a file pointer.

17. What is Dynamic Memory Allocation ?

An array is a collection of fixed number of values of a single type. That is, you need to
declare the size of an array before you can use it.

Sometimes, the size of array you declared may be insufficient. To solve this issue, you can
allocate memory manually during run-time. This is known as dynamic memory
allocation in C programming.

There are 4 ​library functions​ defined under <stdlib.h> makes dynamic memory allocation
in C programming. They are malloc(), calloc(), realloc() and free().

● malloc() allocates single block of requested memory.


● calloc() allocates multiple block of requested memory.
● realloc() reallocates the memory occupied by malloc() or calloc() functions.
● free() frees the dynamically allocated memory.

18. Difference between malloc() and calloc().


malloc calloc

The name malloc stands for ​memory The name calloc stands for ​contiguous allocation​.
allocation​.

malloc() takes one argument that calloc() take two arguments those are: ​number of
is, ​number of bytes​. blocks​ and ​size of each block​.

syntax of malloc(): syntax of calloc():

void *malloc(size_t n);. void *calloc(size_t n, size_t size);


malloc is faster than calloc. calloc takes little longer than mallocbecause of the
extra step of initializing the allocated memory by
zero. However, in practice the difference in speed is
very tiny and not recognizable.

19. What is the difference between ++a and a++?

‘++a” is called prefixed increment and the increment will happen first on a variable. ‘a++'
is called postfix increment and the increment happens after the value of a variable used for
the operations.

20) Describe the modifier in C?

Modifier is a prefix to the basic data type which is used to indicate the modification for
storage space allocation to a variable.

There are 5 modifiers available in C programming language as follows.

● Short
● Long
● Signed
● Unsigned
● long long

Programs to remember:

● Palindrome
● Swapping of two numbers using functions
● Factorial using recursion
● Armstrong number
● Perfect number
● Print your name in vertical like
Input : ​ TCS
Output:
T

 
C
S

You might also like