1

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);
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Sinn0n
  • 61
  • 2

1 Answers1

1

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.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226