In my homework assignments I've got a task to get an input using scanf() of an integer number and check if its a positive negative or zero.
I've coded this:
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
if (scanf("%d", &num) != 1) // Input validation
{
printf("Input error!\n");
return 1;
}
if (num > 0)
{
printf("The number is positive!\n");
}
else if(num == 0)
{
printf("The number is zero!\n");
}
else
{
printf("The number is negative!\n");
}
system("pause");
return 0;
}
The code is fine but when I've tested it with inputs of "0/" or "1ab6" and it only got the first number and ignored the /? or ab6?
I would like to read or get an explanation about how it works in the memory.
// Input validation (scanf is unsafe)wat? no. You should definitely explain why you come to write that comment!mallocfamily of functions.#includedirective:#define _CRT_SECURE_NO_WARNINGS#define _CRT_SECURE_NO_DEPRECATE#define _CRT_NONSTDC_NO_DEPRECATE. Aboutscanf()you should spend at least an hour, possibly more, exploring how it works hands-on with the man pages in front of you.