Ada 004 - Lesson 2 - Ada and C

AdaCore
18 Sept 201824:26

Summary

TLDRThis ADA University lecture delves into mixed language programming, focusing on integrating Ada with C. It covers Ada's unique features for interfacing with foreign languages, as outlined in the 2012 ISO standard. The lecture includes examples of importing and exporting subprograms and memory objects between Ada and C, utilizing the 'Interfaces.C' package for scalars, strings, and pointers. It also addresses potential issues with elaboration and the use of pragmas for generating import statements from C headers. A quiz tests understanding of these concepts, aiming to solidify the knowledge imparted throughout the lecture.

Takeaways

  • 📘 The ADA programming language has unique features for interfacing with foreign languages, including C, as part of its ISO standard 8652 revision 2012.
  • 🔄 The lecture covers the application of Ada's import/export aspects, link name, and external name in mixed language programming with C.
  • 📚 It is recommended to start from the first lecture of the series for a comprehensive understanding of Ada and C mixed language programming.
  • 🔑 The 'Interfaces.C' package is vital for interfacing Ada with C, providing basic types, constants, and subprograms for scalar and string data passing.
  • 📝 The 'Interfaces.C.Strings' package allows Ada programs to work with C-style strings, offering type-safe operations and conversions.
  • 🔍 Ada can import C function pointers and use them as access to subprogram entities, demonstrating interoperability between the two languages.
  • 📌 The 'Interfaces.C.Pointers' package facilitates C-style pointer operations in Ada, including pointer arithmetic and array handling.
  • đŸ› ïž The GCC compiler can generate Ada specifications from C header files using the '-Fdump-ada-spec' flag, simplifying the integration process.
  • 🔒 Ada requires elaboration before accessing exported entities in a C main program, ensuring proper initialization and finalization.
  • 📉 The quiz section of the lecture tests understanding of key concepts, such as importing/exporting subprograms, handling memory objects, and using Ada types with C pointers.
  • 🎓 The lecture concludes with a quiz to reinforce learning, emphasizing the practical application of Ada's features for interfacing with C.

Q & A

  • What is the main subject of the lecture series from ADA University?

    -The main subject of the lecture series is mixed language programming with Ada and C, focusing on the features of the Ada programming language that support integration with the C programming language.

  • What is the purpose of the 'convention' aspect in Ada when interfacing with C?

    -The 'convention' aspect in Ada is used to specify the calling convention of a subprogram when interfacing with C, ensuring that the Ada program correctly calls C functions using the appropriate calling conventions.

  • What is the role of the 'external name' aspect in Ada when importing a C subprogram?

    -The 'external name' aspect is used to identify the name of the subprogram in the C source code, allowing Ada to correctly reference the C function despite any naming differences between the two languages.

  • How can a C subprogram be imported into an Ada program?

    -A C subprogram can be imported into an Ada program by declaring it with the 'Import' aspect and specifying the 'External Name' aspect to match the name of the C function. The Ada main program can then call this imported function using an Ada entity identifier.

  • What is the purpose of the 'Interfaces.C' package in Ada?

    -The 'Interfaces.C' package in Ada provides basic types, constants, and subprograms that allow an Ada program to pass scalars and strings to C functions. It includes types that resemble C types, facilitating the interfacing between Ada and C.

  • Why is the 'Interfaces.C.Strings' package useful in Ada?

    -The 'Interfaces.C.Strings' package is useful because it allows an Ada program to allocate, reference, update, and free C-style strings. It provides types and subprograms for working with C strings in an Ada program, ensuring type safety and ease of use.

  • What is the significance of the 'Interfaces.C.Pointers' package in Ada?

    -The 'Interfaces.C.Pointers' package is significant because it allows the Ada programmer to perform C-style operations on pointers. It includes an access type pointer that is C-compatible and provides functions for pointer arithmetic and copying.

  • What is the issue that may arise when using a C main program with exported Ada entities that require elaboration?

    -An issue that may arise is that the Ada elaboration code, which performs explicit initialization of exported entities, must be executed by the C main program before accessing the exported entities. This is done by calling an external function called 'Ada_Init'.

  • How can Ada generate import pragmas for entities defined in C header files?

    -Ada can generate import pragmas for entities defined in C header files by using the GCC compiler with the '-Fdump-ada-spec' flag along with the name of the C header file. This generates an Ada package specification that imports the functions from the C header.

  • What is the importance of calling 'Ada_Final' in a C main program?

    -The call to 'Ada_Final' is important in a C main program to ensure that all Ada finalization code is executed upon completion of the program. This is necessary for proper resource cleanup and adherence to Ada's deterministic finalization properties.

  • What is the recommended approach for handling C strings in Ada?

    -The recommended approach for handling C strings in Ada is to use the 'Interfaces.C.Strings' package, which provides type-safe subprograms for converting C string pointers into Ada strings, ensuring proper memory management and avoiding potential runtime errors.

