简体   繁体   中英

Implicit declaration of function 'scan_s' [-Wimplicit-function-declaration]

I've been looking at other questions and none of the solutions have worked so I'll ask my own question.

I'm working on a linux VM and having trouble compiling my code, here are my includes, the error received by the compiler and the code its referring to:

Error:

linux.c:156:11: warning: implicit declaration of function 'scan_s' [-Wimplicit-function-declaration]

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "ctype.h"

scanf_s("%[^\n]s", filename, maxFilename);

scanf_s is part of the bounds-checking extension that your compiler may not support. Use #ifdef __STDC_LIB_EXT1__ to see if your library/implementation supports it -- if not, fall back to using something like

char fmt[16];
sprintf(fmt, "%%%d[^\n]", maxFilename);
scanf(fmt, maxFilename, filename);

or

fgets(filename, maxFilename, stdin);
if (char *p = strchr(filename, '\n')) *p = '\0';

instead.

Note that the s in the format string of your example is non-sensical and will not match anything (any s will be absorbed by the %[^\\n] as it is not a newline.

As far as I know, only Microsoft compilers support this extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM