我分配了一个 long 变量,但错误消息声称它是一个 int
I assigned a long variable but the error message claims it´s an int
#include <stdio.h>
#include <cs50.h>
int main(void)
{
long card;
do
{
card = get_long("Type crdit card number\n");
}
while (card < 0);
long c = card;
int i = 1;
for (long o = 10; c >= 10;o *= 10, i++)
{
c = c / 10;
}
for (int h = 0, o = 10; h < i; h++, o *= 10)
{
c = card;
c = c % o;
printf("%ld\n", c);
printf("%ld\n", o);
}
}
我正在做一项要求我处理信用卡号码的作业,所以我一直在使用长变量,例如第 14 行的 'o',但我不断收到“格式指定type 'long' 但当我尝试打印时,参数的类型为 'int'" 作为错误消息,我不知道为什么。我对编程很陌生,所以这可能只是初学者的错误,所以非常感谢任何帮助。
循环中
for (int h = 0, o = 10; h < i; h++, o *= 10)
{
c = card;
c = c % o;
printf("%ld\n", c);
printf("%ld\n", o);
}
o
声明为 int
。您还应该将其声明为 long
.
for (long h = 0, o = 10; h < i; h++, o *= 10) /* use long instead of int */
{
c = card;
c = c % o;
printf("%ld\n", c);
printf("%ld\n", o);
}
#include <stdio.h>
#include <cs50.h>
int main(void)
{
long card;
do
{
card = get_long("Type crdit card number\n");
}
while (card < 0);
long c = card;
int i = 1;
for (long o = 10; c >= 10;o *= 10, i++)
{
c = c / 10;
}
for (int h = 0, o = 10; h < i; h++, o *= 10)
{
c = card;
c = c % o;
printf("%ld\n", c);
printf("%ld\n", o);
}
}
我正在做一项要求我处理信用卡号码的作业,所以我一直在使用长变量,例如第 14 行的 'o',但我不断收到“格式指定type 'long' 但当我尝试打印时,参数的类型为 'int'" 作为错误消息,我不知道为什么。我对编程很陌生,所以这可能只是初学者的错误,所以非常感谢任何帮助。
循环中
for (int h = 0, o = 10; h < i; h++, o *= 10)
{
c = card;
c = c % o;
printf("%ld\n", c);
printf("%ld\n", o);
}
o
声明为 int
。您还应该将其声明为 long
.
for (long h = 0, o = 10; h < i; h++, o *= 10) /* use long instead of int */
{
c = card;
c = c % o;
printf("%ld\n", c);
printf("%ld\n", o);
}