Preprocessor Macro Definitions

4 minutes
Share the link to this page
Copied
  Completed
A macro is a fragment of code that has been given a name. Whenever the name is used, it is replaced by the contents of the macro. There are two kinds of macros. They differ mostly in what they look like when they are used. Object-like macros resemble data objects when used, function-like macros resemble function calls.

Transcript

Hello, welcome to the 66 tutorial in the c++ programming series. And in this poll you look at preprocessor macro definitions. A preprocessor directive occurred before compilation, there are two macro definition which are hash define and hash undeath hash un d f. And the hash hash on def is just default on define an existing hash define. Hash define allows you to assign keywords to pretty much anything constant, aka that can't be assigned a value like a regular variable. General naming convention for hash define throughout the industry is all uppercase, but you can obviously use your own naming convention. You don't have to assign a value to the keyword as you could just define a keyword which can be used to check if obviously, you can check later on if it's been defined, and if it does, you can use it.

Let's just go ahead and open up our project. So what we're gonna do the syntax for a hash define if you put a hash, the key word define the name of the definition of pi. And then after the sign an optional value, it can be anything it can even be code if you want to, which we'll show you later on in this tutorial how to do that 3.14149 and then in our code, we can do just if we get rid of this, so right in our 3.14159 to print out pi, we can just simply whatever point not if we run out and we get the body free, put my foot one foreigner printouts once I don't have to print it, he use it several times. This is a hash definition. It's a hash define should say, you can obtain this value two ways. One, and then you'll get propagated throughout the console, which is pretty cool.

So yeah, that is how you do when a hash define. And to use a hash on define what you do is hash on def, then basically the name of your definitely no parabolically just couldn't define it. Then afterwards, if I were to actually I'll just copy and paste this on your copy and paste this woman, your use of undeclared identifier is defined here. I mean, yeah, you print ch a, okay, but because it gets undefined with this line, it technically no longer exists when you try and do it there. So that is how you use the regular hash defined. So let's just show you how to do a bit more complex and assign some code to it.

So what we can do is define and we're gonna new loin and this is simply going to do a new line. So we're going to do this. Now, if we if I were to just get rid of this temporarily, I remember, we have 3.14159. And then the program and New Mexico t rose on the same line because we haven't terminated the line. But if I just type in a new line, we are 3.1491 3.14159. And then we get the program and it will exit code zero on the next line.

And this is because we have printed or we have put this code, which is printed as new line here. Obviously, you can assign a lot more complex code to you as well. So it's pretty cool. So you can have some constant piece of code, small chunks and you can just mix and match or improve your code. That's it for this part series. The next part when we're looking at preprocessor conditional directives.

If you have any questions feel free to message us at support at sonar systems coda UK email will be in the description you can comment on this video this directly method with why you do all the recordings source code will also be in the description. And as usual, and as usual, thanks for watching and I 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.