Guess The Output series 3

This series contains 4 guess the output questions with explanation.


1) Guess The Output

No error in the above code.

We know about \t \n then what is \b?

\b (backspace) Moves the active position to the previous position on the current line.

The output without \b is value of a is 1

And the output with \b is value of a is.



2) Guess The Output

Again no error in the above program.

Just like \t used for horizontal tab \v is used for vertical tab.

What does vertical tab means?

Instead of printing the output in

123    456

format the output will be print in vertical format

123
    456


3) Guess The Output

The correct answer of above code is option 3 i.e. 3 2 3

Why?

Consider the above code

a[3] = {3,2,1}

  • x = ++a[2]
    • value of a[2] is 1. We have pre- incremented the value of a[2] means first a[2] is increased by 1. Now value of a[2] is 2 and then it assigned to x. So the value of x is 2.
  • y = a[2]++
    • value of a[2] is 2. The above equation changed the value of a[2].
    • Here we post incremented the value of a[2] means first value will be assigned to y and then value of a[2] is again incremented by 1.
    • Now the value of y is 2 and value of a[2] is 3.
  • z = a[x++]
    • Value of x is 2. x++ is post incremented so a[2] is 3 and 3 value is assigned to z and then value of x is incremented by 1 i.e. final value of x is 3
  • Now final value of x is 3, y is 2 and z is 3.
  • So the output is 3 2 3

If you have any queries do let us know in comment section below

Now try to print value of a[0], a[1] and a[2].

#include<stdio.h>

int main() 
{
    int a[3] ={3,2,1};
    int x,y,z;
    x = ++a[2];
    y = a[2]++;
    z = a[x++];
    //printf("%d %d %d",x,y,z);
    printf("\n%d %d %d",a[0],a[1],a[2]);

    return 0;

}


4) Guess The Output

Before going to actual answer I would like to mention some key points about MACROS

What are Macros?

The C pre-processor is a macro pre-processor allows you to define macros that transforms your program before compilation.

These transformations can be the inclusion of header file, macro expansions etc.

All pre-processing directives begin with a # symbol.

Means before compilation process, MACROS are are pre-processed.

What does this mean?

for ex.

#include<stdio.h>

#define P 1
int main()
{
    printf("%d ", P);
}

Means before compilation the value of P is replaced with 1. i.e.

#include<stdio.h>   // header files are also pre-processed


int main()
{
    printf("%d ", 1);
}

To display pre-processed file

you can use following commands

gcc -E -o prefile.i myProgram.c

You can see pre-processed file in prefile.i file

This is how pre-processed file looks like

Pre Processed file of above program

Now we will come to our main question

#include<stdio.h>

#define PRODUCT(x) (x*x*x*x)
int main() 
{
    
    int x=2;
    int result = PRODUCT(x++);
    printf("\n%d %d", x,result);

    return 0;
}

The pre-processed part of above program is

//the part from stdio.h file not showed here
.
.
.   
int main()
    {

        int x=2;
        int result = (x++*x++*x++*x++);
        printf("\n%d %d", x,result);

        return 0;
    }

Now we can easily guess the output of above code.

And the output of above code is

6 120


Admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top