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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:46:21+00:00 2026-05-10T17:46:21+00:00

For example, how can I avoid writing the ‘func_name’ twice? #ifndef TEST_FUN # define

  • 0

For example, how can I avoid writing the ‘func_name’ twice?

#ifndef TEST_FUN #  define TEST_FUN func_name #  define TEST_FUN_NAME "func_name" #endif 

I’d like to follow the Single Point of Truth rule.

Version of C preprocessor:

cpp --version 

Output:

cpp (GCC) 4.1.2 20070626 (Red Hat 4.1.2-14) 
  • 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. 2026-05-10T17:46:22+00:00Added an answer on May 10, 2026 at 5:46 pm

    He who is Shy* gave you the germ of an answer, but only the germ. The basic technique for converting a value into a string in the C pre-processor is indeed via the ‘#’ operator, but a simple transliteration of the proposed solution gets a compilation error:

    #define TEST_FUNC test_func #define TEST_FUNC_NAME #TEST_FUNC  #include <stdio.h> int main(void) {     puts(TEST_FUNC_NAME);     return(0); } 

    The syntax error is on the ‘puts()’ line – the problem is a ‘stray #’ in the source.

    In section 6.10.3.2 of the C standard, ‘The # operator’, it says:

    Each # preprocessing token in the replacement list for a function-like macro shall be followed by a parameter as the next preprocessing token in the replacement list.

    The trouble is that you can convert macro arguments to strings — but you can’t convert random items that are not macro arguments.

    So, to achieve the effect you are after, you most certainly have to do some extra work.

    #define FUNCTION_NAME(name) #name #define TEST_FUNC_NAME  FUNCTION_NAME(test_func)  #include <stdio.h>  int main(void) {     puts(TEST_FUNC_NAME);     return(0); } 

    I’m not completely clear on how you plan to use the macros, and how you plan to avoid repetition altogether. This slightly more elaborate example might be more informative. The use of a macro equivalent to STR_VALUE is an idiom that is necessary to get the desired result.

    #define STR_VALUE(arg)      #arg #define FUNCTION_NAME(name) STR_VALUE(name)  #define TEST_FUNC      test_func #define TEST_FUNC_NAME FUNCTION_NAME(TEST_FUNC)  #include <stdio.h>  static void TEST_FUNC(void) {     printf('In function %s\n', TEST_FUNC_NAME); }  int main(void) {     puts(TEST_FUNC_NAME);     TEST_FUNC();     return(0); } 

    * At the time when this answer was first written, shoosh‘s name used ‘Shy’ as part of the name.

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

Sidebar

Ask A Question

Stats

  • Questions 121k
  • Answers 121k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Check this out: Why do I get 'BOF or EOF'… May 12, 2026 at 12:24 am
  • Editorial Team
    Editorial Team added an answer It won't have anything to do with the DLL being… May 12, 2026 at 12:24 am
  • Editorial Team
    Editorial Team added an answer I think what your asking for is: How do I… May 12, 2026 at 12:24 am

Related Questions

For example, how can I avoid writing the 'func_name' twice? #ifndef TEST_FUN # define
Let me just start out by saying I am completely new to Java web
I'm developing a statistics module for my website that will help me measure conversion
Question How can I make sure my application is thread-safe? Are their any common
Here are two chunks of code that accomplish (what I think is) the same

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.