Sarah H.
Sarah H.

Reputation: 15

C: I keep getting errors that say error: expected expression

I keep getting five errors in my code that say error: expected expression when talking about the variables in my function. I have looked on this site and other places but I can't find what's wrong.

Here's the code:

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

Here's the errors I keep getting:

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 )

Upvotes: 0

Views: 1338

Answers (3)

Sourav Ghosh
Sourav Ghosh

Reputation: 134346

The function call syntax only takes the variable name, not the type.

separate_digits (long int n);

should be

separate_digits (n);

Also, print_array ( int a[10] ); is wrong, you only need to pass the array name to pass the array, like

  print_array ( a );

and so on.

Related, quoting C11, chapter §6.5.2.1, Postfix operators, function call syntax

postfix-expression ( argument-expression-listopt )

and

argument-expression-list:
assignment-expression
argument-expression-list , assignment-expression

there, the "type" is not to be mentioned in argument list, it is

An argument may be an expression of any complete object type.

In other words, each element in the argument-expression-list need to have a type which is not to be written.

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234715

separate_digits (long int n); is not syntactically correct. This is confusing the compiler.

Drop the long int to give separate_digits (n);

Upvotes: 0

Alnitak
Alnitak

Reputation: 339846

You shouldn't be prepending your variables with a type name when you actually pass them to a function.

That's only required in the declarations for those functions, as you've done near the top of the code.

Upvotes: 1

Related Questions