Course Content

  • macros

Course Content

Advanced C Features: Macros

Hello everyone, my name is Atharva and we are continuing with this C programming course….. in which we had discussed in the last video about compilation and execution. So, this process we define with compilation or execution terms. After this we are starting a new topic in this video which is called Macros in C. What Macros is in C basically? So, 1st of all we will understand the meaning of word Macro. So, if we talk about the word Macro, Macro means to explain any big thing in short, giving it a short term. We call it as Macro. In the same way in C programming, we have to represent a big logic in a small term in our program. So, we are using our macros. So, what are Macros, converting a big logic and giving it a small term. We call it as Macros. “Macros can be created using #define directive.” If we want that we should make our very own macro in the program then we use our #define directive, about which we have read earlier. And by using this #define directive. We can make one macro as per our wish so that whenever we want to use or implement that logic in our program, so we will use the macro term that we have given, with which our compiler understands that what are we trying to do. We basically want to represent that logic. So, this is the story behind the macros. Now we can make the macros on our own as well and apart from that in our C programming language, by default there are few Macros that are defined, they are already made, which we can directly use in our program. So, now we will see here, which all Macros there are going to be. So, these are the predefined Macros. In which the first Macro is date and this date basically what it does is it returns the current date to us. In the same way is our time macro, when we are using that macro, the particular moments time, it represents or returns that for us. After these two macros, the third macro is which we call as file. What is does is it returns or prints our file’s name. Along with that if we talk about line macro. Wherever line macro is written in our program. It is on whichever line, that particular line’s number, it returns that to us. So, these are few predefined macros with us, which we will implement even in our practical and we will see how these macros gives us output. We will quickly go to our practical screen, Now we have come inside our text editor and here we are making a simple program, in which we will try that we are able to make the macros as well as we are able to use the predefined macros. So, we will start with the preprocessing command where we have included the stdio.h file. And further we have made use of #define directive, After #define directive we have written pi, which is a term. Whatever we will write in front of it, pi will represents its value. As we know that pi, pie is constant in our math, it has a particular value, 3.14. Whenever we want to use pie in our program. We would want to use the pie’s value. At that time instead of writing the value we will write the term pi, with this our compiler will understand that we are signalling that we have to use this value here. In this way we can use any big value or logic and we can give it a small term or name, with #define directive, which we call as one Macro. So, this way about our user defined macro which in our main function we will be printing going ahead. So, this way about pi function or pi constant. Now we will come ahead of it. So, you can see ahead that we have written printf statement. And in that what we want to print? We are basically using our file macro and what dose this file macro do? It gives the output of our file’s name, So, this gets the output in string’s format and so in the strings statement, we are using string’s format specifier over here. In the same way, like file macro, we also have date macro. And date macro gets the current date which with the help of printf statement, we will print here and see. After that time marco also works similarly. It shows the current time on our output screen. The 4th macro that we are discussing over here, would be our line macro and line macro tells us, suppose this particular line is number 10th position. So, when we wish to print the line macro. It will give us the output 10, with this we will understand where is line macro written in our program. It is written on the 10th line. These are our those predefined macros, about which we don’t have to write, if we use this term directly, with that we get our output. So, this was a program in which we have made user defined macros, as well as predefined macros. Now we will execute it and see. So, first of all we will run it and as soon as we will run it, you will see that it will give us the output (Pause 10 secs for codding). So, you can see that as soon as we wanted to print pi macro, it printed 3.1400 for us. Which we had defined by default on the top with the help of #define directive. Along with that when we said that in whichever file this program is written, give us that file name. So, it got the file’s name. And our file’s name is “macros.c”. So, this predefined macro file that we have with us, it comes with the file name. After that we saw date macro. What does date macro do is it displays the current date as it is. Along with that time macro returns the current time, whatever is the time at this time. After this is our line macro. Line macro prints the line number for us. Since this was printed in the 10th line, you can see it. So, it has printed line 10 for us. So, this was about predefined macros as well as user defined macros. In this video we saw how macros are created and how the predefined macros are used.

 If you think that there is any part of this video or this topic, you might have not understood then you can ask us any query or question related to that topic without any inhibition.

You just have to go on forums.learnvern.com,

you have to type your query there in your own language and we will

come up with the solution or the answer as soon as possible in your own language.

Apart from this if you wish to have a discussion on any topic.

Even that you will be able to do on forums.learnvern.com.

In the next video we will be learning Linked List in C. And this will be the introduction to linked list. Along with linked list there are many other topics which are very useful in advanced C programming. Now you can practise Macros, we will meet you with the Linked List topic in the next video.

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere