PDA

View Full Version : ObjectiveC quick question




larswik
Apr 8, 2008, 02:13 AM
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
Apr 8, 2008, 02:25 AM
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:
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
Apr 8, 2008, 03:42 AM
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
Apr 8, 2008, 03:53 AM
Right. The order of declaration of the integers has absolutely no bearing on the later operations you do with them.

shaggy.mac
Apr 8, 2008, 09:29 AM
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
Apr 8, 2008, 09:40 AM
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.:

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
Apr 8, 2008, 09:41 AM
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
Apr 8, 2008, 12:44 PM
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

SC68Cal
Apr 8, 2008, 02:32 PM
I know this is a fringe case


Let's not scare him too soon. K&R introduces pointers around what, Ch. 5?

larswik
Apr 8, 2008, 03:27 PM
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