strange outputs

This is a discussion on strange outputs within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<conio.h> swap(int x,int y); int main() { int a,b; clrscr(); printf("Enter two numbers "); scanf("%d%d",&a,&b); swap(a,b); printf("\nBefore swapping ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    23

    strange outputs

    Code:
    #include<stdio.h>
    #include<conio.h>
    swap(int x,int y);
    int main()
    {
    int a,b;
    clrscr();
    printf("Enter two numbers ");
    scanf("%d%d",&a,&b);
    swap(a,b);
    printf("\nBefore swapping %d was a and %d was b");
    getch();
    return 0;
    }
    swap(int x,int y)
    {
    x=x+y;
    y=x-y;
    x=x-y;
    printf("\n%d is a and %d is b");
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,292
    Code:
    printf("\nBefore swapping %d was a and %d was b");
    
    // ...
    
    printf("\n%d is a and %d is b");
    You are not passing variables to your "printf()" calls to match the format specifiers.

  3. #3
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Enable all the warnings your compiler can produce.
    And mind the warnings. If you can, treat them as errors!

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    23
    Quote Originally Posted by Matticus View Post
    Code:
    printf("\nBefore swapping %d was a and %d was b");
    
    // ...
    
    printf("\n%d is a and %d is b");
    You are not passing variables to your "printf()" calls to match the format specifiers.
    oops!

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > x=x+y;
    > y=x-y;
    > x=x-y;
    Who's teaching this rubbish?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    You're also passing your variables by value which means that changes to x and y locally within the swap function have no bearing at all on the values passed into the function as a and b. When the function finishes, a and b will still have the same values they did prior to the function call; they will not be swapped. To do what you want you will have to pass references to the variables (pointers) into the function.

    [edit]You're also using nonstandard functions/header (<conio.h>, clrscr, getch)[/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in Inputs and outputs
    By keng1135 in forum C Programming
    Replies: 7
    Last Post: 06-08-2012, 02:04 AM
  2. Different outputs on different OS
    By darksifer in forum C Programming
    Replies: 13
    Last Post: 10-19-2010, 01:45 PM
  3. why i getting different outputs?
    By nkrao123@gmail. in forum C Programming
    Replies: 2
    Last Post: 12-08-2009, 07:33 AM
  4. Two Video Outputs
    By drdroid in forum Tech Board
    Replies: 3
    Last Post: 01-14-2004, 06:31 PM
  5. Outputs
    By kas2002 in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2002, 06:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21