开发学院软件开发VC 排除 vs2005 中的不安全函数警告 阅读

排除 vs2005 中的不安全函数警告

 2008-11-12 19:27:50 来源:WEB开发网 闂傚倷绶氬ḿ褍螞閹绢喖绠柨鐕傛嫹闂傚倷绀侀幉锟犲垂閻㈠灚宕查柟鎵閸庡秵銇勯幒鎴濃偓鐢稿磻閹炬枼妲堟繛鍡楃С濞岊亞绱撻崒姘扁枌闁瑰嚖鎷�婵犵數濮幏鍐川椤撴繄鎹曢梻渚€娼уú銈吤洪妸鈺佺劦妞ゆ帊鑳堕埊鏇㈡煏閸モ晛浠х紒杈╁仱閺佹捇鏁撻敓锟�闂傚倷绶氬ḿ褍螞閹绢喖绠柨鐕傛嫹  闂傚倷鑳舵灙缂佺粯顨呴埢宥夊即閵忕姵鐎梺缁樺姇閻忔氨鈧凹鍓熷娲垂椤曞懎鍓伴梺閫炲苯澧紒澶婄秺瀵濡歌閸嬫捇妫冨☉娆忔殘闂佷紮缍€娴滎剟鍩€椤掑倹鏆柛瀣躬瀹曚即寮借閺嗭箓鏌ㄩ悤鍌涘
核心提示:下面的代码:#include <stdio.h>#include <minmax.h>int main( ){int a,b,c;scanf("%d,%d",&a,&b);c=max(a,b);printf("max=%d",c);return 0;}使用

下面的代码:

#include <stdio.h>
#include <minmax.h>
int main( )
{
  int a,b,c;
  scanf("%d,%d",&a,&b);
  c=max(a,b);
  printf("max=%d",c);
  return 0;
}

使用vs2005编译时会遇到这样一个warning: warning C4996: 'scanf' was declared deprecated

其实 warning C4996的详细含义就是:'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.翻译过来,就是scanf的声明在VS2005中被认为是不安全的,让你使用scanf_S来代替。

知道了原因,那解决就方便了,只要在#include <stdio.h>前面添加#define _CRT_SECURE_NO_DEPRECATE 或者 scanf函数修改为scanf_s即可。具体如下:

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <minmax.h>
int main( )
{
  int a,b,c;
  scanf("%d,%d",&a,&b);
  c=max(a,b);
  printf("max=%d",c);
  return 0;
}

或者

#include <stdio.h>
#include <minmax.h>
int main( )
{
  int a,b,c;
  scanf_s("%d,%d",&a,&b);
  c=max(a,b);
  printf("max=%d",c);
  return 0;
}

Tags:排除 vs 安全

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接