Preprocessor Conditional Directives

4 minutes
Share the link to this page
Copied
  Completed
Preprocessor directives are lines included in the code of programs preceded by a hash sign (#). These lines are not programmed statements but directives for the preprocessor. The preprocessor examines the code before the actual compilation of code begins and resolves all these directives before any code is actually generated by regular statements.

Transcript

Hello, and welcome to the 67th tutorial in the c++ program series. And in this part, we're going to be looking at the preprocessor conditional directives. And these allow you to include or omit a piece of code from your application. They are similar to extends, but unlike your standards, they are not run at runtime. But before, so if a piece of code is emitted, it cannot be included, which can be useful when developing for multiple platforms, you may have platform specific features. So you may have a little quick check.

And it is a window firm it is an Apple phone is a Android device. And perhaps you have some sort of game sent, I mean, some sort of multiplayer features or some sort of in app purchases, features where they work fine if you just do for iOS or Windows Phone or Android, but each one of them have had their own system. They could do some sort of pre secondary conditional directive, because the other piece of code isn't needed at all. Whereas a regular if statement, it is suggests in a way that that code could be needed within the application generally will because it will go for if or else during runtime. But let's show you how to implement a conditional directive. It's an extension of what we did in the previous tutorial, which is hash defined.

We also did hash undefined, but we're not going to be covering that in this tutorial. So I was going to quickly define the key word Yes, we're not going to assign a value to it, we don't need a value. And what we're going to do in here is actually what's going to remove this for a moment. And the same tax for a conditional directive is the equivalent to the if, part if statement is hash, if defined, and then well, the actual namely definition from for Yes. And then we're also gonna point out and the syntax delay is hash health. The syntax for ending the conditional directory is hash.

And if because this is essentially you can think of it as the end curly brace on a if statement and then here we can do something we're going to put this TD Yes, actually show you know for something else so we can illustrate the int x. And if I were to do on equals zero, and then we got those succeeded reasonably our bill succeeded because we have yes hash defined and then check the is yes defined, then it creates the variable i, before it would do x equals zero and run it. We both could do something undeclared identifier is not declared at all. But if you were to just switch this to a No. And when we get it isn't declared because you went here is yes define No, it wasn't anything, then it bumped down to the house conditional directive. So for now we are just going to comment this out.

And we're also going to do, we're going to show you the hash if not defined. So if and def and then here witness put, well, keyword put Yes. And if this isn't defined, we are going to define the keyword Hello. And if and then we're going to use if he would help them define organ do the same to print out saying hello. To end this hash, and they have everyone this we get the key word hello because it goes to here and yes Define reality checks. If yes isn't defined, then define the key word hello that here checks is the key word hello, define which it is and print out this line that says hello, which are in this to the limit.

We get nothing printed out because it goes to here say is or isn't. Yes, define and then it comes up. But yes, it is defined, so skips over this, then it goes to here, instead of defined yet. I mean, no it isn't. So it skips over this pillar code, and it just ends the application. That's it for preprocessor conditional directive.

If you have any more questions or if you have any question in general, feel free to message us at support at sound systems calm okay. email will be in the description. You can comment on this video or just directly messages while you do all the recording for source code will also be in the description. In the next part of the series. We'll be looking at the preprocessor preprocessor long directive. And as usual, thanks for watching and hope you have a great day.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.