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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:27:55+00:00 2026-05-25T15:27:55+00:00

I am defining a macro that evaluates to a constant string, holding the filename

  • 0

I am defining a macro that evaluates to a constant string, holding the filename and the line number, for logging purposes.

It works fine, but I just can’t figure out why 2 additional macros are needed – STRINGIFY and TOSTRING, when intuition suggests simply __FILE__ ":" #__LINE__.

#include <stdio.h>

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define THIS_ORIGIN (__FILE__ ":" TOSTRING(__LINE__))

int main (void) {
  /* correctly prints "test.c:9" */
  printf("%s", THIS_ORIGIN);
  return 0;
}

This just seems like an ugly hack to me.

Can someone explain in detail what happens stage by stage so that __LINE__ is stringified correctly, and why neither of __FILE__ ":" STRINGIFY(__LINE__) and __FILE__ ":" #__LINE__ works?

  • 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-25T15:27:56+00:00Added an answer on May 25, 2026 at 3:27 pm

    Because of the order of expansion. The GCC documentation says:

    Macro arguments are completely macro-expanded before they are substituted into a macro body, unless they are stringified or pasted with other tokens. After substitution, the entire macro body, including the substituted arguments, is scanned again for macros to be expanded. The result is that the arguments are scanned twice to expand macro calls in them.

    So if the argument will be stringified, it is not expanded first. You are getting the literal text in the parenthesis. But if it’s being passed to another macro, it is expanded. Therefore if you want to expand it, you need two levels of macros.

    This is done because there are cases where you do not want to expand the argument before stringification, most common being the assert() macro. If you write:

    assert(MIN(width, height) >= 240);
    

    you want the message to be:

    Assertion MIN(width, height) >= 240 failed
    

    and not some insane thing the MIN macro expands to (in gcc it uses several gcc-specific extensions and is quite long IIRC).

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

Sidebar

Related Questions

Can you define a macro that accesses a normal variable, but in a read-only
I want to write a macro in C that accepts any number of parameters,
How to make a macro that represents a constant NSString value? I'm getting Multi-character
Defining custom components in Facelets is easy and quick but there's one thing I
When defining parameter type that is e.g. in System.Data you have no intellisense and
When defining a custom operator from the limited set of single-character operators that can
Possible Duplicate: C, Macro defining Macro Does anyone know how to pull off something
When defining macros that headers rely on, such as _FILE_OFFSET_BITS , FUSE_USE_VERSION , _GNU_SOURCE
I'm trying to create a Macro function for defining function pointers , functions etc.
Possible Duplicate: C/C++: How to make a variadic macro (variable number of arguments) Just

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.