Time to flex your pointer skills! You'll write a program that reads two integers from the user, swaps them using a pointer-based function, and prints the result.
YOUR TASK
Write a C program that:
Reads two integers from the user
Defines a swap(int *a, int *b) function that exchanges the values
Calls swap and prints both values before and after
Need a hint?
Step-by-step:
1. Declare void swap(int *a, int *b) before main
2. Inside swap: use a temp variable — int tmp = *a; *a = *b; *b = tmp;
3. In main, read two ints with scanf("%d %d", &x, &y);
4. Print before: printf("Before: %d %d\n", x, y);
5. Call swap(&x, &y);
6. Print after: printf("After: %d %d\n", x, y);
Example (input: 3 7):
Before: 3 7
After: 7 3
Code Editor
Ready
Output
Console
Console initialized...
Ready for Project 2: Pointer-Based Swap Utility
> Test your code with the "Check My Code" button
> When it passes, the Complete button will unlock!