-1

I want to show output with a two dimensional array. I print the value of 10,11,12 but I show only one digit 0,1,2. No idea. How to output with char data type. I don't want to change the char value because I need to assign the other X and Y to number[2][1] = 'X';

My code is here:

#include <stdio.h>

int main(void){
    char number[3][4] = {
        {'1','2','3','4'},
        {'5','6','7','8'},
        {'9','10','11','12'}
    };

    printf("%c",number[2][1]);

    return 0;
}

So. I hope you can help me to think how to do it. Thanks you so much!.

ErmIg
  • 3,980
  • 1
  • 27
  • 40
cyberoot
  • 340
  • 2
  • 18
  • Did this code even compile without error? – A.A. Feb 16 '17 at 05:17
  • it show 7 warning message. multi-character character constant. – cyberoot Feb 16 '17 at 05:18
  • Why don't you try to understand what the warnings mean? – Iharob Al Asimi Feb 16 '17 at 05:20
  • Related: http://stackoverflow.com/questions/7755202/multi-character-constant-warnings. – Giorgi Moniava Feb 16 '17 at 05:20
  • 1
    You can't use 10, 11, or 12 as a single character simply because they are not. Each is two separate characters. You can use something like A,B, and C in their place if that works for you. I don't get that part of your question: "i need to assign the other X and Y to number[2][1] = 'X';" – A.A. Feb 16 '17 at 05:24

5 Answers5

1

You are mixing characters with strings, they are not the same. In c, the single quote is used to get the ascii value of a character, it's called a character constant.

When you have multiple characters wrapped by single quotes, it's called a multicharacter constant, and it's value is implementation defined, nevertheless it's still an integer and not a string.

If you want to have an array of strings, then it would be like this

#include <stdio.h>

int main(void)
{
    const char *number[3][4] = {
        {"1",  "2",  "3",  "4"},
        {"5",  "6",  "7",  "8"},
        {"9", "10", "11", "12"}
    };
    printf("%s\n", number[2][1]);
    return 0;
}

Also notice, the "%s" instead of "%c" to print a string.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
1

Insert the numbers into array without quotes. char can hold numbers from -128 to 127 and also use %d instead of %c

#include <stdio.h>

int main(void){
char number[3][4] = {
    {1,2,3,4},
    {5,6,7,8},
    {9,10,11,12}
};

printf("%d",number[2][2]);

return 0;
}
Sagar V
  • 12,158
  • 7
  • 41
  • 68
  • 1
    To be clear, `%c` translates a `char` value into its ASCII representation and prints that. `%d` translates a `char` value (or whatever other integer primitive type) into the string representation of the numeric value. They can both be used on a `char` but have different outputs. – Qix - MONICA WAS MISTREATED Feb 16 '17 at 05:47
  • exactly. In the above code, if you use %c, nothing will get printed. But %d print the number. – Sagar V Feb 16 '17 at 05:50
1

I suggest

//if byte isnt defined so short

   byte c[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};

char in this case may be confusing.

Roy Avidan
  • 769
  • 8
  • 25
0

Simply create a 3D array of char:

char number[3][4][3];

Which allows 3 rows and 4 columns of strings, and each string has a max length of 3, including the null byte \0.

Using something like '11' is invalid, as this is not a single character, as char requires. You need to make these strings, like "11", or just use an int value 11. If you decide to use strings, your format specifier must be %s instead, since you want to print out char[] or char*. %c is used to print out char.

Your program can look like this:

#include <stdio.h>

#define ROWS 3
#define COLS 4
#define STRSIZE 3

int main(void){
    char number[ROWS][COLS][STRSIZE] = {{"1","2","3","4"},
                                        {"5","6","7","8"},
                                        {"9","10","11","12"}};

    printf("%s",number[2][1]);

    return 0;
}

Note: Using const char *number[ROWS][COLS] is more flexible, as you can have any string length. You can also just declare a 2D array of integers, and you would be able to simply have int number[ROWS][COLS] = {{1, 2, 3, 4}, .......}.

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
0

In C/C++ language char data type can hold -128 to +127 range of values. That value can be stored either char c = 'A'; or char c = 65; (using single quoted character or the ASCII value of the character). So you cannot store '10' in a char variable. If you need to store the ASCII value 10 (line feed '\n'), you can use the answer of @Sagar V. If you need to store text 10 ("10" note that is not single quote), you can use the answer of @Iharob Al Asimi.

In your code what you created is a two-dimensional char array. It cannot hold text (string or char*) values. Just look at the compiler warnings. Practice the habit of writing 0 warning codes.

Amith Chinthaka
  • 1,015
  • 1
  • 17
  • 24