473,386 Members | 1,958 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,386 software developers and data experts.

?output

jw
what is its output and why?
#include<iostream>

using namespace std;
#define abs(x) (x>0 ? x:-x)
void main()
{
int a=3,b=5;
cout<<abs(abs(a+b));
}

Feb 16 '06 #1
13 1564
"jw" <ja*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
: what is its output and why?
: #include<iostream>
:
: using namespace std;
:
:
: #define abs(x) (x>0 ? x:-x)
: void main()
: {
: int a=3,b=5;
: cout<<abs(abs(a+b));
: }

We don't do homework.
Try substituting the macros, and see what happens.

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Feb 16 '06 #2
jw wrote:
#include<iostream>

using namespace std;
#define abs(x) (x>0 ? x:-x)
void main()
{
int a=3,b=5;
cout<<abs(abs(a+b));
}


This program is not well-formed and should not compile in the first
place. If it compiles without diagnostic your compiler is broken and
thus its output is not a reliable source of information.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 16 '06 #3
jw wrote:
what is its output and why?
#include<iostream>

using namespace std;
#define abs(x) (x>0 ? x:-x)
void main()
{
int a=3,b=5;
cout<<abs(abs(a+b));
}


What do you think the output is and why?

Read the FAQ 5.2.
Feb 16 '06 #4
jw

Dietmar Kuehl wrote:
jw wrote:
#include<iostream>

using namespace std;
#define abs(x) (x>0 ? x:-x)
void main()
{
int a=3,b=5;
cout<<abs(abs(a+b));
}

it is not a homework am on holiday and studying c++,this code gives 8
as a result but i think it must give 2 i couldnt understand this

Feb 16 '06 #5
jw wrote:
Dietmar Kuehl wrote:
jw wrote:
#include<iostream>

using namespace std;
#define abs(x) (x>0 ? x:-x)
void main()
{
int a=3,b=5;
cout<<abs(abs(a+b));
}

it is not a homework am on holiday and studying c++,this code gives 8
as a result but i think it must give 2 i couldnt understand this


8 looks about right to me.

On paper, do a+b
then apply abs.
then apply abs.

What should "abs" conceptually do?
What does your abs above do?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 16 '06 #6
jw
aww sorry it is (a-b) and gives 8

#define abs(x) (x>0 ? x:-x)
void main()
{
int a=3,b=5;
cout<<abs(abs(a-b));
}

Feb 16 '06 #7
jw wrote:
Dietmar Kuehl wrote:
jw wrote: [...]
> void main()
> { [...] > }
it is not a homework am on holiday and studying c++,this code gives 8
as a result but i think it must give 2 i couldnt understand this


Maybe you should start of studying English first: the above is not
proper sentence. Of course, it doesn't claim to be as all periods
are absent. Second, note that I haven't claimed that it is a
homework assignment. I claimed that your code or even the above
excerpt is illegal C++ which is backed up by basic.start.main
(3.6.1), paragraph 2, second sentence which reads:

It shall have a return type of type int, [...]

The "It" refers to "main()". The stuff following the comma is
irrelevant to the return type of 'main()'. Your turn!

Apparently, you are more interested in the effect of your macros
than the answer to your apparent question (well, strictly speaking
there was no question; I assumed you would be interested in the
output of the program and as I stated, the C++ standard makes no
statement about the output of this malformed program). With
respect to your macros, the actual answer is this: Why bother?
There are uses for macros in C++ but this is none of them. Use a
[n inline] function instead!
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 16 '06 #8
jw wrote:
aww sorry it is (a-b) and gives 8

#define abs(x) (x>0 ? x:-x)
void main()
{
int a=3,b=5;
cout<<abs(abs(a-b));
}


compare with:

#include <iostream>

#define abs(x) (x>0 ? x:-x)
int main() {
int a=3,b=5;
std::cout<<abs((abs((a-b))));
}

