C++ Preprocessor | Find output programs | Set 1

This section contains the C++ find output programs with their explanations on C++ Preprocessor (set 1).
Submitted by Nidhi, on September 11, 2020

Program 1:

#include <iostream>
#include <string.h>

#define ADD(A) A + A
using namespace std;

int main()
{
    int X = 10;
    int Y = 20;
    int Res = 0;

    Res = ADD(X) * ADD(Y);

    cout << Res << endl;

    return 0;
}

Output:

230

Explanation:

Here we defined a macro ADD() to add the number by itself. In the main() function, we declared 3 local variables X, Y, and Res that initialized with 10, 20, and 0 respectively.

Now evaluate the expression.

Res = ADD(X)*ADD(Y);
Res = ADD(10)*ADD(20);
Res = 10+10*20+20; // '*' has high priority then '+' then '*' will calculate before '+'
Res = 10+200+20;
Res = 230;

Then 230 will print on the console screen.

Program 2:

#include <iostream>
#include <string.h>

#define S sum
using namespace std;

int S(int a, int b)
{
    return (a + b);
}
int main()
{
    int X = 10;
    int Y = 20;
    int Z = 0;

    Z = sum(X, Y);

    cout << Z << endl;

    return 0;
}

Output:

30

Explanation:

In the above program, we defined a macro S, then "sum" is substituted at the place of the function name. Then the actual name of the function will be the sum.

In the main() function, here we declared three local variables X, Y, and Z initialized with 10,20, and 30 respectively.

Now we call function sum() and pass argument X and Y then the function will return the sum of X and Y that is 30, which is stored in Z, and then we print Z using cout.

Program 3:

#include <iostream>
#include <string.h>

#define M main()
using namespace std;

int M
{
    int X = 10;
    int Y = 20;
    int Z = 0;

    Z = X + Y;

    cout << Z << endl;

    return 0;
}

Output:

30

Explanation:

In the above program, we define a preprocessor macro M for main(). Then we use M in-place of main() function. The M will be substituted by the main().  In the main() function we declared three local variables X, Y, and Z.

X, Y, and Z are initialized with 10, 20, and 0 respectively. Here, we store the sum of X and Y into variable Z. Then final value Z will be 30 and it will be printed on the console screen.

Program 4:

#include <iostream>
#include <string.h>

#define M int main()

using namespace std;

M
{
    int X = 10;
    int Y = 20;
    int Z = 0;

    Z = X + Y;

    cout << Z << endl;

    return 0;
}

Output:

30

Explanation:

In the above program, we define a preprocessor macro M for main(). Then we use M in-place of main() function. The M will be substituted by int main().  In the main() function we declared three local variables X, Y, and Z.

X, Y, and Z are initialized with 10, 20, and 0 respectively. Here, we store the sum of X and Y into variable Z. Then final value Z will be 30 and it will be printed on the console screen.

Program 5:

#include<iostream>
#include<string.h>

#define M int main(){cout<<20;}

using namespace std;

M

Output:

20

Explanation:

In the above program, we define a preprocessor macro M for main(). Then we use M in-place of the complete definition of the main() function.

In the main() function we print 20 using cout.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.