0

Script:

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

char inn[100];

int main()
{

    inn='EEE';

    if(strcmp(inn,"EEE") == 0){
    printf("ok");
    }

}

Compile Error:

gcc test.c -o test

test.c: In function ‘main’:
test.c:9:9: warning: multi-character character constant [-Wmultichar]
     inn='EEE';
         ^
test.c:9:8: error: incompatible types when assigning to type ‘char[100]’ from type ‘int’
     inn='EEE';

What is the solution ?

Should I change my top declaration or should I do something differently elsewhere ?

  • The delimiters `'` and '`' around some value denotes the value as a character constant. But a character constant can only consist of 1 character. The remainder is ignored or flagged as a warning. – Paul Ogilvie Feb 16 '17 at 13:14
  • 1) Arrays cannot be assigned, 2) string literals need to use `"`, not `'`s. – Sourav Ghosh Feb 16 '17 at 13:15

1 Answers1

2

single quotes are invalid in C for string. They can only be used for single char.

To set "EEE" string to inn variable, use strcpy function:

strcpy(inn, "EEE");

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

char inn[100];
int main() {
    //In C, you have to manually copy string
    strcpy(inn, "EEE"); 
    if (strcmp(inn,"EEE") == 0){
        printf("ok");
    }
}

Remember to include string.h library too.

unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40