expression problem

S

sangeeta chowdhary

Hey guys I am having problem to understand output of this code-

#include<stdio.h>

int main(int j)
{
int i=10,a;
a=i++ / ++i ;
printf("%d. .%d ",a,i);

return 0;
}


As we know postfix ++ has higher precedence than prefix ++ ,and then /
operator.
According to me, i will be incremented to 11 in i++,but 10 will be
used in exp,then ++i should give 12.
so 10/12 will give 0 as integer.
But i am getting 1..12 in gcc.
 
M

Mark Bluemel

Hey guys I am having problem to understand output of this code-

#include<stdio.h>

int main(int j)
{
  int i=10,a;
  a=i++ / ++i ;
  printf("%d. .%d ",a,i);

  return 0;

}

As we know postfix ++ has higher precedence than prefix ++ ,and then /
operator.

Which is about how an expression is to be interpreted (I like to think
of it in terms of where parentheses would be added) but is not
relevant to order of evaluation...
According to me, i will be incremented to 11 in i++,but 10 will be
used in exp,then ++i should give 12.
so 10/12 will give 0 as integer.
But i am getting  1..12 in gcc.

Someone will probably be along shortly to explain why "42" and "a
suffusion of yellow" would be equally valid. Watch out for nasal
demons.

Section 3 of the C FAQ <http://c-faq.com/> is what you should be
reading right now. Question 3.2 in particular.
 
S

Shao Miller

Hey guys I am having problem to understand output of this code-

#include<stdio.h>

int main(int j)
{
  int i=10,a;
  a=i++ / ++i ;
  printf("%d. .%d ",a,i);

  return 0;

}

As we know postfix ++ has higher precedence than prefix ++ ,and then /
operator.
According to me, i will be incremented to 11 in i++,but 10 will be
used in exp,then ++i should give 12.
so 10/12 will give 0 as integer.
But i am getting  1..12 in gcc.
The line:

a=i++ / ++i ;

Is an expression which could result in modification of the value of
'i' twice within the same sequence point bounds. That's undefined by
the C Standard, but can be defined by your implementation, such as GCC.
 
S

Shao Miller

The line:

  a=i++ / ++i ;

Is an expression which could result in modification of the value of
'i' twice within the same sequence point bounds.  That's undefined by
the C Standard, but can be defined by your implementation, such as GCC.
(Minus the semi-colon.)
 
S

Seebs

Hey guys I am having problem to understand output of this code-

No worries.
#include<stdio.h>

int main(int j)

(This is also wrong, and invokes undefined behavior, but that hardly
seems relevant.)
{
int i=10,a;
a=i++ / ++i ;

This is meaningless code. ABSOLUTELY ANY OUTPUT IS ACCEPTABLE.
As we know postfix ++ has higher precedence than prefix ++ ,and then /
operator.

This is totally irrelevant. Precedence is NOT order of evaluation. Since
the "i++" and "++i" are definitely in separate expressions (both are higher
precedence than /), their relative precedence to each other is irrelevant.

What matters is that you have two modifications of the same object (i)
without an intervening sequence point. That means the behavior is undefined.

If the compiler rejected your program, that would be okay.
If the compiler generated code which printed the complete text of the Kama
Sutra, that would be okay.
If the compiler generated code which aborted on execution, that would be
okay.
If the compiler generated code which deleted all your files, that would be
okay.

There are NO REQUIREMENTS on this program. None. It isn't required to
compile. It isn't required to build.

This is a FAQ. Please consider reading the FAQ and then asking questions.

-s
 
S

Stone Zhong

Hey guys I am having problem to understand output of this code-

#include<stdio.h>

int main(int j)
{
  int i=10,a;
  a=i++ / ++i ;
  printf("%d. .%d ",a,i);

  return 0;

}

As we know postfix ++ has higher precedence than prefix ++ ,and then /
operator.
According to me, i will be incremented to 11 in i++,but 10 will be
used in exp,then ++i should give 12.
so 10/12 will give 0 as integer.
But i am getting  1..12 in gcc.

The result is perfect right if we think it in such way:

if an expression statement has i++, then it is equivalent to
{
expression statement;
i = i + 1;
}

if an expression statement has ++i, then it is equivalent to
{
i = i + 1;
expression statement;
}

now we have expression_statement := "a=i++ / ++i ;"

so it becomes
{
"a=i/++i;"
i = i + 1;
}

and latter becomes
{
i = i + 1;
"a=i/i"
i = i + 1;
}

so a is 1
 
S

Shao Miller

Stone said:
The result is perfect right if we think it in such way:

if an expression statement has i++, then it is equivalent to
{
expression statement;
i = i + 1;
}
It might have the same side effect, but is that equivalence defined
somewhere?
if an expression statement has ++i, then it is equivalent to
{
i = i + 1;
expression statement;
}
Except for the missing parentheses and the right-most 'i' is defined to
only be evaluated once (it would be anyway).
now we have expression_statement := "a=i++ / ++i ;"

so it becomes
{
"a=i/++i;"
i = i + 1;
}

and latter becomes
{
i = i + 1;
"a=i/i"
i = i + 1;
}

so a is 1
I think it might be equivalent to:

a = i++ / (i = i + 1);

Even if we grant it to be:

a = i = i + 1 / (i = i + 1);

We are modifying 'i' twice, which isn't allowed. :(
 
E

Eric Sosman

[...]
if an expression statement has i++, then it is equivalent to
{
expression statement;
i = i + 1;
}

if an expression statement has ++i, then it is equivalent to
{
i = i + 1;
expression statement;
}

No, it is not equivalent. The second version has a sequence
point that is not present in the first.
now we have expression_statement := "a=i++ / ++i ;"
[...]
so a is 1

No. The FAQ covers this; please acquaint yourself with the
FAQ before posting again. <http://www.c-faq.com/> -- you will
learn much from it that you (clearly) do not yet know.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top