473,395 Members | 1,653 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.

a problem about using macro?

lgn
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);
}
Nov 13 '05 #1
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.
Nov 13 '05 #2
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.

Nov 13 '05 #3
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;
Nov 13 '05 #4


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.

Nov 13 '05 #5
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"?
Nov 13 '05 #6
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
Nov 13 '05 #7

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

Similar topics

220
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...
11
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...
36
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...
17
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...
53
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;
23
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,...
10
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;...
2
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...
6
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);
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:
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
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
tracyyun
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...

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.