为什么scanf(%d-%d-%d)不起作用,它期望什么?[已关闭]

cwxwcias  于 2023-01-08  发布在  其他
关注(0)|答案(1)|浏览(98)

**已关闭。**此问题为not reproducible or was caused by typos。当前不接受答案。

这个问题是由打字错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
8小时前关门了。
Improve this question
我正在学习C语言的函数,我遇到了一个问题。
这是密码

#include <stdio.h>
#include <stdlib.h>

void equivalenza(int, int , int);

int main()
{
    int ore, minuti, secondi;
    printf("Inserisci ore, minuti e secondi (hh-mm-ss): ");
    scanf("%d-%d-%d", ore, minuti, secondi);
    equivalenza(ore, minuti, secondi);
    return 0;
}

void equivalenza(int ore, int minuti, int secondi)
{
    secondi = secondi + ore*3600;

    secondi = secondi + minuti*60;

    printf("\nSecondi = %d", secondi);
}

我想输出不会启动函数。

Inserisci ore, minuti e secondi (hh-mm-ss): 34-32-52

Process returned -1073741819 (0xC0000005)   execution time : 13.287 s
wfveoks0

wfveoks01#

你需要传递你要扫描的变量的地址。

#include <stdio.h>
#include <stdlib.h>

void equivalenza(int, int , int);

int main()
{
    int ore, minuti, secondi;
    printf("Inserisci ore, minuti e secondi (hh-mm-ss): ");
    scanf("%d-%d-%d", &ore, &minuti, &secondi);
    equivalenza(ore, minuti, secondi);
    return 0;
}

void equivalenza(int ore, int minuti, int secondi)
{
    secondi = secondi + ore*3600;

    secondi = secondi + minuti*60;

    printf("\nSecondi = %d", secondi);
}

您还应该检查scanf的返回值,并在结果后打印一个换行符。int main()应该是int main(void),并且没有必要包含stdlib.h

#include <stdio.h>

void equivalenza(int, int, int);

int main(void)
{
    int ore, minuti, secondi;
    printf("Inserisci ore, minuti e secondi (hh-mm-ss): ");
    if (scanf("%d-%d-%d", &ore, &minuti, &secondi) != 3) 
    {
        printf("Invalid input.\n");
        return 1;
    }

    equivalenza(ore, minuti, secondi);
    return 0;
}

void equivalenza(int ore, int minuti, int secondi)
{
    secondi = secondi + ore*3600;
    secondi = secondi + minuti*60;

    printf("\nSecondi = %d\n", secondi);
}

如果你编译时打开了警告,你应该会收到关于你原始代码的警告。

% gcc -Wall test.c
test.c:10:23: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat]
    scanf("%d-%d-%d", ore, minuti, secondi);
           ~~         ^~~
test.c:10:28: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat]
    scanf("%d-%d-%d", ore, minuti, secondi);
              ~~           ^~~~~~
test.c:10:36: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat]
    scanf("%d-%d-%d", ore, minuti, secondi);
                 ~~                ^~~~~~~
test.c:10:23: warning: variable 'ore' is uninitialized when used here [-Wuninitialized]
    scanf("%d-%d-%d", ore, minuti, secondi);
                      ^~~
test.c:8:12: note: initialize the variable 'ore' to silence this warning
    int ore, minuti, secondi;
           ^
            = 0
test.c:10:28: warning: variable 'minuti' is uninitialized when used here [-Wuninitialized]
    scanf("%d-%d-%d", ore, minuti, secondi);
                           ^~~~~~
test.c:8:20: note: initialize the variable 'minuti' to silence this warning
    int ore, minuti, secondi;
                   ^
                    = 0
test.c:10:36: warning: variable 'secondi' is uninitialized when used here [-Wuninitialized]
    scanf("%d-%d-%d", ore, minuti, secondi);
                                   ^~~~~~~
test.c:8:29: note: initialize the variable 'secondi' to silence this warning
    int ore, minuti, secondi;
                            ^
                             = 0
6 warnings generated.

相关问题