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

increment evaluation

hii,
plz help to knw how this prg wrk in detail
#define CUBE(x) (x*x*x)
main()
{
int a,b=3;
a=CUBE(b++);
printf("%d%d",a,b);
}

Oct 12 '07 #1
13 1701
pr********@gmail.com said:
hii,
plz help to knw how this prg wrk in detail
It doesn't.
#define CUBE(x) (x*x*x)
main()
{
int a,b=3;
a=CUBE(b++);
See the FAQ: http://c-faq.com/cpp/safemacros.html

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 12 '07 #2
On Oct 12, 12:03 am, pradeep...@gmail.com wrote:
hii,
plz help to knw how this prg wrk in detail

#define CUBE(x) (x*x*x)
main()
{
int a,b=3;
a=CUBE(b++);
printf("%d%d",a,b);
}
That is an example of undefined behavior (for several reasons).
You have a varadic function (printf) with no protype in scope.
You have a function macro which is given an argument used more than
once which is incremented causing several updates with no intervening
sequence point.

Probably, you want something like this:

#include <stdio.h>

static long CUBE (const long x)
{
printf("%d * %d * %d ", x, x, x);
return (x * x * x);
}

int main (void)
{
long a;
int b = 3;
a = CUBE(b++);
printf (" = %ld\n", a);

a = CUBE(b);
printf (" = %ld\n", a);

return 0;
}

Oct 12 '07 #3
On Fri, 12 Oct 2007 00:03:17 -0700,
pr********@gmail.com <pr********@gmail.comwrote:
hii,
plz help to knw how this prg wrk in detail
Please, in the future, make an attempt to spell correctly. The little
bit of effort you save in typing, if any, means all your readers have to
work a little bit harder to parse your message. It doesn't look cool
either. It looks silly.
#define CUBE(x) (x*x*x)
main()
{
int a,b=3;
a=CUBE(b++);
printf("%d%d",a,b);
}
This program invokes undefined behaviour, for several reasons. Please
see the C FAQ at c-faq.com, for example, and read questions 3.2, 10.1,
11.35, 15.21, and then read the whole thing. It's really worth the time.

Martien
--
|
Martien Verbruggen |
| In a world without fences, who needs Gates?
|
Oct 12 '07 #4
<pr********@gmail.comschrieb im Newsbeitrag
news:11**********************@v23g2000prn.googlegr oups.com...
hii,
plz help to knw how this prg wrk in detail
This style of writing isn't received well here... this is a news group and
not a chat room
#define CUBE(x) (x*x*x)
main()
{
int a,b=3;
a=CUBE(b++);
printf("%d%d",a,b);
}
It invokes Undefined Behavoir because
a) you don't provide a prototype for a varadic funtion, printf() e.g. by
#include <stdio.h>
b) the output you intend to generate doesn't end with a linefeed ('\n'), so
your program may or may not produce any output
c) the macro CUBE has side effects that modify an operator several times
without a sequence point.

I guess your real question is about the macro CUBE
That macro is expanded by the preprocesser, so your compiler actually sees:

a=(b++*b++*b++);

When b gets incremented is up to the implementation as long as it happens
before the next sequence point.
On implementation I tried this on came up with "606" (i.e a=60 or 3*4*5 and
b=6), another with "276" (i.e. a=27 or 3*3*3 and b=6) and both are right.

Also you shouldn't use main() but rather int main(void) and also end it with
a return 0; (or EXIT_SUCCESS or EXIT_FAILURE from <stdlib.h>)

Bye, Jojo
Oct 12 '07 #5
pr********@gmail.com wrote:
hii,
plz help to knw how this prg wrk in detail

#define CUBE(x) (x*x*x)
main()
{
int a,b=3;
a=CUBE(b++);
printf("%d%d",a,b);
}
Tht prg ds nt wrk t ll; t hs t lst tw xmpls f "ndfnd bhvr". Y ddn't #ncld
<std.hs th cll t prntf sn't dfnd. ls, th xprssn `CUB(b++)` xpnds t
`b++ * b++ * b++`, whch vlts /rqrmnt/ tht n xprssn wrts t mst nc t
vrbl btn sqnc pnts; fr dtls s th FQ.

--
Chrs "aooeooaaiaalepeae ..." Dlln

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Oct 12 '07 #6
pr********@gmail.com wrote:
hii,
plz help to knw how this prg wrk in detail
#define CUBE(x) (x*x*x)
main()
{
int a,b=3;
a=CUBE(b++);
printf("%d%d",a,b);
}
Others have said that this program causes undefined behaviour. What does
this mean? It means Bad Things.

