It is required to enter characters until a point is encountered. This program does not respond to the point. Where is the mistake? (Or offer, please, your working version)

#include <stdio.h> #include <stdlib.h> int main() { char ar[1000]; char ch; int i = 0; scanf_s("%s", &ch); ar[i] = ch; while ( ch!= '.') { scanf_s("%s", &ch); ar[i] = ch; i++; } system("pause"); return 0; } 
  • You are somehow crookedly using scanf_s , and you don’t need it here. Just scanf("%c", &ch); - andy.37

2 answers 2

1) To enter a single character, the scanf function (or its "safe" version of scanf_s ) must be used as follows:

 scanf_s("%c", &ch, 1); 

2) To read a single character from the standard input, you can also use the getchar function:

 ch = getchar(); 

3) Both previous options will work only after pressing the Enter key. If you need an instant reaction after entering the dot symbol, you need to use the getch function:

 ch = getch(); 

But when using this method on the console will not display the entered characters. In order for them to appear, they will need to be displayed by themselves:

 ch = getch(); putchar(ch); 
     int main(void) { printf("Hello World!\n"); char c = '\0'; while (c != '.') { scanf("%c", &c); printf("%c\n", c); } return 0; } 

    I think the problem is calling scanf_c . Firstly, if you need to read a character by character, you should use the %c format, and secondly, scanf_s needs a third argument. Thirdly, you only get the second character entered in ar[0] .

    • Why scanf_s third argument? She and the second may not be. - user194374
    • @kff Took from here: en.cppreference.com/w/c/io/fscanf . I do not have Windows, scanf_c I do not understand the compiler at all) - andy.37
    • Compiler old. Or c11 support c11 not included. And the scanf_s link scanf_s one mandatory parameter (format), and the rest (fillable variables) are optional. You, apparently, confused it with something like fscanf/fscanf_s/sscanf/sscanf_s , they, in fact, usually have at least three arguments. - user194374
    • @kff, frankly, I can't figure out the scanf_s function. Compilers ss, gcc with support for the standard C11 does not see it (Ubuntu 14.04). In the standard (on the links on this site) I can not find it. The fact that she still needs a third argument with the %s%c%] format characters is written in all the links I have seen, including in the above. - andy.37
    • I'm sorry. Was inattentive. That's right, you need one more argument: scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable. scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable. ( msdn.microsoft.com/en-us/library/w40768et.aspx ) - user194374