{"id":6841,"date":"2015-06-22T17:47:00","date_gmt":"2015-06-23T00:47:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2015\/06\/22\/format-specifiers-checking\/"},"modified":"2019-07-31T09:25:41","modified_gmt":"2019-07-31T09:25:41","slug":"format-specifiers-checking","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/format-specifiers-checking\/","title":{"rendered":"Format Specifiers Checking"},"content":{"rendered":"<p class=\"MsoNormal\">By popular request, in Visual Studio 2015 RTM, we&rsquo;ve implemented the checking of arguments given to <span style=\"font-family: Consolas\">printf<\/span>\/<span style=\"font-family: Consolas\">scanf<\/span> and their variations in the C standard library. You can try the examples from this post in our <a href=\"http:\/\/webcompiler.cloudapp.net\/\">online compiler<\/a>.<\/p>\n<h2>Summary<\/h2>\n<p class=\"MsoNormal\">Here is a list of all the formatting warnings that were introduced:<\/p>\n<table class=\"MsoTable15List4Accent5\" style=\"border: currentColor;width: 100%;border-collapse: collapse\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr style=\"height: 15.0pt\">\n<td style=\"width: 30.6pt;border: solid #4472C4 1.0pt;border-right: none;background: #4472C4;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"41\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><strong> <span style=\"font-size: 10.0pt;font-family: 'Calibri Light',sans-serif;color: white\">State<\/span> <\/strong><\/p>\n<\/td>\n<td style=\"width: 28.95pt;border-top: solid #4472C4 1.0pt;border-left: none;border-bottom: solid #4472C4 1.0pt;border-right: none;background: #4472C4;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"39\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><strong> <span style=\"font-size: 10.0pt;font-family: 'Calibri Light',sans-serif;color: white\">Level<\/span> <\/strong><\/p>\n<\/td>\n<td style=\"width: 39.2pt;border-top: solid #4472C4 1.0pt;border-left: none;border-bottom: solid #4472C4 1.0pt;border-right: none;background: #4472C4;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"52\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><strong> <span style=\"font-size: 10.0pt;font-family: 'Calibri Light',sans-serif;color: white\">Number<\/span> <\/strong><\/p>\n<\/td>\n<td style=\"width: 391.5pt;border: solid #4472C4 1.0pt;border-left: none;background: #4472C4;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><strong> <span style=\"font-size: 10.0pt;font-family: 'Calibri Light',sans-serif;color: white\">Text<\/span> <\/strong><\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 15.0pt\">\n<td style=\"width: 30.6pt;border-top: none;border-left: solid #8EAADB 1.0pt;border-bottom: solid #8EAADB 1.0pt;border-right: none;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"41\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">On<\/span><\/p>\n<\/td>\n<td style=\"width: 28.95pt;border: none;border-bottom: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"39\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">W1<\/span><\/p>\n<\/td>\n<td style=\"width: 39.2pt;border: none;border-bottom: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"52\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><a href=\"http:\/\/blogs.msdn.com#C4473\">C4473<\/a><\/p>\n<\/td>\n<td style=\"width: 391.5pt;border-top: none;border-left: none;border-bottom: solid #8EAADB 1.0pt;border-right: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\"> &#8216;&lt;function&gt;&#8217; : not enough arguments passed for format string <\/span><\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 15.0pt\">\n<td style=\"width: 30.6pt;border-top: none;border-left: solid #8EAADB 1.0pt;border-bottom: solid #8EAADB 1.0pt;border-right: none;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"41\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">On<\/span><\/p>\n<\/td>\n<td style=\"width: 28.95pt;border: none;border-bottom: solid #8EAADB 1.0pt;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"39\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">W3<\/span><\/p>\n<\/td>\n<td style=\"width: 39.2pt;border: none;border-bottom: solid #8EAADB 1.0pt;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"52\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><a href=\"http:\/\/blogs.msdn.com#C4474\">C4474<\/a><\/p>\n<\/td>\n<td style=\"width: 391.5pt;border-top: none;border-left: none;border-bottom: solid #8EAADB 1.0pt;border-right: solid #8EAADB 1.0pt;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\"> &#8216;&lt;function&gt;&#8217; : too many arguments passed for format string <\/span><\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 15.0pt\">\n<td style=\"width: 30.6pt;border-top: none;border-left: solid #8EAADB 1.0pt;border-bottom: solid #8EAADB 1.0pt;border-right: none;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"41\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">On<\/span><\/p>\n<\/td>\n<td style=\"width: 28.95pt;border: none;border-bottom: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"39\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">W3<\/span><\/p>\n<\/td>\n<td style=\"width: 39.2pt;border: none;border-bottom: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"52\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><a href=\"http:\/\/blogs.msdn.com#C4475\">C4475<\/a><\/p>\n<\/td>\n<td style=\"width: 391.5pt;border-top: none;border-left: none;border-bottom: solid #8EAADB 1.0pt;border-right: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\"> &#8216;&lt;function&gt;&#8217; : length modifier &#8216;&lt;length&gt;&#8217; cannot be used with type field character &#8216;&lt;conversion-specifier&gt;&#8217; in format specifier <\/span><\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 15.0pt\">\n<td style=\"width: 30.6pt;border-top: none;border-left: solid #8EAADB 1.0pt;border-bottom: solid #8EAADB 1.0pt;border-right: none;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"41\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">On<\/span><\/p>\n<\/td>\n<td style=\"width: 28.95pt;border: none;border-bottom: solid #8EAADB 1.0pt;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"39\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">W3<\/span><\/p>\n<\/td>\n<td style=\"width: 39.2pt;border: none;border-bottom: solid #8EAADB 1.0pt;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"52\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><a href=\"http:\/\/blogs.msdn.com#C4476\">C4476<\/a><\/p>\n<\/td>\n<td style=\"width: 391.5pt;border-top: none;border-left: none;border-bottom: solid #8EAADB 1.0pt;border-right: solid #8EAADB 1.0pt;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\"> &#8216;&lt;function&gt;&#8217; : unknown type field character &#8216;&lt;conversion-specifier&gt;&#8217; in format specifier <\/span><\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 15.0pt\">\n<td style=\"width: 30.6pt;border-top: none;border-left: solid #8EAADB 1.0pt;border-bottom: solid #8EAADB 1.0pt;border-right: none;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"41\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">On<\/span><\/p>\n<\/td>\n<td style=\"width: 28.95pt;border: none;border-bottom: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"39\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">W1<\/span><\/p>\n<\/td>\n<td style=\"width: 39.2pt;border: none;border-bottom: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"52\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><a href=\"http:\/\/blogs.msdn.com#C4477\">C4477<\/a><\/p>\n<\/td>\n<td style=\"width: 391.5pt;border-top: none;border-left: none;border-bottom: solid #8EAADB 1.0pt;border-right: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\"> &#8216;&lt;function&gt;&#8217; : format string &#8216;&lt;format-string&gt;&#8217; requires an argument of type &#8216;&lt;type&gt;&#8217;, but variadic argument &lt;position&gt; has type &#8216;&lt;type&gt;&#8217; <\/span><\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 15.0pt\">\n<td style=\"width: 30.6pt;border-top: none;border-left: solid #8EAADB 1.0pt;border-bottom: solid #8EAADB 1.0pt;border-right: none;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"41\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">On<\/span><\/p>\n<\/td>\n<td style=\"width: 28.95pt;border: none;border-bottom: solid #8EAADB 1.0pt;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"39\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">W1<\/span><\/p>\n<\/td>\n<td style=\"width: 39.2pt;border: none;border-bottom: solid #8EAADB 1.0pt;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"52\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><a href=\"http:\/\/blogs.msdn.com#C4478\">C4478<\/a><\/p>\n<\/td>\n<td style=\"width: 391.5pt;border-top: none;border-left: none;border-bottom: solid #8EAADB 1.0pt;border-right: solid #8EAADB 1.0pt;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\"> &#8216;&lt;function&gt;&#8217; : positional and non-positional placeholders cannot be mixed in the same format string <\/span><\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 15.0pt\">\n<td style=\"width: 30.6pt;border-top: none;border-left: solid #8EAADB 1.0pt;border-bottom: solid #8EAADB 1.0pt;border-right: none;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"41\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">OFF<\/span><\/p>\n<\/td>\n<td style=\"width: 28.95pt;border: none;border-bottom: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"39\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">W4<\/span><\/p>\n<\/td>\n<td style=\"width: 39.2pt;border: none;border-bottom: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"52\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><a href=\"http:\/\/blogs.msdn.com#C4774\">C4774<\/a><\/p>\n<\/td>\n<td style=\"width: 391.5pt;border-top: none;border-left: none;border-bottom: solid #8EAADB 1.0pt;border-right: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\"> &#8216;&lt;function&gt;&#8217; : format string expected in argument &lt;position&gt; is not a string literal <\/span><\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 15.0pt\">\n<td style=\"width: 30.6pt;border-top: none;border-left: solid #8EAADB 1.0pt;border-bottom: solid #8EAADB 1.0pt;border-right: none;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"41\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">On<\/span><\/p>\n<\/td>\n<td style=\"width: 28.95pt;border: none;border-bottom: solid #8EAADB 1.0pt;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"39\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">W3<\/span><\/p>\n<\/td>\n<td style=\"width: 39.2pt;border: none;border-bottom: solid #8EAADB 1.0pt;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"52\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><a href=\"http:\/\/blogs.msdn.com#C4775\">C4775<\/a><\/p>\n<\/td>\n<td style=\"width: 391.5pt;border-top: none;border-left: none;border-bottom: solid #8EAADB 1.0pt;border-right: solid #8EAADB 1.0pt;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\"> nonstandard extension used in format string &#8216;<a name=\"OLE_LINK2\"><\/a>&lt;format-string&gt;&#8217; of function &#8216;&lt;function&gt;&#8217; <\/span><\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 15.0pt\">\n<td style=\"width: 30.6pt;border-top: none;border-left: solid #8EAADB 1.0pt;border-bottom: solid #8EAADB 1.0pt;border-right: none;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"41\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">On<\/span><\/p>\n<\/td>\n<td style=\"width: 28.95pt;border: none;border-bottom: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"39\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">W1<\/span><\/p>\n<\/td>\n<td style=\"width: 39.2pt;border: none;border-bottom: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"52\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><a href=\"http:\/\/blogs.msdn.com#C4776\">C4776<\/a><\/p>\n<\/td>\n<td style=\"width: 391.5pt;border-top: none;border-left: none;border-bottom: solid #8EAADB 1.0pt;border-right: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\"> &lsquo;%&lt;conversion-specifier&gt;&#8217; is not allowed in the format string of function &#8216;&lt;function&gt;&#8217; <\/span><\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 15.0pt\">\n<td style=\"width: 30.6pt;border-top: none;border-left: solid #8EAADB 1.0pt;border-bottom: solid #8EAADB 1.0pt;border-right: none;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"41\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">OFF<\/span><\/p>\n<\/td>\n<td style=\"width: 28.95pt;border: none;border-bottom: solid #8EAADB 1.0pt;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"39\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">W4<\/span><\/p>\n<\/td>\n<td style=\"width: 39.2pt;border: none;border-bottom: solid #8EAADB 1.0pt;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"52\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><a href=\"http:\/\/blogs.msdn.com#C4777\">C4777<\/a><\/p>\n<\/td>\n<td style=\"width: 391.5pt;border-top: none;border-left: none;border-bottom: solid #8EAADB 1.0pt;border-right: solid #8EAADB 1.0pt;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\"> &#8216;&lt;function&gt;&#8217; : format string &#8216;&lt;format-string&gt;&#8217; requires an argument of type &#8216;&lt;type&gt;&#8217;, but variadic argument &lt;position&gt; has type &#8216;&lt;type&gt;&#8217; <\/span><\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 15.0pt\">\n<td style=\"width: 30.6pt;border-top: none;border-left: solid #8EAADB 1.0pt;border-bottom: solid #8EAADB 1.0pt;border-right: none;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"41\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">On<\/span><\/p>\n<\/td>\n<td style=\"width: 28.95pt;border: none;border-bottom: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"39\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">W3<\/span><\/p>\n<\/td>\n<td style=\"width: 39.2pt;border: none;border-bottom: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\" nowrap width=\"52\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><a href=\"http:\/\/blogs.msdn.com#C4778\">C4778<\/a><\/p>\n<\/td>\n<td style=\"width: 391.5pt;border-top: none;border-left: none;border-bottom: solid #8EAADB 1.0pt;border-right: solid #8EAADB 1.0pt;background: #D9E2F3;padding: 0in 5.4pt 0in 5.4pt;height: 15.0pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt\"><span style=\"color: black\">&#8216;&lt;function&gt;&#8217; : unterminated format string &#8216;&lt;format-string&gt;&#8217;<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><a name=\"_Example_1\"><\/a>Example 1<\/h2>\n<p class=\"MsoNormal\">Consider the following snippet taken from real code:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt 0.5in;text-align: left\" align=\"left\"><span style=\"background: white;color: blue;font-family: Consolas\">wchar_t const<\/span> <span style=\"background: white;color: black;font-family: Consolas\">* str = <span style=\"background: white;color: gray;font-family: Consolas;font-size: 9.5pt\">&#8230;<\/span>;<\/span><span style=\"background: white;color: green;font-family: Consolas\">\/\/ Some string to parse<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt 0.5in;text-align: left\" align=\"left\"><span style=\"background: white;color: blue;font-family: Consolas\">char<\/span> <span style=\"background: white;color: black;font-family: Consolas\">buf[<\/span><span style=\"background: white;color: #02befd;font-family: Consolas\">10<\/span><span style=\"background: white;color: black;font-family: Consolas\">];<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt 0.5in;text-align: left\" align=\"left\"><span style=\"background: white;color: blue;font-family: Consolas\">wchar_t<\/span> <span style=\"background: white;color: black;font-family: Consolas\">wbf;<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt 0.5in;text-align: left\" align=\"left\"><span style=\"background: white;color: black;font-family: Consolas\">swscanf_s(str, L<\/span><span style=\"background: white;color: #a31515;font-family: Consolas\">&#8220;%10c %1C&#8221;<\/span><span style=\"background: white;color: black;font-family: Consolas\">, buf, <\/span><span style=\"background: white;color: blue;font-family: Consolas\">sizeof<\/span><span style=\"background: white;color: black;font-family: Consolas\">(buf), &amp;wbf);<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt\"><span style=\"color: black;font-family: Consolas;font-size: 9.5pt\">&nbsp;<\/span><\/p>\n<\/blockquote>\n<p class=\"MsoNormal\">Compiling it with cl.exe and no additional flags (default warning level is 1) will give you 3 warnings (colors are used for clarity of presentation):<\/p>\n<div style=\"border: none;border-left: solid windowtext 1.0pt;padding: 0in 0in 0in 4.0pt;margin-left: .5in;margin-right: .5in\">\n<p class=\"MsoQuote\"><a name=\"C4477\"><\/a> <span style=\"font-family: Consolas\">warning C4477<\/span> <span style=\"font-family: Consolas\"> : &#8216;swscanf_s&#8217; : format string &#8216;%10c&#8217; requires an argument of type &#8216;wchar_t *&#8217;, but variadic argument 1 has type &#8216;char *&#8217;<br \/> note: this argument is used by a <\/span><span style=\"font-family: Consolas;color: #c00000\">conversion specifier<\/span><span style=\"font-family: Consolas\"> <br \/> <\/span><span style=\"font-family: Consolas;color: #aeaaaa\"> note: consider using &#8216;%hc&#8217; in the format string<br \/> note: consider using &#8216;%Tc&#8217; in the format string<br \/> <\/span><span style=\"font-family: Consolas\"> note: consider defining _CRT_STDIO_ISO_WIDE_SPECIFIERS if C99 standard semantics is required <\/span><\/p>\n<\/p><\/div>\n<p class=\"MsoNormal\"> The first warning indicates a mismatch between the type of the expected and the actual argument in the context of <span style=\"font-family: Consolas\">swscanf_s<\/span>. Note that the same actual argument might be valid for that format specifier if you had called a different function (for example, <span style=\"font-family: Consolas\">sscanf_s<\/span>), which is why we include the name of the function in these newly introduced warning messages. If the given conversion specifier would match the actual argument with different length modifiers, then we will list those combinations as suggestions. Note that following these suggestions may not always be the right thing to do, because the conversion specifier or the type of the argument may have to be changed. We will not suggest other conversion specifiers because changing the conversion specifier itself will usually lead to semantic changes, which requires insight into the logic of the program.<\/p>\n<p class=\"MsoNormal\">We chose to refer to the positions of arguments as relative to the beginning of the variadic arguments instead of relative to all the arguments. We wanted this numbering to be consistent with the numbering used by <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/vstudio\/bt7tawza(v=vs.140).aspx\"> positional arguments in _p functions <\/a>, and we found this scheme easier to work with because variadic arguments tend to immediately follow the format string (except in the case of <span style=\"font-family: Consolas\">_l<\/span> functions).<\/p>\n<div style=\"border: none;border-left: solid windowtext 1.0pt;padding: 0in 0in 0in 4.0pt;margin-left: .5in;margin-right: .5in\">\n<p class=\"MsoQuote\"><span style=\"font-family: Consolas\"> warning C4477: &#8216;swscanf_s&#8217; : format string &#8216;%1C&#8217; requires an argument of type &#8216;char *&#8217;, but variadic argument 3 has type &#8216;wchar_t *&#8217;<br \/> note: this argument is used by a <\/span><span style=\"font-family: Consolas;color: #c00000\"> conversion specifier<br \/> <\/span><span style=\"font-family: Consolas\"> note: consider using &#8216;%lC&#8217; in the format string<br \/> <\/span><span style=\"font-family: Consolas;color: #aeaaaa\"> note: consider using &#8216;%llC&#8217; in the format string<br \/> note: consider using &#8216;%LC&#8217; in the format string<br \/> note: consider using &#8216;%wC&#8217; in the format string <\/span><\/p>\n<\/p><\/div>\n<p class=\"MsoNormal\"> If you use \/Za during compilation to disable Microsoft extensions, you will notice that notes suggesting the use of non-standard format specifiers (marked <span style=\"color: #aeaaaa\">with gray color<\/span> above) don&rsquo;t appear, and that you get one additional warning:<\/p>\n<div style=\"border: none;border-left: solid windowtext 1.0pt;padding: 0in 0in 0in 4.0pt;margin-left: .5in;margin-right: .5in\">\n<p class=\"MsoQuote\"><a name=\"C4775\"><\/a> <span style=\"font-family: Consolas\">warning C4775<\/span> <span style=\"font-family: Consolas\"> : nonstandard extension used in format string &#8216;%1C&#8217; of function &#8216;swscanf_s&#8217;<br \/> note: the combination of length modifier &#8221; with type field character &#8216;C&#8217; is non standard <\/span><\/p>\n<\/p><\/div>\n<p class=\"MsoNormal\"> If you compile for x64, where size_t is defined to be <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">typedef<\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">unsigned<\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">__int64<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"> size_t; <\/span>you will also get:<\/p>\n<div style=\"border: none;border-left: solid windowtext 1.0pt;padding: 0in 0in 0in 4.0pt;margin-left: .5in;margin-right: .5in\">\n<p class=\"MsoQuote\"><span style=\"font-family: Consolas\"> warning C4477: &#8216;swscanf_s&#8217; : format string &#8216;%10c&#8217; requires an argument of type &#8216;int&#8217;, but variadic argument 2 has type &#8216;size_t&#8217;<br \/> note: this argument is used as a <\/span><span style=\"font-family: Consolas;color: #c00000\">buffer size<\/span><\/p>\n<\/p><\/div>\n<p class=\"MsoNormal\"> The problem here is that the buffer size is expected to be of type <span style=\"font-family: Consolas\">int<\/span>, which has 4 bytes on x64, but the actual argument has type <span style=\"font-family: Consolas\">size_t<\/span>, which occupies 8 bytes on x64. In some cases, it is possible to get this warning on x86 as well, but you will need to enable warning C4777, which is off by default. To do so, compile with \/w14777 or \/Wall. Unfortunately, C4777 will not help you in this specific case, because the value will have to be declared with type <span style=\"font-family: Consolas\">std::size_t<\/span> for us to detect the potential issue, while here the type of the expression <span style=\"font-family: Consolas\">sizeof(buf)<\/span> is of unnamed type, which is also the underlying type of <span style=\"font-family: Consolas\">size_t<\/span>.<\/p>\n<p class=\"MsoNormal\">Some format specifiers can consume up to 3 arguments from the stack, depending on the function. Because of this, it might be confusing at first to encounter a warni\nng that indicates that a format specifier <span style=\"font-family: Consolas\">%s<\/span> expects an argument of type <span style=\"font-family: Consolas\">int<\/span>. To reduce confusion, we added notes that indicate what such arguments are used for in a given context, with examples <span style=\"color: #c00000\">highlighted in red<\/span> above and below. These notes explain whether the argument is used by the conversion specifier itself, as a required buffer size or as a width or precision field.<\/p>\n<div style=\"border: none;border-left: solid windowtext 1.0pt;padding: 0in 0in 0in 4.0pt;margin-left: .5in;margin-right: .5in\">\n<p class=\"MsoQuote\"><a name=\"C4473\"><\/a> <span style=\"font-family: Consolas\">warning C4473<\/span> <span style=\"font-family: Consolas\"> : &#8216;swscanf_s&#8217; : not enough arguments passed for format string<br \/> note: placeholders and their parameters expect 4 variadic arguments, but 3 were provided<br \/> note: the missing variadic argument 4 is required by format string &#8216;%1C&#8217;<br \/> note: this argument is used as a <\/span><span style=\"font-family: Consolas;color: #c00000\">buffer size<\/span><\/p>\n<\/p><\/div>\n<p class=\"MsoNormal\"> Missing variadic arguments can be as big of a security concern as incorrect types, because they may cause your program to read garbage from the stack. To reflect the severity of these issues, we created C4473 as a level 1 warning.<\/p>\n<p class=\"MsoNormal\">Note that in earlier previews of Visual Studio 2015, C4473 was known as C4317, while <a name=\"C4474\"><\/a>C4474 used to be C4422 and C4776 was C4426. Please make sure to update your pragmas or build scripts if you were suppressing or disabling these warnings using their old numbers.<\/p>\n<h2>Example 2<\/h2>\n<p class=\"MsoNormal\">Consider another simple example we found in &ldquo;real world&rdquo; code:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"margin-top: 0in;margin-right: 0in;margin-bottom: .0001pt;margin-left: .5in;text-align: left\" align=\"left\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">const<\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">char<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">* path= <\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: #a31515;background: white\">&#8220;PATH=%WindowsSdkDir%bin\\\\%_ARCH% ;%PATH%&#8221;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">;<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-top: 0in;margin-right: 0in;margin-bottom: .0001pt;margin-left: .5in;text-align: left\" align=\"left\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">printf_s(path);<\/span><\/p>\n<\/blockquote>\n<p class=\"MsoNormal\"> Compiling this with default flags wouldn&rsquo;t result in any diagnostic messages, giving you a false sense of security, but you can clearly see that there are problems here. Unfortunately, always giving a warning when a format string is not a string literal turned out to generate too many warnings on valid use cases (e.g. localization), so as a compromise we decided to provide this warning as off by default. To enable it, you need to pass \/w14774 or \/Wall, after which you will get:<\/p>\n<div style=\"border: none;border-left: solid windowtext 1.0pt;padding: 0in 0in 0in 4.0pt;margin-left: .5in;margin-right: .5in\">\n<p class=\"MsoQuote\"><a name=\"C4774\"><\/a> <span style=\"font-family: Consolas\">warning<\/span> <a name=\"OLE_LINK4\"><\/a> <span style=\"font-family: Consolas\">C4774<\/span> <span style=\"font-family: Consolas\"> : &#8216;printf_s&#8217; : format string expected in argument 1 is not a string literal<br \/> note: e.g. instead of printf(name); use printf(&#8220;%s&#8221;, name); because format specifiers in &#8216;name&#8217; may pose a security issue<br \/> note: consider using <\/span><span style=\"font-family: Consolas;color: blue\">constexpr<\/span> <span style=\"font-family: Consolas\">specifier for named string literals<\/span><\/p>\n<\/p><\/div>\n<p class=\"MsoNormal\"> We recommend enabling this warning at least occasionally to detect all the places where format checking is not actually happening due to non-literal format strings. The important message here is the second note, which suggests that you use <span style=\"font-family: Consolas\">constexpr<\/span> instead of <span style=\"font-family: Consolas\">const<\/span>. Doing so allows us to evaluate &lsquo;path&rsquo; at compile time and thus perform format checking at the point of its use:<\/p>\n<div style=\"border: none;border-left: solid windowtext 1.0pt;padding: 0in 0in 0in 4.0pt;margin-left: .5in;margin-right: .5in\">\n<p class=\"MsoQuote\"><a name=\"C4476\"><\/a> <span style=\"font-family: Consolas\">warning<\/span> <a name=\"OLE_LINK5\"><\/a> <span style=\"font-family: Consolas\">C4476<\/span> <span style=\"font-family: Consolas\"> : &#8216;printf_s&#8217; : unknown type field character &#8216;W&#8217; in format specifier<br \/> warning C4476: &#8216;printf_s&#8217; : unknown type field character &#8216;b&#8217; in format specifier<br \/> warning C4476: &#8216;printf_s&#8217; : unknown type field character &#8216;_&#8217; in format specifier<br \/> warning C4476: &#8216;printf_s&#8217; : unknown type field character &#8216;;&#8217; in format specifier<br \/> warning C4476: &#8216;printf_s&#8217; : unknown type field character &#8216;P&#8217; in format specifier<br \/> <a name=\"C4778\"><\/a>warning <a name=\"OLE_LINK12\"><\/a>C4778: &#8216;printf_s&#8217; : unterminated format string &#8216;%&#8217; <\/span><\/p>\n<\/p><\/div>\n<p class=\"MsoNormal\"> The solution is to simply use %% instead of %, but, surprisingly, we&rsquo;ve seen way too many occurrences of this bug in real code. The problem can be even more subtle once you realize that &lsquo; &lsquo; (space) is a valid printf flag (see the 4<sup>th<\/sup> warning about <span style=\"font-family: Consolas\">&lsquo;;&rsquo;<\/span> above)<\/p>\n<h2>Example 3<\/h2>\n<p class=\"MsoNormal\">Consider another example:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"margin-top: 0in;margin-right: 0in;margin-bottom: .0001pt;margin-left: .5in;text-align: left\" align=\"left\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">struct<\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">HTMLElement<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-top: 0in;margin-right: 0in;margin-bottom: .0001pt;margin-left: .5in;text-align: left\" align=\"left\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">{<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-top: 0in;margin-right: 0in;margin-bottom: .0001pt;margin-left: .5in;text-align: left\" align=\"left\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">const<\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">char<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">* tag;<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-top: 0in;margin-right: 0in;margin-bottom: .0001pt;margin-left: .5in;text-align: left\" align=\"left\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">virtual<\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">std::string content()<\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">const<\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">=<\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: #02befd;background: white\">0<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">;<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-top: 0in;margin-right: 0in;margin-bottom: .0001pt;margin-left: .5in;text-align: left\" align=\"left\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">};<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-top: 0in;margin-right: 0in;margin-bottom: .0001pt;margin-left: .5in;text-align: left\" align=\"left\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-top: 0in;margin-right: 0in;margin-bottom: .0001pt;margin-left: .5in;text-align: left\" align=\"left\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int<\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">n;<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-top: 0in;margin-right: 0in;margin-bottom: .0001pt;margin-left: .5in;text-align: left\" align=\"left\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">HTMLElement* elem = <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">&#8230;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">;<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-top: 0in;margin-right: 0in;margin-bottom: .0001pt;margin-left: .5in;text-align: left\" align=\"left\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">_tprintf_p(_T(<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #a31515;background: white\">&#8220;&lt;%hhs&gt;%hhs%n&lt;\/%1$hhs&gt;&#8221;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">), elem-&gt;tag, elem-&gt;content().c_str(), &amp;n); <\/span><\/p>\n<\/blockquote>\n<p class=\"MsoNormal\"> This snippet tries to print the content of an html element while at the same time counting how many characters we printed before printing the closing tag. The closing tag is printed via a positional format specifier (<span style=\"font-family: Consolas\">&ldquo;%1$hhs&rdquo;<\/span>) that refers to the first argument (<span style=\"font-family: Consolas\">elem-&gt;tag<\/span>). And since we are in a <span style=\"font-family: Consolas\">_t<\/span> function, we try to ensure it treats &ldquo;<span style=\"font-family: Consolas\">%s<\/span>&rdquo; as a narrow string by writing &ldquo;<span style=\"font-family: Consolas\">hh<\/span>&rdquo;. What we get is the following:<\/p>\n<div style=\"border: none;border-left: solid windowtext 1.0pt;padding: 0in 0in 0in 4.0pt;margin-left: .5in;margin-right: .5in\">\n<p class=\"MsoQuote\"><a name=\"C4475\"><\/a> <span style=\"font-family: Consolas\">warning<\/span> <a name=\"OLE_LINK9\"><\/a> <span style=\"font-family: Consolas\">C4475<\/span> <span style=\"font-family: Consolas\"> : &#8216;_printf_p&#8217; : length modifier &#8216;hh&#8217; cannot be used with type field character &#8216;s&#8217; in format specifier<br \/> <a name=\"C4776\"><\/a>warning <a name=\"OLE_LINK11\"><\/a><a name=\"OLE_LINK10\"><\/a>C4776: &#8216;%n&#8217; is not allowed in the format string of function &#8216;_printf_p&#8217;<br \/> <a name=\"C4478\"><\/a>warning <a name=\"OLE_LINK6\"><\/a>C4478: &#8216;_printf_p&#8217; : positional and non-positional placeholders cannot be mixed in the same format string <\/span><\/p>\n<\/p><\/div>\n<p class=\"MsoNormal\"> The first warning says that &ldquo;<span style=\"font-family: Consolas\">hh<\/span>&rdquo; is not a valid length modifier for &ldquo;<span style=\"font-family: Consolas\">%s<\/span>&rdquo;. The second warning tells you that &ldquo;<span style=\"font-family: Consolas\">%n<\/span>&rdquo; is disallowed in this function. The last warning reminds you that you are not allowed to mix positional and non-positional arguments.<\/p>\n<h2>Example 4<\/h2>\n<p class=\"MsoNormal\">When we tested these warnings, we noticed that a lot of developers where using the following code to print the value of a pointer:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"margin-top: 0in;margin-right: 0in;margin-bottom: .0001pt;margin-left: .5in;text-align: left\" align=\"left\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">const<\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">char<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">* ptr = <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">&#8230;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: green;background: white\"> \/\/ Some pointer<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-top: 0in;margin-right: 0in;margin-bottom: .0001pt;margin-left: .5in;text-align: left\" align=\"left\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">printf(<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #a31515;background: white\">&#8220;%08X&#8221;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">, ptr);<\/span><\/p>\n<\/blockquote>\n<p class=\"MsoNormal\"> We are not sure why this pattern was prevalent to using the standard %p format specifier, but it was so common that we felt the need to elaborate on it. Compiling this code as is on x86 would give you the following warning:<\/p>\n<div style=\"border: none;border-left: solid windowtext 1.0pt;padding: 0in 0in 0in 4.0pt;margin-left: .5in;margin-right: .5in\">\n<p class=\"MsoQuote\"><span style=\"font-family: Consolas\"> warning C4477: &#8216;printf&#8217; : format string &#8216;%08X&#8217; requires an argument of type &#8216;unsigned int&#8217;, but variadic argument 1 has type &#8216;const char *&#8217; <\/span><\/p>\n<\/p><\/div>\n<p class=\"MsoNormal\"> Compiling it on x64 additionally produces:<\/p>\n<div style=\"border: none;border-left: solid windowtext 1.0pt;padding: 0in 0in 0in 4.0pt;margin-left: .5in;margin-right: .5in\">\n<p class=\"MsoQuote\"><span style=\"font-family: Consolas\"> warning C4313: &#8216;printf&#8217;: &#8216;%X&#8217; in format string conflicts with argument 1 of type &#8216;const char *&#8217; <\/span><\/p>\n<\/p><\/div>\n<p class=\"MsoNormal\"> C4313 is an existing warning that was designed to detect integer\/pointer size mismatches. We can get rid of this warning on x86 by converting the pointer to an integral type of the same size as pointers:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"text-align: left;margin-left: 0.5in\" align=\"left\"><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">printf(<\/span><span style=\"background: white;color: #a31515;font-family: Consolas;font-size: 9.5pt\">&#8220;%08X&#8221;<\/span><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">, <span style=\"background: white;color: blue;font-family: Consolas;font-size: 9.5pt\">reinterpret_cast<\/span>&lt;<\/span><span style=\"background: white;color: #2b91af;font-family: Consolas;font-size: 9.5pt\">intptr_t<\/span><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">&gt;(ptr));<\/span><\/p>\n<\/blockquote>\n<p class=\"MsoNormal\">This does not work on x64, because there intptr_t is 8 bytes, while unsigned int is 4.<\/p>\n<div style=\"border: none;border-left: solid windowtext 1.0pt;padding: 0in 0in 0in 4.0pt;margin-left: .5in;margin-right: .5in\">\n<p class=\"MsoQuote\"><span style=\"font-family: Consolas\"> warning C4477: &#8216;printf&#8217; : format string &#8216;%08X&#8217; requires an argument of type &#8216;unsigned int&#8217;, but variadic argument 1 has type &#8216;intptr_t&#8217;<br \/> note: consider using &#8216;%IX&#8217; in the format string <\/span><\/p>\n<\/p><\/div>\n<p class=\"MsoNormal\"> Casting this way also results in printing a truncated value e.g. 9515CED0 instead of the 000000<a name=\"OLE_LINK1\"><\/a><span style=\"color: red\">E2<\/span>9515CED0 produced by %p on x64.<\/p>\n<p class=\"MsoNormal\">To get a warning about such potential truncations on x86, you have to explicitly enable the off-by-default warning C4777 (e.g. by passing \/w14777 on command line):<\/p>\n<div style=\"border: none;border-left: solid windowtext 1.0pt;padding: 0in 0in 0in 4.0pt;margin-left: .5in;margin-right: .5in\">\n<p class=\"MsoQuote\"><a name=\"C4777\"><\/a> <span style=\"font-family: Consolas\">warning<\/span> <a name=\"OLE_LINK8\"><\/a><a name=\"OLE_LINK7\"><\/a><span style=\"font-family: Consolas\">C4777<\/span><span style=\"font-family: Consolas\"> : &#8216;printf&#8217; : format string &#8216;%08X&#8217; requires an argument of type &#8216;unsigned int&#8217;, but variadic argument 1 has type &#8216;intptr_t&#8217;<br \/> note: the sizes of types &#8216;intptr_t&#8217; and &#8216;unsigned int&#8217; might differ on other platforms<br \/> note: consider using &#8216;%IX&#8217; in the format string <\/span><\/p>\n<\/p><\/div>\n<p class=\"MsoNormal\"> The text of the warning C4777 is exactly the same as C4477, but it is given in noisier contexts where the expected and actual types are related on the target platform. For example, <span style=\"font-family: Consolas\">int<\/span> vs. <span style=\"font-family: Consolas\">long<\/span> or <span style=\"font-family: Consolas\">double<\/span> vs. <span style=\"font-family: Consolas\">long double<\/span> on many architectures targeted by Microsoft have the same set of values, while technically being different built-in types. We found in our testing that the number of such mismatches was very high compared to the number of more serious mismatches, with a ratio of about 10 to 1. So, we decided to distinguish the two cases and have the noisier case be off by default.<\/p>\n<p class=\"MsoNormal\">Following the note&rsquo;s suggestion to use the &ldquo;<span style=\"font-family: Consolas\">I<\/span>&rdquo; length modifier:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"text-align: left;margin-left: 0.5in\" align=\"left\"><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">printf(<\/span><span style=\"background: white;color: #a31515;font-family: Consolas;font-size: 9.5pt\">&#8220;%08IX\\n&#8221;<\/span><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">, <span style=\"background: white;color: blue;font-family: Consolas;font-size: 9.5pt\">reinterpret_cast<\/span>&lt;<span style=\"background: white;color: #2b91af;font-family: Consolas;font-size: 9.5pt\">intptr_t<\/span><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">&gt;<\/span><\/span><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">(ptr));<\/span><\/p>\n<\/blockquote>\n<p class=\"MsoNormal\">produces the correct value &ldquo;E29515CED0&rdquo; for the above pointer, but this output is not prepended by zeros to reflect the greater number of bits that pointers on x64 have. To alleviate this, we must also pass the width of the field to print:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt 0.5in;text-align: left\" align=\"left\"><span style=\"background: white;color: blue;font-family: Consolas;font-size: 9.5pt\">const<\/span> <span style=\"background: white;color: #2b91af;font-family: Consolas;font-size: 9.5pt\">size_t<\/span><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\"> MACH_PTR_SIZE = <\/span><span style=\"background: white;color: blue;font-family: Consolas;font-size: 9.5pt\">sizeof<\/span><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">(<\/span><span style=\"background: white;color: blue;font-family: Consolas;font-size: 9.5pt\">void<\/span><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">*);<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt 0.5in;text-align: left\" align=\"left\"><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">printf(<\/span><span style=\"background: white;color: #a31515;font-family: Consolas;font-size: 9.5pt\">&#8220;%0*IX\\n&#8221;<\/span><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">, 2*MACH_PTR_SIZE, <span style=\"background: white;color: blue;font-family: Consolas;font-size: 9.5pt\">reinterpret_cast<\/span>&lt;<span style=\"background: white;color: #2b91af;font-family: Consolas;font-size: 9.5pt\">intptr_t<\/span><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">&gt;<\/span><\/span><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">(ptr));<\/span><\/p>\n<\/blockquote>\n<p class=\"MsoNormal\"> Surprisingly, this gives us another warning (the noisier C4777 on x86 and the stricter C4477 on x64):<\/p>\n<div style=\"border: none;border-left: solid windowtext 1.0pt;padding: 0in 0in 0in 4.0pt;margin-left: .5in;margin-right: .5in\">\n<p class=\"MsoQuote\"><span style=\"font-family: Consolas\"> warning C4777: &#8216;printf&#8217; : format string &#8216;%0*IX&#8217; requires an argument of type &#8216;int&#8217;, but variadic argument 1 has type &#8216;size_t&#8217;<br \/> note: this argument is used as a <\/span><span style=\"font-family: Consolas;color: #c00000\">field width<\/span><span style=\"font-family: Consolas\"> <br \/> note: the sizes of types &#8216;size_t&#8217; and &#8216;int&#8217; might differ on other platforms <\/span><\/p>\n<\/p><\/div>\n<p class=\"MsoNormal\"> which indicates that the field width has to be of type <span style=\"font-family: Consolas\">int<\/span>, not <span style=\"font-family: Consolas\">size_t<\/span>. Making the following modification:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"text-align: left;margin-left: 0.5in\" align=\"left\"><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">printf(<\/span><span style=\"background: white;color: #a31515;font-family: Consolas;font-size: 9.5pt\">&#8220;%0*IX == %p\\n&#8221;<\/span><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">, <\/span><span style=\"background: white;color: blue;font-family: Consolas;font-size: 9.5pt\">int<\/span><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">(2*MACH_PTR_SIZE), <span style=\"background: white;color: blue;font-family: Consolas;font-size: 9.5pt\">reinterpret_cast<\/span>&lt;<span style=\"background: white;color: #2b91af;font-family: Consolas;font-size: 9.5pt\">intptr_t<\/span><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">&gt;<\/span><\/span><span style=\"background: white;color: black;font-family: Consolas;font-size: 9.5pt\">(ptr), ptr); <\/span><\/p>\n<\/blockquote>\n<p class=\"MsoNormal\">finally gets rid of all the warnings and prints the pointer in the same way as using <span style=\"font-family: Consolas\">%p<\/span> (in our implementation) both on x86 and x64. If you prefer a standard length modifier, you can always go with the combination of <span style=\"font-family: courier new,courier\">%tX<\/span> and <span style=\"font-family: courier new,courier\">ptrdiff_t<\/span> instead of <span style=\"font-family: courier new,courier\">%IX<\/span> and <span style=\"font-family: courier new,courier\">intptr_t<\/span>.<\/p>\n<h2>What Next?<\/h2>\n<p class=\"MsoNormal\">Currently, the checking of format specifiers is only done for a predefined set of CRT functions and is not available for user-defined functions that would also benefit from similar checks. If there is enough interest, we will consider extending these warnings to work on such user-defined functions. We would also like to hear about other bugs in the <span style=\"font-family: Consolas\">printf<\/span>\/<span style=\"font-family: Consolas\">scanf<\/span> family of functions that you would like the compiler to detect. Feel free to email me (yuriysol&nbsp;from Microsoft)&nbsp;or comment below and provide any feedback you can think of. Thank you!<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>By popular request, in Visual Studio 2015 RTM, we&rsquo;ve implemented the checking of arguments given to printf\/scanf and their variations in the C standard library. You can try the examples from this post in our online compiler. Summary Here is a list of all the formatting warnings that were introduced: State Level Number Text On [&hellip;]<\/p>\n","protected":false},"author":6384,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[239],"tags":[140,194],"class_list":["post-6841","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-diagnostics","tag-c","tag-diagnostics"],"acf":[],"blog_post_summary":"<p>By popular request, in Visual Studio 2015 RTM, we&rsquo;ve implemented the checking of arguments given to printf\/scanf and their variations in the C standard library. You can try the examples from this post in our online compiler. Summary Here is a list of all the formatting warnings that were introduced: State Level Number Text On [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/6841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/6384"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=6841"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/6841\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=6841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=6841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=6841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}