Let's start by considering the use of the standard scanf. If you write
scanf("%s", &str);
that will be incorrect (although it might work). When passing an array to a function it decays to a pointer to the first element, which is what scanf requires, so the line should be
scanf("%s", str);
If you want to restrict the input length to prevent buffer overflow it can be like this (one less than the array length to allow for the nul terminator)
scanf("%126s", str);
The allegedly safer function scanf_s requires an additional size argument to be passed for each format type %c and %s and %[] so the next line (after correcting the &)
scanf_s("%s", str);
lacks that argument, and the compiler should issue a warning about it. The code should be
scanf_s("%s", str, sizeof str);
but even that is inadequate. The Xscanf family of functions returns the number of values successfully entered. Since users (even myself) are notoriously bad at entering correct input (which may even be malicious) you must always check if the data was correctly entered. Such as
if(scanf_s("%s", str, sizeof str) != 1) {
// inform user and retry etc. etc.
}
As mentioned by @chux it is better to obtain input by using fgets, and then process it by various means, such as sscanf or strtok or strsep or by more direct analysis of the string. In that case, and with sscanf, you can make multiple attempts to process the input, but with scanf you only get one chance. Note that strtok and strsep modify the string, so you would need to work with a copy if you need to make more than one attempt to decode it.
In your second example
scanf_s("%d", &str);
you got "no exception but a long and strange string", but you should have got a compiler warning:
warning C4477: 'scanf_s' : format string '%d' requires an argument of
type 'int ', but variadic argument 1 has type 'char ()[127]'
Note that you did not initialise str to the "empty string" and if you go on to process what you imagine to be a good string after a faulty input, bad stuff can happen.
scanf_s("%s", &str)-->scanf_s("%s", str, sizeof(str))sofscanf_smeans "safe"... well, you still have to check the warnings...scanf- which already has a length restriction feature.fgets(str, sizeof str, stdin);for happy programming life.scanf_sandscanfleads down the road to perdition.