Anyone understand the "Variant" datatype???

Posted on 2004-11-26
Last Modified: 2012-06-22
This is a cross-language question that I posted in the PERL channel. However, it has just as much to do with C, so I am allocating another 500 to see if anyone here can answer it from the C end.

Question by:sapbucket
    LVL 8

    Assisted Solution

    Variant data type
    A Variant is a special kind of data type, since a Variant can mutate. Don't be afraid, it won't mutate to a virus or anything like that. It simply changes types on the fly, as needed. Even a simple operation like addition changes when you add 2 variants together. Take this for example:

       V1 = 101
       V2 = "99.5"

       PRINT V1+V2  '-- Output is 200.5

    As you'll notice V2 is originally assigned a string, but V2 has changed its type to a floating point number when the addition takes place. Note that V2 is still a string, it just mutated itself for that one operation. Similarly,

       V1 = "101"
       V2 = 99.5

       PRINT V1+V2  '-- Output is 10199.5

    In this case, V2 was originally a floating point number, but changed its type during the addition operation to a string. Notice how the order of operation defines the mutation. In the previous example, V1 was a number, so V1+V2 results in a numeric expression, while the above example produces a string.

    You can find some Information here:
    LVL 4

    Accepted Solution

    Variant data type is a special data type that can contain numeric, string, or date data as well as user-defined types and the special values Empty and Null. The VARIANT type is an all purpose data type. The Variant data type has a numeric storage size of 16 bytes and can contain data up to the range of a Decimal, or a character storage size of 22 bytes (plus string length), and can store any character text.

    Here is a simplified version of the VARIANT definition.

    struct tagVARIANT {
        VARTYPE vt; // unsigned short integer type code
        union {
        //  C++ Type      Union Name   Type Tag                Basic Type
        //  --------      ----------   --------                ----------
            long          lVal;        // VT_I4                ByVal Long
            unsigned char bVal;        // VT_UI1               ByVal Byte
            short         iVal;        // VT_I2                ByVal Integer
            double        dblVal;      // VT_R8                ByVal Double
            VARIANT_BOOL  boolVal;     // VT_BOOL              ByVal Boolean
            SCODE         scode;       // VT_ERROR
            DATE          date;        // VT_DATE              ByVal Date
            BSTR          bstrVal;     // VT_BSTR              ByVal String
            IUnknown      *punkVal;    // VT_UNKNOWN
            SAFEARRAY     *parray;     // VT_ARRAY|*           ByVal array
            // A bunch of other types that don't matter here...
            VARIANT       *pvarVal;    // VT_BYREF|VT_VARIANT  ByRef Variant
            void          * byref;     // Generic ByRef        

    Here is an example of creating a variant of type double:

    VARIANT v;
    v.vt= VT_R8;
    v.dblVal = 999.999;

    This example shows a variant containing all the actual data inside it's 16 byte structure. With the more complex string & safearry types, the data is stored separately and the VARIANT structure just contains a pointer to it.

    You can find more details at this link:

    LVL 7

    Expert Comment

    Note that there is no data conversion of the data type in C. It is the programmer's responsibility to read the type he placed in the variable, else Bad Things start to happen.
    LVL 3

    Author Comment

    Thanks for the comments!

    For a COMPLETE programmers model of the Variant type read "Programming Microsoft Visual Basic 6.0" by Francesco Balena. Very good stuff. The comments here helped me to find his book.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now