Hi All,
Thanks so much for the responses! I have been playing around with the below code and have a question regarding it.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void){
char str[5] = {0};
unsigned long long number_1 = 0;
unsigned long long number_2 = 0;
int position = 0;
printf("Input two integers:");
scanf("%[^\n\t]s",str);
int x = 0;
while (str[x] != '\0'){
if (!isdigit(str[x]) && !isspace(str[x])){
printf("Incorrect Input\n");
return EXIT_FAILURE;
}
++x;
}
int a = sscanf(str,"%llu %llu%n",&number_1,&number_2,&position);
return EXIT_SUCCESS;
}
If I run the code with inputs "123 123", this exceeds the number of characters in the array "str", yet, 123 is being stored to both number_1 and number_2.
I would have thought that since the str array only contained "123 1", number_2 would have a value of 1?
I also have assigned a value to the variable named "position" in the sscanf function. My intention with this was to check whether "position" exceeded 5 in which case I would know that the user had entered input longer than 5 characters...
However, if values are being assigned correctly even when more that 5 characters are being assigned, is there any real need for this?
Please forgive me if my code seems somewhat haphazard.
Thanks again for your help.
Kind Regards,
Giri