3 probs ...i m confused

N

nash_r

hi,
i have 3 probs dats getting me all confused.
i m using Turbo C compiler.

1. #define PRODUCT(x) (x*x)
main()
{
int i=3,j;
j=PRODUCT(i+1);
printf("\n%d",j);
}

the prob is the output.I can't understand the logic behind it's
output.

2. #define PRODUCT(x) (x*x)
main()
{
int i=3,j,k;
j=PRODUCT(i++);
k=PRODUCT(++i);
printf("\n%d%d",j,k);
}
the prob in this case is also the output.
in the 'j' case the output come to be 9 and in the 'k' case it comes
to 49.
the quest. is y is the compiler adding it all up after the macro in
case of 'j' and twice b4 the operation in case of 'k' giving an output
of 49.

3. in the same prob as mentioned above if we replace the i++ by i++ in
case of 'j' it shows an error statin "Lvalue required" at compile
time.And if we use the following code

#define PRODUCT(x) (x*x)
main()
{
int i=3,j;
j=PRODUCT(++++i);
printf("\n%d",j);
}
the output observed is 35.

how is the operation proceeding and y is it saying Lvalue required in
case we use i++++ and y 35 (which seems arising from 5 and 7)coming
when according to conventional wisdom it shld be 49 (refering from
prob 2 stated above).

thnks
 
J

Joona I Palaste

nash_r said:
hi,
i have 3 probs dats getting me all confused.
i m using Turbo C compiler.
1. #define PRODUCT(x) (x*x)
main()
{
int i=3,j;
j=PRODUCT(i+1);
printf("\n%d",j);
}

the prob is the output.I can't understand the logic behind it's
output.

PRODUCT(i+1) expands to (i+1*i+1) which is parsed as:
( i + 1*i + 1 )
i.e. add the following numbers together: (i) (1*i) (1), which is the
same as 2*i + 1. If i==3, then this is 2*3 + 1, which is 6 + 1, which
is 7.
2. #define PRODUCT(x) (x*x)
main()
{
int i=3,j,k;
j=PRODUCT(i++);
k=PRODUCT(++i);
printf("\n%d%d",j,k);
}
the prob in this case is also the output.
in the 'j' case the output come to be 9 and in the 'k' case it comes
to 49.
the quest. is y is the compiler adding it all up after the macro in
case of 'j' and twice b4 the operation in case of 'k' giving an output
of 49.

These macro expansions cause undefined behaviour by expanding into
(i++*i++) and (++i*++i), which modify the same variable multiple times
without an intevening sequence points. Therefore the output might be
9, 49, 1234567, -346.643, "Your mother was a hamster and your father
smelt of elderberries!", or your hard drive catching fire, and all this
would be legal according to the C standard.
3. in the same prob as mentioned above if we replace the i++ by i++ in
case of 'j' it shows an error statin "Lvalue required" at compile
time.And if we use the following code

#define PRODUCT(x) (x*x)
main()
{
int i=3,j;
j=PRODUCT(++++i);
printf("\n%d",j);
}
the output observed is 35.

++++i is a constraint violation, so all bets are off. I'm surprised
you even got that to compile.
how is the operation proceeding and y is it saying Lvalue required in
case we use i++++ and y 35 (which seems arising from 5 and 7)coming
when according to conventional wisdom it shld be 49 (refering from
prob 2 stated above).

This is because the operand of the ++ operator(s) is required to be an
lvalue, which i++ or ++i are not. An lvalue is more or less anything
that can be assigned to. Your compiler is using non-standard extension
semantics. The code in the latter examples is invalid according to the
C standard.

All of your questions would have been answered by reading a good
introductory-level C textbook. Can't you find one?

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
 
A

Andreas Kahari

nash_r wrote: said:
1. #define PRODUCT(x) (x*x)
main()
{
int i=3,j;
j=PRODUCT(i+1);
printf("\n%d",j);
}

"PRODUCT(i+1)" expands to "i+1*i+1" which is "i+i+1".

Define your macro as

#define PRODUCT(x) ((x)*(x))

But call it SQUARE or something that describes what it's really
doing. PRODUCT is misleading.

2. #define PRODUCT(x) (x*x)
main()
{
int i=3,j,k;
j=PRODUCT(i++);
k=PRODUCT(++i);
printf("\n%d%d",j,k);
}

"PRODUCT(i++)" expands to "i++*i++" which is something you don't
want to have anywhere near a C program code at all.

See the C FAQ at

http://www.eskimo.com/~scs/C-faq/q3.2.html


[cut]
3. in the same prob as mentioned above if we replace the i++ by i++ in
case of 'j' it shows an error statin "Lvalue required" at compile
time.And if we use the following code

