Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.3 alpha 4/15/85; site elsie.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!elsie!ado From: ado@elsie.UUCP (Arthur David Olson) Newsgroups: net.unix Subject: How do you check for an exact match with a scanf format? Message-ID: <5206@elsie.UUCP> Date: Thu, 22-Aug-85 09:49:14 EDT Article-I.D.: elsie.5206 Posted: Thu Aug 22 09:49:14 1985 Date-Received: Sat, 24-Aug-85 18:41:55 EDT Organization: NIH-LEC, Bethesda, MD Lines: 52 Keywords: scanf Consider the C language source for a program named "isdecimal", which is to print "1" if given one argument that's a decimal number or print "0" otherwise: #includemain(argc, argv) int argc; char * argv[]; { int result; int d; result = argc == 2 && sscanf(argv[1], "%d", &d) == 1; printf("%d\n", result); return !result; } Now the above program does the right thing in most cases; however, if you do a isdecimal 1a the output you get is 1 which isn't right in my book. The best way I've been able to come up with to get "isdecimal" to do the right thing is: #include main(argc, argv) int argc; char * argv[]; { int result; int d; char c; c = '\0'; result = argc == 2 && sscanf(argv[1], "%d%c", &d, &c) == 1 && c == '\0'; printf("%d\n", result); return !result; } which seems a kludge to me. If you have a better, portable (to 4.1bsd especially) way of doing the above ilk of checking, I'd appreciate hearing from you BY MAIL. -- UNIX is an AT&T Bell Laboratories trademark. -- UUCP: ..decvax!seismo!elsie!ado ARPA: elsie!ado@seismo.ARPA DEC, VAX and Elsie are Digital Equipment and Borden trademarks