I am constantly receiving:
error: called object is not a function or function pointer
When using ternary operator like that:
puts("\nx: " (0 == 1) ? "y1\n" : "y2\n");
What am I doing wrong?
I am constantly receiving:
error: called object is not a function or function pointer
When using ternary operator like that:
puts("\nx: " (0 == 1) ? "y1\n" : "y2\n");
What am I doing wrong?
You are attempting to call an object which is neither a function nor a function pointer! In particular, the compiler is seeing the open paren after the string and thinks (as much as a compiler can be said to "think") that you are trying to invoke a function call. You cannot concatenate a string with the ternary operator as you are trying to do. Try:
printf("\nx: %s", (0 == 1) ? "y1\n" : "y2\n");
puts("\nx: " (0 == 1) ? "y1\n" : "y2\n");
This is not the right way to do what you want, because you cannot concatenate C strings like this.
You can do this using puts()
:
puts("\nx: ");
puts((0==1) ? "y1\n" : "y2\n");
To avoid the appending of an unwanted newline character, use fputs()
instead of puts()
.
fputs("\nx: ");
fputs((0==1) ? "y1\n" : "y2\n");
You can not concat the strings the way you do.
The simple solution is to use printf
printf("\nx: %s", (0 == 1) ? "y1\n" : "y2\n");
or if you insist on using puts you need the strcat function from string.h
char s[256] = "\nx: ";
puts(strcat(s, (0 == 1) ? "y1\n" : "y2\n"));
This is your code
"\nx: " (0 == 1) ? "y1\n" : "y2\n"
/* ^ this is ignored */
so it seems as if the string literal was being called as a function -> "\nx: "(0 == 1)
, doesn't it look like that?
You can achieve what you want with the printf()
function like this
printf("\nx: %s\n", (0 == 1) ? "y1" : "y2");