What is the purpose of the `#define` directive in C?

What is the purpose of the #define directive in C?

a) To define macros and constants
b) To include header files
c) To allocate memory
d) To declare variables

Answer:

a) To define macros and constants

Explanation:

The #define directive in C is used to define macros, which are pieces of code that are given a name. Whenever the name is used in the code, it is replaced by the code it represents during preprocessing. Macros can be used for constant values, code snippets, or expressions that are used repeatedly throughout the program.

Using #define helps to avoid magic numbers in code, improves readability, and makes it easier to update the code, as changes need to be made in only one place. However, care should be taken with complex macros, as they can lead to unexpected behavior if not used correctly.

Reference links:

https://www.rameshfadatare.com/learn-c-programming/

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top