i tried solve problem, count amount of lines, blank spaces, , tabs.
my solution incorrect because don't know how use { }.
main () { int newline; int tab; int blank; int c; newline = 0; tab = 0; blank = 0; while ((c = getchar()) != eof) if (c == '\n') ++newline; if (c == '\t') ++tab; if (c == 32) ++blank; printf("lines: %d tabs: %d blanks: %d\n", newline, tab, blank); } in code, new lines being counted. tabs , spaces never counted. know answer add { } around if statements section. know because searched google solution.
perhaps me, k&r not explain when should use { }.
can explain how can know add { } above code? when read code, seems fine without {}. means don't understand usage. why aren't tabs , spaces counted in above code?
is there book on c can recommend? have no programming experience.
one relatively straightforward "rule of thumb": semi-colon(s). referring example, starting @ "while", read forward until see semi-colon. if want beyond semi-colon executed part of while block, need wrap in curly braces.
another way @ it: semi-colon statement terminator. terminated blank = 0 previous statement; terminates not if enclosing while statement. thus, execute following if part of while block, need enclose ifs in curly braces.
oh, , way, c , similar languages not attach syntactic meaning whitespace. @ treated separator. indentation choose apply benefit of human reader only; has no significance compiler.
Comments
Post a Comment