wrong output

V

Vaibhav87

the code is
#define PROD(x) x+x
main()
{
int i=3,j;
j=PROD(i+1);
printf("%d", j);
}

pls tell me the output and how it is so?
 
J

jacob navia

(e-mail address removed) a écrit :
the code is
#define PROD(x) x+x
main()
{
int i=3,j;
j=PROD(i+1);
printf("%d", j);
}

pls tell me the output and how it is so?
Do not cheat. Do your own homework.
 
L

lovecreatesbea...

jacob said:
Roland Csaszar a écrit :
yes, he can't even copy the homework correctly!

The code in the original post compiles and works.

One guy gives some programming truths as follows:
If it compiles, it works.
If it compiles, it's correct.
...

The original code complies with those truths listed on that guy's
homepage. That personal homepage (perhaps includes those truths) was
recommended by the creator of Linux, Linus Torvalds.
 
C

Christopher Benson-Manica

The code in the original post compiles and works.

If by "works" you mean "didn't happen to launch nuclear missiles that
time" then sure. It failed to include a prototype for printf() and
thus invoked UB, in addition to failing to return a value from main.
One guy gives some programming truths as follows:
If it compiles, it works.
If it compiles, it's correct.

Nonsense. Otherwise I could write an operating system in one line of
C code as long as it compiled:

int main(void) {return 0;}
 
F

Fred Kleinschmidt

Christopher Benson-Manica said:
If by "works" you mean "didn't happen to launch nuclear missiles that
time" then sure. It failed to include a prototype for printf() and
thus invoked UB, in addition to failing to return a value from main.


Nonsense. Otherwise I could write an operating system in one line of
C code as long as it compiled:

int main(void) {return 0;}

Well, that compiles. And it works.
Maybe not the way you WANTED it to work, but it does "work".
 
M

Mark McIntyre

the code is
#define PROD(x) x+x
main()
{
int i=3,j;
j=PROD(i+1);
printf("%d", j);
}

pls tell me the output and how it is so?

Macros are quite literally substituted into the code. So you can by
hand rewrite this code to replace PROD with the actual code.

In this case if PROD were defined as
#define PROD(x) x*x
(which would be more logical....) then you will get an interesting
result.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
J

Joe Wright

the code is
#define PROD(x) x+x
main()
{
int i=3,j;
j=PROD(i+1);
printf("%d", j);
}

pls tell me the output and how it is so?

I've taken the liberty to modify your snippet into a C program.

#include <stdio.h>
#define PROD(x) x+x
int main(void)
{
int i = 3, j;
j = PROD(i + 1);
printf("%d\n", j);
return 0;
}

I get 8 as I expect. What do you get? What do you expect?
 
J

Joe Wright

Mark said:
Macros are quite literally substituted into the code. So you can by
hand rewrite this code to replace PROD with the actual code.

In this case if PROD were defined as
#define PROD(x) x*x
(which would be more logical....) then you will get an interesting
result.

Indeed. (3+1+3+1) is 8 as expected. (3+1*3+1) is 7. Huh?
So that's what parentheses are for? ((3+1)*(3+1)) is 16.
 
M

Mark McIntyre

Indeed. (3+1+3+1) is 8 as expected. (3+1*3+1) is 7. Huh?
So that's what parentheses are for? ((3+1)*(3+1)) is 16.

I'm not sure if you're trying to state the bleedin' obvious, or if
you've totally missed the point of my post, or if you think I'm
missing something. I'm not.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
J

Joe Wright

Mark said:
I'm not sure if you're trying to state the bleedin' obvious, or if
you've totally missed the point of my post, or if you think I'm
missing something. I'm not.

Just stating the obvious for others. You hardly ever miss anything.
 
V

Vaibhav87

sorry it's my mistake the macro was
#define PROD (x) x*x
now pls explain me i've stuck on that.
 
K

Kenneth Brody

sorry it's my mistake the macro was
#define PROD (x) x*x

This is why everyone here will tell you "cut and paste the actual code,
rather than (mis)typing it into your post".
now pls explain me i've stuck on that.
From your original post:

[...]
int i=3,j;
j=PROD(i+1);
[...]

What do you expect this line to do? Pretend you're the C preprocessor.
What does this line look like after expanding the macro?

Now, what do you expect the output to be? What output do you get when
you compile and run the program?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Keith Thompson

sorry it's my mistake the macro was
#define PROD (x) x*x
now pls explain me i've stuck on that.

Provide context when posting a followup. Google does this for you;
apparently you're deliberately deleting the context. Read
<http://cfaj.freeshell.org/google/>.

Make some effort to write proper English. Don't use silly little
abbreviations like "pls" for "please". Capitalize the first word of
each sentence, and the word "I". We don't care much about the
occasional spelling or grammatical error, especially if English isn't
your first language, but make some effort to spell things out; this is
a technical discussion forum, not some kind of chat room.

The comp.lang.c FAQ is at <http://www.c-faq.com/>. You've asked
question 10.1.
 
M

Mark McIntyre

sorry it's my mistake the macro was
#define PROD (x) x*x
now pls explain me i've stuck on that.

what is PROD(4+4)?

Write it down by expanding the macro manually, and you will see a
problem.


--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top