Solving C Compile Error: "Invalid Operands

  • Thread starter Thread starter kbaumen
  • Start date Start date
AI Thread Summary
The discussion revolves around a user's issue with compiling a C program that calculates the area of a triangle using the sides provided as command-line arguments. The user encounters compilation errors due to attempting to perform arithmetic operations directly on string values from `argv`, which are character pointers rather than numerical values. It is clarified that to use these strings as numbers, they must be converted using functions like `atof()`, which converts strings to floating-point numbers. The conversation highlights the importance of understanding the return types of standard C functions, noting that `atof()` returns a double, which compilers typically promote to a float. This emphasizes the need for caution in assuming the return types of C functions based solely on their names.
kbaumen
Messages
190
Reaction score
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
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().
 
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.
 
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.
 
Ok, thanks, I'll keep that in mind.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top