Menu

Puzzled by error wrongPrintfScanfArgNum

Alan Cohen
2023-05-30
2023-06-02
  • Alan  Cohen

    Alan Cohen - 2023-05-30

    Cppcheck is normally omniscient, but there's one interesting thing I'm seeing and am wondering if it might be a Cppcheck bug?

    The following line of code:

    printf("computeCycloIMax() = %e A%\n", retval);

    where retval is a double, throws wrongPrintfScanfArgNum printf format string requires 2 parameters but only 1 is given.

    When I run that line it through a couple of C compilers, neither complains and they both return what I expect. So for retval = 35, it prints "computeCycloIMax() = 3.500000e+01 A%"

    Any thoughts are appreciated!

     

    Last edit: Alan Cohen 2023-05-30
    • Andrew C Aitchison

      On Tue, 30 May 2023, Alan Cohen wrote:

      Cppcheck is normally omniscient, but there's one interesting thing
      I'm seeing and am wondering if it might be a Cppcheck bug?

      The following line of code:

      printf("computeCycloIMax() = %e A%\n", retval);

      where retval is a double, throws wrongPrintfScanfArgNum printf
      format string requires 2 parameters but only 1 is given.

      When I run that line it through a couple of C compilers, neither
      complains and they both return what I expect. So for retval = 35, it
      prints 3.500000e+01 A%

      Which compilers did you try;
      gcc and clang both gave warnings for me:

      clang --version

      Ubuntu clang version 15.0.7
      Target: x86_64-pc-linux-gnu
      Thread model: posix
      InstalledDir: /usr/bin

      clang percent.c

      percent.c:10:39: warning: invalid conversion specifier '\x0a' [-Wformat-invalid-specifier]
      printf("computeCycloIMax() = %e A%\n", retval);
      ~^
      1 warning generated.

      gcc --version

      gcc (Ubuntu 12.2.0-17ubuntu1) 12.2.0
      Copyright (C) 2022 Free Software Foundation, Inc.
      This is free software; see the source for copying conditions. There is NO
      warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

      gcc percent.c

      percent.c: In function 'int main()':
      percent.c:10:39: warning: unknown conversion type character '\x0a' in format [-Wformat=]
      10 | printf("computeCycloIMax() = %e A%\n", retval);
      | ^~

      Any thoughts are appreciated!

      As CHR said, use "%%" to print a percent sign.

      --
      Andrew C. Aitchison Kendal, UK
      andrew@aitchison.me.uk

       
      • Alan  Cohen

        Alan Cohen - 2023-06-02

        Thank you very much, I appreciate your taking the time to check this out.

        I don't know the identity of the compilers yet. One of the compilers, I suspect it's an o-o-o-o-ld version of gcc but I'll post if I find out. The other is whatever is backing this online C compiler that I use for checking out quick things. So neither identity is known at the moment.

        One thing is that CPPCheck is flagging an error, while the compilers flag it as a warning.

        Again, thanks.

         
      • Alan  Cohen

        Alan Cohen - 2023-06-02

        gcc is 4.5.2

        Al

         
  • CHR

    CHR - 2023-05-31

    Any literal percent sign needs to be double-escaped: "%%"
    https://en.cppreference.com/w/c/io/fprintf

     
    • Alan  Cohen

      Alan Cohen - 2023-06-02

      Thanks for this.

      Of course, I should have realized it d'oh!

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.