CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Hybrid View

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Is this valid for printf (in MSVC)

    I'm trying to run a program that runs fine when built with gcc - and it builds okay with MSVC but crashes at run time.

    I've traced the crash to a call to vsnprintf() which is getting passed a string like this:- "(Value: %s%s %+05.1lf dB) "

    I've never seen %+05.1lf in a string before - is it a valid argument for MSVC?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,833

    Re: Is this valid for printf (in MSVC)

    Yes. See:

    Code:
    int main() {
    	printf("%+05.1lf\n", 1.456);
    	printf("%+05.1lf\n", 123.456);
    	printf("%+05.1lf\n", -13.456);
    }
    which displays:

    Code:
    +01.5
    +123.5
    -13.5
    + means displays + for +ve numb and - for -ve numb
    0 means pad with 0
    5.1 is width of 5 with 1 digit precision
    lf means long double
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Is this valid for printf (in MSVC)

    Thanks 2kaud - has it always been like that or is this something new - e.g. C17 or whatever ?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,833

    Re: Is this valid for printf (in MSVC)

    It's always been like that except that "%lf" came with c99/C++11 when long double was introduced. Note that that MS VS double and long double are the same.

    PS. Why are we both working on a Sunday morning ??
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Is this valid for printf (in MSVC)

    Haha - quite right!
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  6. #6
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Is this valid for printf (in MSVC)

    Seems like this site's taken a backwards step again (believe it or not, it's only just notified me of 2kaud's replies from a fortnight ago !! )
    "A problem well stated is a problem half solved.” - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured