Search code examples
cfunctiontypesdeclarationqualifiers

Are type qualifiers part of the type of an expression?


In C,

  1. are type qualifiers such as const , volatile, restrict , and _Atomic part of the type of an expression?

    For example

    const int x = 3;
    

    Which is the type of x, const int or int?

  2. Does the type of a function include type qualfiers of its parameters?

Solution

  • In the C Standard there is written (6.2.5 Types)

    26 Any type so far mentioned is an unqualified type. Each unqualified type has several qualified versions of its type,47) corresponding to the combinations of one, two, or all three of the const, volatile, and restrict qualifiers. The qualified or unqualified versions of a type are distinct types that belong to the same type category and have the same representation and alignment requirements....

    However according to function parameters for example these two declarations declare the same one function

    void f( const int );
    void f( int );
    

    From the C Standard (6.7.6.3 Function declarators (including prototypes))

    1. ...(In the determination of type compatibility and of a composite type, each parameter declared with function or array type is taken as having the adjusted type and each parameter declared with qualified type is taken as having the unqualified version of its declared type.)

    Here is a demonstrative program

    #include <stdio.h>
    
    void f( const int );
    
    int main(void) 
    {
        int x = 10;
    
        f( x );
    
        return 0;
    }
    
    void f( int x )
    {
        printf( "The argument is %d\n", x );
    }
    

    Its output is

    The argument is 10
    

    Take into account that however a definition of a function can depend on whether its parameter declared with the qualifier const or without it.