"x.exe has stopped workinng" when reading a string using scanf_s in visual studio 2015?

1

I am having problems with the following code:

#include <stdio.h>
#include <string.h>
#include <conio.h>
int main()
{
 char c[10];
 printf("nombre\n");
 scanf("%s",&c);
 printf("%s",c);
}

The problem is that when entering the name and pressing Enter I get the following error message:

  

"x.exe has stopped working".

I have tried placing more libraries and exchanging char c[10] for char *c

The same code was tested in borland (compiler of c and c ++) and there does not give me any problem, so the problem must be in visual studio.

    
asked by esencia 01.08.2017 в 01:49
source

1 answer

2
  • With the arrays, the name of the variable already works as an address to the memory area defined by the array, it is not necessary to obtain the address using the operator & .

    scanf("%s", c);
    
  • As you define an array of 10 characters, the name you enter can have a maximum of 9 characters (the last one is always used for a character null - %code% that marks the end of the string). You do not put what value you use for the test so keep that in mind.

  •   

    The same code was tested in borland (compiler of c and c ++) and there does not give me any problem, so the problem must be in visual studio.

    C and C ++ are famous because they do not check if the program is poorly written; they were created thinking above all about the speed of execution. That your program calls an uninitialized memory address? Is your problem. It is not defined that the program warns you of the error; in fact, there is no definition of how the program should behave. In some cases it can appear to work , in other cases the program can stop its execution, in others the execution will continue with unexpected behaviors. This is what is called "undefined behavior" ( undefined behavior , UB), and can make debugging programs in complicated C / C ++.

  • answered by 01.08.2017 в 02:32