#define PI 3.14159 //defining MACRO PI to represent the universal value of PIĬout << "The Value of PI is : " << PI << endl In addition, the value will be calculated and then will be shown as output over the display screen. Therefore, when in the main class we call AREA macro it will take the integer values as the input then will calculate according to the defined logic in the macro. In the main class, we have declared two integer variables with the value assigned to them. In the above code, we are calculating the area of the rectangle by defining an AREA macro which takes length and breadth as the input from the user and then calculates the area. Std :: cout << " The Area of the Rectangle is : " << area #define AREA(l, b) (l * b) // defining the MACRO AREA for calculating area Here is another C++ code to demonstrate the working of Macros in C++ programming: In the output, you can see it will automatically start comparing the values when we use MIN ( a, b ) in our main function. After that in the main class, we declared two integer variables x and y and assigned some values to them. In the above code, you can see we have defined a MACRO with name DEBUGGING for checking the progress of our code while debugging and then we have declared one more MACRO as MIN to calculate the minimum from two integers given by the user. Int x, y // declaring two integers x and yĬerr << " we are inside the main function " << endl Ĭout << " The minimum value from the given input is " << MIN(x, y) << endl Ĭerr << " We are moving out of the main function " << endl #define DEBUGGING // defining the MACRO debugging Here is the C++ code to demonstrate the working of Macros in C++ programming: Examples to Implement Macros in C++īelow are the examples of Macros in C++: Example #1 Because it is important to know how to correctly apply the syntax in source code so that it can be reused again and again whenever it is needed. Now we will discuss how does a macro works in programming through C++ code. For example, if in the code it is written like #define AREA (15,16) then the result will be 240. Therefore when you run a code then every time a compiler will come across AREA(l,b) it will replace the value with (l * b). The word AREA in the macro definition is called a Macro definition and ( l , b ) ( l * b ). # is the pre-processor that directs the compiler to preprocess before compiling the source code. Syntax for representing macros in C++: #define AREA ( l , b )( l * b )
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |