# Comprehensive Answers to 66 Important C Programming Questions (1 to 15)
## **1. What is a dangling pointer?**
A dangling pointer occurs when a pointer still points to the memory location of an
object that has already been deleted or deallocated. Accessing such memory can lead
to undefined behavior. Example:
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int));
*ptr = 10;
free(ptr); // Deallocating memory
// ptr is now a dangling pointer
ptr = NULL; // Best practice: Assign NULL to avoid dangling pointer
return 0;
}
```
---
## **2. Define a static variable in C.**
A static variable retains its value across function calls and is initialized only
once. It has a local scope but a lifetime throughout the program execution.
Example:
```c
#include <stdio.h>
void demo() {
static int count = 0; // Static variable
count++;
printf("Count: %d\n", count);
}
int main() {
demo();
demo();
demo();
return 0;
}
```
Output:
```
Count: 1
Count: 2
Count: 3
```
---
## **3. What is the role of the return keyword in a function?**
The `return` keyword exits a function and optionally sends a value back to the
caller. It also terminates the function's execution.
Example:
```c
#include <stdio.h>
int add(int a, int b) {
return a + b; // Returning the sum
}
int main() {
int result = add(5, 7);
printf("Result: %d\n", result);
return 0;
}
```
---
## **4. What does the term 'scope' of a variable mean?**
The scope of a variable refers to the region in a program where the variable can be
accessed. Types of scope:
1. **Local Scope:** Variable is accessible only within a function/block.
2. **Global Scope:** Variable is accessible throughout the program.
3. **Block Scope:** Variable declared inside `{}` is accessible only within those
braces.
---
## **5. What is recursion?**
Recursion is a technique where a function calls itself to solve smaller instances
of the problem. Every recursion must have a base condition to terminate.
Example:
```c
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1; // Base case
return n * factorial(n - 1); // Recursive case
}
int main() {
printf("Factorial of 5: %d\n", factorial(5));
return 0;
}
```
---
## **6. What is the significance of the exit() function in C?**
The `exit()` function terminates a program immediately, bypassing the remaining
code.
```c
#include <stdlib.h>
#include <stdio.h>
int main() {
printf("This will print\n");
exit(0); // Program terminates here
printf("This won't print\n");
return 0;
}
```
---
## **7. What is an infinite loop?**
An infinite loop is a loop that runs endlessly unless explicitly broken. Common
examples:
```c
// Infinite loop using while
while (1) {
printf("Infinite\n");
}
```
---
## **8. Explain the purpose of the continue statement.**
The `continue` statement skips the current iteration of a loop and moves to the
next iteration.
Example:
```c
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
if (i == 2) continue; // Skips when i == 2
printf("%d\n", i);
}
return 0;
}
```
Output:
```
0
1
3
4
```
---
## **9. What is the role of fclose() in file handling in C?**
The `fclose()` function closes an open file and releases any resources associated
with it.
Example:
```c
FILE *file = fopen("example.txt", "r");
// Perform file operations
fclose(file); // Close the file
```
---
## **10. Define a macro in C.**
A macro is a preprocessor directive used for defining constants or functions.
Example:
```c
#define PI 3.14159
#define AREA(r) (PI * (r) * (r))
int main() {
printf("Area: %.2f\n", AREA(5));
return 0;
}
```
---
## **11. What is the purpose of the atoi() function in C?**
The `atoi()` function converts a string into an integer. It is defined in the
`stdlib.h` library.
Example:
```c
#include <stdlib.h>
#include <stdio.h>
int main() {
char str[] = "1234";
int num = atoi(str);
printf("The integer value is: %d\n", num);
return 0;
}
```
---
## **12. How can you initialize an array in C?**
An array in C can be initialized at the time of declaration. For example:
```c
int arr[5] = {1, 2, 3, 4, 5}; // Explicit initialization
int arr[5] = {0}; // All elements initialized to 0
```
---
## **13. What is the difference between malloc() and calloc()?**
1. **malloc()**: Allocates memory without initializing it.
2. **calloc()**: Allocates and initializes memory to zero.
Example:
```c
#include <stdlib.h>
#include <stdio.h>
int main() {
int *ptr1 = (int *)malloc(5 * sizeof(int));
int *ptr2 = (int *)calloc(5, sizeof(int));
free(ptr1);
free(ptr2);
return 0;
}
```
---
## **14. Write the syntax for a for loop in C.**
Syntax:
```c
for (initialization; condition; increment/decrement) {
// Loop body
}
```
Example:
```c
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
return 0;
}
```
---
## **15. How is a structure different from an array in C?**
1. **Array**: Stores multiple values of the same type.
2. **Structure**: Stores multiple values of different types.
Example:
```c
#include <stdio.h>
struct Student {
int id;
char name[50];
};
int main() {
struct Student student1 = {1, "Alice"};
printf("ID: %d, Name: %s\n", student1.id, student1.name);
return 0;
}
```
---