itoa

This is a discussion on itoa within the C Programming forums, part of the General Programming Boards category; I was wondering if I have a declaration such as: Code: #define worker_id1 2311 and at some point of my ...

  1. #1
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116

    itoa

    I was wondering if I have a declaration such as:

    Code:
    #define worker_id1 2311
    and at some point of my program I want to print

    Code:
    worker_id1
    but not the integer value which is 2311,just the string "worker_id1"
    can I use itoa in order to achieve it?

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Nope, itoa is not only non-standard but it doesn't convert a #define into a string.

    Try using stringification.

  3. #3
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by memcpy View Post
    Nope, itoa is not only non-standard but it doesn't convert a #define into a string.

    Try using stringification.
    Thank you for your answer.I have never used that before and although I googled it I couldn't quite understand how it's working..
    Could you please give me a simple example?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,667
    The original problem in this case is under-specified, but I doubt stringification will work.

    printf("%s\n", "worker_id1") will work, since macro expansion does not occur within string literals.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    I think I can help. I took a program:
    Code:
    #include <stdio.h>
    
    #define STRINGIFY(x)  #x
    #define WYSIWYG(x) puts ( x )
    
    #define WORKERID  12345
    
    int main ( void )
    {
        const char *thing = STRINGIFY(WORKERID);
        WYSIWYG(thing);
        return 0;
    }
    Now with specific compiler switches (-E) I was able to produce an in-between stage text file. That file has no #defines or #includes anymore. The interesting part is:
    Code:
    int main ( void )
    {
        const char *thing = "WORKERID";
        puts ( thing );
        return 0;
    }
    And of course, removing the -E I can make a exe and run it.
    Code:
    WORKERID
    So I hope this was instructive.

  6. #6
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    Thank you guys for your answers!I will try both methods!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what 's[i++] = n % 10 + '0';' mean in k&r itoa
    By incorrect in forum C Programming
    Replies: 35
    Last Post: 04-12-2012, 09:19 PM
  2. My own itoa()
    By maxorator in forum C++ Programming
    Replies: 18
    Last Post: 10-15-2006, 12:49 PM
  3. itoa on Dev??
    By jmd15 in forum C++ Programming
    Replies: 15
    Last Post: 07-25-2006, 05:34 PM
  4. itoa()
    By caduardo21 in forum C Programming
    Replies: 2
    Last Post: 06-10-2005, 09:24 AM
  5. itoa
    By Thantos in forum C Programming
    Replies: 2
    Last Post: 09-18-2001, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21