+ 1
Why is the tenary code not working here?
#include <stdio.h> int x=35; int isAdult= if(x<18) ? "Too Young":"Too Old" int main(void){ printf("%u\n", isAdult); return 0; }
10 Réponses
+ 2
[Anonymous]95
assigning string in c is a little tricky: you should copy the string to its new emplacement with the help of <string.h> function (such as the strcpy()):
https://code.sololearn.com/czvpP186n2JD/?ref=app
+ 4
[Anonymous]95
because ternary operator doesn't expect the 'if' keyword... and your variable doesn't have the correct type to receive string ^^
char var[15] = cond ? "true" : "false";
+ 3
[Anonymous]95
I didn't get any error. I think you did some mistake. See here
https://code.sololearn.com/c4lFYNp1axNS/?ref=app
+ 2
I Am AJ ! Thanks so much
+ 1
D_Stark still not working 👇👇👇
char AGE=35;
AGE<18 ? "Too Young":"Too Old";
printf("%s\n", AGE);
+ 1
[Anonymous]95 Your getting muddled up with your types I dont really know c syntax so I guess it will look somthing like this.
int age =35;
char *x = age<18?"to young":"to Old";
printf("%s\n",x);
+ 1
D_Stark I'm getting this error 👇👇
source_file.c:3:11: error: initializer element is not constant
char *x = age<18?"to young":"to Old";
^~~
source_file.c:5:8: error: expected declaration specifiers or ‘...’ before string constant
printf("%s\n",x);
^~~~~~
source_file.c:5:15: error: expected declaration specifiers or ‘...’ before ‘x’
printf("%s\n",x);
^
+ 1
I Am AJ ! This is the error after running the code 👇👇👇
source_file.c:3:15: error: initializer element is not constant
char* adult = (x < 18 ? "Too Young" : "Too Old");
^
source_file.c:4:9: error: expected declaration specifiers or ‘...’ before string constant
printf ("%s", adult);
^~~~
source_file.c:4:15: error: expected declaration specifiers or ‘...’ before ‘adult’
printf ("%s", adult);
^~~~~
+ 1
visph thanks so much...again you've helped
0
[Anonymous]95
In ternary operator no need to write if because it's a short cut of if else statement.
Here should be
int x = 35;
char* adult = (x < 18 ? "Too Young" : "Too Old");
printf ("%s", adult);