Outlines

00:00

📚 Introduction to Mixed Language Programming

In this lecture, Martin Pike introduces the ADA University course on mixed language programming with Ada and C. The lecture covers the features of the Ada programming language that support integration with C, including conventions, import/export aspects, and external names. He emphasizes the importance of starting from the beginning of the lecture series for a comprehensive understanding. The lecture will explore how to mix Ada and C, the use of interface child packages, and how to create main programs in either language that utilize foreign subprograms.

05:02

🔄 Importing C Subprograms into Ada

This section demonstrates how to import a subprogram implemented in C into an Ada main program. It explains the use of the 'external name' aspect to link the subprogram in the C source code with the Ada main program. An example is provided where an Ada program calls a C function that returns the value 10. Additionally, it discusses importing memory objects from C into Ada using conventions and external names, with a focus on type compatibility between Ada and C.

10:06

đŸ“€ Exporting Ada Subprograms to C

This paragraph covers exporting Ada subprograms to be used in a C main program. It highlights the similarities between imported and exported subprograms, with the main difference being the use of the export aspect. Examples are provided showing the declaration of exported Ada subprograms and their implementation. It also demonstrates exporting Ada entities, such as constant memory objects, to C programs and how these entities are used in C code.

15:08

📩 Interfaces and Child Packages

This part explains the package 'interfaces.C,' which provides basic types, constants, and subprograms for Ada to interface with C. It encourages studying this package in detail. Two child packages related to C pointers and strings are introduced. Examples include importing arrays of memory objects from C into Ada and converting C strings to Ada strings using the 'interfaces.C.strings' package. The use of these packages simplifies the integration of Ada with C functions and data structures.

20:09

🔍 Advanced Mixed Language Programming Techniques

Advanced techniques for mixed language programming are discussed, including the use of C function pointers in Ada. The paragraph provides examples of declaring function pointers in C and importing them into Ada as access-to-subprogram types. It also addresses the elaboration issues when using a C main program with exported Ada entities, emphasizing the need for calling 'ada_init' and 'ada_final' functions in the C program. The utility of automatic import pragma generation from C header files using GCC is also highlighted.

đŸ§© Quiz and Practical Applications

The final section of the lecture transitions to a quiz designed to test the understanding of the topics covered. It presents multiple-choice and code-correctness questions, focusing on importing and exporting subprograms, handling memory objects, and mixing Ada and C pointers. Each question is accompanied by an explanation of the correct answer, reinforcing key concepts. The lecture concludes by encouraging students to continue learning through other lectures in the course.

Mindmap

Keywords

💡Ada Programming Language

Ada is a structured, statically typed, imperative, and object-oriented high-level computer programming language. In the context of the video, Ada's unique features for interfacing with other languages, particularly C, are highlighted. The lecture emphasizes Ada's capabilities in foreign language integration, which are essential for mixed-language programming.

💡C Programming Language

C is a general-purpose, procedural computer programming language supporting structured programming. The video discusses the integration of Ada with C, focusing on how functions and memory objects can be shared between the two languages. This interoperability is a core theme of the lecture, showcasing the strengths of combining Ada's strong typing with C's widespread use.

💡Convention

In Ada, 'Convention' refers to the pragma that specifies the calling convention of a foreign subprogram or object. This allows Ada to call C functions and vice versa. The video explains how to use the 'Convention' pragma to import and export functions and variables between Ada and C, which is crucial for mixed-language programming.

💡Import/Export

These terms refer to the ability of Ada to import functions and variables from C, and to export Ada functions and variables to C. The lecture provides detailed examples of importing C subprograms into Ada and exporting Ada subprograms for use in C. This bidirectional interoperability is fundamental to mixed-language applications.

