I have some problem for this output. Also, I also want some examples on how to use char *str.
int main() {
char str[5];
scanf_s("%c", &str);
printf("%s", str);
return 0;
}
The output came out weird for the input "VI".
I have some problem for this output. Also, I also want some examples on how to use char *str.
int main() {
char str[5];
scanf_s("%c", &str);
printf("%s", str);
return 0;
}
The output came out weird for the input "VI".
scanf_s takes pointer as its argument. So you don't need to use &str, because str is implicitly converted to a pointer when used in expressions like an argument expression. You can also pass the buffer size as a parameter in scanf_s.
So you can use
scanf_s("%c", str, 5);
where 5 is the buffer size. Passing a specific buffer size will restrict you taking input more than the size which is missing scanf. In scanf you may take more input than the array or string size (like declared string is char str[4], but you may take input 'Hello') which later causes crashing the program due to overflow. But using particular buffer size in scanf_f will not allow you taking more input than the buffer size. This is where the scanf_s comes in.
Firstly, you need a pointer as an argument to scanf_s, which is exactly what str decays to when passed to a function, so you don't need the & operator, and also you need to specify a buffer size:
scanf_s("%c", str, 5);
You can find the Microsoft documentation here.