StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Program #31 (U3): Call by Value vs Reference (swap)
Download
  1. C Programming
  2. C Programming Practice
Program #30 (U3): Palindrome String Check (case-insensitive) : Program #32 (U3): Iterate Array via Pointer Arithmetic (find max & index)
C Programming Practice

Task: Write two swap functions: one by value (no effect on caller) and one using pointers (effect visible). Show before/after.

 
#include <stdio.h>

 
void swap_by_value(int a, int b) { int t=a; a=b; b=t; }

void swap_by_ref(int* a, int* b) { int t=*a; *a=*b; *b=t; }

 
int main(void) {

    int x, y;

    printf("Enter x y: ");

    if(scanf("%d %d", &x, &y) != 2){ printf("Invalid input.\n"); return 0; }

 
    printf("Before: x=%d y=%d\n", x, y);

    swap_by_value(x, y);

    printf("After swap_by_value: x=%d y=%d (no change)\n", x, y);

    swap_by_ref(&x, &y);

    printf("After swap_by_ref  : x=%d y=%d\n", x, y);

    return 0;

}

 

 

Program #30 (U3): Palindrome String Check (case-insensitive) Program #32 (U3): Iterate Array via Pointer Arithmetic (find max & index)
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website