💡External Name

The 'External Name' aspect in Ada is used to specify the exact name of the foreign function or variable as it appears in the foreign language source code. This ensures that the Ada compiler can correctly link to the corresponding C entity. The video highlights the importance of matching these names exactly to avoid linking errors.

💡Interfaces.C

This is a package in Ada that provides types, constants, and subprograms to facilitate interaction with C. The video discusses how Interfaces.C allows Ada to pass scalars and strings to C functions and handle C pointers and strings. This package is essential for bridging the differences between Ada and C, enabling seamless interoperability.

💡Subprogram

In Ada, a subprogram is a procedure or function that performs a specific task. The video covers how subprograms can be imported from C into Ada and exported from Ada to C. This ability to share subprograms is key to integrating Ada and C codebases effectively.

💡Memory Objects

Memory objects refer to variables or constants in memory that can be shared between Ada and C. The video explains how Ada can import memory objects from C and export its own memory objects for use in C programs. This is important for sharing data between the two languages.

💡Interfaces.C.Strings

A child package of Interfaces.C, this package provides types and subprograms for handling C-style strings in Ada. The video demonstrates how to convert C strings to Ada strings and vice versa, making it easier to work with string data across both languages.

💡Interfaces.C.Pointers

This package includes types and operations for working with C-style pointers in Ada. The video shows how to perform pointer arithmetic and copy operations, which are necessary for handling complex data structures shared between Ada and C. This package helps Ada programmers manipulate C pointers safely and effectively.

Highlights

Introduction to a lecture series on mixed language programming with Ada and C.

Unique features of Ada for interfacing with foreign languages as part of ISO standard 8652 revision 2012.

The importance of understanding Ada aspects involved in Ada and C mixed language programming.

Explanation of Ada's convention, import/export, link name, and external name for interfacing with C.

Demonstration of importing a C subprogram into an Ada program using the C calling convention.

How to import memory objects from C into Ada using Ada types from the interfaces package.

Exporting Ada subprograms and constants to a C main program with different entity names for external references.

The role of the 'interfaces' package in Ada for interfacing with C basic types, constants, and subprograms.

Details of 'interfaces.C' package for working with C pointers and strings in Ada.

Importing and exporting Ada and C strings using the 'interfaces.C.strings' package.

Using the 'interfaces.C.pointers' package for C-style pointer operations in Ada.

Importing C function pointers and using them as access to subprogram entities in Ada.

The necessity of calling 'Ada.Init' in a C main program before using exported Ada entities.

Automatic generation of import pragmas for C header files using the GCC compiler's '-F dump Ada spec' flag.

Quizzes to test understanding of Ada and C mixed language programming concepts.

Common issues and solutions when using C main programs with exported Ada entities that require elaboration.

The use of 'interfaces.T.strings' for type-safe handling of C strings in Ada.

Conclusion of the lecture with an invitation to continue learning Ada through other courses.

Transcripts

play00:01

hello my name is Martin Pike and welcome

play00:05

to this lecture from the ADA University

play00:08

course on mixed language programming

play00:10

with Ada and C the subject of this

play00:14

lecture are the features of the ADA

play00:16

programming language that provides

play00:18

support for integration with the C

play00:21

programming language together we shall

play00:24

complete a series of slides and then you

play00:28

will be assessed on your learning using

play00:30

some quiz questions

play00:35

the ADA programming language offers

play00:38

unique features for interfacing to

play00:40

foreign programming languages that are

play00:43

part of amateur and progressive iso

play00:46

standard 8 652 revision 2012 if you

play00:52

haven't started at the beginning of this

play00:54

lecture series I would strongly suggest

play00:56

that you stop watching this lecture and

play00:59

attend the first lecture in this series

play01:02

it contains significant background

play01:04

information that will greatly help with

play01:07

learning this subject and from this

play01:10

point onwards it is assumed you

play01:13

understand the ADA aspects involved in

play01:16

ADA and see mixed language programming

play01:18

to recap they are convention

play01:24

import/export link name and external

play01:28

name

play01:29

in this lecture we're going to cover how

play01:31

these aspects are applied to mixing Ada

play01:34

and C we're also going to cover the

play01:38

interfaces child packages specific to

play01:41

mixing Ada and C finally we'll look at

