1

I'm using Dev-C++ 5.11 and using compiler options to compile to C11. I'm having trouble using the scanf_s function. I've tried <stdio.h> as well as <stdlib.h>.

Is there anything in the standard library that includes this?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 1
    It is not part of the standard - it is a Microsoft extension – Ed Heal Dec 27 '20 at 23:14
  • Besides using C++ has better mechanisms from reading stdin – Ed Heal Dec 27 '20 at 23:15
  • Still, how can I include scanf_s? –  Dec 27 '20 at 23:15
  • 1
    See [Why didn't gcc implement _s functions?](https://stackoverflow.com/questions/50724726/why-didnt-gcc-implement-s-functions). – dxiv Dec 27 '20 at 23:15
  • Why? 1. It is C. 2. Write C++ – Ed Heal Dec 27 '20 at 23:15
  • Does this answer your question? [Header for scanf\_s function](https://stackoverflow.com/questions/18836661/header-for-scanf-s-function) – Allan Wind Dec 27 '20 at 23:16
  • 1
    _As with all bounds-checked functions, scanf_s , fscanf_s, and sscanf_s are only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including stdio.h._ Source: https://en.cppreference.com/w/c/io/fscanf . Look at the example code they have further down the page. – yano Dec 27 '20 at 23:17
  • @yano [Not quite](https://port70.net/~nsz/c/c11/n1570.html#K.3.1.1p3): "It is implementation-defined whether the functions, macros, and types declared or defined in K.3 and its subclauses are declared or defined by their respective headers if `__STDC_WANT_LIB_EXT1__` is not defined as a macro at the point in the source file where the appropriate header is first included." Also see [paragraph 1](https://port70.net/~nsz/c/c11/n1570.html#K.3.1.1p1) and [paragraph 2](https://port70.net/~nsz/c/c11/n1570.html#K.3.1.1p2). cppreference.com is better than cplusplus.com, but both have issues – Andrew Henle Dec 28 '20 at 01:39
  • 1
    See also [Do you use the TR 24731 'safe' functions?](https://stackoverflow.com/questions/372980/do-you-use-the-tr-24731-safe-functions) – Jonathan Leffler Dec 28 '20 at 02:55

2 Answers2

4

I'm having trouble using the scanf_s function. I've tried <stdio.h> as well as <stdlib.h>. Is there anything in the standard library that includes this?

scanf_s() is part of C since C11. It is in "Annex K, Bounds-checking interfaces" and it is optional. Test __STDC_LIB_EXT1__ for its availability.

An implementation that defines __STDC_LIB_EXT1__ shall conform to the specifications in this annex. C17dr § K.2 2

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • @EdHeal True about what OP reports as using, yet rest of question is generic and the question is so tagged. If Dev-C++ 5.11 with its option set defines `__STDC_LIB_EXT1__`, then proper support for `scanf_s()` is expected. If not, there may be other ways to get get support, albeit perhaps not the exact same functionality as MS's `scanf_s()` differs from C11's in some argument types. In particular, the type of the size argument may differ. – chux - Reinstate Monica Dec 27 '20 at 23:40
  • *In particular, the type of the size argument may differ.* There's no "may" about it - [per the C standard](https://port70.net/~nsz/c/c11/n1570.html#K.3.5.3.2p4): "That argument is immediately followed in the argument list by the second argument, which has type `rsize_t` and gives the number of elements in the array pointed to by the first argument of the pair." – Andrew Henle Dec 28 '20 at 03:51
  • (cont) [Microsoft botches `scanf_s()` pretty spectacularly](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/scanf-s-scanf-s-l-wscanf-s-wscanf-s-l): "The size parameter is of type unsigned, not size_t. Use a static cast to convert a size_t value to unsigned for 64-bit build configurations." The standard-required type `rsize_t` isn't even mentioned. – Andrew Henle Dec 28 '20 at 03:51
4

scanf_s and other Annex K functions are effectively Microsoft-only.

As noted by others, they're optional functions, but the only widely-used development environment that implements them are those from Microsoft, and Microsoft's implementation is non-conforming and not portable.

Per N1967 - Field Experience With Annex K — Bounds Checking Interfaces](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm):

...

Available Implementations

Despite the specification of the APIs having been around for over a decade only a handful of implementations exist with varying degrees of completeness and conformance. The following is a survey of implementations that are known to exist and their status.

While two of the implementations below are available in portable source code form as Open Source projects, none of the popular Open Source distribution such as BSD or Linux has chosen to make either available to their users. At least one (GNU C Library) has repeatedly rejected proposals for inclusion for some of the same reasons as those noted by the Austin Group in their initial review of TR 24731-1 N1106]. It appears unlikely that the APIs will be provided by future versions of these distributions.

Microsoft Visual Studio

Microsoft Visual Studio implements an early version of the APIs. However, the implementation is incomplete and conforms neither to C11 nor to the original TR 24731-1. For example, it doesn't provide the set_constraint_handler_s function but instead defines a _invalid_parameter_handler _set_invalid_parameter_handler(_invalid_parameter_handler) function with similar behavior but a slightly different and incompatible signature. It also doesn't define the abort_handler_s and ignore_handler_s functions, the memset_s function (which isn't part of the TR), or the RSIZE_MAX macro.The Microsoft implementation also doesn't treat overlapping source and destination sequences as runtime-constraint violations and instead has undefined behavior in such cases.

As a result of the numerous deviations from the specification the Microsoft implementation cannot be considered conforming or portable.

NB the conclusion: As a result of the numerous deviations from the specification the Microsoft implementation cannot be considered conforming or portable.

So if you use these functions on a Microsoft compiler, you'll wind up writing non-portable code.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56