Advertising (This ad goes away for registered users. You can Login or Register)

[Tutorial] Introduction to programming using C (VI)

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Wdingdong
Posts: 92
Joined: Tue Aug 02, 2011 5:34 pm

Re: [Tutorial] Introduction to programming using C (VI)

Post by Wdingdong » Mon Jun 04, 2012 1:47 pm

Yeay :D
EDIT:
I have a doubt in this program.

Code: Select all

int x = 15 ;
printf ( "\n%d %d %d", x != 15, x = 20, x < 30 ) ;
Output,
According to me: 0 20 1
Actually: 1 20 1

Similarly,

Code: Select all

int k = 35 ;
printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ;
Output,
According to me: 1 50 1
Actually: 0 50 0

So, How does it work to have a output like that?
Advertising
// Big thanks to people who share information !!!

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VI)

Post by m0skit0 » Mon Jun 04, 2012 2:35 pm

First of all, you should not do such assignations in printf() because it's a bad practice. The results you're getting are because the assignations are evaluated first.

EDIT: right to left actually... :roll:
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

JJS
Big Beholder
Posts: 1416
Joined: Mon Sep 27, 2010 2:18 pm
Contact:

Re: [Tutorial] Introduction to programming using C (VI)

Post by JJS » Mon Jun 04, 2012 2:37 pm

Wdingdong wrote:So, How does it work to have a output like that?
Looks like the line is evaluated from right to left. When writing actual code I would not rely on such edge cases. The standard might specify the correct behavious in this case (or it might leave it open to the implementation).

Fake edit: m0skit0 beat me to it, but I am posting anyway...

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VI)

Post by m0skit0 » Mon Jun 04, 2012 2:38 pm

I also thought they were right to left, but look closer at second.

EDIT: mondays... it's right to left actually...
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

JJS
Big Beholder
Posts: 1416
Joined: Mon Sep 27, 2010 2:18 pm
Contact:

Re: [Tutorial] Introduction to programming using C (VI)

Post by JJS » Mon Jun 04, 2012 2:46 pm

I just remembered something. On architectures that pass the parameters on the stack, the caller has to push them onto the stack from right to left so that the format string is the first one that the callee pops. This is necessary so that it knows how many parameters are passed (by analyzing the format string). This makes me wonder if all functions that expect a variable number of arguments get their parameters through the stack regardless of platform? Would make sense to me...

Wdingdong
Posts: 92
Joined: Tue Aug 02, 2011 5:34 pm

Re: [Tutorial] Introduction to programming using C (VI)

Post by Wdingdong » Mon Jun 04, 2012 2:55 pm

Oh, I never knew that execution can be from right to left. But, normally it is always executed left to right. Like in,

Code: Select all

int a = 5, b, c ;
	b = a = 15 ;
	c = a < 15 ;
	printf ( "\na = %d b = %d c = %d", a, b, c ) ;
It is left to right and I get output 15 15 0, i.e. left to right. So, why does only that two programs take it right to left way?
And, actually, this is a 'Guess the Output' program from a book.
Thanks to both of you( m0skit0 and JJS)
// Big thanks to people who share information !!!

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VI)

Post by m0skit0 » Mon Jun 04, 2012 3:07 pm

JJS wrote:I just remembered something. On architectures that pass the parameters on the stack, the caller has to push them onto the stack from right to left so that the format string is the first one that the callee pops
True. I also thought about this, but though "evaluations are not passed to the stack"... Mondays... Well their results are, indeed xD. This is probably why.
JJS wrote: This makes me wonder if all functions that expect a variable number of arguments get their parameters through the stack regardless of platform?
Probably, although I would expect different behavior depending on the architecture.
Wdingdong wrote: I never knew that execution can be from right to left
The execution is not right to left. Those are parameters, and not instructions.
Wdingdong wrote:Like in,

Code: Select all

    int a = 5, b, c ;
       b = a = 15 ;
       c = a < 15 ;
       printf ( "\na = %d b = %d c = %d", a, b, c ) ;
It is left to right and I get output 15 15 0, i.e. left to right. So, why does only that two programs take it right to left way?
Here left to right or right to left is the same, since evaluating cba or abc has the same result. Keep in mind we're talking about the parameters, and not the format string or the previous instructions.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Wdingdong
Posts: 92
Joined: Tue Aug 02, 2011 5:34 pm

Re: [Tutorial] Introduction to programming using C (VI)

Post by Wdingdong » Tue Jun 05, 2012 4:26 am

What I understood is, it depends upon in which order parameters are passed, CMIIW(Correct Me If I'm Wrong).
Can it change from program to program?
And,
m0skit0 wrote: since evaluating cba or abc has the same result
I didn't understand this.
// Big thanks to people who share information !!!

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VI)

Post by m0skit0 » Tue Jun 05, 2012 6:02 am

Wdingdong wrote:What I understood is, it depends upon in which order parameters are passed, CMIIW(Correct Me If I'm Wrong).
Can it change from program to program?
I don't think it will change, but it's irrelevant. As I said before: you don't have to program like that. That's confusing, unpredictable, hard to read, not maintainable, subject to architecture ABI... Just don't do it.
Wdingdong wrote:I didn't understand this.

Code: Select all

printf ( "\na = %d b = %d c = %d", a, b, c ) ;
Evaluating a, b, c right to left or left to right is the same. But it's not the same evaluating x != 15, x = 20, x < 30 right to left than left to right. Hope this clears it! :)
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

JJS
Big Beholder
Posts: 1416
Joined: Mon Sep 27, 2010 2:18 pm
Contact:

Re: [Tutorial] Introduction to programming using C (VI)

Post by JJS » Tue Jun 05, 2012 6:55 am

Interesting stuff: On x86 (Windows) where parameters are passed on the stack, you get the right-to-left output "1 20 1". On ARM (GNU Linux EABI) you get left-to-right "0 20 1". Parameters are passed in registers there. Things surely get even more confusing and hard to predict when you use more parameters so that some of them are passed in registers and others on the stack :?.

Bottom line: Code like this is not portable.

Post Reply

Return to “Programming and Security”