If you wanted to use all the MS nuances and non-portable library calls, you can do so, but understand that in the end it is teaching you to code using functions that are not documented with the language standard.
To that end, also understand the C "language" has two primary components: the actual language itself (what makes it C code), and an mandatory accompanying library of standard functions (some of which you're familiar with, printf
, scanf
, etc). One can learn the first part, the language, and understand how it works independent of the accompanying standard library. In the end, the latter (the library) provides you a plethora of operations that you can utilize, and if you stay strictly within those defined by the standard, your code will be more robust, portable, and understandable by any C engineer familiar with said-same.
Regardless, the minimal change to make your program "work" using library extensions Microsoft chose to foist upon users of their implementation is:
#include <stdio.h>
// FIXED return type
int main() {
int a, b, c;
char m;
printf("Erstes Argument: ");
scanf_s("%d", &a);
printf("Zweites Argument: ");
scanf_s("%d", &b);
printf("Operation: ");
// FIXED arguments passed to setvbuf
setvbuf(stdin, NULL, _IONBF, 0);
// FIXED arguments passed to scanf_s
// FIXED added white space consumption prior to character read
scanf_s(" %c", &m, 1);
if (m == '*')
{
c = a * b;
printf("%d * %d = %d \n", a, b, c);
}
else
{
printf("Falsche Operation! \n");
}
return 0;
}
A proper, standard compliant version with no dependence on MS-isms could be:
#include <stdio.h>
int main() {
int a, b, c;
char m;
printf("Erstes Argument: ");
scanf("%d", &a);
printf("Zweites Argument: ");
scanf("%d", &b);
printf("Operation: ");
scanf(" %c", &m);
if (m == '*')
{
c = a * b;
printf("%d * %d = %d \n", a, b, c);
}
else
{
printf("Falsche Operation! \n");
}
return 0;
}
Without the feature disablement macro _CRT_SECURE_NO_WARNINGS
, you'll be greeted with a plethora of three warnings from MS stating you should be using their "more secure" functions. While valiant of them to try and make our programs more secure , one could easily argue how it would be better if engineers simply learned how to properly write more secure code.
Finally, your code isn't done. You should be checking your IO calls for success. Right now you're assuming whatever the user provided as input for a
and b
, they're properly parsed, valid integers. If that isn't the case, your code marches on, ultimately using indeterminate data for calculation, and invoking undefined behavior in the process. scanf
has a documented return code for a reason. Use it.