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

  • SEARCH
  • Home
  • 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 840515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:38:10+00:00 2026-05-15T05:38:10+00:00

void main() { int xyz = 123; // original value { // code block

  • 0
void main()
{
    int xyz = 123; // original value
    { // code block starts
        xyz++;
        if(xyz < 1000)
            xyz = 1;
    } // code block ends
    int original_value = xyz; // should be 123
}

void main()
{
    int xyz = 123; // original value
    MACRO_NAME(xyz = 123) // the macro takes the code code that should be executed at the end of the block.
    { // code block starts
        xyz++;
        if(xyz < 1000)
            xyz = 1;
    } // code block ends << how to make the macro execute the "xyz = 123" statement?
    int original_value = xyz; // should be 123
}

Only the first main() works.
I think the comments explain the issue.

It doesn’t need to be a macro but to me it just sounds like a classical “macro-needed” case.

By the way, there’s the BOOST_FOREACH macro/library and I think it does the exact same thing I’m trying to achieve but it’s too complex for me to find the essence of what I need.
From its introductory manual page, an example:

#include <string>
#include <iostream>
#include <boost/foreach.hpp>

int main()
{
    std::string hello( "Hello, world!" );

    BOOST_FOREACH( char ch, hello )
    {
        std::cout << ch;
    }

    return 0;
}
  • 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-15T05:38:10+00:00Added an answer on May 15, 2026 at 5:38 am

    The cleanest way to do this is probably to use an RAII container to reset the value:

    // Assumes T's assignment does not throw
    template <typename T> struct ResetValue
    {
        ResetValue(T& o, T v) : object_(o), value_(v) { }
        ~ResetValue() { object_ = value_; }
    
        T& object_;
        T value_;
    };
    

    used as:

    {
        ResetValue<int> resetter(xyz, 123);
        // ...
    }
    

    When the block ends, the destructor will be called, resetting the object to the specified value.

    If you really want to use a macro, as long as it is a relatively simple expression, you can do this using a for-block:

    for (bool b = false; b == false; b = true, (xyz = 123))
    {
        // ...
    }
    

    which can be turned into a macro:

    #define DO_AFTER_BLOCK(expr) \
        for (bool DO_AFTER_BLOCK_FLAG = false; \
             DO_AFTER_BLOCK_FLAG == false; \
             DO_AFTER_BLOCK_FLAG = true, (expr))
    

    used as:

    DO_AFTER_BLOCK(xyz = 123)
    {
        // ...
    }
    

    I don’t really think the macro approach is a good idea; I’d probably find it confusing were I to see this in production source code.

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

Sidebar

Related Questions

When i have a code block static void Main() { foreach (int i in
This Java code: public class XYZ { public static void main(){ int toyNumber =
Consider the following code in C: void main() { int a=0; for(printf(\nA); a; printf(\nB));
int main(void) { std::string foo(foo); } My understanding is that the above code uses
Why run this code and print whole string? #include <stdio.h> void main() { int
#include stdio.h #include conio.h int main(void) { if(printf(ABC)) { } else { printf(XYZ); }
I tried the following code snippet: void main() { int x = 1,y =
Possible Duplicate: What should main() return in C/C++? Difference between void main and int
#include<stdio.h> void main(){ int x,y,z; x=y=z=1; z=++x||++y&&++z; printf(%d %d %d \n,x,y,z); getch(); } the
Possible Duplicate: Difference between void main and int main? Why is void main() {

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.