C++ ---错误C2664:'int scanf(const char*,...)':无法将参数1从'int'转换为'const char*'

Rém*_*émi 4 c++ scanf

我是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)

Moh*_*ain 6

更改scanf('%d', &row);

scanf("%d", &row);
Run Code Online (Sandbox Code Playgroud)

'%d'是一个类型的多字符文字int.

"%d"另一方面是字符串文字,它与第一个参数的const char *预期兼容scanf.

如果您传递单引号%d,编译器将尝试从int(类型'%d')到const char *(如scanf所预期的)进行隐式转换,并且将失败,因为不存在此类转换.