Open In App

Output of C++ programs | Set 25 (Macros)

Prerequisite – Macros 
 




#include <iostream>
using namespace std;
  
#define a 10
  
int main()
{
    int a = 5;
    cout << "macro variable value: "<< a;
    return 1;
}

Output: 
Error 






#include <iostream>
using namespace std;
  
#define area length * width
  
int main()
{
    int length = 10, width = 20;
    cout << "macro expression area: " << area;
    return 1;
}

Output: 

macro expression area: 200
 cout<< "macro expression area: " << length * width;

an then the values of length and width are substituted and computed. 
 






#include<iostream>
using namespace std;
  
#define sqrt(x) (x*x)
  
int main()
{
    int a = 3, b;
    b = sqrt(a + 5);
    cout<< "Output of b = " << b;
}

Output: 

Output of b =  23
 #define sqrt(x) ( (x) * (x) )




#include <iostream>
using namespace std;
  
#define printf(s) cout << s;
  
int main()
{
    printf("GeeksforGeeks");
    cout << "\nBye Bye";
}

Output:

GeeksforGeeks
Bye Bye




#include <iostream>
using namespace std;
  
#define SQRT(x) ( x * x)
  
int main()
{
    int a,  b= 3;
    a = SQRT(b++);
    cout << a << endl << b;
    return 0;
}

Output: 

12
5

Quiz on Macros
This article is contributed by I.HARISH KUMAR.


Article Tags :
Uncategorized