2008年12月9日星期二

Snippet : string number increment(done with macro)

#include
#include
#include


#define strincr(s,l) ({int i = l - 1; for(;i>=0 && (s)[i] == '9';(s)[i--] = '0'); (i>= 0) && (++(s)[i]); s;})

char a[4] = "999";

int main(int argv, char **args){

char *s = strincr(a, strlen(a));

strincr(s, strlen(s));

printf("%s", s);
return 0;
}


Just for fun, that might not be faster than by the regular way of using 'atoi'

没有评论: