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?
Question
- 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?
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:
#define PI 3.14
int main() {
float radius = 5;
float area = PI * radius * radius;
printf("Area of the circle = %f", area);
return 0;
}
In this example, PI is a macro which is defined as 3.14. Whenever PI is encountered in the code, it is replaced by 3.14 by the compiler.
- Bit fields: Bit fields in C are used to store multiple, logical, neighboring bits, where each bit represents some information. The syntax to declare a bit field is:
struct structure_name {
type member_name : width ;
};
Example:
struct packed_struct {
unsigned int f1:1;
unsigned int f2:1;
unsigned int f3:1;
unsigned int f4:1;
unsigned int type:4;
unsigned int my_int:9;
} pack;
In this example, 'pack' is a structure with bit fields. Each field represents some information and takes up only that many bits in the memory.
- Enumeration: Enumeration is a user-defined data type in C. It is used to assign names to the integral constants which makes a program easy to read and maintain. The keyword 'enum' is used to define enumerated data types.
Example:
enum week {Sun, Mon, Tue, Wed, Thu, Fri, Sat};
In this example, a new data type 'week' is created which has seven possible values - Sun, Mon, Tue, Wed, Thu, Fri, Sat.
- Self-referential structures: A structure in C is called self-referential if it has a pointer member that points to the structure of the same type.
Syntax:
struct self_referential {
int member;
struct self_referential *ptr;
};
In this syntax, 'self_referential' is a structure which has an integer member and a pointer 'ptr' that points to the same structure type.
- C program to demonstrate Enumeration type:
#include<stdio.h>
enum week {Sun, Mon, Tue, Wed, Thu, Fri, Sat};
int main() {
enum week day;
day = Wed;
printf("%d",day);
return 0;
}
In this program, 'week' is an enumerated data type and 'day' is a variable of this data type. The program will print '3' as output because 'Wed' is the fourth value in the enumeration and enumeration values start from '0' by default.
Similar Questions
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?
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?
1) Explain the use of typedef keyword and #define preprocessor directive.2) How to include user defined header files into your program? Give an example.3) What are the situations where a pointer becomes a dangling pointer? Explain with an example.4) Explain the enum with an example.5) Explain about Nested Structures with examples?
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.
Attempt all questions in brief. 2 x 10 = 20a. Differentiate between Object and Class.b. Describe the difference between Abstract Classes and Interfaces.c. List down the different types of Relationships in Class diagrams.d. Explain the terms Association, Aggregation, and Composition relationships.e. List down the different phases of Object-Oriented Analysis.f. Differentiate between Procedural and Object-Oriented language features.g. What is the namespace in C++? Explain its significance.h. Describe the concept of typecasting in C++.i. Discuss operator overloading in C++.j. Differentiate between private and public members of a class 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.