Solving C Compile Error: "Invalid Operands

  • Thread starter kbaumen
  • Start date
In summary, the code compiled without error, but when the program attempted to calculate the area of the triangle, it failed.
  • #1
kbaumen
192
0
Hi.

I'm kinda learning C at home by myself and was just to compile another small program when I encountered problem with compiling.

Here's the code:
Code:
#include <stdio.h>
#include <math.h>

int main(int argc, char **argv)
{
    float p, S;

    if (argc != 3)
    {
        fprintf(stderr, "Usage: %s <side_a> <side_b> <side_c> \n", argv[0]);
    }

    p = (argv[1] + argv[2] + argv[3]) / 2;                                //13th line
    S = sqrt(p * (p - argv[1]) * (p - argv[2]) * (p - argv[3]));   //14th line

    printf("Area of this triangle is: %f \n", S);

    return 0;
}

and here are the lines from compiler:
Code:
kbau@kbox:~/prj/c/test/exercises$ gcc uzd1.c -o uzd1 -Wall
uzd1.c: In function ‘main’:
uzd1.c:13: error: invalid operands to binary +
uzd1.c:14: error: invalid operands to binary -
uzd1.c:14: error: invalid operands to binary -
uzd1.c:14: error: invalid operands to binary -

I'm getting frustrated why isn't this working. I don't understand why is there a problem.

I don't really have any work to show you except the code, I'm just curious why isn't it working. Help would be appreciated.
 
Last edited:
Technology news on Phys.org
  • #2
argv[n] is not a number. It is a char *-- a "string", a series of typed ASCII characters. In order to treat a string like a number, you must first convert it from a "string" to a number. See the man pages for atoi() and atof().
 
  • #3
Coin said:
argv[n] is not a number. It is a char *-- a "string", a series of typed ASCII characters. In order to treat a string like a number, you must first convert it from a "string" to a number. See the man pages for atoi() and atof().

Yeah, I figured that out when I woke up during the night but thanks anyway. atof() is the one I need 'cause I have a float there.
 
  • #4
Actually atof returns a double, not a float. Most compilers will "promote" the double to a float for you.

The reason I mention this - there are a lot of standard C functions that look like they might return a float - modf, atof, for example - when they return a double instead. You need to be careful about assuming what some C function returns just by the name of the function.
 
  • #5
Ok, thanks, I'll keep that in mind.
 

Related to Solving C Compile Error: "Invalid Operands

What does "Invalid Operands" mean in a C compile error?

"Invalid Operands" is a common C compile error that occurs when the compiler encounters an expression or operation that is not valid according to the rules of the C programming language. This could be due to a typo, missing variable, or incorrect use of operators.

How can I fix the "Invalid Operands" error in C?

To fix the "Invalid Operands" error, you will need to carefully review the code and identify the source of the error. Make sure all variables are properly declared and spelled correctly, and that the correct operators are used for the intended operation. Additionally, double-check for any missing or extra parentheses or brackets.

Why am I getting an "Invalid Operands" error when I compile my code?

The "Invalid Operands" error is typically caused by a mistake in the code, such as a typo or incorrect use of operators. It could also be due to a missing or incorrect header file. It is important to carefully review the code and make any necessary corrections to resolve the error.

Can using the wrong data type cause an "Invalid Operands" error in C?

Yes, using the wrong data type can cause an "Invalid Operands" error in C. For example, if you try to perform arithmetic operations on a string instead of a numerical data type, the compiler will flag it as an invalid operand. Make sure to use the correct data types for the intended operations in your code.

Are there any common mistakes that can lead to an "Invalid Operands" error in C?

Yes, there are some common mistakes that can result in an "Invalid Operands" error in C. These include using incorrect operators, misspelling variables or functions, and not properly declaring variables before using them. It is important to carefully review and test your code to identify and fix any potential mistakes that could lead to this error.

Similar threads

  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
6
Views
969
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
479
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
671
Back
Top