-1

I'm studying for C test soon, and I will appreciate your help with a few code reading I can't figure out myself, the first one is:

main()
{
    char *p = "Hello", *q = "world!";
    while (*p * *q)
        p++, ++q;
    printf("%c", *q - *p);
}

If you guys can help me understand the output and what is going on there that will help me a lot.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Natezone
  • 1
  • 5

1 Answers1

0

The code you posted seems wrong to me.

main()

should probably be int main() (Declare main prototype).

{
    char *p = "Hello", *q = "world!";

should better be const char *p; (What is the type of a string literal in C++?).

    while (*p * *q)

while the value pointed to by p multiplied with the value pointed to by q is different from zero

        p++, ++q;

increment both p and q.

    printf("%c", *q - *p);

It is that the printf is not part of the while loop. In any case it prints the arithmetic difference of the values pointed to.

}
Community
  • 1
  • 1
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137