C error: function definition is not allowed here, i already put function above int main(void), erased brackets, error still persists

C error: function definition is not allowed here, i already put function above int main(void), erased brackets, error still persists

我正在尝试解决这个名为 Credit 的 CS50 问题,我是编码新手,所以如果您能帮我找出我在这里输入的错误,我将不胜感激。我试图计算用户提示的数字的位数,编译后我一直收到此错误。错误说 ab.c:20:5: error: function definition is not allowed here.谢谢你的回答!

#include <stdio.h>
#include <cs50.h>

int get_number_digits(long x);
int main(void)

{

    //Prompt for input
    long digits = get_long("Card Number: \n");

    //Count the digits 
    int count = get_number_digits(digits);
    


    //Function for getting number of digits
    int get_number_digits(long x)
    {
         int number;
         for (number = 0; x > 0; number++)
      {
         x = x / 10;
      }
    }
}

嵌套函数不是标准 C 的一部分。但是它们可能会起作用,具体取决于您使用的编译器。所以最好将 get_number_digits 函数放在 main.

之外

并且您忘记将 return 语句放入 get_number_digits 函数中。

#include <stdio.h>
#include <cs50.h>

int get_number_digits(long x);
int main(void)

{

    //Prompt for input
    long digits = get_long("Card Number: \n");

    //Count the digits 
    int count = get_number_digits(digits);

}

//Function for getting number of digits
int get_number_digits(long x)
{
     int number;
     for (number = 0; x > 0; number++)
  {
     x = x / 10;
  }
  return number;
}