Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 当输入大于9时,为什么该程序会崩溃?_C - Fatal编程技术网

C 当输入大于9时,为什么该程序会崩溃?

C 当输入大于9时,为什么该程序会崩溃?,c,C,我不知道这为什么会在我的机器上崩溃,但确实如此 给您一个正整数N: 如果1条件a>=1 | | a=1&&a这是您的代码,请改进: #include <stdio.h> const char* itos2(int); // A proper C function prototype int main(void) { int a; // Checking the return value of scanf is *required*! // If it doe

我不知道这为什么会在我的机器上崩溃,但确实如此

给您一个正整数N:


如果1条件
a>=1 | | a=1&&a这是您的代码,请改进:

#include <stdio.h>

const char* itos2(int);

// A proper C function prototype
int main(void)
{ 
   int a;

   // Checking the return value of scanf is *required*!
   // If it doesn't return a value which is equal to the number of expected
   // assignments, then your variables are left unassigned, leading to 
   // Undefined Behavior!
   if (scanf("%i", &a) != 1) {
       fprintf(stderr, "Invalid input\n");
       return 1;
   }

   // It's usually better practice to not have to worry about a valid range
   // of inputs when calling a function. Let this function worry about the
   // input checking.
   printf("%s", itos2(a));

   return 0;
}

const char* itos2(int a)
{
    const char* str [] = {
        "zero",
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine",
    };

    // Check boundary conditions. Checking just one boundary case and 
    // returning is simpler and less error-prone than your previous code.
    if (a < 0)
        return "Less than zero";
    if (a > 9)
        return "Greater than nine";

    // Why bother with a switch/case statement when you can simply
    // use the input value to index directly into an array of the strings?
    return str[a];
}

您的条件
(a>=1 | | a=0
@alvits为什么在注释中而不是在实际答案中回答?谢谢,现在我明白了为什么默认标签会导致问题,我的编译器没有抛出任何“控件到达无效语句结尾警告”,所以我责怪tooDo没有将解决方案编辑到问题中。现在看起来像是您问的“我有这个代码:但它不起作用。”我会撤销你所有的编辑,但因为很难说,我可能已经解开了一些本该留下的东西。在这种情况下,请随意重新做。我喜欢这种优化!@hec一定要告诉你的老师答案来自stackoverflow-credit where credit is due:-)我会把它添加到源代码中,问题来自黑客等级
#include <stdio.h>

const char* itos2(int);

// A proper C function prototype
int main(void)
{ 
   int a;

   // Checking the return value of scanf is *required*!
   // If it doesn't return a value which is equal to the number of expected
   // assignments, then your variables are left unassigned, leading to 
   // Undefined Behavior!
   if (scanf("%i", &a) != 1) {
       fprintf(stderr, "Invalid input\n");
       return 1;
   }

   // It's usually better practice to not have to worry about a valid range
   // of inputs when calling a function. Let this function worry about the
   // input checking.
   printf("%s", itos2(a));

   return 0;
}

const char* itos2(int a)
{
    const char* str [] = {
        "zero",
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine",
    };

    // Check boundary conditions. Checking just one boundary case and 
    // returning is simpler and less error-prone than your previous code.
    if (a < 0)
        return "Less than zero";
    if (a > 9)
        return "Greater than nine";

    // Why bother with a switch/case statement when you can simply
    // use the input value to index directly into an array of the strings?
    return str[a];
}
gcc -Wall -Werror ...