0% found this document useful (0 votes)
14 views7 pages

C_Language_QnA (1)

Uploaded by

bgmi6044
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)
14 views7 pages

C_Language_QnA (1)

Uploaded by

bgmi6044
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
You are on page 1/ 7

Short Answer Questions:

1. What is Preprocessor?

A preprocessor in C is a tool that processes the source code before it is compiled. It handles directives like

2. Define Structure. How do you access structure members/fields?

A structure in C is a user-defined data type that groups variables of different types under one name.

Syntax:

struct StructureName {

data_type member1;

data_type member2;

};

Accessing Members:

Use the dot operator (.) for direct access or the arrow operator (->) if accessing via a pointer.

struct StructureName obj;

obj.member1 = value; // Direct access

3. What is a file? What are the various modes of files available in C Language?

A file is a storage medium used to store data permanently.

Modes:

- r (read)

- w (write)

- a (append)
- rb, wb, ab (binary modes)

- r+, w+, a+ (read and write).

4. Give an example for pointers to pointer.

int x = 10;

int *p = &x;

int **pp = &p;

5. What are the applications of an array?

Arrays are used for:

- Storing and manipulating collections of data.

- Implementing data structures like matrices, stacks, and queues.

- Sorting and searching algorithms.

6. Discuss the different modes available for opening a file.

- r: Open for reading.

- w: Open for writing (creates/overwrites).

- a: Open for appending.

- r+, w+, a+: Read and write combinations.

7. List any three preprocessor directives in C and explain their purpose.

- #include: Includes external libraries or header files.

- #define: Defines macros or constants.

- #ifdef: Conditional compilation.

8. Differentiate between L-value and R-value of pointers with an example.


L-value: Refers to a memory location (modifiable).

R-value: Refers to the value stored at a memory location.

Example:

int x = 10;

int *ptr = &x; // ptr is L-value, *ptr is R-value.

9. What is a self-referential structure in C? Provide an example.

A structure that includes a pointer to itself.

Example:

struct Node {

int data;

struct Node *next;

};

10. Define an array. Write its applications.

An array is a collection of elements of the same data type stored at contiguous memory locations.

Applications:

- Data storage for large datasets.

- Matrix operations.

- Sorting and searching.

11. Which one takes less memory, structure or union? And why?

A union takes less memory because it allocates memory equal to the size of its largest member, while a str

12. Why is it necessary to give the size of an array in an array declaration?


To allocate sufficient memory for the array elements at compile time.

13. Define a string.

A string is an array of characters terminated by a null character (\0).

Long Answer Questions:

1. How do you declare a 3D array? Explain with an example.

Syntax:

data_type array_name[size1][size2][size3];

Example:

int arr[2][3][4];

2. Write a C program to sort elements using Bubble Sort Technique.

#include<stdio.h>

void bubbleSort(int arr[], int n) {

for (int i = 0; i < n - 1; i++) {

for (int j = 0; j < n - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}
}

int main() {

int arr[] = {64, 34, 25, 12, 22};

int n = sizeof(arr)/sizeof(arr[0]);

bubbleSort(arr, n);

for (int i = 0; i < n; i++)

printf("%d ", arr[i]);

return 0;

3. Write a C Program to copy a string into another without String function.

#include<stdio.h>

void stringCopy(char *source, char *destination) {

while (*source) {

*destination = *source;

source++;

destination++;

*destination = '\0';

int main() {

char source[] = "Hello";

char destination[20];

stringCopy(source, destination);
printf("Copied String: %s", destination);

return 0;

4. Explain the various Dynamic memory allocation functions.

- malloc: Allocates memory of specified size and returns a void pointer.

- calloc: Allocates memory for an array and initializes it to zero.

- realloc: Resizes previously allocated memory.

- free: Frees allocated memory.

5. How do we define and declare a Structure?

Definition:

struct StructureName {

data_type member1;

data_type member2;

};

Declaration:

struct StructureName obj;

6. Write a C program to merge two files into a third file.

#include<stdio.h>

int main() {

FILE *file1, *file2, *file3;


char ch;

file1 = fopen("file1.txt", "r");

file2 = fopen("file2.txt", "r");

file3 = fopen("file3.txt", "w");

while ((ch = fgetc(file1)) != EOF)

fputc(ch, file3);

while ((ch = fgetc(file2)) != EOF)

fputc(ch, file3);

fclose(file1);

fclose(file2);

fclose(file3);

return 0;

You might also like