c - Why "%d" is not equivalent to " %d" as a format string in scanf -


i'm reading book , solving problems. question

for each of following pairs of scanf format strings, indicate whether or not 2 strings equivalent. if they're not, show how can distinguished.

(a) "%d" veruss " %d"

(b) "%d-%d-%d" versus "%d -%d -%d"

(c) "%f" versus "%f "

(d) "%f,%f" versus "%f, %f"

my solution (a) equivalent since scanf discards white space. (b) not equivalent since scanf matches - white space . (c), not equivalent since scanf put white space in buffer. (d), equivalent since scanf discards white space. according chegg solutions, preceding questions not equivalent. wrong? in post, make sure answers correct in comparison chegg solutions. i've read book , have decent knowledge scanf.

"%d" , " %d" same per op's reasoning.

they same expected numeric input "123" , " 456". remaining consideration file pointer on failure "abc" versus " xyz"? "%d" itself, first consumes leading white-space. no difference.

... conversion specification executed in following steps: c11dr §7.21.6.2 7

input white-space characters ... skipped, unless specification includes [, c, or n specifier. §7.21.6.2 8

then conversion of text numeric input (for "%d") happens.

below code demonstrates equivalence.

void next_testi(const char *s, const char *fmt, const char *pad) {   rewind(stdin);   int = 0;   int count = scanf(fmt, &i);   int next = fgetc(stdin);   printf("format:\"%s\",%s count:%2d, i:%2d, next:%2d, text:\"%s\"\n", //       fmt, pad, count, i, next, s); }  void next_test(const char *s) {   file *fout = fopen("test.txt", "w");   fputs(s, fout);   fclose(fout);    freopen("test.txt", "r", stdin);   next_testi(s, "%d", " ");   next_testi(s, " %d", "");   puts(""); }  int main() {   next_test("3");   next_test(" 4");   next_test("");   next_test(" ");   next_test("+");   next_test(" -");   next_test("x");   next_test(" y"); } 

output

format:"%d",  count: 1, i: 3, next:-1, text:"3"  // scanf() return value 1:success format:" %d", count: 1, i: 3, next:-1, text:"3"  format:"%d",  count: 1, i: 4, next:-1, text:" 4" format:" %d", count: 1, i: 4, next:-1, text:" 4"  format:"%d",  count:-1, i: 0, next:-1, text:""  // scanf() return value eof, next eof format:" %d", count:-1, i: 0, next:-1, text:""  format:"%d",  count:-1, i: 0, next:-1, text:" " format:" %d", count:-1, i: 0, next:-1, text:" "  format:"%d",  count: 0, i: 0, next:43, text:"+" // scanf() return value 0 format:" %d", count: 0, i: 0, next:43, text:"+"  format:"%d",  count: 0, i: 0, next:45, text:" -" format:" %d", count: 0, i: 0, next:45, text:" -"  format:"%d",  count: 0, i: 0, next:88, text:"x" format:" %d", count: 0, i: 0, next:88, text:"x"  format:"%d",  count: 0, i: 0, next:89, text:" y" format:" %d", count: 0, i: 0, next:89, text:" y" 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -