0

How many times CppBuzz.com is printed?

   int main()
    {
      int a = 0;
      while(a++ < 5-++a)
      printf("CppBuzz.com");
      return 0;
     }

how to solve the expression (5-++a) ?

rpj
  • 93
  • 9

2 Answers2

1

There is a bigger problem than determining loop counter in your code - undefined behavior.

The line

  while(a++ < 5-++a)

tries to increment the same variable a more than once without a sequence point, it invokes undefined behavior.

That said, if you don't want any conversion specification to happen, don't use printf(), use puts() instead.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • With the GCC compiler, the statement gets printed even if a has undefined behavior. How is this possible? – rpj Dec 10 '19 at 09:00
  • @PadmaR Please note that [undefined](https://en.cppreference.com/w/cpp/language/ub) doesn't necessarily mean "it won't work". – Bob__ Dec 10 '19 at 09:06
1

Code invoked undefined behaviour. Here multiple time variable a increments.

GCC Compiler generates warning:

prog.c: In function 'main':
prog.c:8:21: warning: operation on 'a' may be undefined [-Wsequence-point]
       while(a++ < 5-++a)
                     ^~~
msc
  • 33,420
  • 29
  • 119
  • 214