_snprintf() -> _snprintf_s()

Posted on 2006-05-02
Last Modified: 2008-01-09

Has anyone had experience using:


I was using _snprintf() before but now visual studio 2005 says to use _snprintf_s() instead. What headers must I include? I just have a standard win32 console project.

Question by:minnirok
    LVL 8

    Accepted Solution

    As far as I know _snprintf_s() has just better buffer overflow control.
    Locate in MSDN description of the function and you will see there what you need to include.
    LVL 5

    Expert Comment


    LVL 8

    Expert Comment

    LVL 86

    Assisted Solution

    See ("_snprintf_s, _snprintf_s_l, _snwprintf_s, _snwprintf_s_l ") as well as  ("Security Enhancements in the CRT") about the difference. You still can use the 'old' versions by placing a


    in your code or disabling the warning. NOTE that '_snprintf_s()' isn't compatzible with other compilers.
    LVL 11

    Assisted Solution

    If Microsoft support strtod and strtol I would use them directly instead of parsing out % directives in a string which is neither fast nor typesafe. For example:

    int x;
    sprintf(s, "%ld", x);

    is a runtime error.

    char* buffer = ... // point to whatever you want and...
    char* p = buffer;
    strtod(x, p); // print x into the buffer

    The state of the art of C++ streams is surprisingly bad, and you can get massive speed improvements from writing your own stream; it's really too bad some library doesn't provide a nice one.
    LVL 48

    Assisted Solution

    If compiiler knows buffer length, you can replace sprint with sprintf_s and this will compile:

    char s[100];
    sprintf(s, ...);
    replace with:
    sprintf_s(s, ...);  // works with same parameters as printf

    There is macro which converts such call to valid sprintf_s call.
    If compiler doesn't know buffer size, you must supply additional parameter.

    Just replace and sprintf with sprintf_s and most of them will compile. In lines which are not compiled, add size_t sizeOfBuffer parameter. Notice that sizeOfBuffer must be equal or less then actual buffer size. If sizeOfBuffer is more than actual buffer size, program will crash.

    void DoSomething(char* s)
        sprintf_s(s, bufferSize, ...);   // here you must add buffer size because compiler doesn't know it

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now