What is the difference between the pre-processor directive and the if statement?

Asked

Viewed 51 times

3

In what situation should I use the directive #if? And the reserved word if?

1 answer

6


The #if is a conditional that occurs in a phase prior to the compilation, is as if it were another language, the preprocessor understand these commands and generates a different text according to the source making some processing and making some decisions. That is, the decision is made by the compiler, that #if is instructing the compiler what it should do.

The decision is only whether a part of the text that is in this block should continue in the source for the second phase of the compilation to use, or whether that should be discarded. And the condition that’s there is extremely simple and only with data that can be decided at compile time, so basically it only checks for the presence of a compile variable or not.

This is a somewhat more sophisticated technique, to determine parts of the code according to the platform, or to the product edition (which Features must have), or which compiler you are using, or if you have done something, or if one type must be formed in one way or another for some reason. So if you don’t master the rest well it shouldn’t be your focus. It’s another language for programmers who master every cycle.

The if is part of the application you are creating, its effective execution only occurs during the execution of your application. There at the source it is just an instruction that the compiler will translate into machine language or another way. And of course, conditions can be much more sophisticated and use execution data. It is a command of the C language, it is no other language.

It is important to have this distinction of having two different languages in different execution contexts, so confusion significantly reduces. By chance the two have the same syntax for a command that acts similarly but not the same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.