C Questions
C Questions
Limitations of arrays in C
Arrays in C have several limitations:
Fixed size that must be declared at compile time (unless using dynamic allocation)
No bounds checking (can lead to buffer overflows)
Cannot be resized without creating a new array
Cannot return entire arrays from functions (only pointers to them)
No built-in methods to find length or perform operations like sorting
Dangling pointer: A pointer that points to memory that has been freed or is no longer valid.
Near pointer: 16-bit pointer that can access only the current segment (64KB)
Far pointer: 32-bit pointer with 16 bits for segment and 16 bits for offset
Huge pointer: Similar to far pointers but with normalized segment:offset
4. Use of #define in C
#define is a preprocessor directive used to define constants and macros:
// Define a constant
#define PI 3.14159
// Define a macro
#define SQUARE(x) ((x) * (x))
int main() {
float radius = 5.0;
float area = PI * SQUARE(radius);
printf("Area: %f\n", area);
return 0;
}
5. Static function in C
A static function is only visible within the file it's defined in. It can't be called from other
files.
Use double quotes for your own header files and angle brackets for system headers:
7. Nested loops
Nested loops are loops inside other loops. The inner loop completes all its iterations for each
iteration of the outer loop.
Compilation process:
1. Source code (.c) → Compiler → Object file (.o)
2. Object file(s) → Linker → Executable file
int main() {
int size, i, sum = 0;
int *numbers;
// Allocate memory
numbers = (int *)malloc(size * sizeof(int));
if (numbers == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Get values
for (i = 0; i < size; i++) {
printf("Enter number %d: ", i+1);
scanf("%d", &numbers[i]);
sum += numbers[i];
}
// Release memory
free(numbers);
return 0;
}
#include <stdio.h>
int main() {
char ch = 'A';
int ascii = (int)ch; // Explicit conversion (optional)
return 0;
}
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr; // Points to first element
return 0;
}
#include <stdio.h>
#include <string.h>
// Without typedef
struct Person {
char name[50];
int age;
};
// With typedef
typedef struct {
char name[50];
int age;
} Student;
int main() {
// Without typedef
struct Person person1;
strcpy(person1.name, "John");
person1.age = 25;
// With typedef
Student student1;
strcpy(student1.name, "Alice");
student1.age = 20;
return 0;
}
#include <stdio.h>
int main() {
int outer = 10;
return 0;
}
#include <stdio.h>
int main() {
int a = 10;
{
int b = 20;
printf("Inner block can access a: %d\n", a);
{
int c = 30;
printf("Innermost block can access a: %d, b: %d\n", a, b);
}
return 0;
}
16. When is the return statement mandatory in a
function?
The return statement is mandatory in functions with non-void return types:
#include <stdio.h>
int main() {
int num = 42;
void *vptr = #
return 0;
}
#include <stdio.h>
int main() {
int value = 42;
int *ptr = &value; // Pointer to value
int **ptrToPtr = &ptr; // Pointer to pointer
return 0;
}
#include <stdio.h>
int main() {
int value = 42;
int *ptr1 = &value; // Level 1
int **ptr2 = &ptr1; // Level 2
int ***ptr3 = &ptr2; // Level 3
int ****ptr4 = &ptr3; // Level 4
return 0;
}
This verifies that pointers can be extended to any level, limited only by practical
considerations.