C++ Preprocessor | Find output programs | Set 2

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

Program 1:

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

#pragma startup sayHello
#pragma exit sayHello
using namespace std;

void sayHello()
{
    cout << "Hello World";
}

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

    Z = X + Y;

    cout << Z << endl;

    return 0;
}

Output:

30
Note: Output will vary to the compiler to compiler. 

Explanation:

The output of the above program will vary to the compiler to compiler, because #pragma startup and #pragma exit do not work on every compiler.

#pragma stratup and #pragma exit directives are used to set the priority of functions. We can call function before main() function using #pragma stratup directive.

Program 2:

#include <iostream>
using namespace std;

#pragma pack(1)

typedef struct
    {
    char A;
    int B;
    char C;
} STR;

int main()
{
    cout << sizeof(STR) << endl;
    return 0;
}

Output:

6

Explanation:

Here, we used the #pragma pack(1) directive, which is used to avoid structure padding. If we did not use #pragrma pack(1) then it will print 12 on the console screen. Then it stores data block-wise, the block size in a 32-bit system is 4 byte. Then,

A occupy 1st block with 3 padded space.
B occupy 2nd block.
C occupies 3rd block with 3 padded space.

Program 3:

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

using namespace std;

int main()
{

#define SQUARE(A) (A * A)

    float AREA = 0;

    AREA = 3.14 * SQUARE(5);

    cout << AREA << endl;

    return 0;
}

Output:

78.5

Explanation:

We can also define the preprocessor directive inside the main() function. Here we define SQUARE() to find out the square of a specified number.

Now evaluate the expression.

AREA = 3.14*(5*5);
AREA = 3.14*25;
AREA = 78.5

Program 4:

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

using namespace std;

int main()
{

#define SIZE(A) (char*)(&A + 1) - (char*)(&A)

    int size = 0;
    float pi = 3.14F;

    size = SIZE(pi);

    cout << size << endl;

    return 0;
}

Output:

4

Explanation:

Here, we defined a macro called SIZE, which is used to calculate the size of the specified variable. Here we typecast the specified variable into character pointer and subtract the address of the specified variable by the pointer which is one position ahead then the result will be the size of the specified type.

Program 5:

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

using namespace std;

int main()
{

#define SIZE(A) (char*)(&A + 1) - (char*)(&A)

    int size = 0;

    size = SIZE(3.14F);

    cout << size << endl;

    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:13:17: error: lvalue required as unary ‘&’ operand
     size = SIZE(3.14F);
                 ^
main.cpp:9:26: note: in definition of macro ‘SIZE’
 #define SIZE(A) (char*)(&A + 1) - (char*)(&A)
                          ^
main.cpp:13:17: error: lvalue required as unary ‘&’ operand
     size = SIZE(3.14F);
                 ^
main.cpp:9:44: note: in definition of macro ‘SIZE’
 #define SIZE(A) (char*)(&A + 1) - (char*)(&A)
                                            ^

Explanation:

It will generate a syntax error because here we passed the value to the defined macro to find out the size, but in the macro definition we calculated the size based on the address of the variable and we passed constant, so the address of "3.14F" cannot be found. That's why it will generate an error.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.