Error C4996: 'scanf': this function or variable may be unsafe in programming c

I created a small application to find the maximum number using a custom function with a parameter. When I run it, it displays this message

Error 1 error C4996: 'scanf': this function or variable may not be safe. Instead, consider using scanf_s. To disable obsolescence, use _CRT_SECURE_NO_WARNINGS. See the online help for more information.

What should I do to solve this problem?

This is my code.

#include<stdio.h> void findtwonumber(void); void findthreenumber(void); int main() { int n; printf("Fine Maximum of two number\n"); printf("Fine Maximum of three number\n"); printf("Choose one:"); scanf("%d", &n); if (n == 1) { findtwonumber(); } else if (n == 2) { findthreenumber(); } return 0; } void findtwonumber(void) { int a, b, max; printf("Enter a:"); scanf("%d", &a); printf("Enter b:"); scanf("%d", &b); if (a>b) max = a; else max = b; printf("The max is=%d", max); } void findthreenumber(void) { int a, b, c, max; printf("Enter a:"); scanf("%d", &a); printf("Enter b:"); scanf("%d", &b); printf("Enter c:"); scanf("%d", &c); if (a>b) max = a; else if (b>c) max = b; else if (c>a) max = c; printf("The max is=%d", max); } 
+5
source share
1 answer

This seems to be just a compiler warning.

Using scanf_s prevents a possible buffer overflow.
See: http://code.wikia.com/wiki/Scanf_s

A good explanation of why scanf can be dangerous: Disadvantages of scanf

So, as you said, you can try replacing scanf with scanf_s or disable the compiler warning.

+8
source

All Articles