Reputation: 21
I've already created the macros for the assignment, but cannot find the syntax error I get when running the program. Any ideas would be greatly appreciated, thanks.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define ODD(X) ((X) & 01)
#define BITON(X,N) (((X) >> N) & 01)
#define ALLON(X,S,E) (((X) & ((((int) pow(2,(E-S))-1) << (E-S))) ^ (((int) pow(2,E-S)-1) << (E-S)))
//-----------------------------------------------------------------------------
int main(void) {
unsigned int U1,BitNumber,Start,End;
printf("Enter an integer : ");
scanf("%ud",&U1);
printf("%u is %s\n",U1,ODD(U1)?"odd":"even");
printf("Enter an integer and a bit number : ");
scanf("%u %d",&U1,&BitNumber);
printf("%u has bit %d %s\n",U1,BitNumber,BITON(U1,BitNumber)?"on":"off");
printf("Enter an integer, start and end bit numbers : ");
scanf("%u %u %u",&U1,&Start,&End);
printf("%u has %s those bits on\n",U1,ALLON(U1,Start,End)?"all":"not all");
return(EXIT_SUCCESS);
}
//-----------------------------------------------------------------------------
Error:
BitOps.c: In function ‘main’:
BitOps.c:23:77: error: expected ‘)’ before ‘;’ token
printf("%u has %s those bits on\n",U1,ALLON(U1,Start,End)?"all":"not all");
^
BitOps.c:26:1: error: expected ‘;’ before ‘}’ token
}
Upvotes: 0
Views: 102
Reputation: 140609
The macro definition of ALLON
has an extra open parenthesis in it. The compiler can't be sure that it's wrong until it reaches the semicolon at the end of the line where ALLON
is used, so the error message complains about that line (which is fine) rather than the definition of ALLON
, but the definition of ALLON
is where the problem is.
Upvotes: 1
Reputation: 11921
Enable -Wall
flags and compile the code, its very informative, don't ignore it. There are few bugs in your code as
01
replace with 0x1
that serve actual purpose.BitNumber
is declared as unsigned
but %d
format specifier is used. Compile your code with -Wall
flag.ALLON()
macro expansion is not correct, which I correctly added below.Here is expected one
#define ODD(X) ((X) & 0x1)
#define BITON(X,N) (((X) >> N) & 0x1)
#define ALLON(X,S,E) (((X) & ( ((((int) pow(2,(E-S))-1) << (E-S))) ^ (((int) pow(2,E-S)-1) << (E-S)))) )
int main(void) {
unsigned int U1,BitNumber,Start,End;
printf("Enter an integer : ");
scanf("%u",&U1);
printf("%u is %s\n",U1,ODD(U1)?"odd":"even");
printf("Enter an integer and a bit number : ");
scanf("%u %u",&U1,&BitNumber);
printf("%u has bit %d %s\n",U1,BitNumber,BITON(U1,BitNumber)?"on":"off");
printf("Enter an integer, start and end bit numbers : ");
scanf("%u %u %u",&U1,&Start,&End);
printf("%u has %s those bits on\n",U1,ALLON(U1,Start,End) ?"all":"not all");
return(EXIT_SUCCESS);
}
Upvotes: 0