0

I was having some problem for my first C program:

#include "stdafx.h"
#include <stdio.h>

int main()
{
    float farenheit, celsius;

    printf("Enter the temperature in degree F: ");
    scanf("%f", &farenheit);

    celsius = ((float)5 / 9) * (farenheit - 32);
    printf("Converted degree in C: %f", celsius);
    return 0;
}

And in my stdafx.c file, I included the no secure warning:

#include "stdafx.h"
#define _CRT_SECURE_NO_WARNINGS

However, when I tried to run the program, it threw me this error message:

scanf is unsafe. Consider using scanf_s instead. To disable deprecation, use the CRT_SECURE_NO_WARNNGS.

I thought I already included it into the stdafx.c file?

chux - Reinstate Monica
  • 127,356
  • 13
  • 118
  • 231
QWERTY
  • 2,227
  • 9
  • 37
  • 74
  • It should probably be in your main program, not in the stdafx.c. Anyway, please check the duplicate link for preferred ways to solve this issue. (Visual Studio is unsafe. Consider using gcc instead.) – Lundin Aug 13 '15 at 09:47
  • The macro _CRT_SECURE_NO_WARNINGS should be placed before include of stdio.h file. So, in your case, just move your #define of _CRT_SECURE_NO_WARNINGS to the file "stdafx.h". If you include "stdafx.h" before the other headers, this will resolve your issue. – Kirill Aug 13 '15 at 09:49
  • 1
    I see. That solved it but do you have any ideas why it should be put in front? – QWERTY Aug 13 '15 at 09:52
  • Because deep inside the file stdio.h there is a preprocessor check, if the macro "_CRT_SECURE_NO_WARNINGS" is not defined, warning should be issued. That's why this macro should be defined before include of stdio.h – Kirill Aug 13 '15 at 09:54
  • I see, alright thanks a lot for the helps! – QWERTY Aug 13 '15 at 09:58
  • 2
    @Lundin I don't agree that this is a duplicate question. Here Denise asks what's wrong in his concrete case and how this macro works "under the hood". While in the other question the main point is mainly how to define the macro _CRT_SECURE_NO_WARNINGS globally without including it in every source file. – Kirill Aug 13 '15 at 10:00

0 Answers0