Validating unsigned integers with scanf()

This is a discussion on Validating unsigned integers with scanf() within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main() { unsigned int x = 0; int scanf_value = scanf("%u", &x); if (scanf_value != 1) ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64

    Validating unsigned integers with scanf()

    Code:
    #include <stdio.h>
    
    int main() {
    	unsigned int x = 0;
    	int scanf_value = scanf("%u", &x);
    	if (scanf_value != 1) {
    		printf("The user has not entered a valid unsigned integer\n");
    	}
    	else {
    		printf("The user has entered a valid unsigned integer\n");
    	}
    	return 0;
    }
    Why is it that if I input the value -1 it says the user has entered a valid unsigned integer? Isn't scanf() supposed to return the number of successful conversions?

    Thank you.
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    How would a negative unsigned value be valid?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, it's probably a bit dependant on the implementation of scanf() whether it checks if "sign given" for unsigned numbers. In the glibc (Gnu C library) it doesn't seem to check specifically for this.

    Perhaps you want to check it yourself?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    Yeah, I assumed scanf() would fail once it encountered a minus sign. It seems not to be the case. I solved the problem using the following code:

    Assuming the user won't enter more than 512 characters:

    Code:
    char input [512];
    int scanf_value = scanf("%[0-9]", input);
    
    if (scanf_value != 1) {
            printf("The user has not entered a valid unsigned integer\n");
    }
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    My preferred method is to read user input as a string and use strtol or strtoul to attempt conversion.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    Quote Originally Posted by Dave_Sinkula View Post
    My preferred method is to read user input as a string and use strtol or strtoul to attempt conversion.
    You mean like this?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    	char input [512];
    	unsigned int x = 0;
    	fgets(input, 512, stdin);
    	char* input_ptr = (char*)input;
    	
    	x = strtoul(input, &input_ptr, 10);
    	if (*input_ptr != '\n') {
    		printf("Not an unsigned integer!\n");
    	}
    	return 0;
    }
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    More or less. Some additional doo-dads might help. Maybe add a check for a leading - as well.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think the scanf("%[0-9]", string) would accept input like 1abc as "one entry", which means that something that isn't technically a number could still be "accepted input". And of course, if you don't clear the input buffer, the NEXT input will fail.

    fgets() and strto[u]l is much better in that respect. I'm not sure if strtoul actually checks for negative numbers - it may still accept those [I seem to remember that all strto* functions use a common function, and it may actually not do any checking specifically to ensure negative input is unaccepted by unsiged input].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Image rotation - doesn't always work
    By ulillillia in forum C Programming
    Replies: 12
    Last Post: 05-03-2007, 01:46 PM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 03:49 PM
  3. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM
  4. ANY BODY WILLING TO HELP ME WITH Microsoft Visual C++
    By BiG pImPiN fOoL in forum C++ Programming
    Replies: 12
    Last Post: 11-04-2001, 06:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21