Thread: Simple question, trying to understand this code...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Simple question, trying to understand this code...

    What is the return type of the function main in ANSI C? does it have a return type at all?


    Also, can someone help me understand whats going on in this code, and how would I be able to predict what the output of this code would be once compiled without a computer?

    Code:
    #include <stdio.h>
    
    int f(int *a, int *b);
    
    int main() {
    
      int a=9, b=11;
    
      if (f(&b,&a))
        printf("a = %d,b = %d\n",a,b);
      else
        printf("Zero is the answer.\n");
    
      b = f(&a, &b);
    
      printf("a = %d,b = %d\n",a,b);
      return 0;
    }
    
    int f(int* a, int* b) {
      int c;
      c = 2*(*a)%(*b);
      (*a) = c + (3*(*b)%(*a));
      (*b) = c - (2*(*b)%(*a));
      printf("a = %d,b = %d\n",*a,*b);
      return c;
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) The return type is int. It is so in order to indicate to the system how the program terminated, normally or not.

    2) Just grab a pencil and a piece of paper and follow the values of the variables through the code.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    On a side note, returning a value from 'main' *is* optional (for any other function the result would be undefined) and the default return value is always zero (I think).

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > returning a value from 'main' *is* optional
    Not in C it isn't.

    That little magic cookie only works in C++.
    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.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    can someone be kind of enough to add comments to the code above so I understand whats goign on within the code, I tried to compile the code and learn from it like that, but I still dont really understand it:/


    /*I need to be able to predict the output of the code without using a computer when im taking the TEST that is a bout to come up..

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Salem
    Not in C it isn't.

    That little magic cookie only works in C++.
    It works in C, according to the 1999 edition of the C standard. Other than that it does not.

    Quote Originally Posted by matthayzon89
    can someone be kind of enough to add comments to the code above so I understand whats goign on within the code, I tried to compile the code and learn from it like that, but I still dont really understand it:/
    So far, what are you able to understand of it?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Im really lost because there is a b in the code and also a *b in the code.... Also, b is declared in the very beginning as 11 and then it is redefined...

    IS there any way someone can tell me why the first line of output is what it is ( basically explain the logic in one example)


    Code:
    #include <stdio.h>
    
    int f(int *a, int *b); //this declares and integer a and b in function f. Correct? the '*' next to the variable represents that it is a pointer, right? where exactly is it pointing to?
    
    int main() {
    
      int a=9, b=11;  
    
      if (f(&b,&a))
        printf("a = %d,b = %d\n",a,b);
      else
        printf("Zero is the answer.\n"); //so under what conditions is this going to print out?
    
      b = f(&a, &b);
    
      printf("a = %d,b = %d\n",a,b);
      return 0; //what is the significance of having a return type?
    }
    
    int f(int* a, int* b) {
      int c;  //why does this program print out more than once? there isnt a loop anywhere... 
      c = 2*(*a)%(*b);
      (*a) = c + (3*(*b)%(*a));
      (*b) = c - (2*(*b)%(*a));
      printf("a = %d,b = %d\n",*a,*b);
      return c; // why does this say return c?
    }
    Last edited by matthayzon89; 03-23-2010 at 11:38 AM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1. No, it declares a pointer to an integer, to be named a, and another to be named b.
    2. Look at the printf. It says what condition will execute it: "zero is the answer".
    3. main needs to return a value to your operating system.
    4. f is called more than once.
    5. The function returns a value which you are testing in #2 of this list, and using again in #4 of this list.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Thanks Quzah.
    ...I am trying to follow the logic on paper, having a difficult time, can someone PLEASE just explain the logic for the first line of output so I can understand this once and for all? that will help me alot.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int f(int *a, int *b);
    Prototype a function f which returns an int, has two arguments a and b which are pointers to integers.

    The reason you are prototyping the function is so that the lines after that know about it, because you haven't actually defined the function. You're just saying "Hey, if you see f, this is what it looks like...". Then later on, at the bottom, you're saying, "Ok, here is what f does."


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    That kind of clarifies, but there is really too much going on in that program that I DONT understand, theres an '*a' and an 'a'. Theres a *b and a 'b'. Explaining it without providing an example does not really help me, I am a beginner. Can you please explain the logic behind the output of ONLY the first line of the program and why it is the way it is??

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you're going from a book or are in a class, you need to go back and re-read the section on pointers.
    Code:
    int a;
    int *pa;
    The first declares an integer. It holds a value, such as 5.
    The second declares a pointer to an integer. It holds a value, but the value it holds is a memory address.

    If you want to use a variable that you've declared outside a function, inside the function you're calling, and you want any changes made by that function to affect the original variable, then you need to pass its memory address, instead of the value it holds:
    Code:
    int x = 5;
    ...
    foo( x ); /* pass the value stored in x to foo */
    foo( 5 ); /* the same as the above, with the exception of not getting the 5 from the variable x */
    Compared to:
    Code:
    int x = 5; /* store 5 in an int named x */
    int *px = &x; /* store the memory address of x in px */
    ...
    bar( px ); /* pass the value in px to bar */
    bar( &x ); /* pass the address of x to bar */
    Those two lines have the same effect.

    Everything you pass to a function in C is just a value. What that value represents, determines what you can do with it. Just like the difference between a float and an int.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  2. Hopefully simple question, input streams
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2006, 01:59 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Large code Simple Question need help
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 10-25-2002, 07:55 PM
  5. simple input and string manipulation question
    By Stig in forum C Programming
    Replies: 1
    Last Post: 12-15-2001, 01:33 PM