The C programming language is defined by a set of standards published by
ISO. The main ones are C89 (the first one), and C99 (the most recent).
The standard describes what a valid C compiler (or to use the Standard's
language, "implementation") must do. In certain places, the standard
gives the implementation a range of options - e.g. int can be any size,
as long as it's 16 bits or more.

For a program which causes undefined behaviour (UB), the standard places
no restrictions on the implementation. This means that the
implementation can do *anything it likes* when it encounters UB. What
this means is that a compiler which is in every way a working and
correct C compiler can fail to compile your code - or worse, compile
your code into something with bugs in it. Given there are hundreds (if
not thousands) of C implementations out there, this is a Bad Thing.

Of course, you may find that *your* compiler works exactly as you expect
it to. This is allowed by the Standard, because it places no
restrictions on the compiler. But one of the reasons for writing in C is
that there are many different C compilers on many different types of
computer, and if you do not invoke UB then the Standard guarantees your
program will work on *all* of these compilers without needing a rewrite.
This is called "portability". If you rely on quirks of your own
compiler, it may not compile on any other.

Why would you want to write code that will compile on any C compiler?
Well, it might be nice if you could give (or sell!) your nice Windows
program to Linux users at no extra cost to yourself. Another reason is
that if you write C code that works on lots of C compilers, then users
of all those compilers will know what you mean and will be able to help.
If you write C code which only works on your compiler, then you cut
yourself off from that expertise, because users of other compilers are
not familiar with the quirks of your compiler.

There are sometimes good reasons for writing code for which the Standard
does not define correct behaviour. This includes using system-specific
features such as graphics, networks, when standard C code to achieve
what you want doesn't exist. However in this situation standard C code
to do what you want *does* exist, so you should write standard C.

--
Philip Potter pgp <atdoc.ic.ac.uk
Oct 12 '07 #7
Martin Ambuhl <ma*****@earthlink.netwrites:
pr********@gmail.com wrote:
>hii,
plz help to knw how this prg wrk in detail

Given that you are so sloppy with your writing that "hii", "plz",
"knw", "prg", and "wrk" are all childish gibberish, it is unlikely
that you have enough respect for a language to ever be a competent
programmer.

Of course, your program doesn't work at all, so it makes no sense to
explain in detail how it works. It has undefined behavior.
What IS wrong with you people? You *know* others have already pointed
this out.
Oct 12 '07 #8
On Oct 12, 12:03 pm, pradeep...@gmail.com wrote:
hii,
plz help to knw how this prg wrk in detail

#define CUBE(x) (x*x*x)
main()
{
int a,b=3;
a=CUBE(b++);
printf("%d%d",a,b);

}- Hide quoted text -

- Show quoted text -
Be alert - Some books have solutions for this and it is a
famous question in few C tests. But , in reality this cannot
have a defined solution.

Ans - Undefined Behaviour.

The Macro CUBE has side effects that modify an operator several times
without a sequence point.

Refer C-Faq and Kernighan & Ritchie books for the correct
answer.

Karthik Balaguru

Oct 12 '07 #9
karthikbalaguru wrote:
On Oct 12, 12:03 pm, pradeep...@gmail.com wrote:
>hii,
plz help to knw how this prg wrk in detail

