我是C ++的新手,正在尝试构建此非常简单的代码,但我不明白为什么会收到此错误:
Error 1 error C2664: 'int scanf(const char *,...)' : cannot convert argument 1 from 'int' to 'const char *'
这是代码:
// lab.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
int main(int argc, char* argv[])
{
int row = 0;
printf("Please enter the number of rows: ");
scanf('%d', &row);
printf("here is why you have entered %d", row);
return 0;
}
更改scanf('%d', &row);
为
scanf("%d", &row);
'%d'
是类型为的多字符文字int
。
"%d"
另一方面是字符串文字,它与第一个参数const char *
所期望的兼容scanf
。
如果您传递单引号%d
,则编译器将尝试进行从int
(的类型'%d'
)到const char *
(由scanf期望的)隐式转换,并且将失败,因为不存在这种转换。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句