C Interview Questions and Answers for 2 years experience
-
What is the difference between `int`, `float`, and `double` data types?
- Answer: `int` stores integers (whole numbers), `float` stores single-precision floating-point numbers (numbers with decimal points), and `double` stores double-precision floating-point numbers (numbers with decimal points, higher precision than `float`). `double` offers more significant digits and a wider range than `float` but requires twice the memory.
-
Explain the concept of pointers in C.
- Answer: Pointers are variables that store the memory address of other variables. They are declared using the `*` operator. Pointers allow for dynamic memory allocation, efficient data manipulation, and passing data to functions without copying large amounts of data.
-
What is the purpose of the `void` keyword?
- Answer: `void` indicates the absence of a type. It's used in function declarations to specify that a function does not return any value, and in pointer declarations to indicate that a pointer can point to any data type (though this is less common and often less safe than type-specific pointers).
-
How do you declare and initialize an array in C?
- Answer: Arrays are declared using the data type, array name, and size within square brackets. For example: `int numbers[5] = {1, 2, 3, 4, 5};` This declares an integer array named `numbers` with 5 elements and initializes them.
-
Explain the difference between `malloc` and `calloc` in dynamic memory allocation.
- Answer: Both `malloc` and `calloc` allocate memory dynamically. `malloc` allocates a single block of memory of a specified size and does not initialize it. `calloc` allocates multiple blocks of memory, each of a specified size, and initializes all bytes to zero.
-
What is a function prototype? Why is it important?
- Answer: A function prototype declares the function's return type, name, and parameters before its definition. It's crucial for the compiler to understand how to use the function before encountering its definition, allowing for better error detection and code organization.
-
What are header files and why are they used?
- Answer: Header files (`.h`) contain function declarations, macro definitions, and other preprocessor directives. They are included in source code using `#include` to provide access to external functions and definitions, promoting modularity and code reusability.
-
Explain the difference between `static` and `auto` variables.
- Answer: `auto` variables (the default) have local scope and are created when the function is entered and destroyed when it exits. `static` variables retain their value between function calls and have static storage duration.
-
What is a structure in C? Give an example.
- Answer: A structure is a user-defined data type that groups variables of different data types under a single name. Example: `struct Person { char name[50]; int age;};`
-
How do you pass arrays to functions in C?
- Answer: Arrays are passed to functions by reference (passing the address of the first element). Changes made to the array within the function will affect the original array.
-
Explain the concept of recursion in C.
- Answer: Recursion is a programming technique where a function calls itself. It's used to solve problems that can be broken down into smaller, self-similar subproblems. A recursive function must have a base case to stop the recursion.
-
What are preprocessor directives in C? Give some examples.
- Answer: Preprocessor directives are instructions that are processed before the actual compilation. Examples include `#include`, `#define`, `#ifdef`, `#ifndef`, etc. They are used for macro definition, conditional compilation, and including header files.
-
What is the difference between a `while` loop and a `do-while` loop?
- Answer: A `while` loop checks the condition before executing the loop body. A `do-while` loop executes the loop body at least once and then checks the condition.
-
Explain the use of `break` and `continue` statements in loops.
- Answer: `break` terminates the loop completely. `continue` skips the current iteration and proceeds to the next iteration.
Thank you for reading our blog post on 'C Interview Questions and Answers for 2 years experience'.We hope you found it informative and useful.Stay tuned for more insightful content!