Thread: Can somone please break this down for me

  1. #1
    Registered User
    Join Date
    Mar 2012
    Location
    US
    Posts
    49

    Exclamation Can somone please break this down for me

    int
    Code:
    #include <stdio.h>
    int f(int a, int b);
    int main() {
       int a = 6, b = 7, c = 8;    
      b = f(c+2, 3*a-b);
      printf("a = %d, b = %d, c = %d\n", a, b, c);    
      return 0;
    }
    
    int f(int a, int b) {
      a = a + b;
      b = a - b;
      printf("a = %d, b = %d\n", a, b);
      return 42;
    }
    I have a final today and im trying to understand this part?

    a=21 b=10
    a=6 b=42 c=8

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Which part exactly?

    Get out a bit of paper. Write columns for every variable (be VERY careful as f defines its own local a and b and you have main definitions with exactly the same name but they will be DIFFERENT variables). Run through the program in your head writing down the values of the variables at each line.

    Like this:

    Code:
    Line No  |  A   |   B   |   C   |   f's A   |   f's B
    1          |  6   |   7   |   8    |    ?      |      ?

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    What exactly do you not understand about the output?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What don't you understand?

    Jim

  5. #5
    Registered User
    Join Date
    Mar 2012
    Location
    US
    Posts
    49
    Im having a hard time understanding how they get 42 for b than the second time they get a=21 and b =10

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What value is your function returning, where is that value being placed?

    Jim

  7. #7
    Registered User
    Join Date
    Mar 2012
    Location
    US
    Posts
    49
    Its returning a=21 b=10
    a=6 b=42 c=8

    This is a practice example from a test review, we have to write it on paper but I just wanted to see how they got the output they did. I can understand the a and c in the first function but idk how they got 42 for b. Or a=21 and b=10

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What is this function returning?
    Code:
    int f(int a, int b) {
      a = a + b;
      b = a - b;
      printf("a = %d, b = %d\n", a, b);
      return 42;
    }
    Where is that return value placed?
    Code:
    b = f(c+2, 3*a-b);
    Jim

  9. #9
    Registered User
    Join Date
    Mar 2012
    Location
    US
    Posts
    49
    Ok but how does b go from 42 to 10?

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It doesn't "go from 42 to 10". They are two separate variables. One is a local variable in main, the other is a parameter to the f() function.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It doesn't "go from 42 to 10". They are two separate variables. One is a local variable in main, the other is a parameter to the f() function.

    EDIT: do what gardhr says. That's the only way to actually understand what is happening, trace it line by line on paper. Be the compiler and computer. Run this program in your head.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The variable b never goes from 42 to 10. This is your reported output:
    a=21 b=10
    a=6 b=42 c=8
    The variable b in the function has a value of 10, after the function returns b has a value of 42 in main. The variable b in the function is not the same variable b that is in the function.

    Jim

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    It's all a question of scope.
    Code:
    #include <stdio.h>
    int f(int a, int b);
    int main() {
       int a = 6, b = 7, c = 8;    
      b = f(c+2, 3*a-b);
      printf("a = %d, b = %d, c = %d\n", a, b, c);    
      return 0;
    }
    
    int f(int a, int b) {
      a = a + b;
      b = a - b;
      printf("a = %d, b = %d\n", a, b);
      return 42;
    }
    These b's are completely different variables.
    Changes you make to b inside f() do NOT change the value of b in main.
    b (in main) changes with the return result of f() - that's all.

    Rename them to fred and barney if it'll make you feel better.
    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.

  14. #14
    Registered User
    Join Date
    Mar 2012
    Location
    US
    Posts
    49
    Oh wow Lol the paper really helped. Thanks fells.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Stepping through it in a debugger really helps too!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can somone please break this down for me
    By Bsmooth01 in forum C Programming
    Replies: 9
    Last Post: 04-12-2012, 08:13 AM
  2. can somone help my mate?
    By swgh in forum C++ Programming
    Replies: 3
    Last Post: 04-29-2005, 04:33 AM
  3. can somone please help me with this code?
    By amsy in forum C Programming
    Replies: 1
    Last Post: 10-25-2004, 12:23 PM
  4. Could somone test this?
    By joeyzt in forum Windows Programming
    Replies: 6
    Last Post: 07-18-2003, 04:20 AM