-1
#include<stdio.h>


int main(){
char array[3][3]={{'2','1','3'},{'4','5','9'}};
array[0][0]='51';

}

Error warning: multi-character character constant [-Wmultichar] array[0][0]='51'; ^~~~ 17.4.c:6:17: warning: overflow in implicit constant conversion [-Woverflow]

bucz
  • 35
  • 2
  • 6
  • 8
    You can't. `char` can only hold one character per definition. – Osiris Jan 17 '19 at 18:58
  • 1
    Possible duplicate of [Multi-character constant warnings](https://stackoverflow.com/questions/7755202/multi-character-constant-warnings) – Osiris Jan 17 '19 at 18:59
  • 1
    Your [question should be updated](https://stackoverflow.com/posts/54242552/edit) to reflect what you are/were hoping for had that assignment been sensible. I.e. What do you want the content of both rows of `array` to look like when you're done? – WhozCraig Jan 17 '19 at 19:06

3 Answers3

1

If you want to store two decimal digits in one char you can actually use 4 bit nibbles to store the digits

int two_to_one(const char *number)
{
    return *number - '0' + ((*(number + 1) - '0') << 4);
}

char *char one_to_two(int ch, char *buff)
{
    buff[1] = ch >> 4;
    buff[0] = ch & 0xf;
    buff[2] = 0;

    return buff;
}
0___________
  • 60,014
  • 4
  • 34
  • 74
0

Char can only hold one symbol. '51' is two symbols. It could be three if you would write it between double brackets ("51") because C-type strings are always finished with \0. To hold more than one symbol you should use char pointers and double brackets or access them differently using one dimension:

char* array[3] = {"one", "two", "three"}; 
char string[3][7] = {"one", "two", "three"};

The second line tells that 3 string containing at most 7 characters (including \0) can be used. I've chose such a number because "three" consists of 6 symbols.

Artūras Jonkus
  • 134
  • 1
  • 7
0

If you want to use multi-character constants, you gave to store them in integral variables larger than chars. For example, this works - in a certain way, meaning, it stores a multi-character:

int x = '52';
SergeyA
  • 61,605
  • 5
  • 78
  • 137