Programming For Engineers Multiple Source Files

7m ago
6 Views
1 Downloads
1.72 MB
12 Pages
Last View : 5d ago
Last Download : 3m ago
Upload by : Kamden Hassan
Transcription

Programming for Engineers Multiple Source Files ICEN 200 – Spring 2018 Prof. Dola Saha 1

C Source Files Ø A C program may be divided among any number of source files. By convention, source files have the extension .c. Ø Each source file contains part of the program, primarily definitions of functions and variables. Ø One source file must contain a function named main, which serves as the starting point for the program. Ø 2

Advantage of Splitting Ø Splitting a program into multiple source files has significant advantages: § Grouping related functions and variables into a single file helps clarify the structure of the program. § Each source file can be compiled separately, which saves time. § Functions are more easily reused in other programs when grouped in separate source files. 3

Header Ø Problems that arise when a program is divided into several source files: § How can a function in one file call a function that’s defined in another file? § How can a function access an external variable in another file? § How can two files share the same macro definition or type definition? Ø The answer lies with the #include directive, which makes it possible to share information among any number of source files. 4

Header Ø Ø Ø Ø Ø The #include directive tells the preprocessor to insert the contents of a specified file. Information to be shared among several source files can be put into such a file. #include can then be used to bring the file’s contents into each of the source files. Files that are included in this fashion are called header files (or sometimes include files). By convention, header files have the extension .h. 5

MACRO Most large programs contain macro definitions and type definitions that need to be shared by several source files. Ø These definitions should go into header files. Ø 6

Example MACRO Suppose that a program uses macros named BOOL, TRUE, and FALSE. Ø Their definitions can be put in a header file with a name like boolean.h: Ø #define BOOL int #define TRUE 1 #define FALSE 0 Ø Any source file that requires these macros will simply contain the line #include "boolean.h" 7

Example Sharing MACRO Ø A program in which two files include boolean.h: 8

MACRO Sharing – Why? Ø Advantages of putting definitions of macros and types in header files: § Saves time. We don’t have to copy the definitions into the source files where they’re needed. § Makes the program easier to modify. Changing the definition of a macro or type requires editing a single header file. § Avoids inconsistencies caused by source files containing different definitions of the same macro or type. 9

Sharing Function Prototype Suppose that a source file contains a call of a function f that’s defined in another file, foo.c. Ø Calling f without declaring it first is risky. Ø § The compiler assumes that f’s return type is int. § It also assumes that the number of parameters matches the number of arguments in the call of f. So, we put f’s prototype in a header file (foo.h), then include the header file in all the places where f is called. Ø We’ll also need to include foo.h in foo.c, enabling the compiler to check that f’s prototype in foo.h matches its definition in foo.c. Ø 10

Sharing Variable To share a variable among files, we put its definition in one source file, then keyword extern is used to declare a variable without defining it. Ø For example, Ø § int i; // in file1.c § extern int i; // in file2.c Ø extern informs the compiler that i is defined elsewhere in the program, so there’s no need to allocate space for it. 11

Compiling Multiple Source Files hello.h helloExample.c helloFn.c gcc helloExample.c helloFn.c -o hello ./hello Hello ICEN 200! 12

5 Header ØThe #includedirective tells the preprocessor to insert the contents of a specified file. ØInformation to be shared among several source files can be put into such a file. Ø#includecan then be used to bring the file's contents into each of the source files. ØFiles that are included in this fashion are called header files(or sometimes include files).

Related Documents:

Bruksanvisning för bilstereo . Bruksanvisning for bilstereo . Instrukcja obsługi samochodowego odtwarzacza stereo . Operating Instructions for Car Stereo . 610-104 . SV . Bruksanvisning i original

10 tips och tricks för att lyckas med ert sap-projekt 20 SAPSANYTT 2/2015 De flesta projektledare känner säkert till Cobb’s paradox. Martin Cobb verkade som CIO för sekretariatet för Treasury Board of Canada 1995 då han ställde frågan

service i Norge och Finland drivs inom ramen för ett enskilt företag (NRK. 1 och Yleisradio), fin ns det i Sverige tre: Ett för tv (Sveriges Television , SVT ), ett för radio (Sveriges Radio , SR ) och ett för utbildnings program (Sveriges Utbildningsradio, UR, vilket till följd av sin begränsade storlek inte återfinns bland de 25 största

Hotell För hotell anges de tre klasserna A/B, C och D. Det betyder att den "normala" standarden C är acceptabel men att motiven för en högre standard är starka. Ljudklass C motsvarar de tidigare normkraven för hotell, ljudklass A/B motsvarar kraven för moderna hotell med hög standard och ljudklass D kan användas vid

LÄS NOGGRANT FÖLJANDE VILLKOR FÖR APPLE DEVELOPER PROGRAM LICENCE . Apple Developer Program License Agreement Syfte Du vill använda Apple-mjukvara (enligt definitionen nedan) för att utveckla en eller flera Applikationer (enligt definitionen nedan) för Apple-märkta produkter. . Applikationer som utvecklas för iOS-produkter, Apple .

och krav. Maskinerna skriver ut upp till fyra tum breda etiketter med direkt termoteknik och termotransferteknik och är lämpliga för en lång rad användningsområden på vertikala marknader. TD-seriens professionella etikettskrivare för . skrivbordet. Brothers nya avancerade 4-tums etikettskrivare för skrivbordet är effektiva och enkla att

Den kanadensiska språkvetaren Jim Cummins har visat i sin forskning från år 1979 att det kan ta 1 till 3 år för att lära sig ett vardagsspråk och mellan 5 till 7 år för att behärska ett akademiskt språk.4 Han införde två begrepp för att beskriva elevernas språkliga kompetens: BI

**Godkänd av MAN för upp till 120 000 km och Mercedes Benz, Volvo och Renault för upp till 100 000 km i enlighet med deras specifikationer. Faktiskt oljebyte beror på motortyp, körförhållanden, servicehistorik, OBD och bränslekvalitet. Se alltid tillverkarens instruktionsbok. Art.Nr. 159CAC Art.Nr. 159CAA Art.Nr. 159CAB Art.Nr. 217B1B