Usually there are not differents (in bytes) between short and int,
else i would to prefer (and it would to have logically more sense):
unsigned short idx = 0;
There is no guarantee that short and int are the same size, and on most
common systems today they are certainly different (16-bit short, 32-bit
int).
Of course, for this particular program, unsigned (or even signed) char
would work just as well since the string is hardcoded and known to be
rather short.
for(idx = 0 ; str[idx] != '\0' ; idx++) {
(a for-loop is safer is the string is empty, and comparing a
character with the terminating character is better than comparing
it with the NULL pointer)
In this case, for me, there are not differences, of course that you
wrote is good, too!
It's poor practice to test your data for validity _after_ using it. A
while loop is just as simple as a do-while loop, yet it does not suffer
the same defect. I've never found a valid use for do-while loops other
than the macro hack.
Again, in this particular case it doesn't matter since the string is
hardcoded and known to be at least one character long, but defensive
coding is a good habit to practice.
printf("%c", ...) is severe overkill compared to putchar(). True, a
decent compiler may substitute the latter for you when appropriate, but
why write something that is _more_ complicated than required? (Plus,
putchar() is a macro, which implies it'll be faster than printf(),
though the Standard doesn't guarantee that.)
IMHO, while loops are more natural with an iterator, which is the
simpler and more idiomatic construct for sequentially traversing a string:
char *s = str;
while (*s) {
putchar(*s++);
}
but for loops are more natural with indexes:
size_t idx;
for (idx=str; !str[idx]; idx++) {
putchar(str[idx]);
}
That's a matter of style, though; either combination can work.
missing on my code is the *always* present "return 0"
It's implied with C99 and later. In C89, which you seem to be obsessed
with, your code is incorrect without an explicit return.
S