Can't typecast to int in C; Error: Expected expression before int

int arr5[8]={10,20,30,40,50,60,70,80};
len=8;
int beg=0,mid,end=len-1,loc;
mid=int((beg+end)/2);
printf("%d",mid);

I was trying to write a code for binary search but can’t typecast the value of (beg+end)/2 to mid.

 error: expected expression before 'int'
         mid=int((beg+end)/2);
             ^~~

>Solution :

For starters using the type specifier int in this statement

mid=int((beg+end)/2);

does not make a sense.

It is enough to write

mid=(beg+end)/2;

As all operands in the expression in the right side of the assignment have the type int then the evaluated value also has the type int.

As for the casting then in C you have to write

mid= ( int )((beg+end)/2);

Leave a Reply