Ex10 - Use comma to initialize letter in for loop

I’m confused about one of the extra credit tasks in ex10:

  • Use the ‘,’ (comma) to initialize letter in the for loop

I’m confused by how to accomplish this, and I’m not sure I’m even understanding the ask correctly.

To me, it sounds like the goal is to take the example code

for (i = 0; argv[1][i] != '\0'; i++) {
    char letter = argv[1][i]
    ....
}

and change is so that letter is initialized inside the for(...) bit. Something like

for (i = 0, char letter = argv[1][i]; argv[1][i] != '\0'; i++) {
    ....
}

I can’t get this to work for me, however, and from my googling it sounds the maybe this is not even possible. Am I totally misinterpreting what is being asked here?

I actually don’t know if you can declare it in there in C99, but I know you could declare it at the top and then initialize it there.

One problem with “just try it and see if it works” is you may see the compiler not complain at all but secretly the compiler see this as “undefined behavior” then goes crazy mangling your code without warning. If you do this, be sure to turn on all the warnings and pedantic settings you can to get every error flagged.

Ah thanks, for the prompt reply. I think I was just mixing up declaring with initializing in my head.

In this case the compiler did thoroughly scold me for attempting to declare a variable inside the parenthesis :sweat_smile:. At this point in the book I’m still just using the -Wall and -g flags when compiling, but I recall in one of the videos you mentioned that -Wall (deceptively) does not actually print out all warnings … I’ll have to remember to look into which flags to use to get it to be fully verbose.

Yes, -Wall means “Wellllllllll maybe all”. Here’s a bunch you can try:

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

I think:

-Wall -Wextra -pedantic

That’s a good start. There’s also -Werror and various other options that really flag every possible problem. Try flipping on all the ones you can and see what you get.

A free service run by Zed A. Shaw for learncodethehardway.org.