Q:

C++ Preprocessor | Find output programs | Set 2

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on C++ Preprocessor (set 2).

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;
}

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;
}

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;
}

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;
}

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;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

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.

Answer Program 2:

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.

Answer Program 3:

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

Answer Program 4:

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.

Answer Program 5:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Preprocessor | Find output programs | Set 3... >>
<< C++ Preprocessor | Find output programs | Set 1...