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

C code with macros

Hi ,all

#define MAX(x,y) ((x)>(y)?(x):(y)
main()
{
intx=5,y=5;
printf("maximum is %d",MAX(++x,++y));
}

this is a code using macros....
the output for this code is 6
is my answer right...

i hav a little bit confusion whether the maximum will be 6 or 5...
please clear my confusion..

Thanks in advance....
Aug 6 '07 #1
7 1548
weaknessforcats
9,208 Expert Mod 8TB
The macro expands to:
Expand|Select|Wrap|Line Numbers
  1.  ((++x)>(++y)?(++x):(++y)
  2.  
x and y are altered multiple times before the end of the statement (called a sequence point). Changing a variable more than once within a given sequence point produces indeterminate results.

Therefore, any answer may be expected.
Aug 6 '07 #2
Hey max value will increment two times so take a close look at the code by replacing the macro call by its body
Aug 7 '07 #3
ok....i will try some more codes with macros..
Aug 7 '07 #4
Answer I think is 7.
Because the result i.e ++y will be calculated twice.
Once during the comparision (++x > ++Y) and again during answering.
++x : ++y.
This sort of arguments should not be used in macros, as their answer cant be predicted easily and they may give unexpected results.

Thanks
Pradeep
Aug 9 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Did anyone read my Post#2??
Aug 9 '07 #6
JosAH
11,448 Expert 8TB
Did anyone read my Post#2??
I did, but I wasn't participating in this thread ;-) You're right though: the OP should
be happy that no daemons flew out of his nose after doing such naughty deeds.

kind regards,

Jos
Aug 9 '07 #7
RRick
463 Expert 256MB
In this case, I think undefined means that no specific/single value can be expected. Are you going to get -23457898 as an answer? Probably not, but if you're expecting 7 you're going to be just as disappointed. What happens is compiler specific and that spells trouble.
Aug 9 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

37
by: michele.simionato | last post by:
Paul Rubin wrote: > How about macros? Some pretty horrible things have been done in C > programs with the C preprocessor. But there's a movememnt afloat to > add hygienic macros to Python. Got any...
242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
8
by: Michael Winter | last post by:
In a recent post ("About C error" by Victor, 21 Sep 2003), comments were made about the poster's use of macros. What I would like to know is why they are considered bad? I'm not referring to...
3
by: J Krugman | last post by:
I'm trying to read some source code that makes very heavy use of macros and other pre-processor directives. To make matters worse, macro definitions are stacked several layers deep (i.e. macros...
27
by: Sune | last post by:
Hi! Pre-requisites: ------------------- 1) Consider I'm about to write a quite large program. Say 500 K lines. 2) Part of this code will consist of 50 structs with, say, no more than at most...
11
by: San | last post by:
hi there, I am new to c++ and tryig to learn the basics of the c++ concepts. While I was reading the templates, I realize that the templates are a syntax that the compilar expands pased upon the...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
1
by: RobertK | last post by:
I need to write a c# code that will open up word documents and delete all the macros in it. Can some one please give me a code example or any ideas on how to do this. I already know how to...
5
by: Salad | last post by:
If I click on the Macros button in the database window there is 1 macro that exists...AutoExec. If I click on the Modules button and enter the VB Editor window there's the menu option Tools. ...
4
NeoPa
by: NeoPa | last post by:
Introduction: Macro Security Levels in MS Office applications are recommended to be set to High. This stops any VBA code associated with a project from running, unless it is signed (with a...
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
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?
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
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
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
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.