String input using C scanf_s

后端 未结 5 1168
刺人心
刺人心 2020-12-11 08:42

I\'ve been trying to look for answer myself, but I can\'t find one. I want to insert a part of the programming that reads in a string like \"Hello\" and stores and can disp

相关标签:
5条回答
  • 2020-12-11 08:54
        #include<stdio.h>
    
        int main()
        {
          char name[64];
    
          printf("Enter your name: ");
          scanf("%s", name);
          printf("Your name is %s\n", name);
    
         return 0;
        }
    
    0 讨论(0)
  • 2020-12-11 08:55

    I used this very often in my university classes so this should work fine in Visual Studio (tested in VS2013):

    char name[64]; // the null-terminated string to be read
    scanf_s("%63s", name, 64);
    // 63 = the max number of symbols EXCLUDING '\0'
    // 64 = the size of the string; you can also use _countof(name) instead of that number
    // calling scanf_s() that way will read up to 63 symbols (even if you write more) from the console and it will automatically set name[63] = '\0'
    // if the number of the actually read symbols is < 63 then '\0' will be stored in the next free position in the string
    // Please note that unlike gets(), scanf() stops reading when it reaches ' ' (interval, spacebar key) not just newline terminator (the enter key)
    // Also consider calling "fflush(stdin);" before the (eventual) next scanf()
    

    Ref: https://msdn.microsoft.com/en-us/library/w40768et.aspx

    0 讨论(0)
  • 2020-12-11 08:56

    From the specification of fscanf_s() in Annex K.3.5.3.2 of the ISO/IEC 9899:2011 standard:

    The fscanf_s function is equivalent to fscanf except that the c, s, and [ conversion specifiers apply to a pair of arguments (unless assignment suppression is indicated by a *). The first of these arguments is the same as for fscanf. That argument is immediately followed in the argument list by the second argument, which has type rsize_t and gives the number of elements in the array pointed to by the first argument of the pair. If the first argument points to a scalar object, it is considered to be an array of one element.

    and:

    The scanf_s function is equivalent to fscanf_s with the argument stdin interposed before the arguments to scanf_s.

    MSDN says similar things (scanf_s() and fscanf_s()).

    Your code doesn't provide the length argument, so some other number is used. It isn't determinate what value it finds, so you get eccentric behaviour from the code. You need something more like this, where the newline helps ensure that the output is actually seen.

    char name[64];
    if (scanf_s("%s", name, sizeof(name)) == 1)
        printf("Your name is %s\n", name);
    
    0 讨论(0)
  • 2020-12-11 08:57

    you should do this : scanf ("%63s", name);

    Update:

    The below code worked for me:

    #include <stdio.h> 
    int main(void) { 
        char name[64]; 
        scanf ("%63s", name); 
        printf("Your name is %s", name); 
        return 0; 
    }
    

    if you are using visual studio, go to Project properties -> Configuration Properties -> C/C++-> Preprocessor -> Preprocessor Definitions click on edit and add _CRT_SECURE_NO_WARNINGS click ok, apply the settings and run again.

    Note: this is only good if you are doing your homework or something like that and it's not recommended for production.

    0 讨论(0)
  • 2020-12-11 09:09
    #include<stdio.h>
    
    int main()
    {
      char name[64];
      printf("Enter your name: ");
      gets(name);
      printf("Your name is %s\n", name);
     return 0;
    }
    
    0 讨论(0)
提交回复
热议问题