0

Hi i am trying to use sscanf to test if a string is integer or not but it is always giving zero as return value(j) and when it is giving 1(sometimes without any changes) its not populating correct value in receivable variable(i) !Here is my code:

#include <stdio.h>                               

int main ()                                      
{                                                

  char sentence []="123456";                     
  int i=0;                                       

   j = sscanf(sentence,"%[0-9]",&i);              
   return 0;                                      
}   

Please let me know where i am going wrong !I know there are other ways to test a string for integer but i want to know where i am going wrong ! Help will be highly appreciated ! Thanks in advance

abhinav singh
  • 856
  • 6
  • 23
  • 45
  • You declare the array `sentence` as type `char`, therefore it will contain chars. Try `int sentence2[5] = {1,2,3,4};` and check the output. – d0rmLife Apr 04 '13 at 03:45
  • 3
    Testing a *leading* integer is easy. `%d`. Or did you need something more obtuse than that? The set-notation is only viable for char-types (including strings), not integers. so no matter what this needs to change. – WhozCraig Apr 04 '13 at 03:46
  • I have some confusion here ..as per my understanding it will check value in variable sentence and if its between 0-9 it will convert successfully and if not it wont and return 0 . If i am right then it should return 1 . Please correct me if i am wrong as i am not able to completely understand suggestions suggested by you guys ! :( – abhinav singh Apr 04 '13 at 04:23
  • You're confused. As I stated above, and below, set-based format specifiers (thats the `[0-9]` in your format string) can only extract *character* data. I.e. it needs to go into a *character* buffer. If a field was able to be extracted, `sscanf()` will return 1. That is correct. Review the [documentation for `sscanf()`](http://en.cppreference.com/w/c/io/fscanf) for more information. But above all, realize **No character-to-integer conversion takes place when scanning with set-based format specifiers**. – WhozCraig Apr 04 '13 at 05:02

2 Answers2

1

If you want to use sscanf() for validating digits-only (and I don't recommend you do), your format is off. You must use only char-types for gathering the extracted data:

Try this:

char message[] = "123456";
char buffer[128];

if (sscanf(message, "%127[0-9]",buffer) == 1)
    printf("Success : %s\n", buffer);

and play around with the range (change it to [2-9], for example) to assist in understanding how set-based extraction works. But again, you can only do them into a char-type buffer.

That said, use strtol() or atoi() (but only if you must) if you need the value, or if you just want to validate digit content, isdigit() may be a better fit. You decide.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • basically i want to capture both integer and float values in one go ! so if i have to use isdigit() function then i have to pick each character one by one and then check for digit escaping the decimal .. Am i correct ? – abhinav singh Apr 04 '13 at 04:10
  • @abhinavsingh Yeah, which would be a royal pain. If this is for a genuine lexer, you may have to do it regardless. You better validate that you don't need to support E exponent format for your floats, btw. (also a royal pain) – WhozCraig Apr 04 '13 at 04:13
0

Could also try strtol() for what you want. See: atol() v/s. strtol()

Community
  • 1
  • 1
SeKa
  • 1,825
  • 12
  • 7
  • @WhozCraig Thanks for catching that. Don't know what I was thinking (brain fart). Edited Answer. – SeKa Apr 04 '13 at 03:53