0

I am still quite new to C, and I keep getting an error in Code Blocks which stops me from running my programs. I get the error "implicit declaration of functions printf_s() and scanf_s(). Here is my code:

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>

int main(void)
{
  int age = 0;
  char name[20];

  printf_s("Enter your age: ");
  scanf_s("%d", &age);

  print_s("Enter your name: ");
  scanf_s("%s", name, sizeof(name));

  printf_s("Your name is %s and you are %d years old.\n", name, age);

  return 0;
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
ThE411
  • 33
  • 8

3 Answers3

3

printf_s and scanf_s are only available if __STDC_LIB_EXT1__ is defined by the library implementation. It is added since C11 standard.

First you have to check __STDC_LIB_EXT1__ is defined or not, then only you should use printf_s or scanf_s.

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
int main(void)
{
  int age = 0;
  char name[20];
  #ifdef __STDC_LIB_EXT1__
      printf_s("Enter your age: ");
      scanf_s("%d", &age);
      print_s("Enter your name: ");
      scanf_s("%s", name, sizeof(name));
      printf_s("Your name is %s and you are %d years old.\n", name, age);
  #else
      printf("Enter your age: ");
      scanf("%d", &age);
      print("Enter your name: ");
      scanf("%19s", name);
      printf("Your name is %s and you are %d years old.\n", name, age);
  #endif
  return 0;
}
jblixr
  • 1,345
  • 13
  • 34
1

The features you are using are "optional" according to the ISO C Standard.

Apparently, the compiler/library bundled with Code::Blocks does not implement this option. So your choices now are:

  • Don't use the features
  • Use a compiler which does implement this option
  • Use conditional code as shown in jblixr's answer

Also, you could log a feature request with MinGW (or MinGW-w64).

Why does the C Standard include optional functions? Typically this is the result of political processes; one faction on the standards committee wanted the feature and another didn't, so the compromise is to make the feature optional. Another example of this is variable-length arrays.

M.M
  • 138,810
  • 21
  • 208
  • 365
-2

Standard function names have no _s so the standard compliant version of the program would be

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
int main(void)
{
  int age = 0;
  char name[20];

  printf("Enter your age: ");
  scanf("%d", &age);

  print("Enter your name: ");
  scanf("%19s", name);

  printf("Your name is %s and you are %d years old.\n", name, age);

  return 0;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • I know the _s is pretty unnecessary here, but the book is forcing me to use the _s for future reference. I still need to fix the error. – ThE411 May 25 '16 at 03:29
  • 2
    Ok, read another book. You are likely using a compiler that expects standard code. Standard code SHOULD compile on any compiler while this variation the book is teaching you requires a specific compiler AFAIK, the MS C++ compiler (*it does compile [tag:c] code but it does not differentiate between the languages I think*). If the book is forcing you to learn that it's IMHO not a good book. A good book would teach the basics and standards only. Giving you freedom to choose when you gain experience. – Iharob Al Asimi May 25 '16 at 03:31