#define CUBE(x) (x*x*x)
main()
{
int a,b=3;
a=CUBE(b++);
printf("%d%d",a,b);

Be alert - Some books have solutions for this and it is a
famous question in few C tests. But , in reality this cannot
have a defined solution.

Ans - Undefined Behaviour.

The Macro CUBE has side effects that modify an operator
variable.
several times without a sequence point.

Refer C-Faq and Kernighan & Ritchie books for the correct
answer.
--
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Oct 12 '07 #10
Chris Dollin <ch**********@hp.comwrites:
karthikbalaguru wrote:
>On Oct 12, 12:03 pm, pradeep...@gmail.com wrote:
>>#define CUBE(x) (x*x*x)
a=CUBE(b++);
>The Macro CUBE has side effects that modify an operator

variable.
>several times without a sequence point.
The macro 'CUBE()' has no side effects.
The expression 'b++' has a side effect.

Using an expression with a side effect as an argument to a macro is a
Bad Idea if that macro evaluates that argument more than once.

The macro 'CUBE()' evaluates its argument more than once.

mlp
Oct 13 '07 #11
"Mark L Pappin" <ml*@acm.orga écrit dans le message de news:
m3************@Claudio.Messina...
Chris Dollin <ch**********@hp.comwrites:
>karthikbalaguru wrote:
>>On Oct 12, 12:03 pm, pradeep...@gmail.com wrote:
>>>#define CUBE(x) (x*x*x)
a=CUBE(b++);
>>The Macro CUBE has side effects that modify an operator

variable.
>>several times without a sequence point.

The macro 'CUBE()' has no side effects.
The expression 'b++' has a side effect.

Using an expression with a side effect as an argument to a macro is a
Bad Idea if that macro evaluates that argument more than once.

The macro 'CUBE()' evaluates its argument more than once.
Technically, the macro does not "evaluate" its arguments, it just expands to
a sequence of tokens that may be part of an expression where multiple
occurrences of a side effect invokes undefined behaviour.

int a = CUBE(b++); // invokes undefined behaviour;
size_t size = sizeof(CUBE(b++)); // far fetched, but OK.

Even the fact that the same argument be present more than once in the macro
expansion is not necessarily an issue:

#define NP(x,y) ((y) 0) ? ((x) + 1) : ((x) - 1))

int a = NP(b++, 1); // side effect only evaluated once.

In the OPs macro, we have more than one misunderstanding on how macros
operate: the macro argument is expanded 3 times in such a way as to cause
undefined behaviour for most side effects *and* it is not properly
parenthesized in the expansion, causing it to not evaluate as expected:

CUBE(a+1) -a+1*a+1*a+1 -3 * a + 1

You can fix this by always surrounding macro arguments with pairs of
parentheses in the definition:

#define CUBE(x) ((x)*(x)*(x))

but is will not prevent the issue related to side effects.

Macros are *very* difficult to master, newbies should avoid them. The OP
can accomplish the same effect safely and correctly with an inline function:

static inline int cube(int x) { return x * x * x; }

--
Chqrlie.
Oct 15 '07 #12
On Oct 14, 11:33 pm, "Charlie Gordon" <n...@chqrlie.orgwrote:
[snip]
Macros are *very* difficult to master, newbies should avoid them. The OP
can accomplish the same effect safely and correctly with an inline function:

static inline int cube(int x) { return x * x * x; }
Note:
'inline' keyword requires C99

There are several C90 compilers that support it as an extension, but
it is not required by the langauge.

Probably, you already know that but I am not sure if the OP does.

Oct 15 '07 #13
"user923005" <dc*****@connx.coma écrit dans le message de news:
11**********************@t8g2000prg.googlegroups.c om...
On Oct 14, 11:33 pm, "Charlie Gordon" <n...@chqrlie.orgwrote:
[snip]
>Macros are *very* difficult to master, newbies should avoid them. The OP
can accomplish the same effect safely and correctly with an inline
function:

static inline int cube(int x) { return x * x * x; }

Note:
'inline' keyword requires C99

There are several C90 compilers that support it as an extension, but
it is not required by the langauge.

Probably, you already know that but I am not sure if the OP does.
The OP should understand that it is easier to find a compiler that supports
inline functions for any desktop PC than to master the art of C macros ;-)

--
Chqrlie.
Oct 16 '07 #14

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

Similar topics

1
by: MSDousti | last post by:
Hi, I'm confused with C++'s unary increment operator(++). Consider the following code: ////////////////////////////////////////////////////////////////////// #include <iostream> using...
18
by: eddiew_AUS | last post by:
while playing around to check <blah> for my personal project, I discovered an anomaly with my code, compiled using (cringe) visual studio ver 6: #include<iostream> using namespace std; ...
7
by: Rajorshi | last post by:
Hi, I am relatively new to c programming. I have a question regarding C's increment (++) operator. KnR says that unary operators like ++ associate from right to left. Having that information, I...
13
by: Oleg Kharitonov | last post by:
Hello! I have found an interesting thing with postfix operator ++. What should contain the variable i after exceution the following code: int i = 5; i = i++; In VC++ 7.1 and VC++ 2005...
14
by: pai | last post by:
hi.. I have a simple doubt about increment operators.. Can any one tell me what is really happening.. Eg: int i=0,ans; ( ans = ++i ++i // This
5
by: Stuart | last post by:
Hi all, Iv'e got a page that has a mass amount of input fields, all of which require a decimal figure. To make it easier when it comes to inputting data, I'm trying to setup + and - links that...
13
by: jehugaleahsa | last post by:
Hello: In C++, you had to distinguish between post and pre increments when overloading. Could someone give me a short demonstration of how to write these? I get the impression that are...
13
by: bintom | last post by:
Is there any reason why the following C++ code behaves as it does ? int i; i=1; cout << (++i)++; Output: 2 i=1; cout << ++(++i); Output: 3
8
by: bintom | last post by:
Why doe the following C++ code behaves as it does? int i; i=1; cout << (++i)++; Output: 2 i=1; cout << ++(++i); Output: 3 i=1; cout << (i++)++; Output: Error. LValue required...
5
by: =?ISO-8859-1?Q?Marcel_M=FCller?= | last post by:
Hi, I have a question about the execution sequence of the postfix increment operator with respect to a function call. void foo(int type, int*& data); int* sequence;
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: 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
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
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...

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.