C++ Preprocessor | Find output programs | Set 3

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

Program 1:

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

#define student(R, F) cout << "Roll Number: " << #R << " Fees: " << #F

using namespace std;

int main()
{
    int roll_no = 101;
    int fees = 1000;

    student(roll_no, fees);

    return 0;
}

Output:

Roll Number: roll_no Fees: fees

Explanation:

The above program will print "Roll Number: roll_no Fees: fees" on the console screen. Let's understand the program, we defined student() macro that uses the pound symbol '#', which is used to print variable name instead of value. Then the roll_no and fees will be printed instead of their values.

Program 2:

#include <iostream>
#include <stdio.h>

#define MACRO(A, B) cout << A##B;

using namespace std;

int main()
{
    int A = 101;
    int B = 102;
    int AB = 103;

    MACRO(A, B);

    return 0;
}

Output:

103

Explanation:

The above code will print 103 on the console screen, because here we used token pasting "##" operator, which is used to concatenate the name of two variable, in our case, we concatenate A and B, and it becomes AB, then the value of AB will print on the console screen. That is 30.

Program 3:

#include <iostream>
#include <stdio.h>

#define A 10

#ifdef A
#define A 20
#else
#define A 30
#endif

using namespace std;

int main()
{
    cout << A << endl;

    return 0;
}

Output:

20

Explanation:

The above code will print 20 on the console screen. In the above program, we used conditional preprocessor directives. Here we defined a macro called A that contains value A, and check if A is defined then it will be defined by 20 otherwise A will be defined by 30. That's why 20 will be printed on the console screen.

Program 4:

#include <iostream>
#include <stdio.h>

#define A 10
#define B 20

#ifdef A > B
#define Large A
#else
#define Large B
#endif

using namespace std;

int main()
{
    cout << Large << endl;

    return 0;
}

Output:

10

Explanation:

In the above program, we defined two macros A and B, then we checked the condition (A>B) using #ifdef, but using #ifdef we can check specified macro is defined or not, that's why here #ifdef check macro "A" is defined then it will be defined "Large" by "A", and then it will print "10" on the console screen.

Program 5:

#include <iostream>
#include <stdio.h>

#define A 10
#define B 20

#ifndef B = A
#define Large A
#else
#define Large B
#endif

using namespace std;

int main()
{
    cout << Large << endl;

    return 0;
}

Output:

20

Explanation:

In the above program, we defined two macros A and B, then we used (B=A) in #ifndef, but using #ifndef we can check specified macro is not defined, that's why here #ifndef check macro "B" is not defined then it will be defined "Large" by "B" in #else directive, and then it will print "20" on the console screen.






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.