59.2k views
1 vote
Consider the C program below, and tell us what is it supposed to do.

#include
int main()
{
Int a=10,b=20, ∗P1=&a, ∗P2=&b, temp;
∗P1= *P1+ ∗P2
∗P2= ∗P1− ∗P2
∗P1= ∗P1− ∗P2
Return 0;

}
a. Pointer Program to add two numbers
b. The program is incorrect
c. Pointer Program to swap two numbers without using the 3 rd variable
d. Pointer Program to subtract two numbers
e. Pointer Program to swap two numbers using the 3 rd variable

User Saeed Prez
by
7.6k points

1 Answer

3 votes

Final answer:

The C program provided is an example of a pointer program to swap two numbers using the 3rd variable. It uses pointers to swap the values of two variables without directly assigning them.

Step-by-step explanation:

The C program provided is an example of a pointer program to swap two numbers using the 3rd variable. It uses pointers to swap the values of two variables without directly assigning them.

Here's how it works:

  1. Declare and initialize two variables, a and b, with values 10 and 20 respectively.
  2. Create two pointers, P1 and P2, and assign them the addresses of variables a and b.
  3. Use the third variable, temp, to store the value of P1.
  4. Assign the value of P1 to the sum of P1 and the value pointed by P2, effectively adding the values of a and b and storing the result in a.
  5. Assign the value of P2 to the difference of P1 and the value pointed by P2, effectively subtracting the value of b from the new value of a and storing the result in b.
  6. Assign the value of temp to P1, effectively swapping the values of a and b using the 3rd variable.

The program then returns 0 to indicate successful execution.

User Kekoa
by
9.0k points