What is #define (Macros) ,How to use?? : POST - 5


 Have you ever got the same doubt??

  • Don't worry I'll help you to get out of this
    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. #define k 6-2
    4. int main()
    5. {
    6.       int a;
    7.        a = k*k*k;
    8.        cout<<a;
    9.       return 0;
    10. }
  • Here #define is called macros
  • It substitutes the k value with the expression next to  k in code 
  • It won't evaluate the expression
  • Many of us thinks it evaluates the expression so they will assume k = 4 and substitutes                  a =  4 * 4 *4 and finally they decide a = 64;
  • But above approach is wrong ;
  • First it will substitute the expression so k remains same as 6-4 .
  • So , a = 6 - 2 * 6 - 2 * 6 - 2 
  • Now it will get evaluated
  • As per preferences first multiplication will be done ,
  • So, a = 6 - 12 - 12 - 2
  • Hence  a = -20
  • See the video for clarification 
  • Ask doubts in comment box

Guys if you have any doubts, please let me know

Post a Comment (0)
Previous Post Next Post

How to start Coding | coding for beginners | Lecture 1