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 8551277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:09:43+00:00 2026-06-11T14:09:43+00:00

When I define this macro: #define SQR(x) x*x Let’s say this expression: SQR(a+b) This

  • 0

When I define this macro:

#define SQR(x) x*x

Let’s say this expression:

SQR(a+b)

This expression will be replaced by the macro and looks like:

a+b*a+b

But, if I put a ++ operator before the expression:

++SQR(a+b)

What the expression looks like now? Is this ++ placed befor every part of SQR paramete? Like this:

++a+b*++a+b

Here I give a simple program:

#define SQR(x) x*x
int a, k = 3;
a = SQR(k+1) // 7
a = ++SQR(k+1) //9
  • 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-06-11T14:09:44+00:00Added an answer on June 11, 2026 at 2:09 pm

    When defining macros, you basically always want to put the macro parameters in parens to prevent the kind of weird behaviour in your first example, and put the result in parens so it can be safely used without side-effects. Using

    #define SQR(x) ((x)*(x))
    

    makes SQR(a+b) expand to ((a+b)*(a+b)) which would be mathematically correct (unlike a+b*a+b, which is equal to ab+a+b).

    Putting things before or after a macro won’t enter the macro. So ++SQR(x) becomes ++x*x in your example.

    Note the following:

    int a=3, b=1;
    SQR(a+b) // ==> a+b*a+b = 3+1*3+1 = 7
    ++SQR(a+b) // ==> ++a+b*a+b ==> 4 + 1*4 + 1 = 9
               // since preincrement will affect the value of a before it is read.
    

    You’re seeing the ++SQR(a+b) appear to increment by 2 since the preincrement kicks in before a i read either time, i.e. a increments, then is used twice and so the result is 2 higher than expected.

    NOTE As @JonathanLeffler points out, the latter call invokes undefined behaviour; the evaluation is not guaranteed to happen left-to-right. It might produce different results on different compilers/OSes, and thus should never be relied on.

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

Sidebar

Related Questions

Say I have a macro like this: #define SET_TYPE_NAME(TYPE, NAME) \ template<typename T> \
I have a macro that looks like this: #define coutError if (VERBOSITY_SETTING >= VERBOSITY_ERROR)
i'm having trouble trying to define this function-like macro, which takes 4 vector magnitudes
If you have a macro like this in C: #define SLICE_VERSION 20110614 How to
Can you do something like this with a macro in C ? #define SUPERMACRO(X,Y)
I have some Objective-C code that looks like this: #define myVar 10 float f
I have macro like this: #define error_exit(format, ...) \ error_at_line(EXIT_FAILURE, 0, __FILE__, __LINE__, format,
For example, never define a macro like this: #define DANGER 60 + 2 This
I want to use a macro like this #define Return(x) {call_my_function(); return (x);} is
I have a macro like this: #define SHOW_EXPR(x) printf (%s=%d\n, #x, (x)) It works:

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.