play01:45

how you can have a main program in

play01:47

either language and make use of forged

play01:50

foreign language sub programs

play01:56

we start off by looking at how a

play02:00

subprogram implemented in c can be

play02:02

imported into an a domain program the

play02:06

example code shows an ADA main program

play02:09

declaring a nested sub program that is

play02:13

an imported function using the c calling

play02:16

convention

play02:17

the external name aspect has been used

play02:20

to identify the name of the sub program

play02:22

in the C source code the subsequent

play02:26

fragment of C code shows the sub program

play02:29

implementation that returns the numeric

play02:32

value 10

play02:34

the body code of the ADA main program

play02:37

cause the imported C function using the

play02:40

ADA entity identifier which causes the

play02:43

value 10 to be output

play02:48

it is also possible to import memory

play02:52

objects from C as we can see in this

play02:55

fragment of ADA code the familiar

play02:58

Convention import and external named

play03:01

aspects are used to specify the ADA

play03:04

entity used to identify the foreign

play03:07

language memory object in this case the

play03:11

memory object is called the length and

play03:13

it is defined in the subsequent snippet

play03:16

of C code as being a constant size

play03:20

underscore T typed value of 20

play03:23

you will notice the use of an ADA type

play03:26

from interfaces see that resembles the C

play03:30

size underscores T type from the

play03:33

standard Lib header file we will come on

play03:37

to the details of interfaces see later

play03:40

in this lecture but for now understand

play03:43

its vital role in this subject the

play03:46

output from the ADA main program would

play03:48

be the value 20

play03:54

exporting an ADA sub program to a C main

play03:57

program it's just as simple as importing

play03:59

a C sub program into ADA the exported

play04:03

sub program entity looks very similar to

play04:05

an imported sub program with the only

play04:08

difference being a swapping off the

play04:10

import aspect to the export aspect

play04:14

of course there is a need for an

play04:16

implementation of the exported sub

play04:18

program in ada

play04:20

here we show a package specification

play04:23

containing the declaration of an

play04:26

exported sub program and notably the ADA

play04:29

entity name is different from the name

play04:31

you used to reference it externally

play04:35

there is a simple implementation in The

play04:37

Associated package body that returns the

play04:40

value 60

play04:42

the example C main program here cause

play04:45

this exported ADA sub program to print

play04:47

out the value

play04:52

in this example we export an ADA entity

play04:56

to see that's a constant memory object

play04:59

with a value 80

play05:02

the same aspects have been used as when

play05:04

exporting a subprogram but the external

play05:07

name aspect has been altered in this

play05:10

case to the length

play05:12

on the seaside of the program the main

play05:15

function again has to call ADA in it and

play05:18

ADA final and then can make use of the

play05:21

exported ADA entity the output from this

play05:25

particular C main program would be the

play05:28

value 80

play05:30

you

play05:34

the package interfaces dot C contains

play05:37

the basic types constants and sub

play05:40

programs that allow an ADA program to

play05:43

pass scalars and strings to see

play05:45

functions

play05:46

the minimum number of types constants

play05:49

and sub programs are provided to enable

play05:51

interfacing to see foreign language

play05:54

entities

play05:56

the reader is encouraged to study the

play05:58

contents of this package in detail

play06:00

offline from the course as it contents

play06:03

are very valuable to a programmer

play06:06

interfaces see is the parent package of

play06:09

two important child packages

play06:13

related specifically to see pointers and

play06:15

strings to areas where C and ADA differ

play06:19

considerably

play06:24

it is also possible to import a raise of

play06:27

memory objects from C into ADA in this

play06:31

example we see a piece of C code that

play06:33

declares for size T typed memory objects

play06:37

in an array this array is an imported

play06:40

into an ADA entity that has been typed

play06:43

using an ADA array declaration

play06:47

the a demain program iterates over the

play06:50

array printing out the hex version of

play06:52

the C memory object

play06:55

in this particular case the hex values

play06:57

10 20 30 and 40 are output to the

play07:01

console

play07:05

the interfaces see dot strings package

play07:08

is defined in the ADA programming

play07:10

language reference manual as a package

play07:13

declaring types and sub programs

play07:15

allowing an ADA program to allocate

play07:17

reference update and free c style

play07:21

strings

play07:22

in this example we see a snippet of C

play07:25

