Unit Iv
Unit Iv
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:
Uses of Pointers:
14. Write a C Program to Compute the Sum of Elements Stored in an Array Using
Pointers.
#include <stdio.h>
int main( ) {
int sum = 0;
return 0;
}
Output:
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.
1. malloc( ): Allocates a specified number of bytes and returns a pointer to the first
byte.
o Ex:
free(ptr);
Advantages: Efficient memory usage and support for variable-sized data structures.
Disadvantages: Improper usage may lead to memory leaks.
1. Input Functions:
2. Output Functions:
Definition:
Preprocessors are directives that are processed before the actual compilation of a
program.
Common Preprocessor Directives in C:
o Ex:
o #include <stdio.h>
o Ex:
#define PI 3.14
3. #ifdef / #ifndef: Conditional compilation directives.
o Ex:
#ifdef PEC
#endif
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 Ex:
3. Parameterized Macros:
o Allow arguments.
o Ex:
#define MAX(a, b) ((a) > (b) ? (a) : (b))
Advantages:
Disadvantages:
Difficult to debug.
Syntax:
int main(int argc, char *argv[]) {
}
UNIT IV
Example Program:
#include <stdio.h>
if (argc < 2) {
return 1;
}
return 0;
Execution:
$ ./program John
#include <stdio.h>
#include <string.h>
if (argc != 2) {
int isPalindrome = 1;
UNIT IV
isPalindrome = 0;
break;
if (isPalindrome) {
} else {
return 0;
}
Execution:
$ ./program madam