Copyright © tutorialspoint.com
The ifdef statement is executed at parse time not runtime. This allows you to change the way your program operates in a very efficient manner.
Since the ifdef statement works at parse time, runtime values cannot be checked, instead special definitions can be set or unset at parse time as well.
The syntax of ifdef statement is:
ifdef macro then -- Statements will execute if the macro is defined. end if |
If the boolean expression evaluates to true then the block of code inside the if statement will be executed. If not the first set of code after the end of the ifdef statement will be executed.
The ifdef checks the macros defined by using with define keywords. There are plenty of macros defined like WIN32_CONSOLE, WIN32 or LINUX and you can define your own macros as follows:
with define MY_WORD -- defines |
You can undefined an already defined word as follows:
without define OTHER_WORD -- undefines |
#!/home/euphoria-4.0b2/bin/eui with define DEBUG integer a = 10 integer b = 20 ifdef DEBUG then puts(1, "Hello, I am a debug message one\n") end ifdef if (a + b) < 40 then printf(1, "%s\n", {"This is true if statement!"}) end if if (a + b) > 40 then printf(1, "%s\n", {"This is not true if statement!"}) end if |
This would produce following result:
Hello, I am a debug message one This is true if statement! |
You can take one action if given macro is defined otherwise you can take another action in case given macro is not defined.
The syntax of ifdef...elsedef statement is:
ifdef macro then -- Statements will execute if the macro is defined. elsedef -- Statements will execute if the macro is not defined. end if |
#!/home/euphoria-4.0b2/bin/eui ifdef WIN32 then puts(1, "This is windows 32 platform\n") elsedef puts(1, "This is not windows 32 platform\n") end ifdef |
When I run this program on my Linux machine it produces following result:
This is not windows 32 platform |
You can check multiple macros using ifdef...elsifdef statement.
The syntax of ifdef...elsifdef statement is:
ifdef macro1 then -- Statements will execute if the macro1 is defined. elsifdef macro2 then -- Statements will execute if the macro2 is defined. elsifdef macro3 then -- Statements will execute if the macro3 is defined. ....................... elsedef -- Statements will execute if the macro is not defined. end if |
#!/home/euphoria-4.0b2/bin/eui ifdef WIN32 then puts(1, "This is windows 32 platform\n") elsifdef LINUX then puts(1, "This is LINUX platform\n") elsedef puts(1, "This is neither Unix nor Windows\n") end ifdef |
When I run this program on my Linux machine it produces following result:
This is LINUX platform |
Copyright © tutorialspoint.com