Thread: sprintf_s in unix?

  1. #1
    Registered User
    Join Date
    Nov 2006

    Question sprintf_s in unix?

    Hi all
    I have a very large program I want to run in my unix computer (Mac) and Windows at the same time.
    so I have defined:

    #ifndef WIN32
    void sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...) {
    	sprintf(buffer, format, ...); //ERROR: expected primary-expression before '...' token
    How can I solve this error?

    I use to have
    #define	sprintf_s(buffer, buffer_size, stringbuffer, number) (sprintf(buffer, stringbuffer, number))
    but it does not work with any number of arguments as the real sprintf_s

    Help is appreciated, thanks

    Mac OS 10.6 Snow Leopard : Darwin

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    sprintf_s is not part of the standard C library so is not portable.

    However, snprintf is, and you should be able to accomplish what you want with that. It's nearly identical, I think.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2006
    Thanks MK27, I completely forgot about snprintf().
    Mac OS 10.6 Snow Leopard : Darwin

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    AFAIK, it does not work correctly in MSVC. The return value etc.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    GCC supports vararg macros.

    #define	sprintf_s(buffer, buffer_size, stringbuffer, ...) (sprintf(buffer, stringbuffer, __VA_ARGS__))
    Not 100% sure about the syntax.

    BTW, VC++2005+ does too.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to program in unix
    By Cpro in forum Linux Programming
    Replies: 21
    Last Post: 02-12-2008, 10:54 AM
  2. Setting up a Unix box
    By @nthony in forum Tech Board
    Replies: 6
    Last Post: 07-22-2007, 10:22 PM
  3. UNIX (Linux, BSD, etc) Programming :: UNIX
    By kuphryn in forum Linux Programming
    Replies: 6
    Last Post: 04-01-2004, 08:44 PM
  4. Unix Sockets
    By prvindia in forum Linux Programming
    Replies: 5
    Last Post: 03-11-2003, 09:16 AM
  5. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 07:37 AM