0
int main() {
    int y;
    scanf("%d", &y);
    char *array = (char*)malloc (y);
    for (int index_y = 0; index_y < y; index_y++)
    {
        scanf("%c", &array[index_y]);
    }

    printf("%c test", array[0]);
}

my input is

3
“###”

and array[0]my output is a NULL or \n: ' '

so if i want print my array correctly i must print it from array[1] to array[3];

is there any solution to replace it so i can print it dirrectly from [0] to [2]?

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • I have added a code block for your input. However, it is unclear to me whether I did it correctly, because it is unclear to me what exactly belongs to your input. Therefore, I suggest that you check it and correct it if necessary. Note that you can [edit] your question. – Andreas Wenzel Nov 26 '22 at 13:07
  • @wohlstad: I strongly advise against adding a trailing `\n` to the format string, as `scanf` will not only attempt to match a single [whitespace](https://en.wikipedia.org/wiki/Whitespace_character) character, but will rather continue waiting for user input until it encounters a non-whitespace character. See the following question for further information: [What is the effect of trailing white space in a scanf() format string?](https://stackoverflow.com/q/19499060/12149471) – Andreas Wenzel Nov 26 '22 at 13:51
  • @AndreasWenzel you are right. Thanks for the correction. – wohlstad Nov 26 '22 at 13:53
  • @huxiuleiwe note that my previous comment was not a good idea, and I removed it. See above. – wohlstad Nov 26 '22 at 13:55

0 Answers0