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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:20:10+00:00 2026-05-20T08:20:10+00:00

Suppose I have a macro defined as this: #define FOO(x,y) \ do { int

  • 0

Suppose I have a macro defined as this:

#define FOO(x,y) \
do {
  int a,b;
  a = f(x);
  b = g(x);
  y = a+b;
} while (0)

When expanding the macro, does GCC “guarantee” any sort of uniqueness to a,b? I mean in the sense that if I use FOO in the following manner:

int a = 1, b = 2;
FOO(a,b);

After, preprocessing this will be:

int a = 1, b = 2;
do {
  int a,b;
  a = f(a);
  b = g(b);
  b = a+b;
} while (0)

Can/will the compiler distinguish between the a outside the do{} and the a inside the do? What tricks can I use to guarantee any sort of uniqueness (besides making the variables inside have a garbled name that makes it unlikely that someone else will use the same name)?

(Ideally functions would be more useful for this, but my particular circumstance doesn’t permit that)

  • 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-20T08:20:11+00:00Added an answer on May 20, 2026 at 8:20 am

    Macros perform just string substitution. The semantic is low and the the compiler have a limited knowledge of the preprocessor (essentially #pragma which in fact is not a preprocessor keyword, and source line info).

    In your case a and b are not initialized local value. Behavior is unpredictible.
    Your expanded code is equivalent to the following one.

    int a = 1, b = 2;
    do {
      int a___,b___;
      a___ = f(a___);
      b___ = g(b___);
      b___ = a___+b___;
    } while (0)
    

    To avoid such case in c++ prefer the use of inline function or template.
    If you use a c 1999 compliant compiler, you can use inline in c language.
    http://en.wikipedia.org/wiki/Inline_function

    In c you can make safer macro by defining longer variable and surrounding parameter by () :

    #define FOO(x,y) \
    do {
      int FOO__a,FOO__b;
      FOO__a = f(x);
      FOO__b = g(x);
      y = FOO__a+FOO__b + (y)*(y);
    } while (0)
    

    Note : I changed your example by adding a (y)*(y) to illustrate the case

    It is also a good practice to use only once macro parameter.
    This prevent side effects like that:

    #define max(a,b) a>b?a:b
    max(i++,--y)
    

    Max will not return what you want.

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

Sidebar

Related Questions

Suppose you have the following code: (ns foo) (defmacro defproject [project-name version & args]
Suppose you have two static libraries A and B such that A references methods
We have inherited a very convolved project (500kloc) with a lot of preprocessor conditional
I have been using metaprogramming quite a lot, but sometimes the combination of c
I came across this problem via a colleague today. He had a design for
I have one question with mixing C-string and fortran-string in one file. Supposed I
Here's the problem: In order to #import the correct version of ADO for my
I've an application that has three layers: 1. data layer: exposes Entity Framework entities
Working through Michael Hartl's RailsTutorial and came across the following error - even though
I am attempting to find a logging framework for a Cocoa application, written in

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.