13.1 Interfacing to C
Interfacing to C with GNAT can use one of two approaches:
- The types in the package Interfaces.C may be used.
- Standard Ada types may be used directly. This may be less portable to
other compilers, but will work on all GNAT compilers, which guarantee
correspondence between the C and Ada types.
Pragma Convention C may be applied to Ada types, but mostly has no
effect, since this is the default. The following table shows the
correspondence between Ada scalar types and the corresponding C types.
Ada Type
|
C Type
|
---|
Integer
|
int
|
Short_Integer
|
short
|
Short_Short_Integer
|
signed char
|
Long_Integer
|
long
|
Long_Long_Integer
|
long long
|
Short_Float
|
float
|
Float
|
float
|
Long_Float
|
double
|
Long_Long_Float
|
This is the longest floating-point type supported by the hardware.
|
Additionally, there are the following general correspondences between Ada
and C types:
- Ada enumeration types map to C enumeration types directly if pragma
Convention C is specified, which causes them to have int
length. Without pragma Convention C, Ada enumeration types map to
8, 16, or 32 bits (i.e., C types signed char, short,
int, respectively) depending on the number of values passed.
This is the only case in which pragma Convention C affects the
representation of an Ada type.
- Ada access types map to C pointers, except for the case of pointers to
unconstrained types in Ada, which have no direct C equivalent.
- Ada arrays map directly to C arrays.
- Ada records map directly to C structures.
- Packed Ada records map to C structures where all members are bit fields
of the length corresponding to the
type'Size
value in Ada.