我是C++的新手,我正在尝试构建这个非常简单的代码,但我不明白为什么会出现这个错误:
Error 1 error C2664: 'int scanf(const char *,...)' : cannot convert argument 1 from 'int' to 'const char *'
Run Code Online (Sandbox Code Playgroud)
这是代码:
// 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;
}
Run Code Online (Sandbox Code Playgroud)
更改scanf('%d', &row);到
scanf("%d", &row);
Run Code Online (Sandbox Code Playgroud)
'%d'是一个类型的多字符文字int.
"%d"另一方面是字符串文字,它与第一个参数的const char *预期兼容scanf.
如果您传递单引号%d,编译器将尝试从int(类型'%d')到const char *(如scanf所预期的)进行隐式转换,并且将失败,因为不存在此类转换.
| 归档时间: |
|
| 查看次数: |
3396 次 |
| 最近记录: |