I have test the following program, why the result is not 0 2 4 but 0 2 3?
Can someone tell me why the same macro produce different result? Thanks!
#include <stdio.h>
#define max(a,b) ((a)>(b) ? (a): (b))
test()
{
printf("%d\n", 1);
}
int main(int argc, char* argv[])
{
int a=0, b=0;
printf("%d %d\n", a, b);
max(++a, b);
printf("%d %d\n", a, b);
max(++a, b+10);
printf("%d %d\n", a, b);
} 6 1643
"lgn" <li*******@163.com> wrote in message
news:bp**********@mail.cn99.com... I have test the following program, why the result is not 0 2 4 but 0 2 3? Can someone tell me why the same macro produce different result? Thanks!
#include <stdio.h> #define max(a,b) ((a)>(b) ? (a): (b)) test() { printf("%d\n", 1);
I think this is an error as 1 is not defined. However a variable
(identifier) should start by an alphabet (alphanumeric). }
int main(int argc, char* argv[]) { int a=0, b=0; printf("%d %d\n", a, b); max(++a, b); // first attempt printf("%d %d\n", a, b); max(++a, b+10); // second attempt printf("%d %d\n", a, b);
Should end with a return statement
}
The result is 0 2 3 for variable a as the varaible a gets incremented only
once in the second attempt unlike 2 times in the first attempt, as the
comparison fails in the second attempt.
Thanks,
Praveen Kumar.
lgn wrote: I have test the following program, why the result is not 0 2 4 but 0 2 3? Can someone tell me why the same macro produce different result? Thanks!
#include <stdio.h> #define max(a,b) ((a)>(b) ? (a): (b)) test() { printf("%d\n", 1); }
int main(int argc, char* argv[]) { int a=0, b=0; printf("%d %d\n", a, b); max(++a, b);
Evaluates ++a twice so a should be 2.
printf("%d %d\n", a, b); max(++a, b+10);
Evaluates ++a once so a should be 3.
Only one of the conditional operands to the conditional
operator is evaluated at any given evaluation of the conditional
operator. b+10 is larger than 2 (which is what it is compared
against) and so b is returned by the macro. And since b is returned
a++ is never evaluated.
--
Thomas.
sahukar praveen wrote: "lgn" <li*******@163.com> wrote:
.... printf("%d\n", 1);
I think this is an error as 1 is not defined.
You mean the value of 1 is undefined?
Or do you mean 1 is not declared?
Jirka, int 1;
lgn wrote: I have test the following program, why the result is not 0 2 4 but 0 2 3? Can someone tell me why the same macro produce different result? Thanks!
#include <stdio.h> #define max(a,b) ((a)>(b) ? (a): (b)) test() { printf("%d\n", 1); }
int main(int argc, char* argv[]) { int a=0, b=0; printf("%d %d\n", a, b); max(++a, b);
a is incremented once to 1, is now greater than b (0) and so is
incremented again to 2 for the macro result.
printf("%d %d\n", a, b); max(++a, b+10);
a is incremented once to 3, is NOT greater than "b+10" (10) and so is
not incremented again but stays at 3.
printf("%d %d\n", a, b);
return 0;
}
Regards,
Ed.
Groovy hepcat sahukar praveen was jivin' on Mon, 24 Nov 2003 20:04:28
+0530 in comp.lang.c.
Re: a problem about using macro?'s a cool scene! Dig it! "lgn" <li*******@163.com> wrote in message news:bp**********@mail.cn99.com... I have test the following program, why the result is not 0 2 4 but 0 2 3? Can someone tell me why the same macro produce different result? Thanks!
#include <stdio.h> #define max(a,b) ((a)>(b) ? (a): (b)) test() { printf("%d\n", 1);
I think this is an error as 1 is not defined. However a variable (identifier) should start by an alphabet (alphanumeric).
What on Earth are you talking about? That's an integer constant
expression, you fool! Duh!
To quote Homer Simpson's brain, "Now look sad and say d'oh."
--
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"?
sahukar praveen <sa************@yahoo.co.in> wrote: "lgn" <li*******@163.com> wrote in message news:bp**********@mail.cn99.com... I have test the following program, why the result is not 0 2 4 but 0 2 3? Can someone tell me why the same macro produce different result? Thanks!
#include <stdio.h> #define max(a,b) ((a)>(b) ? (a): (b)) test() { printf("%d\n", 1);
I think this is an error as 1 is not defined. However a variable (identifier) should start by an alphabet (alphanumeric).
You're kidding, right?
Alex This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Brandon J. Van Every |
last post by:
What's better about Ruby than Python? I'm sure there's something. What is
it?
This is not a troll. I'm language shopping and I want people's answers. I
don't know beans about Ruby or have...
|
by: Capstar |
last post by:
Hi,
I am working on an application, which will run embedded without an OS.
The app is build up out of a couple of well defined parts. At first I
wanted to keep those parts seperated and use...
|
by: Peng Jian |
last post by:
#define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
I'm a beginner learning C. I can't see what *(a) == *(b) is for.
If either a or b is a null pointer, this may cause crash.
And if...
|
by: kj |
last post by:
How can one test if a pointer has been "freed" (i.e. with free())?
My naive assumption was that such a pointer would equal NULL, but
not so.
Thanks,
kj
--
NOTE: In my address everything...
|
by: Jeff |
last post by:
In the function below, can size ever be 0 (zero)?
char *clc_strdup(const char * CLC_RESTRICT s)
{
size_t size;
char *p;
clc_assert_not_null(clc_strdup, s);
size = strlen(s) + 1;
|
by: Steven T. Hatton |
last post by:
This is one of the first obstacles I encountered when getting started with
C++. I found that everybody had their own idea of what a string is. There
was std::string, QString, xercesc::XMLString,...
|
by: haomiao |
last post by:
I want to implement a common list that can cantain any type of data,
so I declare the list as (briefly)
---------------------------------------
struct list
{
int data_size;
int node_num;...
|
by: Senthil |
last post by:
Hi All
I need to create an Excel report and create a command button and
have to run a macro on the
click event that will print all the pages in the Excel workbook.
I am able to create the report...
|
by: Michael B Allen |
last post by:
Hi,
I have a macro that looks like the following:
#define MMSG msgno_loc0(LOC0, LOC1) && msgno_mmsg0
which if used in some code like:
MMSG("foo=%s", foo);
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |