C:我不断收到错误说错误:预期表达

时间:2016-11-02 15:41:17

标签: c compiler-errors syntax-error function-calls

我的代码中不断出现五个错误:错误:在讨论我函数中的变量时的预期表达式。我看过这个网站和其他地方,但我找不到什么错误。

以下是代码:

#include <stdio.h>

#define TRUE 1
#define FALSE 0

void separate_digits (long int n);
void print_array ( int a[10] );
int digits_different ( int a[10] );
int divisible ( int a[10], int n );

int main ()
{
    long int n;

printf("Enter a positive integer or 0 (zero) to end:");
scanf("%ld\n", &n);

while ( n != 0)
{
    if (n < 0)
    {
        printf("Wrong input\n");
    }

    else
    {
        separate_digits (long int n);
    }

    printf("Enter a positive integer or 0 (zero) to end:");
    scanf("%ld\n", &n);
}

printf("*** Program Terminated ***\n");

}

void separate_digits (long int n)
{
    int a[10] = {0};
    int digit;
    int num = n;

while ( num > 0)
{
    digit = num % 10;

    ++a[digit];

    num = num / 10;
}

print_array ( int a[10] );

if ( a[0] != 0 )
{
    printf("Wrong input for the second part.\n");
    printf("Input should not contain zero.\n");
}

else if ( digits_different ( int a[10] ) == FALSE )
{
    printf("Wrong input for the second part.\n");
    printf("Input should not contain each digit more than once.\n");
}

else if ( divisible ( int a[10], int n ) == FALSE )
{
    printf("%1ld is not divisible by its digits.\n", n);
}

else
{
    printf("%1ld is divisible by its digits.\n", n);
   }
}

void print_array ( int a[10] )
{
int i;

printf("\n\n\n");
printf("Digits:         0   1   2   3   4   5   6   7   8   9\n");
printf("Occurrences:    ");

for (i = 0; i < 10; i++)
{
    printf("a[i]   ");
}
}

 int digits_different ( int a[10] )
{
int i;

for (i = 0; i < 10; i++)
{
    if (a[i] > 1)
        return FALSE;

    else
        return TRUE;
}
}

int divisible ( int a[10], int n )
{
int i;
int num;

for (i = 0; i < 10; i++)
{
    if (a[i] == 0)
        continue;
    else if (num % i != 0)
        return FALSE;
}

return TRUE;
}

以下是我不断遇到的错误:

Lab_Assignment_6_Sarah_H.c:27:21: error: expected expression
         separate_digits (long int n);
                                     ^
Lab_Assignment_6_Sarah_H.c:53:16: error: expected expression
         print_array ( int a[10] );
                  ^
Lab_Assignment_6_Sarah_H.c:61:31: error: expected expression
        else if ( digits_different ( int a[10] ) == FALSE )
                                 ^
Lab_Assignment_6_Sarah_H.c:67:24: error: expected expression
        else if ( divisible ( int a[10], int n ) == FALSE )
                          ^
Lab_Assignment_6_Sarah_H.c:67:35: error: expected expression
        else if ( divisible ( int a[10], int n ) == FALSE )

3 个答案:

答案 0 :(得分:1)

当您将变量实际传递给函数时,不应该使用类型名称添加变量。

只有声明中才需要这些功能,因为您已经在代码顶部附近完成了。

答案 1 :(得分:1)

函数调用语法只接受变量名,而不是类型。

separate_digits (long int n);

应该是

separate_digits (n);

另外,print_array ( int a[10] );是错误的,你只需要传递数组名来传递数组,比如

  print_array ( a );

等等。

相关,引用C11,章节§6.5.2.1,后缀运算符,函数调用语法

  

postfix-expression(argument-expression-list opt

  

参数表达式列表:
  赋值表达式
   argument-expression-list,assignment-expression

那里,参数列表中没有提到“type”,它是

  

参数可以是任何完整对象类型的表达式。

换句话说, argument-expression-list 中的每个元素都需要有一个不能的类型。

答案 2 :(得分:0)

separate_digits (long int n);在语法上不正确。这让编译器感到困惑。

放弃long int以提供separate_digits (n);