0% found this document useful (0 votes)
16 views8 pages

C Programming Q&A Guide

This document provides a comprehensive set of questions and answers related to C programming, covering topics from basic concepts to advanced features. It includes explanations of data types, operators, control statements, functions, pointers, memory management, structures, file handling, and preprocessor directives. Additionally, it presents coding questions with example solutions, such as calculating factorials and checking for palindromes.

Uploaded by

kanimagnus616
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

C Programming Q&A Guide

This document provides a comprehensive set of questions and answers related to C programming, covering topics from basic concepts to advanced features. It includes explanations of data types, operators, control statements, functions, pointers, memory management, structures, file handling, and preprocessor directives. Additionally, it presents coding questions with example solutions, such as calculating factorials and checking for palindromes.

Uploaded by

kanimagnus616
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Programming - Questions and Answers

# 1. Basic C Programming Questions & Answers

## Introduction to C
1. What is C programming?
- C is a general-purpose, structured programming language developed by

2. Why is C called a middle-level language?


- C combines features of both high-level and low-level languages, allowin

3. What are the key features of C?


- Simple syntax, portability, efficiency, modularity, rich library functions,

4. What is the structure of a C program?


```c
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
```

## Data Types & Variables


5. What are the basic data types in C?
- `int`, `float`, `double`, `char`, `void`
6. What is the difference between `int`, `float`, `double`, and `char`?
- `int` -> Stores integers (4 bytes)
- `float` -> Stores decimal numbers (4 bytes)
- `double` -> Stores large decimal numbers (8 bytes)
- `char` -> Stores a single character (1 byte)

7. What is the purpose of `sizeof()` in C?


- It returns the memory size (in bytes) of a data type or variable.
```c
printf("%lu", sizeof(int)); // Output: 4
```

## Operators & Expressions


8. What is the difference between `=` and `==`?
- `=` is an assignment operator (`a = 5;`).
- `==` is a comparison operator (`if (a == 5)`).

9. What does the `++` operator do?


- Increments a variable by 1.
```c
int x = 5;
x++; // x becomes 6
```

10. What is a ternary operator?


- A shorthand for `if-else`:
```c
int a = 10, b = 20;
int max = (a > b) ? a : b; // max = 20
```

## Control Statements
11. How does an `if` statement work?
```c
if (a > b) {
printf("a is greater");
} else {
printf("b is greater");
}
```

12. What is the difference between `while` and `do-while` loops?


- `while` -> Checks condition before execution.
- `do-while` -> Executes at least once, then checks condition.

```c
int i = 1;
do {
printf("%d", i);
i++;
} while (i <= 5);
```
# 2. Intermediate C Programming Questions & Answers

## Functions
13. What is the difference between `call by value` and `call by reference`?
- **Call by value**: Passes a copy of the variable, changes don't reflect in
- **Call by reference**: Passes the memory address, changes reflect in th

```c
void modify(int *x) { *x = 10; }
```

## Arrays & Strings


14. How do you declare a 2D array?
```c
int matrix[3][3]; // 3x3 matrix
```

15. How to reverse a string in C?


```c
#include <string.h>
void reverse(char str[]) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
```

# 3. Advanced C Programming Questions & Answers

## Pointers
16. What is a pointer?
- A pointer is a variable that stores the memory address of another variab

```c
int x = 10;
int *ptr = &x; // ptr stores address of x
```

17. How do you swap two numbers using pointers?


```c
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
```

## Memory Management
18. What is the difference between `malloc()` and `calloc()`?
- `malloc(size)` -> Allocates uninitialized memory.
- `calloc(n, size)` -> Allocates zero-initialized memory.
19. How do you free allocated memory?
```c
free(ptr); // Releases allocated memory
```

## Structures & Unions


20. What is a structure in C?
```c
struct Person {
char name[20];
int age;
};
```

21. What is the difference between structure and union?


- **Structure** -> Stores all members separately.
- **Union** -> Shares memory for all members.

