main.c:12:25: 警告:格式“%ld”需要“long int”类型的参数,
main.c:12:25: warning: format ‘%ld’ expects argument of type ‘long int’,
我在尝试编译我的代码时遇到此错误...
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
void powRec(long int firstrNum, long int secondNum)
{
int n;
if (secondNum == 1)
;
{
printf("The result is %ld", firstrNum);
return;
}
n = firstrNum * powRec(firstrNum, secondNum - 1);
printf("The result is %ld.", n);
}
int main()
{
powRec(-2, 3);
return 0;
}
我想知道问题出在 printf 上还是我遗漏了什么?
如消息所述,您正在通过将错误类型的数据传递给 printf()
.
来调用 未定义的行为
%ld
期望 long int
,但传递的 n
是 int
。您应该使用 %d
而不是打印 int
。
另一种选择是将 n
的类型更改为 long int
以匹配格式说明符。
另请注意:
- 您应该使用缩进使您的代码更易于阅读。
powRec
的return类型是void
,所以n = firstrNum * powRec(firstrNum, secondNum - 1);
行(尤其是乘法)无效
if (secondNum == 1); {
中的分号可能会让您的代码表现出与您预期的不同。
我在尝试编译我的代码时遇到此错误...
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
void powRec(long int firstrNum, long int secondNum)
{
int n;
if (secondNum == 1)
;
{
printf("The result is %ld", firstrNum);
return;
}
n = firstrNum * powRec(firstrNum, secondNum - 1);
printf("The result is %ld.", n);
}
int main()
{
powRec(-2, 3);
return 0;
}
我想知道问题出在 printf 上还是我遗漏了什么?
如消息所述,您正在通过将错误类型的数据传递给 printf()
.
%ld
期望 long int
,但传递的 n
是 int
。您应该使用 %d
而不是打印 int
。
另一种选择是将 n
的类型更改为 long int
以匹配格式说明符。
另请注意:
- 您应该使用缩进使您的代码更易于阅读。
powRec
的return类型是void
,所以n = firstrNum * powRec(firstrNum, secondNum - 1);
行(尤其是乘法)无效if (secondNum == 1); {
中的分号可能会让您的代码表现出与您预期的不同。