Solving C Compile Error: "Invalid Operands

  • Thread starter Thread starter kbaumen
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a C compilation error encountered while trying to compute the area of a triangle using user-provided side lengths. Participants explore the nature of the error and the necessary conversions from string to numeric types in C programming.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • The original poster expresses frustration over compilation errors related to invalid operands, specifically in the context of using command-line arguments.
  • Some participants point out that command-line arguments (argv[n]) are strings (char *) and must be converted to numeric types to perform arithmetic operations, suggesting the use of functions like atoi() and atof().
  • One participant clarifies that atof() returns a double, not a float, and notes that compilers typically promote doubles to floats when necessary, cautioning against assumptions about return types of standard C functions.

Areas of Agreement / Disagreement

Participants generally agree on the need to convert strings to numbers for arithmetic operations, but there is no consensus on the best approach or the implications of using different functions like atoi() versus atof().

Contextual Notes

There is an implicit assumption that the user is familiar with basic C programming concepts, but the discussion does not address potential limitations or nuances in the use of conversion functions.

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.
 

Similar threads

Replies
4
Views
5K
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
Replies
14
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K