#define PRODUCT(x) (x*x)
main()
{
int i=3,j;
j=PRODUCT(++++i);


That's just illegal.
 
D

Default User

nash_r said:
#define PRODUCT(x) (x*x)
main()
{
int i=3,j;
j=PRODUCT(++++i);
printf("\n%d",j);
}


That's funny. I tried to compile this with Turbo C, and received the
following diagnostics:

#include <stdio.h>

#define PRODUCT(x) (x*x)
main()
{
int i=3,j;
j=PRODUCT(++++i);
printf("\n%d",j);
}

Compiling
D:\TC\NONAME.C:
Error D:\TC\NONAME.C 7: Lvalue required in function
main
Error D:\TC\NONAME.C 7: Lvalue required in function
main
Warning D:\TC\NONAME.C 8: Possible use of 'j' before definition in
function main
Warning D:\TC\NONAME.C 9: 'i' is assigned a value which is never used in
function main




Brian Rodenborn
 
M

Mark McIntyre

hi,
i have 3 probs dats getting me all confused.
i m using Turbo C compiler.

1. #define PRODUCT(x) (x*x)
main()
{
int i=3,j;
j=PRODUCT(i+1);

this is a classic macro mistake. Write out the expansion by hand, and
you;ll see it.

ALWAYS bracket macro arguments
#define PRODUCT(x) ((x)*(x))
j=PRODUCT(++++i);

now you have Undefined Behaviour. You can't apply a pre-increment
operator to a pre-increment operator, because the result of ++ is not
an lvalue. An lvalue can be thought of something that would go on the
left of an assignment.

If your compiler didn't complain about this, its probably because you
confused it.
 
P

Peter Shaggy Haywood

Groovy hepcat nash_r was jivin' on 10 Oct 2003 03:01:15 -0700 in
comp.lang.c.
3 probs ...i m confused's a cool scene! Dig it!
1. #define PRODUCT(x) (x*x)

#include said:

int main(void)
{
int i=3,j;
j=PRODUCT(i+1);
printf("\n%d",j);

return 0;
}

the prob is the output.I can't understand the logic behind it's
output.

It's very simple. The expression j=PRODUCT(i+1) expands to
j=(i+1*i+1). This is equivalent to j = i + i + 1, or j = 3 + 3 + 1,
or, of course, j = 7.
There are two things you need to watch out for when you use
function-like macros. First, make sure arguments in the expansion text
are surrounded by parentheses so that adjacent expressions are not
parsed incorrectly. For example:

#define PRODUCT(x) ((x)*(x))

Second, be careful of using arguments more than once in the
expansion text. If the expression passed to the macro has side
effects, there may be strange results - even undefined behaviour,
which is what you are seeing below.
2. #define PRODUCT(x) (x*x)

#include said:

int main(void)
{
int i=3,j,k;
j=PRODUCT(i++);
k=PRODUCT(++i);
printf("\n%d%d",j,k);

return 0;
}
the prob in this case is also the output.

Now, this one causes undefined behaviour. The expression
j=PRODUCT(i++) expands to j=(i++*i++), which invokes undefined
behaviour by modifying an object more than once without an intervening
sequence point. Similarly, the expression k=PRODUCT(++i) also causes
undefined behaviour.
in the 'j' case the output come to be 9 and in the 'k' case it comes
to 49.
the quest. is y is the compiler adding it all up after the macro in
case of 'j' and twice b4 the operation in case of 'k' giving an output
of 49.

Mu.
3. in the same prob as mentioned above if we replace the i++ by i++ in
^^^^^^^^^^^^^^^^^^^^^^
Huh?
case of 'j' it shows an error statin "Lvalue required" at compile
time.And if we use the following code

#define PRODUCT(x) (x*x)

#include said:

int main(void)
{
int i=3,j;
j=PRODUCT(++++i);
printf("\n%d",j);

return 0;
}
the output observed is 35.

No, it isn't. This will not even compile. (Well, technically,
theoretically, it might. As long as the error is diagnosed, the
compiler can do whatever it likes with this broken code. But in the
real world, compilers will cease compilation.) The expression ++++i
parses as ++(++i), which, of course, is nonsensical. You cannot
increment a non-lvalue. (An lvalue is an object. i is an object, and
therefore an lvalue. But ++i is not an object, and therefore not an
lvalue. Thus it cannot be applied to the ++ operator again.)
Similarly, i++++ is an error.
how is the operation proceeding and y is it saying Lvalue required in
case we use i++++ and y 35 (which seems arising from 5 and 7)coming
when according to conventional wisdom it shld be 49 (refering from
prob 2 stated above).

Mu.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

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
474,085
Messages
2,570,597
Members
47,218
Latest member
GracieDebo

Latest Threads

Top