ObjectiveC quick question

Discussion in 'Mac Programming' started by larswik, Apr 8, 2008.

  1. macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    Learning the program and had a quick question that they didn't explain. This code works fine I am just understanding it a little better.

    #import <stdio.h>

    int main(int argc, char *argv[])
    {
    int value1, value2, sum;

    value1 = 50;
    value2 = 25;

    sum = value1 + value2;

    printf ("the sum of %i and %i is %i\n", value1, value2, sum);

    return 0;
    }



    NOW in the printf line they use 3 %i. I know those are the integers for the 3 I set up. My question is how do they know which ones they are? In the MAIN I establish 3 integers VALUE1, VALUE2, SUM. Is it using those in the order that I write them or is it taking them from the second part of the PRINTF line where I have them separated by a comma?

    In short, I am trying to understand how each %i knows which integer it is assigned to? I hope that is a clear explanation.

    Thanks,

    -Lars
     
  2. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #2
    Each '%i' you have in your formatting line expects one (integer, in this case) parameter to be passed to it. In your case, the first %i will be replaced with value1 since that is the first integer parameter, the second %i with value 2, and so on.

    If you had changed it around as such:
    Code:
    printf ("sum: %i\nvalue1: %i\nvalue2: %i", sum, value1, value2);
    ...then the first %i would be replaced with sum, the second with value1, etc.
     
  3. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    OK, I think I understand. So when I declare the integers it does not matter what order I write them in. The order of %i is how they are listed in the printf?

    2 examples:

    printf ("cow: %i\nduck: %i\nbird: %i", cow, duck, bird);

    printf ("bird: %i\ncow: %i\nduck: %i", bird, cow, duck);

    is that right?

    -Lars
     
  4. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #4
    Right. The order of declaration of the integers has absolutely no bearing on the later operations you do with them.
     
  5. macrumors newbie

    Joined:
    Apr 8, 2008
    #5

    Just so you know

    NSLog() works the same way

    Example

    NSLog(@"Cow: %i Bird: %i Duck: %i", cow, duck, bird);

    You would use NSLog to try to help yourself when debugging your applications but its not limited to just debugging...
     
  6. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    While this isn't really related to the order things are declared, the only reason you might be careful with declaration order is if you are initializing things based on the value of a previous declared value, i.e.:

    Code:
    void function(int val) {
      int a = val+1;
      int b = a*2;
      int *c = &b;
    
      printf("Value of a: %d\tValue of b: %d\tAddress in c: %p\n",a,b,c);
    }
    I know this is a fringe case, but obviously here you could not declare b first since you're using a in the expression used for initialization. You could rework the arithmatic for a and b, but definitely in the case of c you need to have b declared before you get its address.

    -Lee
     
  7. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #7
    He can't use NSLog() unless he has imported and linked in the Foundation framework because NSLog is declared in NSObjCRuntime.h.

    Just stick to printf() for simple C++ programs.
     
  8. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #8
    Thanks! I am only on page 24 so the other stuff is a little out of my grasp at the moment. I want to make sure what I read I understand before I move on, that is why this board is great!

    Thanks,

    -Lars
     
  9. macrumors 68000

    Joined:
    Feb 23, 2006
    #9
    Let's not scare him too soon. K&R introduces pointers around what, Ch. 5?
     
  10. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #10
    The Book I am reading is called Programming in Objective-C by Stephen G. Kochan. I started to learn Java a couple of years ago but had to stop when mom got sick. Much of what I have seen in the 23 pages is close to Java in a way.

    -Lars
     

Share This Page