Incremented Expression passing in Turbo C

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
popel
New poster
Posts: 33
Joined: Fri Mar 15, 2002 2:00 am
Location: Dhaka, Bangladesh
Contact:

Incremented Expression passing in Turbo C

Post by popel »

Is it a problem of Turbo C compiler ?

Consider the following code:

[c]
void main()
{
int n,d;

n=5;
printf("%d ",d = ++n + ++n);

n=5;
d = ++n + ++n;
printf("%d ", d);
}
[/c]

both printf should output same value, and it does that in vc++.
but in Turbo C first printf prints 13 and the second one 14.

I disassembled the programs in TC and found the difference, that is, for first case:

Code: Select all

   ;		n=5;
   ;	
	mov	si,5
   ;	
   ;		d = ++n + ++n;
   ;	
	inc	si
	inc	si
	mov	ax,si
	add	ax,si
	mov	di,ax

for the second case:

Code: Select all


   ;		n = 5;
   ;	
	mov	si,5
   ;	
   ;		printf("%d\n",d = ++n + ++n);
   ;	
	inc	si
	mov	ax,si
	inc	si
	mov	dx,si
	add	ax,dx
	mov	di,ax
	push	ax
	.........so on

can anyone tell me, what is the reason ? or is it a bug of tc ?
μδ. ταηνιπ αλ αμιη

CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

Re: Incremented Expression passing in Turbo C

Post by CDiMa »

popel wrote:Is it a problem of Turbo C compiler ?
No
popel wrote:both printf should output same value,
The ANSI C standard doesn't specify that.
popel wrote:can anyone tell me, what is the reason ? or is it a bug of tc ?
Both tc and vc are right. ANSI C says that the behaviour of your code is unspecified and so any result is admissible. Read this thread for a more indepth discussion on the matter.

Ciao!!!

Claudio

Post Reply

Return to “C”