code declaring a function that returns a

play07:28

pointer to the string hello world the

play07:31

AIDA

play07:32

main program imports the c function as

play07:34

an eider entity called getstring

play07:38

the main program then goes on to use

play07:40

features of the interfaces seeds or

play07:43

strings package to convert the string

play07:45

pointer into a suitable data access type

play07:48

and subsequently accesses the target

play07:51

object as an ADA string the ADA string

play07:55

is then suitable for input into the ADA

play07:59

textio put line sub program which will

play08:03

produce the program's output of the

play08:05

string hello world

play08:10

exporting ADA strings to see main

play08:13

programs can be made simpler by using

play08:16

the interfaces dot C dot strings package

play08:19

in this code example we see a set of

play08:22

constant ADA strings being declared

play08:25

using types from interfaces that cedar

play08:27

strings the data structure is exported

play08:31

using the sea Convention as the

play08:33

externally named entity some strings the

play08:37

C main program is able to access the

play08:40

array of strings and iterate through

play08:42

them printing them out until it finds a

play08:45

null pointer please note the use of the

play08:48

null underscore PTR constant from

play08:50

interfaces c dot strings

play08:57

the child generic package interfaces

play09:00

cedar pointers allows the ADA programmer

play09:03

to perform c-style operations on

play09:06

pointers

play09:08

it includes an access type pointer that

play09:11

is C compatible and corresponds to one

play09:14

use of C's element star

play09:18

the generic allows two styles of usage

play09:20

one in which the array is terminated by

play09:24

a special terminating element and the

play09:27

other in which the programmer needs to

play09:30

keep track of the length

play09:32

this is achieved by providing two

play09:35

versions of a function called value that

play09:39

dereferences a pointer and delivers the

play09:42

designated array one provided with the

play09:46

Terminator and the other

play09:47

with the legs

play09:50

the package also provides several

play09:52

pointer arithmetic functions and copy

play09:55

procedures that copy the contents of a

play09:58

source pointer into the array designated

play10:01

by a destination pointer if generics are

play10:05

new to you then I would suggest you

play10:08

visit the programming in the large

play10:09

course from the ADA University where

play10:12

they are covered by a dedicated lecture

play10:18

ada can also import C function pointers

play10:22

and use them as access to sub program

play10:25

entities the C code declares a function

play10:30

called C underscore function that

play10:32

accepts a pointer to a size and score T

play10:35

object that is eventually dereference as

play10:37

an input to printf

play10:40

the func function pointer is assigned

play10:43

the address of this C underscore

play10:44

function

play10:46

the a domain program declares an access

play10:49

to sub program type that matches the

play10:51

signature of the C function pointer it

play10:55

then imports the func entity from C and

play10:57

D references the pointer to invoke the C

play11:00

function that eventually outputs 10

play11:03

using printf

play11:05

you

play11:08

in this slide we're going to deal with

play11:11

an issue that may arise when using a C

play11:13

main program and exported ADA entities

play11:17

that require elaboration the example

play11:20

code snippet shows an ADA package

play11:22

specification exporting a memory object

play11:25

that is explicitly initialized with the

play11:28

result of calling the function f

play11:31

the explicit initialization is performed

play11:34

by the ADA elaboration code and this

play11:37

must be executed by the C main program

play11:39

before attempting to access the exported

play11:43

entities the C main program must call an

play11:48

external function called ADA in it to

play11:51

execute the ADA elaboration code and if

play11:54

this was admitted the C main program

play11:56

would display a random value almost

play11:59

certainly not 55

play12:01

the sea main program must also call the

play12:04

external function ADA final upon

play12:07

completion to ensure all ADA

play12:10

finalization code is also executed

play12:17

the canals contain a very useful feature

play12:20

for automatically generating import

play12:23

pragmas for entities defined in c header

play12:26

files

play12:27

the example had a foul here called

play12:30

Sealift H provides three function

play12:33

prototypes with various return types and

play12:35

parameters

play12:37

it is possible to produce an ADA a

play12:40

patent specification for this C header

play12:42

file by passing a particular flag to the

play12:45

GCC compiler the - F dump ADA spec flag

play12:49

is passed to GCC along with the name of

play12:52

the C header file the - capital C flag

play12:56

is used to ensure comments from the C

play12:58

header file are copied into the

