Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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
 

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
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.
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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
 

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
Right. The order of declaration of the integers has absolutely no bearing on the later operations you do with them.
 

shaggy.mac

macrumors newbie
Apr 8, 2008
11
0
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


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...
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
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.
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.