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);
}
CodePudding user response:
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.
CodePudding user response:
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)
- ...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.");
}
CodePudding user response:
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.
CodePudding user response:
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);
}
CodePudding user response:
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.