-1

I have written this program many times in different ways but I am not getting the output and am unable to find out the error, is there anyone who can point out the logical error in this program?

#include<stdio.h>
#include<string.h>

int main()
{
    float temp;
    char c[15]; 
    
    printf("Enter temperature in F degree=");
    scanf("%f",&temp);
    
    c = (temp>=80    ? strcpy(c,"swimming"):
        (60<=temp<80 ? strcpy(c,"tennis"):
        (40<=temp<60 ? strcpy(c,"Golf"):
                       strcpy(c,"skiing 1."))));    
    
    printf("%s",c);
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 10
    You cannot assign something to an array. Just drop the `c =`. But this kind of code is terrible. Just stick to good old if/else for this use case. – Jabberwocky Oct 22 '21 at 12:52
  • 3
    Forget about the ternary and ask yourself what this would do: `c = strcpy(c,"swimming")` – Support Ukraine Oct 22 '21 at 12:53
  • 3
    Don't abuse the `?:` operator like that. Use the good old if/else, your code will be much clearer. And `60<=temp<80` most likely doesn't do what you want. Comparison operators don't chain in C. – Mat Oct 22 '21 at 12:53
  • 1
    `60<=temp<80` this probably doesn't do what you expect it to. – 500 - Internal Server Error Oct 22 '21 at 12:55
  • You do not have to check if `temp<80` when `temp>=80` already evaluated to false. – mch Oct 22 '21 at 12:56
  • So, what language are you coming from? C is a very low level language. You cannot simply use Python-like coding and such. – JHBonarius Oct 22 '21 at 12:57
  • [Chaining multiple greater than/less than operators](https://stackoverflow.com/questions/6961643/chaining-multiple-greater-than-less-than-operators) – Lundin Oct 22 '21 at 13:04

5 Answers5

4

You can't assign anything to c. An arrays in C decays to a pointer to its first element. This happens for the left-operand of = operator as well. And such a pointer is not an L-value. It rather a temporary result of computation like 1+1,foo(1), &c or (int)c.

R-values to not designate objects thus they cannot be assigned.

From 6.3.2.1p3:

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ''array of type'' is converted to an expression with type ''pointer to type'' that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

tstanisl
  • 13,520
  • 2
  • 25
  • 40
  • *Arrays in C decays to pointer to its first element. This happens for the left-operand of = operator. And such a pointer it not an L-value.* IMO it's more clear to say "An array decays to the **address** of its first element" because that captures the "not an lvalue" better. You can't **assign** a different street address to your house - your house is where it is. The same thing with an array in C - you can't assign its address because the array **has** an address, but a pointer **holds** an address. That works with arrays passed to functions, too, because function parameters are lvalues. – Andrew Henle Oct 22 '21 at 13:35
3

Arrays do not have the assignment operator. They are non-modifiable lvalues.

From the C Standard (6.3.2.1 Lvalues, arrays, and function designators)

  1. ...A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const qualified type.

So instead of this assignment statement

c = (temp>=80    ? strcpy(c,"swimming"):
    (60<=temp<80 ? strcpy(c,"tennis"):
    (40<=temp<60 ? strcpy(c,"Golf"):
                   strcpy(c,"skiing 1.")))); 

where you are using wrong logical expressions like 60<=temp<80 (that always evaluates to logical true because the subexpression 60 <= temp evaluates either to integer 0 or 1 that in any case is less than 80) you should write

(temp>=80    ? strcpy(c,"swimming"):
    (60<=temp && temp <80 ? strcpy(c,"tennis"):
    (40<=temp && temp <60 ? strcpy(c,"Golf"):
                   strcpy(c,"skiing 1.")))); 

Though for readability it would be much better to rewrite this statement with using if-else statements like

if ( temp>=80 )
{
    strcpy(c,"swimming");
}
else if ( temp >= 60 )
{
    strcpy(c,"tennis");
}
else if ( temp >= 40 )
{
    strcpy(c,"Golf");
}
else
{
    strcpy(c,"skiing 1.");
} 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

Assigning the return value from strcpy (which is char *) to your variable c (which is char array) is illegal C code.

A simple if ... else if ... else if ... else ... with a strcpy in each block would be a better solution.

If you really want a single code line you can put the ternary stuff inside the strcpy like:

strcpy(c, temp >= 80 ? "swimming" : 
          temp >= 60 ? "tennis" : 
          temp >= 40 ? "Golf" : 
          "skiing 1.");

Notice: In the second part it's sufficient to check for temp >= 60. You don't need the temp < 80 as that is already true due to the previous temp >= 80 condition.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0

You can assign pointers but not arrays. The main confusion is that arrays decay to pointers in expressions. But pointers and arrays are completely different.

If you want to assign use pointers.

Another problem here are logical expressions.

60<=temp<80 is always true as it is equivalent of (60<=temp)<80. (60<=temp) will result in 1 or 0 and those values are always lower than 80

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    float temp;
    char *c=malloc(15); 
    
    printf("Enter temperature in F degree=");
    scanf("%f",&temp);
    
    c = (temp>=80    ? strcpy(c,"swimming"):
        (60<=temp && temp <80 ? strcpy(c,"tennis"):
        (40<=temp && temp <60 ? strcpy(c,"Golf"):
                       strcpy(c,"skiing 1."))));    
    
    printf("%s",c);
}
0___________
  • 60,014
  • 4
  • 34
  • 74
0

click for code hereYou can't assign a value to a character array that is returned by the string function .This string function returns value to the destination char Array which is provided in the string function

But you can use the Ternary operator in this way ->> Remove this part (c =).And also change this part from (40<temp<60 ) to (temp<40 && temp>60) in all parts of code where you wrote this. Just write the ternary operation which you want to perform then your code will work.