0

I downloaded MinGW-64 and ran this little snippet of code (shortened as it was originally a stack, queue and other DS implementations):

#include <iostream>
#include <cstdio>

int main()
{
    const int _sizeS = 20;
    .
    .
    .
    int _tmp;
    printf_s("\nEnter an alpha-numeric character to quit: ");
    
    std::cin >> _tmp;
    return 0;
}

This compiles perfectly in VS2019, and does what I want it to.

However, I use g++ pyrmd.cpp at the command line and get an error:

pyrmd.cpp: In function 'int main()':
pyrmd.cpp:6:2: error: 'printf_s' was not declared in this scope; did you mean 'printf'?
    6 |  printf_s("\n Enter a character: \n");
      |  ^~~~~~~~
      |  printf

Does anyone have a clue as to why this is happening, and how to resolve it?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
SuperTC
  • 1
  • 1
  • 1
    it means the compiler is unable to find printf_s function and probably it is a typo you probably want printf and not printf_s – Smit Shah May 30 '21 at 14:38
  • 1
    There is no [printf_s](https://en.cppreference.com/w/cpp/io/c/fprintf) function in standard C++. – PaulMcKenzie May 30 '21 at 14:39
  • The `printf_s` function was only made part of the [Standard C library at C11](https://en.cppreference.com/w/c/io/fprintf). Maybe you need to set your g++ compiler to use that. – Adrian Mole May 30 '21 at 14:39
  • https://stackoverflow.com/questions/50724726/why-didnt-gcc-or-glibc-implement-s-functions – interjay May 30 '21 at 14:43
  • @SmitShah Just checked back. The `stdio.h` of MinGW64 doesn't have the print_s version, but the VS2019 libraries do. So, yeah my mistake. – SuperTC May 30 '21 at 14:43
  • @PaulMcKenzie There's no [printf_s] in the MinGW64 _include libraries_. It is, however, present in the `stdio.h` of VS2019 libraries. Just looked into the header files in both of them. – SuperTC May 30 '21 at 14:47
  • @interjay Yup. Just discovered it myself. My bad. Guess, I'll stick VS2019 for now. – SuperTC May 30 '21 at 14:48
  • @AdrianMole As the person just below referenced a previous post and I found out looking at the header files in both GCC and VS2019, the [printf_s] is not present in GCC. Also, you're right. Just changed the compiler settings in VS2019 and got an error mountain. – SuperTC May 30 '21 at 14:52
  • You can just switch to the portable `printf` instead. – interjay May 30 '21 at 14:56
  • @SuperTC: Answers should be posted in the "Add Answer" section; they should never be edits to the question. It's OK to post an answer to your own question. – Nicol Bolas May 30 '21 at 14:59
  • @NicolBolas Will keep that in mind next time. Sorry, I'm noob at the moment. – SuperTC May 30 '21 at 15:01
  • @SuperTC My point is that there is no *standard* C++ function called `printf_s`. A C++ compiler need not implement it -- there is no mandate to include this function. Also, you cannot determine if there is an actual implementation of `printf_s` or any other function just because you see a declaration in the header. All a header does is allow your code to *compile*. When it comes to linking, that function actually needs to exist, and not just as a prototype or declaration. So looking at a header and saying "that function exists" is not totally correct. – PaulMcKenzie May 30 '21 at 16:36
  • @PaulMcKenzie Yup, you're right. But, widely used and officially (or rather, from Verified Publisher) documented functions can be verified by looking at the header files. If the function is deprecated, the compiler will produce a warning. My error was the lack of declaration in the given scope. And while the ```printf_s``` function is standard c library function, it was ported to c++ using the ```cstdio``` header. I was just messing around anyway. If it was an official project, I would have made different c files and c++ files and used functions that were more readily portable. – SuperTC May 30 '21 at 17:32

1 Answers1

0

Figured out myself and confirmed from the comments that the problem was with GCC itself.

The include libraries of GCC don't have the declarations for printf_s. Both the header files stdio.h and cstdio lack the declarations. VS2019 however has the MSVC compiler which provides the _s functions.

SuperTC
  • 1
  • 1