macros are evil. Just use an inline function.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 16 '06 #9
jw
i know i can use inline func. but i saw this code in a
competition,output was asked and many of the competitors said 2 but the
ans. is 8 ?

Feb 16 '06 #10
jw wrote:
aww sorry it is (a-b) and gives 8

#define abs(x) (x>0 ? x:-x)
void main()
{
int a=3,b=5;
cout<<abs(abs(a-b));
}


This program is still broken...

Of course, the macro use results in an expression evaluating to
"8"! What else should be result? Just write replace the macro
with the corresponding expression or, even better, have the
compiler do it for, usually using the "-E" switch. For example,
with gcc you can look at the preprocessed output using something
like

g++ -E tst.cpp

if you store the above code in tst.cpp (you will probably want
to omit the include directive to get concise output...).
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 16 '06 #11
jw wrote:
i know i can use inline func. but i saw this code in a
competition,output was asked and many of the competitors said 2 but the
ans. is 8 ?


Of course it is "8" (assuming the program is correct to have a
return type of 'int' for 'main()'). Macros just to textual
replacement and do not in any way affect operator precedence of
the resulting expression. Just do the textual replacement and you'd
see that the result is effectively "-(-a-b)" which, of course,
evaluates to "8" if "a" is "3" and "b" is "5".
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 16 '06 #12
jw wrote:
aww sorry it is (a-b) and gives 8

#define abs(x) (x>0 ? x:-x)
void main()
{
int a=3,b=5;
cout<<abs(abs(a-b)); abs(a-b) expands to (a-b>0 ? a-b:-a-b)

abs(abs(a-b)) expands to

(a-b>0 ? a-b:-a-b)>0 ? (a-b>0 ? a-b:-a-b):-(a-b>0 ? a-b:-a-b) }


quite simply this is obviously a mess and not what was intended
but does give the result 8. Perhaps now you'll see why macros can
cause so much grief and should generally be avoided.

JB
Feb 16 '06 #13
...and if you still insist on using macros, follow macro rule no. 2:
Always enclose your args in parentheses!
so instead of:
#define abs(x) (x>0 ? x:-x)
use:
#define abs(x) ((x)>0 ? (x):-(x))

-80

Feb 17 '06 #14

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

Similar topics

4
by: Mark Wilson CPU | last post by:
This must be easy, but I'm missing something... I want to execute a Perl script, and capture ALL its output into a PHP variable. Here are my 2 files: -------------------------------------...
3
by: edgekaos | last post by:
Is method 2 valid? Method 1: wstring input = L"STRING"; wstring output = input; transform(output.begin(), output.end(), output.begin(), towupper); Method 2: wstring input = L"STRING";...
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
24
by: kalamantina | last post by:
#include "stdafx.h" #include <stdio.h> #define output( x ) printf( #x "\r\n" );fflush( stdout ) class CMyBase { public: CMyBase() { output( CMyBase() ); f(*this);
0
by: newbie | last post by:
i'm a newbie of c language. can anyone help me to implement the code so that I can get the ciphertext from the output. thanks. #ifndef _3DES_H #define _3DES_H #ifndef uint8 #define uint8 ...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
3
by: MatsL | last post by:
Hi, This is seriously driving me crazy, could anyone explain to me why neither of these doesn't produce XHTML compliant output (it is being called in Render() btw): output.WriteLine("<img...
3
by: undshan | last post by:
I am writing a code that needs to open a file, create an output file, run through my function, prints the results to the output file, and closes them within a loop. Here is my code: #include...
5
by: amit.uttam | last post by:
Hey everyone, I've recently jumped big time into python and I'm working on a software program for testing automation. I had a question about proper logging of output. What I would like is: 1....
2
by: gabosom | last post by:
Hi! I've been breaking my head trying to get the output variables from my Stored Procedure. This is my SP code CREATE PROCEDURE GetKitchenOrderDetail( @idService int, --outPut Variables ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.