Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6793071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:03:25+00:00 2026-05-26T18:03:25+00:00

Which macro statement may cause an unexpected results ? #define YEAR_LENGTH 365 #define MONTH_LENGTH

  • 0

Which macro statement may cause an unexpected results ?

#define YEAR_LENGTH   365
#define MONTH_LENGTH  30
 #define DAYCALC(y, m, d) ((y * YEAR_LENGTH) + (m * MONTH_LENGTH) + d)

 int main()
 {
    int x = 5, y = 4 , z = 1;
    cout << DAYCALC(x *3 , y %3 , z) << endl ;
    cout << DAYCALC(x +12 , y  , 300) << endl ;
    cout << DAYCALC(x , 40 - y , 3+z) << endl ;
    cout << DAYCALC(x  , y  , (z+50)) << endl ;
    cout << DAYCALC(x  , y %3 , z) << endl ;
    cout << DAYCALC(4 % x , y++ , z) << endl;
    return 0;
 }

I run the program very well w/o any unexpected results.

Are there some hidden exceptions ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:03:26+00:00Added an answer on May 26, 2026 at 6:03 pm

    You have an operator precendence problem. Macros are literally expanded as text copy and paste.

    For example:

    DAYCALC(x , 40 - y , 3+z)
    

    gets expanded to:

    ((40 - y * YEAR_LENGTH) + (x * MONTH_LENGTH) + 3+z)
    

    Note that 40 - y * YEAR_LENGTH, is not what you want due to operator precedence.

    So you need to put () around your parameters in the macro:

    #define DAYCALC(y, m, d)     (((y) * YEAR_LENGTH) + ((m) * MONTH_LENGTH) + (d))
    

    In general, if a macro parameter appears more than once in the macro, side effects such as y++ (in your last statement) will also be applied more than once. So it’s something to be careful of.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a macro which refreshes all fields in a document (the equivalent of
Trying to create a macro which can be used for print debug messages when
I have written an Excel VBA macro which imports data from a HTML file
Can someone post a Visual Studio macro which goes through all C# source files
How would you write (in C/C++) a macro which tests if an integer type
For learning and demonstrating, I need a macro which prints its parameter and evaluates
I'm currently developing a cross-platform C application. Is there any compiler macro which is
I have created a macro for excel which will pop up form that contains
Is there a __CLASS__ macro in C++ which gives the class name similar to
I am writing a macro in MS Word which should find all highlighted text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.