Title: How to fill a two dimensional array with chars in C?
Tags: c;arrays
Question: The point of the code is to create a 2D array of dimensions set by the user. After being prompted for the width and height, the user must put in char values into each position in the 2D array. When implemented with int: (int mat, int value), the algorithm works fine, however when trying to implement with char(char mat, char value), the output is not consistent with the integer version. The only changes are the type of array and value, as well as setting the scanf/printf("%d") to scanf/printf("%c). Any way you can implement this 2D array with chars?
#include <stdio.h&gt;
int main() {
int width;
int height;
printf("Enter col and row values between 2 and 20.\n");
scanf("%d %d", &amp;width, &amp;height);
printf("The numbers you typed was %d %d\n", width, height);
char mat[height][width];
int i;
int j;
char value;
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
printf("Enter your value.\n");
scanf("%c", &amp;value);
mat[i][j] = value;
}
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
printf("%c", mat[i][j]);
}
printf("\n");
return 0;

Here is the accepted answer: Change this statement the following way
scanf(" %c", &amp;value);
^^^^
Also change this statement something like
printf("Enter your character value: ");
that it would be more clear what the program expects to be entered.

Here is another answer: ... the output is not consistent with the integer version
Well, of course not. It will be characters rather than integers. Otherwise, it will be the same.
Also, your input loop is really weird.
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
printf("Enter your value.\n");
scanf("%c", &amp;value);
mat[i][j] = value;
It seems like you're trying to prompt for each character, but then you only read a single character. I can't imagine how you expect anyone to use that. If I want to put in an "A", I type "A", but then nothing will happen until I press enter. But that will input two characters. So this code seems somewhat poorly designed.
As a quick fix, you could try something like:
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
printf("Enter your value.\n");
do scanf("%c", &amp;value);
while (value == '\n' || value == '\r');
mat[i][j] = value;
Comment for this answer: The `do`/`while` loop is bogus. You could instead first ignore pending newlines with `scanf("%*[\r\n]");` and then read the char with `value = ' '; scanf("%c", &value);`
Comment for this answer: Or use `scanf(" %c", ...)`. But, of course, that prevents actually inserting any whitespace characters, if that is desirable...

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:11/14/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c><arrays>', 'Title': 'How to fill a two dimensional array with chars in C?', 'ViewCount': 2791, 'id': 40598553, 'user_AboutMe': None, 'user_AccountId': 9646485.0, 'user_DisplayName': 'Ruhe', 'user_Reputation': 79.0, 'user_Views': 46.0, 'user_id': 7159073.0}}