play13:01

generated ADA package specification

play13:04

the resulting ADA package specification

play13:06

for the example C header file is shown

play13:09

at the bottom of this slide

play13:12

each function has been imported as an

play13:14

identity and appropriate data types have

play13:17

been used for return values and

play13:19

parameters note the use of interfaces T

play13:23

dot strings for the character points a

play13:25

return value from function 3

play13:28

a time of writing a to 2005 pragmas are

play13:32

generated but these could be manually

play13:34

converted to a de 2012 aspects perhaps

play13:38

in the future support for a tour 2012

play13:40

aspects will be added

play13:45

we've now reached the end of the slide

play13:47

section of this lecture you should now

play13:50

have enough knowledge of this subject to

play13:53

complete a small quiz with questions

play13:56

designed to test your understanding each

play13:59

question is marked and you will have a

play14:01

chance to review your score at the end

play14:03

of the lecture good luck

play14:08

question one kicks off with an example

play14:11

of importing a C sub program into an ADA

play14:15

main program the ADA entity get

play14:19

underscore length is a function

play14:21

represented by convention import and

play14:24

external name aspects the ADA main

play14:27

program body calls get length if you

play14:31

think this code is correct then please

play14:33

click the tick icon

play14:35

otherwise click the line of code that is

play14:38

incorrect

play14:45

the answer is no the code is incorrect

play14:48

and will fail to link into an executable

play14:51

the external name string must match the

play14:55

foreign language entity name exactly

play14:58

including the use of the correct case

play15:01

here we used the camel case string get

play15:05

length in the Declaration of the ADA

play15:07

entity for the imported C sub program

play15:10

however the C code used all lowercase

play15:14

for the Declaration of the get length

play15:16

function

play15:21

question 2 covers exporting an ADA sub

play15:25

program for use by a C main program the

play15:28

function get underscore length will

play15:31

return the value 60 when called study

play15:34

this code carefully if you think this

play15:37

code is correct and please click the

play15:39

check icon

play15:39

otherwise click the area of the code

play15:42

that is incorrect

play15:49

the answer is no the code is incorrect

play15:53

and will fail to execute correctly there

play15:56

must be a call to aid in it before the

play16:00

use of any exported identities by the C

play16:04

main program

play16:08

question three demonstrates the use of

play16:11

an imported memory object using the C

play16:14

convention the program declares an

play16:17

identity using the name the underscore

play16:20

length but specifies a different

play16:22

external name for the entity in the

play16:24

foreign language

play16:26

the foreign language has declared the

play16:28

memory object as a constant value 20

play16:32

if you think this code is correct then

play16:34

please click the tick icon otherwise

play16:36

click the line of code that is incorrect

play16:44

the answer is yes the code is correct

play16:47

ada is able to use memory objects

play16:50

imported from foreign languages without

play16:52

having the responsibility for setting

play16:55

their values

play17:00

question four mixes the use of a to

play17:02

access types with C pointers but without

play17:05

using any pointer arithmetic when the C

play17:08

program executes it intends to output

play17:11

the number 50 the example code is much

play17:15

larger than previous questions so please

play17:17

take your time to read it through and

play17:19

understand it if you think the code is

play17:22

correct and please click the tick icon

play17:24

otherwise click the line of code that is

play17:27

incorrect

play17:34

the answer is yes the code is correct it

play17:38

is perfectly safe to mix ADA access

play17:40

types with C pointers as long as the

play17:43

types from interfaces dot C are used as

play17:46

the target types

play17:51

question 5 presents a method of

play17:54

accessing an array of C constants

play17:56

imported as an identity the body of the

play18:00

a domain program iterates over the

play18:03

imported entity until the value 9999 is

play18:07

encountered again this code is quite

play18:10

advanced so please take some time to

play18:12

review it before attempting the question

play18:14

of correctness if you think this code is

play18:17

correct and please click the tick icon

play18:20

otherwise click the line of code that is

play18:23

incorrect

play18:30

the answer is yes the code is correct

play18:33

but it is not recommended to do this

play18:36

kind of access type of manipulation

play18:39

there are some big assumptions being

play18:41

made by the code in the question slide

play18:43

about the imported see data structure a

play18:46

better solution is shown here it is

play18:50

restricted to a single use of the tick

play18:53

