Compatible Type and Composite Type in C Programming

0
In C programming, understanding the concepts of compatible types and composite types is crucial for writing efficient and error-free code. Here, we will discuss these concepts in detail.

Compatible Type

Two types are said to have a compatible type if their types are the same. Additional rules for determining whether two types are compatible are described in the C standard for type specifiers, type qualifiers, and declarators. 

Moreover, two structure, union, or enumerated types declared in separate translation units are compatible if their tags and members satisfy certain requirements. For instance, if one is declared with a tag, the other should be declared with the same tag. If both are completed anywhere within their respective translation units, then additional requirements apply. 

All declarations that refer to the same object or function should have a compatible type; otherwise, the behavior is undefined.

Composite Type

A composite type can be constructed from two types that are compatible. It is a type that is compatible with both of the two types and satisfies certain conditions. 

For instance, if both types are array types, the composite type follows certain rules. If one type is an array of known constant size, the composite type is an array of that size. If one type is a variable length array whose size is specified, the composite type is a variable length array of that size. 

If only one type is a function type with a parameter type list (a function prototype), the composite type is a function prototype with the parameter type list. If both types are function types with parameter type lists, the type of each parameter in the composite parameter type list is the composite type of the corresponding parameters.

For an identifier with internal or external linkage declared in a scope in which a prior declaration of that identifier is visible, if the prior declaration specifies internal or external linkage, the type of the identifier at the later declaration becomes the composite type.
C
// Forward references
array declarators (6.7.6.2);
For example, given the following two file scope declarations:
C
int f(int (*)(), double (*)[3]);
int f(int (*)(char *), double (*)[]);
The resulting composite type for the function is:

C
int f(int (*)(char *), double (*)[3]);
Tags

Post a Comment

0Comments
Post a Comment (0)