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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
On Tue, 30 May 2023, Alan Cohen wrote:
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);
| ^~
As CHR said, use "%%" to print a percent sign.
--
Andrew C. Aitchison Kendal, UK
andrew@aitchison.me.uk
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.
gcc is 4.5.2
Al
Any literal percent sign needs to be double-escaped:
"%%"
https://en.cppreference.com/w/c/io/fprintf
Thanks for this.
Of course, I should have realized it d'oh!