access attribute and is made type safe

play18:56

by using this structure returned by the

play18:59

value sub program from the interfaces

play19:02

dot C dot points a package

play19:05

it is probably debatable about which

play19:07

solution is better but in the interests

play19:10

of fairness both have been presented in

play19:13

this lecture

play19:17

question six is a multiple-choice

play19:19

question and it poses the question which

play19:23

of these packages is not part of the

play19:25

hierarchy of packages rooted in

play19:28

interfaces si please select your chosen

play19:31

answer and then click Submit to see if

play19:34

you are correct

play19:43

the answer was interfaces see

play19:46

sub-programs this is not part of the

play19:50

interfaces c package hierarchy

play19:56

question 7 asks what output will this a

play20:00

demain program produce by using an

play20:03

imported c string will it fail to

play20:06

compile output the hello world string

play20:09

will raise program error exception at

play20:12

runtime make your selection and click

play20:15

Submit to see if you are correct

play20:24

the answer is the hello world strength

play20:27

as you can see it's very easy to use see

play20:31

strings

play20:31

imported into ADA main programs

play20:35

you

play20:38

moving on to question eight we see an

play20:41

ADA package specification exporting a

play20:44

constant string using the C Convention

play20:46

and the external name is ADA string the

play20:50

C main program uses an externally

play20:53

declared string entity called ADA string

play20:55

in a call to printf

play20:58

we'll the C main program failed to

play21:00

compile enter an infinite loop and never

play21:03

return or print the hello world string

play21:06

make our selection and then click the

play21:09

submit button to check your answer

play21:16

the answer was the hello world strength

play21:19

again this demonstrates how easy it is

play21:22

to use strongly typed ADA string

play21:24

declarations in a C main program with

play21:28

this example you can see that declaring

play21:30

all your C strings then ADA packages

play21:33

helps introduce typesafe data structures

play21:36

into your C programs

play21:41

the penultimate question again asks to

play21:44

identify what string output an aider

play21:47

main program will produce here we use

play21:50

data types from interfaces seed strings

play21:53

to work with imported c functions that

play21:56

use char star entities interfaces to c

play22:00

dot strings provides various type safe

play22:02

sub programs for working with c strings

play22:05

what do you think the output from this

play22:07

aida program will be will it be the

play22:10

hello world string nothing due to a

play22:13

program error exception at runtime

play22:15

all well the code failed to compile in

play22:17

the first place

play22:18

select your answer and click the submit

play22:21

button to see if you are correct

play22:29

the answer is the hello world string

play22:32

first the chart access array object

play22:35

returned by the imported C function is

play22:38

given to the to Charles pointer sub

play22:42

program from interfaces dot C dot

play22:44

strings the result of this sub program

play22:47

is given two value from interfaces cedar

play22:50

string which will return an ADA string

play22:53

version of the character pointer this is

play22:56

then permissible to be passed to the put

play22:58

line operation from ADA textio

play23:04

our final question continues the theme

play23:07

of using interfaces seed of strings this

play23:11

code declares a constant set of ADA

play23:13

strings and exports them as the concei

play23:16

convention entity called some strings

play23:18

the C main program uses these strings in

play23:21

a loop what do you think the output from

play23:24

the C main program will be

play23:27

will it be gibberish code or the ADA

play23:31

code raises a constraint error exception

play23:33

at runtime or the hello world string

play23:36

select your answer and click the submit

play23:39

button to see if you are correct

play23:45

not surprisingly the answer was the

play23:48

hello world string you could use this

play23:51

type of constant aid of structure to

play23:53

limit the need for error-prone C strings

play23:55

and gain the type safety of ADA strings

play23:58

that concludes the quiz please click the

play24:02

continue button to review your score

play24:07

congratulations you have completed this

play24:10

lecture I hope you have found it a

play24:13

valuable step in learning the ADA

play24:15

programming language and that you

play24:17

continue on to the other lectures in

play24:20

this course from the ADA University

play24:22

thank you

Rate This
★
★
★
★
★

5.0 / 5 (0 votes)

Étiquettes Connexes
Ada ProgrammingC ProgrammingLanguage IntegrationCoding LectureProgramming CourseSoftware DevelopmentTechnical LearningQuiz AssessmentForeign Language InterfaceAda University
Besoin d'un résumé en anglais ?