- Erlang - Web Programming
- Erlang - Drivers
- Erlang - Performance
- Erlang - Concurrency
- Erlang - OTP
- Erlang - Distributed Programming
- Erlang - Ports
- Erlang - Databases
- Erlang - Emails
- Erlang - Processes
- Erlang - Funs
- Erlang - Binaries
- Erlang - BIFS
- Erlang - Guards
- Erlang - Pattern Matching
- Erlang - Preprocessors
- Erlang - Header Files
- Erlang - Macros
- Erlang - Exceptions
- Erlang - Records
- Erlang - Tuples
- Erlang - Maps
- Erlang - Atoms
- Erlang - File I/O
- Erlang - Lists
- Erlang - Strings
- Erlang - Numbers
- Erlang - Recursion
- Erlang - Modules
- Erlang - Functions
- Erlang - Decision Making
- Erlang - Loops
- Erlang - Operators
- Erlang - Variables
- Erlang - Data Types
- Erlang - Shell
- Erlang - Basic Syntax
- Erlang - Environment
- Erlang - Overview
- Erlang - Home
Erlang Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Erlang - Macros
Macros are generally used for inpne code replacements. In Erlang, macros are defined via the following statements.
-define(Constant, Replacement).
-define(Func(Var1, Var2,.., Var), Replacement).
Following is an example of macros using the first syntax −
Example
-module(helloworld). -export([start/0]). -define(a,1). start() -> io:fwrite("~w",[?a]).
From the above program you can see that the macro gets expanded by using the ‘?’ symbol. The constant gets replaced in place by the value defined in the macro.
The output of the above program will be −
Output
1
An example of a macro using the function class is as follows −
Example
-module(helloworld). -export([start/0]). -define(macro1(X,Y),{X+Y}). start() -> io:fwrite("~w",[?macro1(1,2)]).
The output of the above program will be −
Output
{3}
The following additional statements are available for macros −
undef(Macro) − Undefines the macro; after this you cannot call the macro.
ifdef(Macro) − Evaluates the following pnes only if the Macro has been defined.
ifndef(Macro) − Evaluates the following pnes only if Macro is undefined.
else − Allowed after an ifdef or ifndef statement. If the condition was false, the statements following else are evaluated.
endif − Marks the end of an ifdef or ifndef statement.
When using the above statements, it should be used in the proper way as shown in the following program.
-ifdef(<FlagName>). -define(...). -else. -define(...). -endif.Advertisements