Error "{ Expected After If Statement"

How do I fix error: expected ')' for an 'if' statement?

Here is a working program with fixed logic and syntax.

#include <stdio.h>

/* check if number is odd or even */

/**
* returns 1 if number is odd, 0 otherwise
*/
int oddOrEven(int p)
{
return (1 & p);
}

int main()
{

if (oddOrEven(3))
printf("Odd");
else
printf("Even");

return 0;
}

Syntax changes:

  • Function oddOrEven() moved before main(). You can also write the function prototype and keep the definition at the original position.
  • Changed void oddoreven(int 3); to oddOrEven(3). This is the proper syntax for a function call.

Logic explained:

You wrote

something like n = 1111 1110 | x results in n = 1111 1111 if last bit of x is 1 hence, if ~n = 0 then number is odd

However,

it depends on the size of your integer type.

n = 1111 1110 | x results in n = 0011 1111 1111 if x is 0011 1100 1111.

For n = 1111 1111, ~n = 0 only if the size of n is 8 bits.

In binary, the number 1 means a certain number of zeros with 1 at the end.

& is the bitwise and operator.

Hence,

1 & n will result in 1 if and only if the last bit of x is 1.

error: expected declaration or statement at end of input in c

Normally that error occurs when a } was missed somewhere in the code, for example:

void mi_start_curr_serv(void){
#if 0
//stmt
#endif

would fail with this error due to the missing } at the end of the function. The code you posted doesn't have this error, so it is likely coming from some other part of your source.

C error: Expected expression before int

This is actually a fairly interesting question. It's not as simple as it looks at first. For reference, I'm going to be basing this off of the latest C11 language grammar defined in N1570

I guess the counter-intuitive part of the question is: if this is correct C:

if (a == 1) {
int b = 10;
}

then why is this not also correct C?

if (a == 1)
int b = 10;

I mean, a one-line conditional if statement should be fine either with or without braces, right?

The answer lies in the grammar of the if statement, as defined by the C standard. The relevant parts of the grammar I've quoted below. Succinctly: the int b = 10 line is a declaration, not a statement, and the grammar for the if statement requires a statement after the conditional that it's testing. But if you enclose the declaration in braces, it becomes a statement and everything's well.

And just for the sake of answering the question completely -- this has nothing to do with scope. The b variable that exists inside that scope will be inaccessible from outside of it, but the program is still syntactically correct. Strictly speaking, the compiler shouldn't throw an error on it. Of course, you should be building with -Wall -Werror anyways ;-)


(6.7) declaration:
declaration-specifiers init-declarator-listopt ;
static_assert-declaration
(6.7) init-declarator-list:
init-declarator
init-declarator-list , init-declarator
(6.7) init-declarator:
declarator
declarator = initializer
(6.8) statement:
labeled-statement
compound-statement
expression-statement
selection-statement
iteration-statement
jump-statement
(6.8.2) compound-statement:
{ block-item-listopt }
(6.8.4) selection-statement:
if ( expression ) statement
if ( expression ) statement else statement
switch ( expression ) statement

Why am I getting IndentationError: expected an indented block?

As the error message indicates, you have an indentation error. It is probably caused by a mix of tabs and spaces.



Related Topics



Leave a reply



Submit