0

So I wrote this code

char *word = "Metal Gear";
int counter = 0;
printf("word = ");

for(;;)
{
     printf("%c", *word);
     *(word++);
     counter++;
     if(*word == '\0')
        break;
}

The variable counter to count the length of my word string.
How come this code does not print the null character?
I know that I told it to break once it encounters a null character, but my print statement is before the if clause, so shouldn't it print it then break out of the loop.
How come there's no difference if the if clause is at the very beginning of the for loop ?!

update: while I'm at it, does c allow variable length for arrays or not?
I'm confused by this because I read that it allows it and that it doesn't.
My assumption that c99 allows it but many articles and blogs haven't been updated since the release of c99 standard. Am I correct?

4
  • 1
    you are incrementing your pointer after printing. Once incremented, you check for null, and exit the for. Commented Apr 24, 2014 at 13:56
  • It makes sense now. Thanks man. Commented Apr 24, 2014 at 14:13
  • no way, when print function see the null character, it will stop printing. Commented Apr 24, 2014 at 14:21
  • If the code you posted has you confused, then I wouldn't advise you to concern yourself with VLAs yet. Also, don't ask a completely unrelated question in the same post! Make a different post. Commented Apr 24, 2014 at 14:22

3 Answers 3

1

How come this code does not print the null character?

Because you break the loop before it is printed. Also, a null character likely does not have any visual representation, so even if you print it, you would probably see nothing.

my print statement is before the if clause, so shouldn't it print it then break out of the loop.

You increase the pointer by 1 after printing, so when determining when to break, you always check the next character after the one you just printed.

Sign up to request clarification or add additional context in comments.

2 Comments

I see. Suppose my if clause is checking for the last character before the null statement(in this the 'r'), is there any arrangement in the body of the for loop that would prevent the last character from being printed ?
update: I got it. I was confused because before this code, I've only payed attention to the overall logic of the code and never payed to the possible differences the order of the statements make. Thank you for your answer.
0

First thing: printf expects a valid (i.e. non-NULL) pointer for its %c argument so passing it a NULL is officially undefined and is smart

Second : According to logic You break from for loop before printing. If you want to check try following.

 #include <stdio.h>
 #include <string.h>

 int main()
  {
       char *word = "Metal Gear";
       int counter = 0;
       printf("word = ");

    for(;;)
     {
      printf("%c ", *word);
      if(*word == '\0'){
          printf("%d", counter);
          break;
       }
        *(word++);
        counter++;
     }
      return 0;
   }

As in printf, i have given space extra it will print as "word = M e t a l G e a r 10" with 2 space gap between 'r' & '10'

Comments

0

while I'm at it, does c allow variable length for arrays or not? I'm confused by this because I read that it allows it and that it doesn't. My assumption that c99 allows it but many articles and blogs haven't been updated since the release of c99 standard. Am I correct?

VLAs were introduced with C99. Sites that say C allows them are correct; sites that say C does not allow VLAs are out of date.

However, there are still C compilers/implementations in use that target C89 (C90, C94) (eg: gcc -std=c89 -pedantic ...). For these compilers, VLAs are not an option.

Anyway, except for very small arrays, you should not use VLAs in your code :)
Mainly because VLAs have no error checking.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.