1. What is C Language?
Answer: C is a high-level, general-purpose programming language developed by Dennis Ritchie in the early 1970s. It is known for its efficiency and flexibility, making it widely used in system programming and embedded systems.
2. What are the key features of C?
Answer:
Structured Language: Supports structured programming paradigms.
Low-level Access: Provides low-level access to memory through pointers.
Efficiency: Produces efficient machine code.
Rich Library: Includes a standard library with built-in functions.
3. What is the difference between printf() and scanf()?
Answer: printf() is used to output formatted data to the console, while scanf() is used to read formatted input from the user.
4. Explain the difference between malloc() and calloc().
Answer:
malloc(size_t size)
allocates a specified number of bytes but does not initialize them.
calloc(size_t num, size_t size)
allocates memory for an array of elements and initializes all bytes to zero.
5. What are pointers in C?
Answer: Pointers are variables that store memory addresses of other variables. They are used for dynamic memory allocation, arrays, and function arguments.
6. How do you declare a pointer variable?
Answer: A pointer variable is declared by placing an asterisk (*) before its name, indicating that it will hold the address of another variable type:
int *ptr; // Pointer to an integer
7. What is a function pointer?
Answer: A function pointer is a variable that stores the address of a function. It allows for dynamic function calls and can be used for callback functions:
void (*funcPtr)(int); // Pointer to a function taking an int argument
8. What is recursion in C?
Answer: Recursion occurs when a function calls itself directly or indirectly to solve a problem. It typically has a base case to terminate the recursive calls.
9. Explain the difference between local and global variables.
Answer:
Local Variables: Declared within a function and accessible only within that function's scope.
Global Variables: Declared outside all functions and accessible from any function within the same file or other files if declared as extern.
10. What is a structure in C?
Answer: A structure is a user-defined data type that groups related variables of different types under one name. It allows for creating complex data types:
struct Student {
char name[50];
int age;
};
11. How do you read from and write to files in C?
Answer:
To read from a file:
FILE *file = fopen("file.txt", "r");
char buffer[100];
fgets(buffer, sizeof(buffer), file);
fclose(file);
To write to a file:
FILE *file = fopen("file.txt", "w");
fprintf(file, "Hello World");
fclose(file);
12. What are the different storage classes in C?
Answer:
Automatic (auto): Default storage class for local variables.
Register: Suggests storing variable in CPU registers for faster access.
Static: Preserves variable value between function calls.
External (extern): Indicates that the variable is defined in another file.
13. What is the purpose of the volatile keyword?
Answer: The volatile keyword indicates that a variable's value may be changed by external factors (like hardware or interrupts) and prevents the compiler from optimizing it out.
14. How do you swap two numbers without using a third variable?
Answer:
a = a + b;
b = a - b;
a = a - b;
15. What is segmentation fault?
Answer: A segmentation fault occurs when a program tries to access memory that it's not allowed to access, often due to dereferencing null or uninitialized pointers.
16. Explain how dynamic memory allocation works in C.
Answer: Dynamic memory allocation allows allocating memory at runtime using functions like malloc(), calloc(), or realloc(), which can be freed later using free().
17. What is the difference between strcpy() and strncpy()?
Answer:
strcpy(dest, src) copies the entire string from source to destination until it encounters a null character.
strncpy(dest, src, n) copies up to n characters from source to destination but does not null-terminate if the length of source exceeds n.
18. How do you check if a number is prime in C?
Answer:
int isPrime(int num) {
if (num <= 1) return 0;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) return 0;
}
return 1;
}
19. What are macros in C?
Answer: Macros are preprocessor directives defined using #define that allow code substitution before compilation:
#define PI 3.14
20. Explain what an array is in C.
Answer: An array is a collection of elements of the same type stored in contiguous memory locations, allowing easy access via indexing:
int arr[10]; // Array of integers with size 10
Comments