1) What are Macros? Explain macros with an example program?2) What are structure pointers? .Write a C program that uses structure pointer to refer structure members(attributes) using the arrow (->) operator?3) Write a C program that uses “array of structures” to store 3(three) “BOOK” records based on“Book_Pages, Book_Price and Book_Name” and to display all the records of 3(three) Books.4) Explain an Array of pointers with syntax and examples.5) Explain about malloc(), calloc() and realloc() with examples? Differentiate between malloc()and calloc().6) What do you mean by conditional compilation in preprocessing directives? Explain with examples.7) Explain the concept of pointers to structures and self referential structure with examples.
Question
- What are Macros? Explain macros with an example program?2) What are structure pointers? .Write a C program that uses structure pointer to refer structure members(attributes) using the arrow (->) operator?3) Write a C program that uses “array of structures” to store 3(three) “BOOK” records based on“Book_Pages, Book_Price and Book_Name” and to display all the records of 3(three) Books.4) Explain an Array of pointers with syntax and examples.5) Explain about malloc(), calloc() and realloc() with examples? Differentiate between malloc()and calloc().6) What do you mean by conditional compilation in preprocessing directives? Explain with examples.7) Explain the concept of pointers to structures and self referential structure with examples.
Solution
- Macros: In C programming, macros are a piece of code in a program which is given some name. Whenever this name is encountered by the compiler the compiler replaces the name with the actual piece of code. The ‘#define’ directive is used to define a macro.
Example:
#include<stdio.h>
#define PI 3.14
int main() {
float radius, area;
printf("\nEnter the radius of Circle : ");
scanf("%f", &radius);
area = PI*radius*radius;
printf("\nArea of Circle : %f", area);
return (0);
}
In this program, PI is a macro which is defined as 3.14. Whenever PI is encountered in the program, it is replaced by 3.14 by the compiler.
- Structure Pointers: A structure pointer is a pointer variable that can point to a structure of its type.
Example:
#include <stdio.h>
struct student {
char name[50];
int roll;
};
int main() {
struct student s1 = {.roll = 123, .name = "John"};
struct student *ptr;
ptr = &s1;
printf("Name: %s\n", ptr->name);
printf("Roll Number: %d", ptr->roll);
return 0;
}
In this program, ptr is a structure pointer which points to the structure s1. The arrow operator (->) is used to access the members of the structure using the structure pointer.
- Array of Structures: An array of structures in C is an array where each element is of the structure type.
Example:
#include <stdio.h>
struct book {
int pages;
float price;
char name[50];
} b[3];
int main() {
int i;
for(i=0; i<3; i++) {
printf("\nEnter details of book %d\n", i+1);
printf("Enter name: ");
scanf("%s", b[i].name);
printf("Enter pages: ");
scanf("%d", &b[i].pages);
printf("Enter price: ");
scanf("%f", &b[i].price);
}
printf("\nDisplaying Information of books\n");
for(i=0; i<3; i++) {
printf("\nBook %d\n", i+1);
printf("Name: %s\n", b[i].name);
printf("Pages: %d\n", b[i].pages);
printf("Price: %.2f\n", b[i].price);
}
return 0;
}
In this program, b is an array of structures where each element is of the type struct book. The details of 3 books are entered by the user and then displayed.
- Array of Pointers: An array of pointers is an array that holds memory addresses. Each element of an array of pointers holds a memory address of another variable.
Syntax:
datatype *array_name[size];
Example:
#include <stdio.h>
int main() {
int num[3] = {10, 20, 30};
int *ptr[3];
for(int i = 0; i < 3; i++) {
ptr[i] = &num[i];
}
for(int i = 0; i < 3; i++) {
printf("Value of num[%d] = %d\n", i, *ptr[i]);
}
return 0;
}
In this program, ptr is an array of pointers. Each element of ptr holds the address of each element of the array num.
- malloc(), calloc(), and realloc() are library functions that perform dynamic memory allocation in C programming.
malloc() is a function that allocates a specified number of bytes of memory and returns a pointer of type void which can be cast into a pointer of any form.
Example:
int *ptr;
ptr = (int*) malloc(100 * sizeof(int));
calloc() is another function that dynamically allocates the specified number of blocks of memory of the specified type. It initializes each block with a default value '0'.
Example:
int *ptr;
ptr = (int*) calloc(100, sizeof(int));
realloc() is a library function that changes the size of the memory block pointed to by ptr to size bytes.
Example:
ptr = realloc(ptr, 200 * sizeof(int));
The main difference between malloc() and calloc() is that malloc() allocates single block of memory whereas calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero.
- Conditional Compilation: Conditional compilation is a technique used in C programming to include or exclude part of the program based on certain conditions. It is done using preprocessor directives like #if, #else, #elif, #endif, #ifdef, #ifndef etc.
Example:
#define DEBUG
#ifdef DEBUG
printf("Debugging information");
#endif
In this program, the printf statement will only be included in the program if DEBUG is defined.
- Pointers to Structures: A pointer to a structure is similar to a pointer to any other variable. It allows you to refer to the same structure using the pointer.
Example:
struct student {
char name[50];
int roll;
};
int main() {
struct student s1 = {.roll = 123, .name = "John"};
struct student *ptr;
ptr = &s1;
printf("Name: %s\n", ptr->name);
printf("Roll Number: %d", ptr->roll);
return 0;
}
Self Referential Structure: A structure that contains a pointer member to point to the same structure type is known as self referential structure.
Example:
struct node {
int data;
struct node *next;
};
In this structure, the pointer next is a pointer to a structure of type node. This type of structure is commonly used in linked lists and trees.
Similar Questions
1) What are Macros? Explain macros with an example program?2) What are structure pointers? .Write a C program that uses structure pointer to refer structure members(attributes) using the arrow (->) operator?3) Write a C program that uses “array of structures” to store 3(three) “BOOK” records based on“Book_Pages, Book_Price and Book_Name” and to display all the records of 3(three) Books.4) Explain an Array of pointers with syntax and examples.5) Explain about malloc(), calloc() and realloc() with examples? Differentiate between malloc()and calloc().6) What do you mean by conditional compilation in preprocessing directives? Explain with examples.7) Explain the concept of pointers to structures and self referential structure with examples.
11) What is the use of the enum keyword in C?12) What is Structure padding?13) What is the use of the bit field in C?14) What is meant by an anonymous structure?15) What is meant by nested structures?16) What are self referential structures?17) What is the use of #include preprocessor directive?18) What is the use of .(dot) operator?19) What is the use of ->(arrow) operator?20) What is the use of pointer variables in C?
6) Explain about macros with an example.7) Explain about Bit fields with examples.8) What is an enumeration? Give an example for enumeration.9) What are self-referential structures? Write the syntax for the same.10) Write a C program to demonstrate Enumeration type?
16) What are Anonymous structures? Give an example.17) List out any 3 differences between static and dynamic memory allocations?18) Write an example program to demonstrate embedded nested structure?19) Write a C program to find the maximum and minimum of two numbers using macros?20) Explain the difference between arrays of structures and arrays within structures?
Explain declaration of pointer variables, initialization and accessing a variable through its pointer. Explain how union is declared and how union member is accessed. Explain realloc() and free() function with example. Explain array within structures with example. Explain file opening and closing in C.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.