How to take one character at a time as input from a string of characters without space in c?

后端 未结 1 672
忘了有多久
忘了有多久 2021-01-27 14:25

Suppose,\"5181 2710 9900 0012\"- is a string of digits.I need to take one single digit at a time as input from the string of number without space to make arithmatic operations .

相关标签:
1条回答
  • 2021-01-27 15:21

    Since scanf is the inverse of printf, you could verify this by printing any number with the modifier (just a little tip).*

    In general, the number before the format is a 'width' modifier. In this case it means you're only reading one byte into a number. If you specify %d, it may be a number of arbitrary length.

    Example:

    #include <stdio.h>
    
    int main() {
        int a;
        sscanf("1234", "%d", &a);
        printf("%d\n", a); // prints 1234
        sscanf("1234", "%1d", &a); 
        printf("%d\n"m a); // prints 1
    }
    


    *) this appears to be false for this particular case. Makes sense that numbers are not truncated when specifiying a %d format, though, since that would change the meaning of the number. However, for many cases you could try what printf would do to predict scanf's behavior. But of course, reading the manual or docs on it is always the more helpful approach :)

    0 讨论(0)
提交回复
热议问题