다음을 통해 공유


Microsoft Extensions to C

OverviewHow Do ICompiler Options

The following are Microsoft extensions to the ANSI C standard:

Keywords

The keywords __based, __cdecl, __declspec, __except, __fastcall, __finally, __leave, __stdcall, and __try are Microsoft-specific.

Casts

The Microsoft compiler supports the following two non-ANSI casts:

  • Use of non-ANSI casts to produce l-values:

    char *p;
    (( int * ) p )++;
    

    The preceding example could be rewritten to conform with the ANSI C standard as follows:

    p = ( char * )(( int * )p + 1 );
    
  • Non-ANSI casting of a function pointer to a data pointer:

    int ( * pfunc ) ();
    int *pdata;
    pdata = ( int * ) pfunc;
    

    To perform the same cast while maintaining ANSI compatibility, you must cast the function pointer to an int before casting it to a data pointer:

    pdata = ( int * ) (int) pfunc;
    

Variable-Length Argument Lists

The Microsoft compiler supports use of a function declarator that specifies a variable number of arguments, followed by a function definition that provides a type instead:

void myfunc( int x, ... );
void myfunc( int x, char * c )
{ }

Single-Line Comments

The Microsoft C compiler supports single-line comments, which are introduced with two forward slash (//) characters:

// This is a single-line comment.

Scope

The Microsoft C compiler supports the following scope-related features:

  • Redefinitions of extern items as static:

    extern int clip();
    static int clip()
    {}
    
  • Use of benign typedef redefinitions within the same scope:

    typedef int INT;
    typedef int INT;
    
  • Function declarators have file scope:

    void func1()
    {
        extern int func2( double );
    }
    void main( void )
    {
        func2( 4 );    //  /Ze passes 4 as type double
    }                  //  /Za passes 4 as type int
    
  • Use of block-scope variables initialized with nonconstant expressions:

    int clip( int );
    int bar( int );
    void main( void )
    {
        int array[2] = { clip( 2 ), bar( 4 ) };
    }
    int clip( int x )
    {
        return x;
    }
    int bar( int x )
    {
        return x;
    }
    

Data Declarations and Definitions

The Microsoft C compiler supports the following data declaration and definition features:

  • Mixed character and string constants in an initializer:

    char arr[5] = {'a', 'b', "cde"};
    
  • Bit fields with base types other than unsigned int or signed int.

  • Declarators without either a storage class or a type:

    x;
    void main( void )
    {
        x = 1;
    }
    
  • Unsized arrays as the last field in structures and unions:

    struct zero
    {
        char *c;
        int zarray[];
    };
    
  • Unnamed (anonymous) structures:

    struct
    {
        int i;
        char *s;
    };
    
  • Unnamed (anonymous) unions:

    union
    {
        int i;
        float fl;
    };
    
    • Unnamed members:
    struct s
    {
    unsigned int flag : 1;
    unsigned int : 31;
    }
    

Intrinsic Floating-Point Functions

The Microsoft compiler supports inline generation of the x86 Specific —> atan, atan2, cos, exp, log, log10, sin, sqrt, and tan functions END x86 Specific when the Generate Intrinsic Functions (/Oi) option is specified. For C, ANSI conformance is lost when these intrinsics are used, because they do not set the errno variable.