【问题标题】:I assigned a long variable but the error message claims it´s an int我分配了一个长变量,但错误消息声称它是一个 int
【发布时间】:2021-01-08 02:26:37
【问题描述】:
#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”,但我不断收到“格式指定类型“长”但是当我尝试打印时,参数的类型为“int”作为错误消息,我不知道为什么。我对编程很陌生,所以这可能只是初学者的错误,所以非常感谢任何帮助。

【问题讨论】:

    标签: c variables integer long-integer cs50


    【解决方案1】:

    在循环中

    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);
    }
    

    【讨论】:

    • 非常感谢你,但是,有没有办法在同一个 for 循环中声明两个不同类型的变量?
    猜你喜欢
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多