C Global functions Declarations Definitions Preprocessor Directives Dr

C++ Global functions Declarations & Definitions Preprocessor Directives Dr. Mark L. Hornick CS-1030 Dr. Mark Hornick 1

Before there was C++ l there was just plain C l l No classes = no class methods Instead, there were just functions… l l l No classes in C Instead: structs – more on those later (maybe) Today in C++, they are called global functions E. g. main() is a global function …and variables CS-1030 Dr. Mark Hornick 2

Splitting an program among multiple source (. c) files Scenario: Divide the implementation of a program into two source files: l Demo. c defines the main() and get. User. Input() and print. Output() functions l B. c defines the calculate() function In order to call calculate() from within Demo. cpp, the calculate() function must be declared in Demo. c Likewise, in order to call print. Output() from within B. c, the print. Output() function must be declared in B. c, or someplace else (like B. h) CS-1030 Dr. Mark Hornick 3

Declarations vs Definitions l Declarations announce the existence of something to the compiler l l Declarations can be announced in many places Definitions specify what those “somethings” are l Definitions can only be provided once CS-1030 Dr. Mark Hornick 4
![Function declarations l Syntax l l [extern] int func 1(int, float); This just announces Function declarations l Syntax l l [extern] int func 1(int, float); This just announces](http://slidetodoc.com/presentation_image_h2/9c01dff4b4c8657bf7ea60983787e20e/image-5.jpg)
Function declarations l Syntax l l [extern] int func 1(int, float); This just announces to the compiler that, somewhere, there is a corresponding definition (containing a body) for the function CS-1030 Dr. Mark Hornick 5

Function definitions l Syntax l l int func 1(int a, float b) { <C++ statements go here…> } This defines what the function actually is CS-1030 Dr. Mark Hornick 6

Global variable declarations l Syntax l l extern int x; This just announces to the compiler that, somewhere, there is a corresponding definition for the variable CS-1030 Dr. Mark Hornick 7

Global variable definitions l Syntax int x=10; l l l This defines what the variable actually is Memory for the variable is allocated What about initialization? l Not required, but if not supplied, value is undefined CS-1030 Dr. Mark Hornick 8

#include directive l Typing a lot of function and variable declarations can be tedious and error-prone l Instead, we type those declarations only once in a header file, and merge the declarations in that file with those in the. c file l l A header file usually has the. h file extension The merging is done via the #include directive: #include <file. h> l l A header file can recursively #include other header files This can continue many levels deep, which is often the case CS-1030 Dr. Mark Hornick 9

#include details l The #include directive l l l Instructs the compiler to literally include the contents of the specified file Included files are referred to as “header files” Header files often end with. h extension l l Syntax 1: #include <file> l l Searches directory path(s) specified by the INCLUDE environment variable Syntax 2: #include “file” l l Notable exception: C++ standard library header files First searches the current directory (where the. cpp file exists, then searches directory path(s) specified by the INCLUDE environment variable Syntax 3: #include “<absolute path>” l l Searches for the specific file only at the specific location Ex: #include “C: My Documentssample. txt” CS-1030 Dr. Mark Hornick 10

C/C++ Compiler l The compiler processes each source file (. c) individually l l The compiler makes two passes through the source file First pass inspects the file for Preprocessor Directives l l l Output is an object file (. o) The term “object file” has nothing to do with C++ objects; the naming is a legacy from the era before OO #include #define #ifdef #pragma Many others… The second pass parses the C statements and (ultimately) generates the. o file l l . o contains processor-specific instructions References to “other stuff” not found within the original source file CS-1030 Dr. Mark Hornick 11

#define directive l In C, used often to alias constant values to mnemonics l l l #define A 1 Equates a symbol named “A” with literal 1 Everywhere A subsequently appears, the preprocessor substitutes the literal value 1 CS-1030 Dr. Mark Hornick 12

#ifdef l l Used for conditional compilation of code #define DEBUG 1 … #ifdef DEBUG for(…) // some C code goes here #endif The code within the #ifdef is compiled only if DEBUG is non-zero CS-1030 Dr. Mark Hornick 13

Preprocessor documentation l Search “preprocessor” in the online help CS-1030 Dr. Mark Hornick 14
- Slides: 14