PRB: Undesired Side Effects from toascii, tolower, toupper

Last reviewed: August 26, 1997
Article ID: Q98588
The information in this article applies to:
  • The C Run-time (CRT) included with: - Microsoft C for MS-DOS, versions 6.0, 6.0a, 6.0ax - Microsoft C/C++ for MS-DOS, version 7.0 - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51 - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1, 4.0, 4.1,

         4.2, 5.0
    

SYMPTOMS

In an application compiled with Microsoft C/C++, using the toascii, tolower, or toupper macros with a pointer value produces incorrect results, such as corrupted strings or a GP fault, depending on which compiler you use.

CAUSE

Because the tolower and toupper macros evaluate the input argument twice, the compiler performs any pointer arithmetic specified in a macro argument twice. This problem occurs most often with macros that affect an individual character because character pointers are common arguments for these macros.

RESOLUTION

The text below presents two methods to address the behavior the side effects cause.

  1. Modify the code to remove the pointer arithmetic expression from the macro argument. In the code fragment below, because the argument to the toupper macro does not change, the side effect does not occur when the macro evaluates the argument a second time.

         while (*x != '\0')
    
            {
            *x = (char) toupper((int) *x);
            x++;
            }
    
    

  2. The compiler provides more than one implementation for some functions. For example, to use a different implementation of the tolower or toupper function, perform the following three steps:

    a. Specify the /Za option (ANSI compatibility) on the compiler

          command line.
    

    b. Use the _tolower() or _toupper() functions in place of the

          tolower or toupper macros.
    

    c. After including the <ctype.h> file in your application source

          code, use the #undef tolower or #undef toupper statement to
          remove the macro definition from the code.
    

MORE INFORMATION

Sample Code

   /*
    * Compile options needed: none
    */

   #include <stdio.h>
   #include <ctype.h>

   char string[20]="this is a string";

   void main(void)
   {
      char *x;

      x = string;
      while (*x != '\0')
         *x = (char)toupper((int) *(x++));
      printf("%s\n", string);
   }

The sample code above may produce the following output:

   thHssiI    ttiIgg
Keywords          : CRTIss
Version           : 6.0 6.0a 6.0ax 7.0 1.0 1.5 1.51 2.0 2.1 4.0 4.1 4.2 5.0
Platform          : MS-DOS NT WINDOWS
Issue type        : kbprb


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 26, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.