473,396 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

wrong output

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?

Sep 7 '06 #1
15 2183
Va*******@gmail.com 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.
Sep 7 '06 #2
Va*******@gmail.com wrote:
the code is
#define PROD(x) x+x
Seems like a funny definition of 'product' :)

--
Roland Csaszar ----------- \\\ /// -------------- +43 316 495 2129
Software Development ------ \\\ /// ----------- http://www.knapp.com
KNAPP Logistics Automation - \\V// - mailto:ro************@knapp.com
Sep 7 '06 #3
Roland Csaszar a écrit :
Va*******@gmail.com wrote:

>>the code is
#define PROD(x) x+x


Seems like a funny definition of 'product' :)
yes, he can't even copy the homework correctly!
Sep 7 '06 #4
jacob navia wrote:
Roland Csaszar a écrit :
Va*******@gmail.com wrote:

>the code is
#define PROD(x) x+x

Seems like a funny definition of 'product' :)
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.

Sep 7 '06 #5
lovecreatesbea...@gmail.com <lo***************@gmail.comwrote:
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;}

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Sep 7 '06 #6

"Christopher Benson-Manica" <at***@ukato.freeshell.orgwrote in message
news:ed**********@chessie.cirr.com...
lovecreatesbea...@gmail.com <lo***************@gmail.comwrote:
>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;}
Well, that compiles. And it works.
Maybe not the way you WANTED it to work, but it does "work".

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
>
--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.

Sep 7 '06 #7
On 7 Sep 2006 05:49:50 -0700, in comp.lang.c , "Va*******@gmail.com"
<Va*******@gmail.comwrote:
>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
Sep 7 '06 #8
Va*******@gmail.com wrote:
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?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Sep 8 '06 #9
Mark McIntyre wrote:
On 7 Sep 2006 05:49:50 -0700, in comp.lang.c , "Va*******@gmail.com"
<Va*******@gmail.comwrote:
>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.
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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Sep 8 '06 #10
On Thu, 07 Sep 2006 21:19:19 -0400, in comp.lang.c , Joe Wright
<jo********@comcast.netwrote:
>Mark McIntyre wrote:
>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.
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
Sep 8 '06 #11
Mark McIntyre wrote:
On Thu, 07 Sep 2006 21:19:19 -0400, in comp.lang.c , Joe Wright
<jo********@comcast.netwrote:
>Mark McIntyre wrote:
>>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.

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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Sep 9 '06 #12
sorry it's my mistake the macro was
#define PROD (x) x*x
now pls explain me i've stuck on that.

Sep 13 '06 #13
"Va*******@gmail.com" wrote:
>
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:Th*************@gmail.com>
Sep 13 '06 #14
"Va*******@gmail.com" <Va*******@gmail.comwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 13 '06 #15
On 13 Sep 2006 06:46:08 -0700, in comp.lang.c , "Va*******@gmail.com"
<Va*******@gmail.comwrote:
>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
Sep 13 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jepsensen | last post by:
Dear Everybody. I got a problem with my cpp code. I'm trying to calculate a very simple Discrete Cosine Transform (DCT), but my c++ code seams to calculate a wrong result. I have just started...
1
by: Qiangning Hong | last post by:
I decide to seperate my data collection routine from my data analysis and storage program to a seperate process, so I try to use the new subprocess model in Python 2.4. The main program spawns...
28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
3
by: mosimu | last post by:
I am using Visual C++ .NET 2003. I have discovered the following problem and did not find any other mention of it on the forums. This code was being run in Debug mode, console application, with...
0
by: Jim Andersen | last post by:
I am using Microsoft.ApplicationBlocks.Data (v 2.0.0.0). I have this parameter array I pass to a stored procedure. The last one is an output parameter. So I did this: Line 1: arParms(8) = New...
16
by: msundaram.visvanathan | last post by:
Hi, i tried to write this small code in C which would: Input :a b c d Output:abcd code: #include<stdio.h>
0
by: arlie_maija | last post by:
Hey - I'm writing a control that contains a DataGrid, and I'm unable to get the update event to fire. When I click the update link, the edit event fires. heres the details... my control...
5
by: Simon Brooke | last post by:
This is supposed to be a very simple XSL stylesheet to strip styling information out of HTML documents - it could not be more basic. And yet, it doesn't work. I'm obviously getting something very...
12
by: broli | last post by:
#include<stdio.h> #include<stdlib.h> struct point { double x, y, z;
27
by: Dave | last post by:
I'm having a hard time tying to build gcc 4.3.1 on Solaris using the GNU compilers. I then decided to try to use Sun's compiler. The Sun Studio 12 compiler reports the following code, which is in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.