5 Replies - 1973 Views - Last Post: 19 March 2013 - 03:36 PM Rate Topic: -----

#1 stickman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-March 13

error: invalid operands to binary.

Posted 19 March 2013 - 03:59 AM

#include <stdio.h>		/* give access to the stdio.h library */

#define Vc	6.5			/*Vc = 6.5v*/
#define Ve	1.3			/*Ve = 1.3v*/
#define Ic	3*10^(-3)	/*Ic = 3*10^(-3)A*/
#define Hfe	100

int main(void)
{
	float Vcc;
	float Vin;
	float R1;
	float R2;
	float R3;
	float R4;
	float Vout;
	float Ib;
	float Ks;
	float Vb;
	
	printf("Enter Vin: ");	/*Enter the input voltage in volts. 0.01 - 0.05*/
	scanf("%f",	   &Vin);
	
	printf("Enter Vcc: ");	/*Input of Vcc in volts. 9.0, 10.0 or 12.0*/
	scanf("%f",	&Vcc);
	
	Ib = Ic/Hfe;			/*Calculates Ib*/
	R1 = (Vcc -Vc)/Ic;		/*Calculates R1*/
	R2 = Ve /(Ic + Ib);	    /*Calculates R2*/
	Vb = Ve+0.7;			/*Calculates Vb*/	 
	R4 = Vb/(5+Ib);			/*Calculates R4*/
	R3 = (Vcc-Vb)/(6*Ib);	/*Calculates R3*/
	Ks = R1/R2;				/*Calculates Ks*/
	Vout = Vin*Ks;			/*Calculates Vout*/
	
	printf("Resistors: \n");
	printf("	R1 =	%.f ohm \n" , R1);
	printf("	R2 =	%.f ohm \n" , R2);
	printf("	R3 =	%.f ohm \n" , R3);
	printf("	R4 =	%.f ohm \n" , R4);
	printf("Output voltage: \n");
	printf("	Vout =	%.1f v \n" , Vout);

	return 0;
}



I keep getting the error message "error: invalid operands to binary^". I've tried changing around floats and rearranging the equations with no success. I tried looking up the error code, but there was nothing useful that i could find on any forums or alike.

Any help is greatly appreciated :)/>

Is This A Good Question/Topic? 0
  • +

Replies To: error: invalid operands to binary.

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2112
  • View blog
  • Posts: 3,230
  • Joined: 21-June 11

Re: error: invalid operands to binary.

Posted 19 March 2013 - 05:07 AM

^ is the bit-wise xor operator in C and it only works with integer operands.

If you want to use exponentiation, use the pow function from math.h.
Was This Post Helpful? 1
  • +
  • -

#3 stickman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-March 13

Re: error: invalid operands to binary.

Posted 19 March 2013 - 05:25 AM

I've tried including math.h with the pow function and it doesn't change anything.

Something i forgot to include in the original post, the error code appears for lines 28 and 29 only. The rest seems to be fine.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2112
  • View blog
  • Posts: 3,230
  • Joined: 21-June 11

Re: error: invalid operands to binary.

Posted 19 March 2013 - 06:23 AM

On lines 28 and 29, you're getting an error because, due to reasons of precedence, one of the operands to ^ is a float.

The only other line where you use IC is line 27. On that line you don't get an error because here both operands to ^ are integers. However that does not mean that the line is fine. As I said ^ won't do what you want it to - even when it doesn't cause a compile error.

Which you should always be mindful of when defining preprocessor constants like that. Most of your constants will break in some contexts due to precedence. I recommend you make liberal use of parentheses.
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: error: invalid operands to binary.

Posted 19 March 2013 - 01:31 PM

#define Ic	3 / 1000.0	/*Ic = 3*10^(-3)A*/



Since the power never changes, why not just hard code the value?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3568
  • View blog
  • Posts: 11,089
  • Joined: 05-May 12

Re: error: invalid operands to binary.

Posted 19 March 2013 - 03:36 PM

I usually let the compiler do the hardwork of figuring out that a constant never changes. For example,
I would rather see:
#define SecondsInAYear (60 * 60 * 24 * 365)


versus:
#define SecondsInAYear 31536000


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1