## File Handling
22. How do you open and read a file in C?
```c
FILE *fptr = fopen("[Link]", "r");
char ch = fgetc(fptr);
fclose(fptr);
```
## Preprocessor Directives
23. What does `#define` do?
```c
#define PI 3.14
```

24. How do you prevent multiple inclusions of a header file?


```c
#ifndef HEADER_FILE
#define HEADER_FILE
// Code here
#endif
```

## Coding Questions
1. Factorial using recursion
```c
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
```

2. Check if a string is a palindrome


```c
int isPalindrome(char str[]) {
int i = 0, j = strlen(str) - 1;
while (i < j) {
if (str[i] != str[j]) return 0;
i++; j--;
}
return 1;
}
```

Common questions

Powered by AI

Preprocessor directives like `#ifndef HEADER_FILE`, `#define HEADER_FILE`, and `#endif` are used to wrap header file contents. This conditional inclusion prevents multiple inclusions by checking if the file has been included already, thereby avoiding redefinition errors and reducing compilation time . This practice is crucial for maintaining clean and error-free code, particularly in complex projects.

Structures allocate memory for all members separately, meaning the total memory is the sum of all members, allowing simultaneous access to each. Unions allocate memory for the largest member, sharing this space among all members, meaning only one can be used at a time . This allows structures to store different data types together efficiently, whereas unions are efficient for saving memory when storing one of many types at a time.

`malloc(size)` allocates uninitialized memory of specified size while `calloc(n, size)` allocates zero-initialized memory for an array of `n` elements, each of `size` bytes .

Pointers enhance functionality by allowing direct manipulation of memory addresses. For example, pointers can be used to iterate over an array: `int *ptr = &arr[0];` allows pointer arithmetic for traversal. In memory management, pointers are crucial for dynamic allocation, enabling functions like `malloc()` and `free()` to efficiently manage memory . This allows for dynamic data structures like linked lists.

C is termed a middle-level language because it incorporates features from both high-level and low-level languages. This means it supports high-level operations, providing abstractions like structured programming, while allowing manipulation of hardware-level processes, such as direct memory access . This classification implies that C is versatile, being suitable for system programming, like operating system development, and application programming.

In "call by value", a copy of the actual parameter's value is passed, so modifications inside the function do not affect the actual parameter. In "call by reference", the function receives an address and modifications affect the original. For instance: `void modify(int *x) { *x = 10; }` demonstrates call by reference, allowing changes to the original variable . The choice depends on whether modification to the original is desired.

The `sizeof()` operator measures the byte size of data types or variables, aiding in memory allocation decisions and optimizing data structure layouts for efficient use of memory. For instance, `printf("%lu", sizeof(int));` returns 4 on most systems, informing program design in cross-platform applications . It's crucial for dynamic memory allocation to prevent buffer overflow and optimize data storage, as in cache line optimization.

Understanding bit-level operations is essential for tasks demanding performance and low-level system programming, as they allow direct manipulation of hardware and memory-efficient data processing. Common uses include implementing data compression, cryptographic algorithms, and systems involving hardware interfacing . Bit manipulation is often used to set, toggle, or clear specific bits in flags and masks, optimizing space in resource-constrained environments.

Control structures enable conditional execution and iteration, crucial for implementing efficient algorithms. An `if` statement, like `if (a > b)`, directs execution flow without duplicating code, enhancing maintainability . Loops, e.g., `while (i <= 5)`, allow repeated execution, reducing redundancy and optimizing performance by iteratively processing sequences like arrays or buffers, fundamental for operations like searching and sorting.

A `do-while` loop executes the loop body at least once before checking the condition. For example: `int i = 1; do { printf("%d", i); i++; } while (i <= 5);` prints numbers 1 to 5 regardless of the initial value of `i` . In contrast, a `while` loop checks the condition before any execution; if `i` was greater than 5 initially, the loop would never execute.

You might also like