Organizational Research By

Surprising Reserch Topic

char s100 i 2 bottonbottle it doesnt work why


char s100 i 2 bottonbottle it doesnt work why  using -'c'

why doesnt work?

#include <stdio.h>
int main()
{
    int i = 2;
    char s[100] = (i == 2)? "botton":"bottle";
    printf ("%c", s[0]);
    return 0;
}


[Error] invalid initializer
    

asked Sep 9, 2015 by Wil1227
0 votes
4 views



Related Hot Questions

1 Answer

0 votes

Compiling with -Wall shows the error:

$ gcc -Wall test.c
test.c:5:10: error: array initializer must be an initializer list or string literal
    char s[100] = (i == 2)? "botton":"bottle";
         ^
1 error generated.

However, you can initialize with strcpy():

#include 
#include 
#include 

int main()
{
    int i = 2;
    char s[100] = {0};
    strcpy(s, (i == 2) ? "botton" : "bottle");
    printf("%c\n", s[0]);
    return EXIT_SUCCESS;
}
answered Sep 9, 2015 by LatashaHutch

...