Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading