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

Unit Iv

Uploaded by

Kore Ramesh
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)
8 views8 pages

Unit Iv

Uploaded by

Kore Ramesh
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/ 8

UNIT IV

1. What is dynamic memory allocation.


Dynamic memory allocation uses functions like malloc() and calloc() to allocate memory
at runtime.

2. Write about conditional compilation.


Conditional compilation compiles code based on conditions using preprocessor
directives like #ifdef, #endif.

3. Define self-referential structure.


A self-referential structure contains a pointer to an instance of the same structure.

4. What is the use of file status functions?


List its examples. These functions check a file's status, e.g., fopen(), fclose().

5. List any two features of pointer.


Pointers store variable addresses and enable dynamic memory allocation.

6. Write about fseek( ) function.


The fseek() function moves the file pointer to a specific location in a file.

7. Define text file.


A text file stores plain text and can be read by text editors.

8. What is the use of calloc( ) function.


calloc( ) allocates memory for an array and initializes it to zero.

9. Define nested macros.


Nested macros are defined within other macros.
UNIT IV
10. List Different Categories of Standard I/O Functions.
Categories of I/O functions include formatted I/O (printf( )), file I/O (fopen( )), and
character I/O (getchar( )).

11. What is Pointer Arithmetic.


Pointer arithmetic involves adding/subtracting to navigate memory locations.

12. What are the various modes of opening a file.


File modes include r, w, a, r+, w+, a+.

LONG

13. What is a Pointer? List Some of the Features and Uses of Pointers.

Definition:
A pointer is a variable that stores the memory address of another variable.

Features of Pointers:

1. Efficiency: Allows direct access to memory for faster computation.


2. Dynamic Memory Allocation: Enables runtime allocation of memory.

3. Function Arguments: Facilitates pass-by-reference for modifying values in a


called function.
4. Data Structures: Used in dynamic data structures like linked lists, trees, and
graphs.
5. Access to Hardware: Allows interaction with low-level hardware.

Uses of Pointers:

1. Accessing arrays and strings efficiently.

2. Managing dynamic memory allocation.

3. Implementing data structures such as linked lists.

4. Passing large data structures to functions without duplication.

5. Memory-intensive applications where performance is critical.


UNIT IV

14. Write a C Program to Compute the Sum of Elements Stored in an Array Using
Pointers.

#include <stdio.h>

int main( ) {

int arr[] = {10, 20, 30, 40, 50};

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

int *ptr = arr; // Pointer to the array

int sum = 0;

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

sum += *(ptr + i); // Access array elements using pointers

printf("Sum of elements in the array: %d\n", sum);

return 0;
}

Output:

Sum of elements in the array: 150

15. Explain the Term Dynamic Memory Allocation and Its Various Functions.

Definition:
Dynamic memory allocation allows programs to allocate memory during runtime, instead
of at compile-time.

Functions for Dynamic Memory Allocation in C:

1. malloc( ): Allocates a specified number of bytes and returns a pointer to the first
byte.

o Ex: int *ptr = (int *)malloc(15);


UNIT IV

2. calloc( ): Allocates memory for an array and initializes all elements to 0.

o Ex: int *ptr = (int *)calloc(15, sizeof(int));

3. realloc( ): Resizes previously allocated memory to a new size.

o Ex:

ptr = (int *)realloc(ptr, 15 * sizeof(int));

4. free( ): Deallocates previously allocated memory.


o Ex:

free(ptr);

Advantages: Efficient memory usage and support for variable-sized data structures.
Disadvantages: Improper usage may lead to memory leaks.

16. List and Explain Different Categories of Standard I/O Functions.

1. Input Functions:

o scanf( ): Reads formatted input from the standard input.

o gets( ): Reads a string until a newline is encountered. (Deprecated due to


safety concerns)

2. Output Functions:

o printf( ): Outputs formatted data to the standard output.


o puts( ): Outputs a string followed by a newline.

3. File I/O Functions:

o fopen( ): Opens a file.

o fscanf( ) / fprintf( ): Reads/writes formatted data from/to a file.

o fclose( ): Closes an open file.

4. Character I/O Functions:

o getchar( ) / putchar( ): Reads/Writes a single character.


UNIT IV

17. Discuss About Preprocessors.

Definition:
Preprocessors are directives that are processed before the actual compilation of a
program.
Common Preprocessor Directives in C:

1. #include: Includes standard or user-defined header files.

o Ex:

o #include <stdio.h>

2. #define: Defines symbolic constants or macros.

o Ex:

#define PI 3.14
3. #ifdef / #ifndef: Conditional compilation directives.

o Ex:

#ifdef PEC

printf("Pallavi Engineering College\n");

#endif

4. #undef: Undefines a macro.

5. #pragma: Provides special instructions to the compiler like ON or OFF.

18. What is a Macro? Explain in Detail About It.

Definition:
A macro is a fragment of code defined using the #define directive. It is replaced by its
value or code during the preprocessing stage.
Types of Macros:

1. Object-like Macros:

o Replace constants.

o Example:

#define PI 3.14
UNIT IV
2. Function-like Macros:

o Replace function-like expressions.

o Ex:

#define SQUARE(x) ((x) * (x))

3. Parameterized Macros:

o Allow arguments.

o Ex:
#define MAX(a, b) ((a) > (b) ? (a) : (b))

Advantages:

 Speeds up execution as it avoids function calls.

 Provides code reusability.

Disadvantages:

 It can lead to errors if not used carefully.

 Difficult to debug.

19. Explain in Detail About Command-Line Arguments with an Example.

Command-line arguments allow passing parameters to a program during execution.

Syntax:
int main(int argc, char *argv[]) {

// argc: Argument count

// argv: Argument vector (array of strings)

}
UNIT IV

Example Program:

#include <stdio.h>

int main(int argc, char *argv[]) {

if (argc < 2) {

printf("Usage: %s <name>\n", argv[0]);

return 1;
}

printf("Hello, %s!\n", argv[1]);

return 0;

Execution:

$ ./program John

Output: Hello, John!

20. Write a Program to Find the Palindrome Using Command-Line Arguments.

#include <stdio.h>

#include <string.h>

int main(int argc, char *argv[]) {

if (argc != 2) {

printf("Usage: %s <string>\n", argv[0]);


return 1;

char *str = argv[1];

int len = strlen(str);

int isPalindrome = 1;
UNIT IV

for (int i = 0; i < len / 2; i++) {

if (str[i] != str[len - i - 1]) {

isPalindrome = 0;

break;

if (isPalindrome) {

printf("%s is a palindrome.\n", str);

} else {

printf("%s is not a palindrome.\n", str);

return 0;
}

Execution:

$ ./program madam

Output: madam is a palindrome.

You might also like