Originally Posted by
Toliot
[C]an you please clarify what you mean? Is there a more... effective way to call variables from the printf function (I'm so sorry if I used the wrong terminology right there, coming from ruby/python and even there it has been years since I touched that craft). Anything you guys tell me now will probably assist me greatly down the line so I don't make the wrong habits early.
Nah, it's just that the author doesn't apply his criticisms of K&R to his own code. Specifically,
has undefined behavior if foo is NULL or points to an unterminated string, so (for example) his function (taken from chapter 47)
Code:
bstring read_line(const char *prompt)
{
printf("%s", prompt);
bstring result = bgets((bNgetc)fgetc, stdin, '\n');
check_debug(result != NULL, "stdin closed.");
check(btrimws(result) == BSTR_OK, "Failed to trim.");
return result;
error:
return NULL;
}
is just as incorrect and defective as the K&R 'copy' function he criticizes in the final chapter, and for the same reasons. (I therefore find it ironic and amusing that this function is basically meant to be a "safer" wrapper around fgets.) He uses %s everywhere without checking bounds, so the suggestion that he should go back and change it everywhere was just me being snide.
(For your enlightenment, if you have a string that might be unterminated and you want to print it without overstepping bounds, this is how you do it:
Code:
printf("%.*s", MAX_STRING, s);
where MAX_STRING is the longest the array s can be. Usually this isn't necessary.)
Originally Posted by
King Dude
I suggest checking out "C Programming for Dummies" from your local library and taking a C programming class at a community college, using the book as a reference.
Really? I mean, I have nothing against the book, but it has fairly lukewarm reviews and I'm not sure why you'd suggest it over... well, any one of a thousand mediocre C books out there. What do you like about it?
Bookmarks