A simple transplantability problem

zhaozj2021-02-16  50

The first time I published an article, my level is extremely general, so there may be no big value here, I hope that I will not waste your time. If so, I apologize.

Look at the following paragraph:

// String length

INT Strlen (const char str []) {for (int i = 0; str [i]! = '/ 0'; i) {Continue;} Return i;}

This code is compiled without problems in the VC6.0 environment, but compiling under DEV-C is wrong, the compiler prompts for use Obsole Binding at 'I', meaning the discarded I.

The reason is that the C standard specifies that the local variables in the cycle are over, his life is over, and the follow-up of this variable is illegal.

The compiler of the time does not meet the C standard in some respects, and VC6.0 is such a compiler. Therefore, the above paragraph does not conform to the C standard code, but it can be compiled smoothly without errors.

You can make some simple modifications to compile them correctly under different compilers:

INT Strlen (const char str []) {INT i; for (i = 0; str [i]! = '/ 0'; i) Continue; Return i;}

The definition declaration of the variable i is mentioned in front of the loop.

Since the various compilers now meet C standards, some codes that do not meet the C standard can be successfully compiled, and before the compiler can comply with C standards, we have a long way to go. Maybe it will be very dark, so don't forget to make your code don't rely on a particular implementation, so that the transplant is unnecessary.

转载请注明原文地址:https://www.9cbs.com/read-25045.html

New Post(0)