### Set Environment Variable Example Source: https://devdocs.io/gnu_cobol/index Example demonstrating how to set an environment variable using two DISPLAY statements. Note that this affects sub-shells but not the parent shell. ```cobol DISPLAY "MY_ENV_VAR" UPON ENVIRONMENT-NAME DISPLAY "Demonstration Value" UPON ENVIRONMENT-VALUE ``` -------------------------------- ### COB_PRE_LOAD Example Source: https://devdocs.io/gnu_cobol/index Specifies modules loaded during startup, which can include COBOL programs or C functions. Extensions should not be included in the module names. ```shell Environment name: COB_PRE_LOAD Parameter name: pre_load Purpose: modules that are loaded during startup, can be used to CALL COBOL programs or C functions that are part of a module library Type: string Note: the modules listed should NOT include extensions, the runtime will use the right ones on the various platforms, COB_LIBRARY_PATH is used to locate the modules Example: PRE_LOAD COBOL_function_library:external_c_library ``` -------------------------------- ### COB_LIBRARY_PATH Example Source: https://devdocs.io/gnu_cobol/index Sets paths for dynamically-loadable modules. The default paths are always added. ```shell Environment name: COB_LIBRARY_PATH Parameter name: library_path Purpose: paths for dynamically-loadable modules Type: string Note: the default paths .:/installpath/extras are always added to the given paths Example: LIBRARY_PATH /opt/myapp/test:/opt/myapp/production ``` -------------------------------- ### GnuCOBOL INITIALIZE Statement Examples Source: https://devdocs.io/gnu_cobol/index This comprehensive example demonstrates multiple forms of the INITIALIZE statement in GnuCOBOL. It shows default initialization, initialization with FILLER, initializing all alphanumeric fields to a specific value, and initializing numeric fields to a specific value. The COBDUMP program is used to visualize the memory contents. ```COBOL IDENTIFICATION DIVISION. PROGRAM-ID. DemoInitialize. DATA DIVISION. WORKING-STORAGE SECTION. 01 Item-1. 05 I1-A VALUE ALL '*'. 10 FILLER PIC X(1). 10 I1-A-1 PIC 9(1) VALUE 9. 05 I1-B USAGE BINARY-CHAR. 05 I1-C PIC A(1) VALUE 'C'. 05 I1-D PIC X/X VALUE 'ZZ'. 05 I1-E OCCURS 2 TIMES PIC 9. PROCEDURE DIVISION. 000-Main. DISPLAY "MOVE HIGH-VALUES TO Item-1" PERFORM 100-Init-Item-1 CALL "COBDUMP" USING Item-1 DISPLAY " " DISPLAY "INITIALIZE Item-1" INITIALIZE Item-1 CALL "COBDUMP" USING Item-1 PERFORM 100-Init-Item-1 DISPLAY " " DISPLAY "INITIALIZE Item-1 WITH FILLER" MOVE HIGH-VALUES TO Item-1 INITIALIZE Item-1 WITH FILLER CALL "COBDUMP" USING Item-1 PERFORM 100-Init-Item-1 DISPLAY " " DISPLAY "INITIALIZE Item-1 ALL TO VALUE" MOVE HIGH-VALUES TO Item-1 INITIALIZE Item-1 ALPHANUMERIC TO VALUE CALL "COBDUMP" USING Item-1 PERFORM 100-Init-Item-1 DISPLAY " " DISPLAY "INITIALIZE Item-1 REPLACING NUMERIC BY 1" MOVE HIGH-VALUES TO Item-1 INITIALIZE Item-1 REPLACING NUMERIC BY 1 CALL "COBDUMP" USING Item-1 PERFORM 100-Init-Item-1 DISPLAY " " STOP RUN . 100-Init-Item-1. MOVE HIGH-VALUES TO Item-1 . ``` -------------------------------- ### START Statement Syntax Source: https://devdocs.io/gnu_cobol/index This is the general syntax for the START statement in GNU COBOL. It allows specifying the starting point using FIRST, LAST, or a specific KEY condition. Optional INVALID KEY and NOT INVALID KEY clauses can be used for error handling. ```cobol START file-name-1 ~~~~~ [ { FIRST } ] { ~~~~~ } { LAST } { ~~~~ } { KEY { IS EQUAL TO | IS = | EQUALS } identifier-1 } { ~~~~~ ~~~~~~ } { IS GREATER THAN | IS > } { ~~~~~~~ } { IS GREATER THAN OR EQUAL TO | IS >= } { ~~~~~~~ ~~ ~~~~~ } { IS NOT LESS THAN } { ~~~ ~~~~ } { IS LESS THAN | IS < } { ~~~~ } { IS LESS THAN OR EQUAL TO | IS <= } { ~~~~ ~~ ~~~~~ } { IS NOT GREATER THAN } ~~~ ~~~~~~~ [ INVALID KEY imperative-statement-1 ] ~~~~~~~ [ NOT INVALID KEY imperative-statement-2 ] ~~~ ~~~~~~~ [ END-START ] ~~~~~~~~~ ``` -------------------------------- ### Full Program Example with EVALUATE Source: https://devdocs.io/gnu_cobol/index A complete GNU COBOL program demonstrating the EVALUATE statement with user input and conditional output. This example shows how to integrate EVALUATE into a program's main execution flow. ```COBOL DISPLAY Test-Digit " is PRIME" WITH NO ADVANCING WHEN FALSE ALSO ANY DISPLAY Test-Digit " is EVEN" WITH NO ADVANCING END-EVALUATE EVALUATE Test-Digit WHEN < 5 DISPLAY " and it's small too" WHEN < 8 DISPLAY " and it's medium too" WHEN OTHER DISPLAY " and it's large too" END-EVALUATE END-PERFORM DISPLAY "Bye!" STOP RUN . ``` -------------------------------- ### GnuCOBOL Data Item Definitions for CORRESPONDING Example Source: https://devdocs.io/gnu_cobol/index Example data item definitions used to illustrate the rules and valid matches for the CORRESPONDING clause. ```cobol 01 Q. 03 X. 05 A PIC 9(1). 05 G1. 10 G2. 15 B PIC X(1). 05 C. 10 FILLER PIC X(1). 05 G3. 10 G4. 15 D PIC X(1). 05 E PIC X(1). 05 F REDEFINES V1 PIC X(1). 05 G. 10 G6 OCCURS 4 TIMES PIC X(1). 05 H PIC X(4). 05 I PIC 9(1). 05 J. 10 K. 15 M PIC X(1). ``` ```cobol 01 Y. 02 A PIC X(1). 02 G1. 03 G2. 04 B PIC X(1). 02 C PIC X(1). 02 G3. 03 G5. 04 D PIC X(1). 02 G6 PIC X(1). 02 E PIC 9(1). 02 F PIC X(1). 02 G PIC X(4). 02 H OCCURS 4 TIMES PIC X(1). 66 I RENAMES E. 02 J. 03 K. 04 L. 05 M PIC X(1). ``` -------------------------------- ### GnuCOBOL Compilation and Execution Example Source: https://devdocs.io/gnu_cobol/index This snippet demonstrates the command-line steps for compiling a GnuCOBOL program and linking it with a C main program, followed by execution and output. ```bash C:\Users\Gary\Documents\Programs> cobc -S subcob.cbl C:\Users\Gary\Documents\Programs> gcc mainc.c subcob.s -o mainc.exe -llibcob C:\Users\Gary\Documents\Programs> mainc.exe Starting mainc... Starting cobsub.cbl Arg1=Arg1 Arg2=Arg2 Arg3=+0123456789 Back Arg1=Xrg1 Arg2=Yrg2 Arg3=987654321 Returned value=2 C:\Users\Gary\Documents\Programs> ``` -------------------------------- ### CBL_GC_FORK Usage Example Source: https://devdocs.io/gnu_cobol/index This example demonstrates how to use the CBL_GC_FORK subroutine to create a child process. It includes logic for both the parent and child processes, handling return values, and waiting for the child to complete. ```cobol IDENTIFICATION DIVISION. PROGRAM-ID. prog. DATA DIVISION. WORKING-STORAGE SECTION. 01 CHILD-PID PIC S9(9) BINARY. 01 WAIT-STS PIC S9(9) BINARY. PROCEDURE DIVISION. CALL "CBL_GC_FORK" RETURNING CHILD-PID END-CALL EVALUATE TRUE WHEN CHILD-PID = ZERO PERFORM CHILD-CODE WHEN CHILD-PID > ZERO PERFORM PARENT-CODE WHEN CHILD-PID = -1 DISPLAY 'CBL_GC_FORK is not available on the current' ' system!' PERFORM CHILD-CODE MOVE 0 TO CHILD-PID PERFORM PARENT-CODE WHEN OTHER MULTIPLY -1 BY CHILD-PID END-MULTIPLY DISPLAY 'CBL_GC_FORK returned system error: ' CHILD-PID END-EVALUATE STOP RUN. CHILD-CODE. CALL "C$SLEEP" USING 1 END-CALL DISPLAY "Hello, I am the child" MOVE 2 TO RETURN-CODE. PARENT-CODE. DISPLAY "Hello, I am the parent" CALL "CBL_GC_WAITPID" USING CHILD-PID RETURNING WAIT-STS MOVE 0 TO RETURN-CODE EVALUATE TRUE WHEN WAIT-STS >= 0 DISPLAY 'Child ended with status: ' WAIT-STS WHEN WAIT-STS = -1 DISPLAY 'CBL_GC_WAITPID is not available on the ' 'current system!' WHEN WAIT-STS < -1 MULTIPLY -1 BY WAIT-STS END-MULTIPLY DISPLAY 'CBL_GC_WAITPID returned system error: ' WAIT-STS END-EVALUATE. ``` -------------------------------- ### Simple ALLOCATE Example Source: https://devdocs.io/gnu_cobol/index Allocates storage for a BASED data item. The address of the allocated block becomes the base address of the data item. ```COBOL ALLOCATE My-01-Item ``` -------------------------------- ### Setting and Testing External Switch (Unix/Cygwin/OSX) Source: https://devdocs.io/gnu_cobol/index This command-line example demonstrates setting the COB_SWITCH_1 environment variable to ON and then executing a COBOL program that checks this switch. ```bash $ COB_SWITCH_1=ON $ export COB_SWITCH_1 $ ./testprog Switch 1 Is On $ ``` -------------------------------- ### VALUE Clause Examples with ALL Source: https://devdocs.io/gnu_cobol/index Demonstrates the use of the VALUE clause with and without the ALL option for alphanumeric and numeric literals. The _b_ denotes a space. ```COBOL PIC X(5) VALUE ‘A’ *> A_bbbb_ ``` ```COBOL PIC X(5) VALUE ALL ‘A’ *> AAAAA ``` ```COBOL PIC 9(3) VALUE 1 *> 001 ``` ```COBOL PIC 9(3) VALUE ALL ‘1’ *> 111 ``` -------------------------------- ### IF Statement with Compact Syntax (Error Example) Source: https://devdocs.io/gnu_cobol/index Shows the same IF statement logic as the previous example but written compactly, illustrating how compilers ignore indentation for scope determination. ```COBOL IF Loan-Balance < 10000 MULTIPLY Loan-balance BY 0.04 GIVING Interest ELSE MULTIPLY Loan-Balance BY 0.045 GIVING Interest DISPLAY "Interest Amount = " Interest ``` -------------------------------- ### Setting and Testing External Switch (Windows) Source: https://devdocs.io/gnu_cobol/index This command-line example demonstrates setting the COB_SWITCH_1 environment variable to ON and then executing a COBOL program that checks this switch. ```batch C:>SET COB_SWITCH_1=ON C:>testprog Switch 1 Is On C:> ``` -------------------------------- ### AWAY-FROM-ZERO Rounding Examples Source: https://devdocs.io/gnu_cobol/index Demonstrates rounding behavior for the AWAY-FROM-ZERO mode. Rounding is to the nearest value of larger magnitude. ```COBOL -3.510 => -4 | +3.510 => +4 -3.500 => -4 | +3.500 => +4 -3.499... => -4 | +3.499... => +4 -2.500 => -3 | +2.500 => +3 -2.499... => -3 | +2.499... => +3 ``` -------------------------------- ### Defining a Fixed-Size Table Source: https://devdocs.io/gnu_cobol/index Example of defining a table with a fixed number of entries (4 in this case) for quarterly revenue. ```COBOL 05 QUARTERLY-REVENUE OCCURS 4 TIMES PIC 9(7)V99. ``` -------------------------------- ### NEAREST-TOWARD-ZERO Rounding Examples Source: https://devdocs.io/gnu_cobol/index Demonstrates rounding for NEAREST-TOWARD-ZERO mode. If two values are equally near, the one with the smaller absolute value is chosen. ```COBOL -3.510 => -4 | +3.510 => +4 -3.500 => -3 | +3.500 => +3 -3.499... => -3 | +3.499... => +3 -2.500 => -2 | +2.500 => +2 -2.499... => -2 | +2.499... => +2 ``` -------------------------------- ### REPOSITORY Paragraph Example Source: https://devdocs.io/gnu_cobol/index Enables all intrinsic functions without 'FUNCTION' keyword, names user-defined functions, and specifies aliases for intrinsic and user-defined functions. ```cobol REPOSITORY. FUNCTION ALL INTRINSIC. FUNCTION MY-FUNCTION-1. FUNCTION MY-FUNCTION-2 AS "MF2". FUNCTION STANDARD-DEVIATION AS "SIGMA". ``` -------------------------------- ### REPLACE String-Clause Example Source: https://devdocs.io/gnu_cobol/index Example of using the LEADING clause to replace a prefix in words. This specific example changes words starting with '0100-' to '020-'. ```COBOL LEADING ==0100-== BY ==020-== ``` -------------------------------- ### COB_PHYSICAL_CANCEL Example Source: https://devdocs.io/gnu_cobol/index Enables physically unloading a dynamically-loadable module on CANCEL. This can free RAM and allow module changes at runtime but increases CALL resolution time. ```shell Environment name: COB_PHYSICAL_CANCEL Parameter name: physical_cancel Purpose: physically unload a dynamically-loadable module on CANCEL, this frees some RAM and allows the change of modules during run-time but needs more time to resolve CALLs (both to active and not-active programs) Alias: default_cancel_mode, LOGICAL_CANCELS (0 = yes) Type: boolean (evaluated for true only) Default: false Example: PHYSICAL_CANCEL TRUE ``` -------------------------------- ### REPLACE String-Clause Example - Deletion Source: https://devdocs.io/gnu_cobol/index Example of using the LEADING clause to delete a prefix from words. This code removes all '0100-' prefixes. ```COBOL LEADING ==0100-== BY ==== ``` -------------------------------- ### REPLACE Phrase-Clause Example Source: https://devdocs.io/gnu_cobol/index Example of using the Phrase-Clause to replace entire words or literals. This code replaces 'UPON PRINTER' with another string. ```COBOL ==UPON PRINTER== BY ==ON THE PRINTER== ``` -------------------------------- ### Build Executable Program Source: https://devdocs.io/gnu_cobol/index Use the -x option to build an executable program from the input files. This is the default behavior when compiling a main program. ```bash cobc -x myprogram.cob ``` -------------------------------- ### Direct Execution Example (Unix/OSX) Source: https://devdocs.io/gnu_cobol/index Execute a GnuCOBOL program compiled with the -x switch on Unix-like systems. The program is typically a binary file without a specific extension. Arguments are passed directly after the program name. ```bash /usr/local/printaccount ACCT=6625378 ``` -------------------------------- ### Create Directory using C$MAKEDIR Source: https://devdocs.io/gnu_cobol/index Creates a new directory. Only the lowest-level directory in the path can be created; parent directories must exist. Sets RETURN-CODE to 0 for success or 128 for failure. ```cobol CALL "C$MAKEDIR" USING dir-path ``` -------------------------------- ### Alphanumeric Data Item Examples Source: https://devdocs.io/gnu_cobol/index Illustrates different ways to define an alphanumeric data item with a specific format. All provided examples are functionally equivalent. ```COBOL AA99999AAA ``` ```COBOL A(2)9(5)A(3) ``` ```COBOL XXXXXXXXXX ``` ```COBOL X(10) ``` -------------------------------- ### Direct Execution Example (Windows) Source: https://devdocs.io/gnu_cobol/index Execute a GnuCOBOL program compiled with the -x switch on Windows systems. The program is typically an .exe file. Arguments are passed directly after the program name. ```batch C:\Users\Me\Documents\Programs\printaccount.exe ACCT=6625378 ``` -------------------------------- ### Java 'Hello World' Program Source: https://devdocs.io/gnu_cobol/index A basic 'Hello World' program in Java. This is a standard entry point for many Java applications. ```java Class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } ``` -------------------------------- ### IF Statement with Implicit Scope (Error Example) Source: https://devdocs.io/gnu_cobol/index An example demonstrating an IF statement where the scope of the ELSE clause is incorrectly assumed due to indentation. This code contains an error. ```COBOL IF Loan-Balance < 10000 MULTIPLY Loan-Balance BY 0.04 GIVING Interest ELSE MULTIPLY Loan-Balance BY 0.045 GIVING Interest DISPLAY "Interest Amount = " Interest ``` -------------------------------- ### Switch-Status Condition Example Source: https://devdocs.io/gnu_cobol/index Associate an external switch name with condition names in the SPECIAL-NAMES paragraph to test the ON/OFF status of the switch. This example checks if SWITCH-1 is ON. ```cobol ENVIRONMENT DIVISION. SPECIAL-NAMES. SWITCH-1 ON STATUS IS Switch-1-Is-ON. ... PROCEDURE DIVISION. ... IF Switch-1-Is-ON DISPLAY "Switch 1 Is On" END-IF ... ``` -------------------------------- ### Display Default Build Information Source: https://devdocs.io/gnu_cobol/index Execute this command to view default environment variable values and other build-specific information for GnuCOBOL. ```bash cobc -i ``` -------------------------------- ### COBOL Data Item Naming Convention Example Source: https://devdocs.io/gnu_cobol/index This example demonstrates a convention for naming subordinate data items using an acronym to indicate their parent 01-level item. This aids in locating related data definitions. ```COBOL 01 WS-File-Status-Message-TXT. 05 FILLER PIC X(13) VALUE 'Status Code: '. 05 WS-FSM-Status-CD PIC 9(2). 05 FILLER PIC X(11) VALUE ', Meaning: '. 05 WS-FSM-Msg-TXT PIC X(25). ``` -------------------------------- ### CPU Benchmark Data Sample Source: https://devdocs.io/gnu_cobol/index This is a sample dataset of CPU benchmark scores, vendor, family, and model, formatted as comma-separated values. Each line represents a single CPU record. ```text 03145,AMD,A10,4600M 05421,AMD,FX,6100 03917,Intel,Core i5,4300U 14291,Intel,Core i7,4960X 05813,AMD,FX,6120 01743,Intel,Core i5,4300Y 02505,AMD,A10,4655M 06194,AMD,FX,6200 04804,Intel,Core i5,4330M 03449,AMD,A10,4657M 06388,AMD,FX,6300 03604,Intel,Core i5,4350U 04251,AMD,A10,5700 07017,AMD,FX,6350 06282,Intel,Core i5,4430 02758,AMD,A10,5745M 06163,AMD,FX,8100 05954,Intel,Core i5,4430S 03332,AMD,A10,5750M 06605,AMD,FX,8120 06517,Intel,Core i5,4440 03253,AMD,A10,5757M 06845,AMD,FX,8140 07061,Intel,Core i5,4570 04798,AMD,A10,5800B 07719,AMD,FX,8150 06474,Intel,Core i5,4570R 04677,AMD,A10,5800K 08131,AMD,FX,8320 06803,Intel,Core i5,4570S 04767,AMD,A10,6700 09067,AMD,FX,8350 02503,Intel,Core i5,4570T 05062,AMD,A10,6800K 09807,AMD,FX,9370 07492,Intel,Core i5,4670 00677,AMD,A4,1200 10479,AMD,FX,9590 07565,Intel,Core i5,4670K 00559,AMD,A4,1250 03076,Intel,Core i3,3110M 06351,Intel,Core i5,4670T 01583,AMD,A4,3300 03301,Intel,Core i3,3120M 03701,Intel,Core i7,3517U 01237,AMD,A4,3300M 03655,Intel,Core i3,3130M 03449,Intel,Core i7,3517UE 01227,AMD,A4,3305M 03820,Intel,Core i3,3210 04588,Intel,Core i7,3520M 01263,AMD,A4,3310MX 02266,Intel,Core i3,3217U 03912,Intel,Core i7,3537U 01193,AMD,A4,3320M 04219,Intel,Core i3,3220 04861,Intel,Core i7,3540M 01343,AMD,A4,3330MX 03724,Intel,Core i3,3220T 04009,Intel,Core i7,3555LE 01625,AMD,A4,3400 04407,Intel,Core i3,3225 06144,Intel,Core i7,3610QE 01768,AMD,A4,3420 02575,Intel,Core i3,3227U 07532,Intel,Core i7,3610QM 01685,AMD,A4,4300M 01885,Intel,Core i3,3229Y 06988,Intel,Core i7,3612QE 01169,AMD,A4,4355M 04259,Intel,Core i3,3240 06907,Intel,Core i7,3612QM 01919,AMD,A4,5000 03793,Intel,Core i3,3240T 05495,Intel,Core i7,3615QE 01973,AMD,A4,5150M 04414,Intel,Core i3,3245 07310,Intel,Core i7,3615QM 02078,AMD,A4,5300 04757,Intel,Core i3,3250 07759,Intel,Core i7,3630QM 01632,AMD,A4,5300B 03443,Intel,Core i3,4000M 07055,Intel,Core i7,3632QM 02305,AMD,A4,6300 02459,Intel,Core i3,4010U 06516,Intel,Core i7,3635QM 01634,AMD,A6,1450 02003,Intel,Core i3,4010Y 04032,Intel,Core i7,3667U 01964,AMD,A6,3400M 04904,Intel,Core i3,4130 04271,Intel,Core i7,3687U 02101,AMD,A6,3410MX 04041,Intel,Core i3,4130T 03479,Intel,Core i7,3689Y 02078,AMD,A6,3420M 05115,Intel,Core i3,4330 08347,Intel,Core i7,3720QM 02277,AMD,A6,3430MX 05117,Intel,Core i3,4340 08512,Intel,Core i7,3740QM 01995,AMD,A6,3500 03807,Intel,Core i5,3210M 09420,Intel,Core i7,3770 02798,AMD,A6,3600 03995,Intel,Core i5,3230M 09578,Intel,Core i7,3770K 02892,AMD,A6,3620 03126,Intel,Core i5,3317U 09074,Intel,Core i7,3770S 03232,AMD,A6,3650 04101,Intel,Core i5,3320M 08280,Intel,Core i7,3770T ``` -------------------------------- ### Generate Program Listing Source: https://devdocs.io/gnu_cobol/index The -t option generates a standard program listing and places it into the specified file. Use --tlines= to specify the number of lines per page. ```bash cobc -t listing.txt myprogram.cob ``` ```bash cobc --tlines=60 -t listing.txt myprogram.cob ``` -------------------------------- ### EVALUATE Statement Example with Conditions Source: https://devdocs.io/gnu_cobol/index Demonstrates the use of the EVALUATE statement with multiple WHEN clauses, utilizing boolean conditions (Digit-Is-Odd, Digit-Is-Prime) and the ALSO keyword for compound conditions. This example shows how to handle different combinations of conditions. ```COBOL IDENTIFICATION DIVISION. PROGRAM-ID. DEMOEVALUATE. DATA DIVISION. WORKING-STORAGE SECTION. 01 Test-Digit PIC 9(1). 88 Digit-Is-Odd VALUE 1, 3, 5, 7, 9. 88 Digit-Is-Prime VALUE 1, 3, 5, 7. PROCEDURE DIVISION. P1. PERFORM UNTIL EXIT DISPLAY "Enter a digit (0 Quits): " WITH NO ADVANCING ACCEPT Test-Digit IF Test-Digit = 0 EXIT PERFORM END-IF EVALUATE Digit-Is-Odd ALSO Digit-Is-Prime WHEN TRUE ALSO FALSE DISPLAY Test-Digit " is ODD" WITH NO ADVANCING WHEN TRUE ALSO TRUE ``` -------------------------------- ### Configure File Handling Source: https://devdocs.io/gnu_cobol/index Use -fmf-files to match Micro Focus format for sequential and relative files. -foptional-file treats all files as OPTIONAL unless explicitly specified otherwise. ```cobol -fmf-files Sequential & Relative files will match Micro Focus format ``` ```cobol -foptional-file treat all files as OPTIONAL * unless NOT OPTIONAL specified ``` -------------------------------- ### GNU Cobol CBL_GC_HOSTED Example Source: https://devdocs.io/gnu_cobol/index Demonstrates accessing C hosted variables (stdin, stdout, stderr, argc, argv, errno) and timezone information using CBL_GC_HOSTED. Requires system support for timezone variables. ```cobol IDENTIFICATION DIVISION. PROGRAM-ID. HOSTED. DATA DIVISION. WORKING-STORAGE SECTION. 01 Argc BINARY-LONG. 01 Argv POINTER. 01 Stdin POINTER. 01 Stdout POINTER. 01 Stderr POINTER. 01 Errno POINTER. 01 Err BINARY-LONG BASED. 01 Domain FLOAT-LONG VALUE 3.0. 01 Tzname POINTER. 01 Tznames POINTER BASED. 05 Tzs POINTER OCCURS 2. 01 Timezone BINARY-LONG. 01 Daylight BINARY-SHORT. *> PROCEDURE DIVISION. call "CBL_GC_HOSTED" using stdin "stdin" display "stdin : " stdin call "feof" using by value stdin display "feof stdin : " return-code call "CBL_GC_HOSTED" using stdout "stdout" display "stdout : " stdout call "fprintf" using by value stdout by content "Hello" & x"0a" call "CBL_GC_HOSTED" using stderr "stderr" display "stderr : " stderr call "fprintf" using by value stderr by content "on err" & x"0a" call "CBL_GC_HOSTED" using argc "argc" display "argc : " argc call "CBL_GC_HOSTED" using argv "argv" display "argv : " argv call "args" using by value argc argv call "CBL_GC_HOSTED" using errno "errno" display "&errno : " errno set address of err to errno display "errno : " err call "acos" using by value domain display "errno after acos(3.0): " err ", EDOM is 33" call "CBL_GC_HOSTED" using argc "arg" display "'arg' lookup : " return-code call "CBL_GC_HOSTED" using null "argc" display "null with argc : " return-code display "argc is still : " argc *> the following only returns zero if the system has HAVE_TIMEZONE set call "CBL_GC_HOSTED" using daylight "daylight " display "'timezone' lookup : " return-code if return-code not = 0 display "system doesn't has timezone" else display "timezone is : " timezone call "CBL_GC_HOSTED" using daylight "daylight " display "'daylight' lookup : " return-code display "daylight is : " daylight set environment "TZ" to "PST8PDT" call static "tzset" returning omitted on exception continue end-call call "CBL_GC_HOSTED" using tzname "tzname" display "'tzname' lookup : " return-code *> tzs(1) will point to z"PST" and tzs(2) to z"PDT" if return-code equal 0 and tzname not equal null then set address of tznames to tzname if tzs(1) not equal null then display "tzs #1 : " tzs(1) end-if if tzs(2) not equal null then display "tzs #2 : " tzs(2) end-if end-if end-if goback. end program hosted. ``` -------------------------------- ### cobcrun Execution Example (Unix/OSX) Source: https://devdocs.io/gnu_cobol/index Execute a GnuCOBOL dynamically-loadable module using cobcrun on Unix-like systems. The program's directory must be in the current PATH or the current directory. ```bash cd /usr/local cobcrun printaccount acct=6625378 ``` -------------------------------- ### AND vs. OR Precedence Example Source: https://devdocs.io/gnu_cobol/index Demonstrates the default precedence where AND is evaluated before OR, and how parentheses can override this default. ```COBOL FALSE AND FALSE OR TRUE AND TRUE ``` ```COBOL (FALSE AND FALSE) OR (TRUE AND TRUE) ``` ```COBOL (FALSE AND (FALSE OR TRUE)) AND TRUE ``` -------------------------------- ### FORTRAN Variable Assignment Source: https://devdocs.io/gnu_cobol/index A simple example of variable assignment and arithmetic operations in FORTRAN. ```fortran EXT = PRICE * IQTY INVTOT = INVTOT + EXT ``` -------------------------------- ### ACCEPT FROM COMMAND-LINE Syntax Source: https://devdocs.io/gnu_cobol/index Use this syntax to retrieve the entire set of arguments entered on the command line that executed the program. Parsing the returned data into meaningful information is the user's responsibility. Be aware that shells may expand arguments with '*' unless they are quoted. ```cobol ACCEPT identifier-1 ~~~~~~ FROM { COMMAND-LINE } ~~~~ { ~~~~~~~~~~~~ } { ARGUMENT-NUMBER } { ~~~~~~~~~~~~~~~ } { ARGUMENT-VALUE } { ~~~~~~~~~~~~~~ } { [ ON EXCEPTION imperative-statement-1 ] } { ~~~~~~~~~ } { [ NOT ON EXCEPTION imperative-statement-2 ] } [ END-ACCEPT ] ~~~ ~~~~~~~~~ ~~~~~~~~~~ ``` -------------------------------- ### Get Mathematical Constant PI Source: https://devdocs.io/gnu_cobol/index Returns the mathematical constant PI. No arguments or parentheses are required. ```COBOL PI ``` -------------------------------- ### Converting Radians to Degrees Source: https://devdocs.io/gnu_cobol/index Example of converting an angle from radians to degrees using the PI intrinsic function. ```COBOL COMPUTE degrees = ( radians * 180 ) / FUNCTION PI ``` -------------------------------- ### cobcrun Execution Example (Windows) Source: https://devdocs.io/gnu_cobol/index Execute a GnuCOBOL dynamically-loadable module using cobcrun on Windows systems. The program's directory must be in the current PATH or the current directory. ```batch cd C:\Users\Me\Documents\Programs cobcrun printaccount.exe acct=6625378 ``` -------------------------------- ### TRUNCATION Rounding Examples Source: https://devdocs.io/gnu_cobol/index Illustrates rounding for TRUNCATION mode. Rounding is to the nearest value with a smaller magnitude. ```COBOL -3.510 => -3 | +3.510 => +3 -3.500 => -3 | +3.500 => +3 -3.499... => -3 | +3.499... => +3 -2.500 => -2 | +2.500 => +2 -2.499... => -2 | +2.499... => +2 ``` -------------------------------- ### Variable-Length Table with DEPENDING ON Source: https://devdocs.io/gnu_cobol/index Example of a variable-length table where the number of accessible entries is determined by an identifier at runtime. ```COBOL 05 SALES OCCURS 4 TIMES PIC 9(7)V99. ``` -------------------------------- ### Create a Directory Source: https://devdocs.io/gnu_cobol/index Use CBL_CREATE_DIR to create a new directory. Only the lowest-level directory in the path can be created; parent directories must already exist. ```cobol CALL "CBL_CREATE_DIR" USING dir-path ~~~~ ~~~~~ ``` -------------------------------- ### Get Module Path Source: https://devdocs.io/gnu_cobol/index Retrieves the full path to the executable version of the GnuCOBOL program. No arguments are needed. ```cobol MODULE-PATH ``` -------------------------------- ### GnuCOBOL 'Hello World' Program Source: https://devdocs.io/gnu_cobol/index A 'Hello World' program written in GnuCOBOL. It demonstrates the basic structure and output statement in COBOL. ```cobol IDENTIFICATION DIVISION. PROGRAM-ID. HelloWorld. PROCEDURE DIVISION. DISPLAY "Hello World!". ``` -------------------------------- ### Get Module ID Source: https://devdocs.io/gnu_cobol/index Retrieves the primary entry-point name (PROGRAM-ID or FUNCTION-ID) of the program. No arguments are needed. ```cobol MODULE-ID ``` -------------------------------- ### C Main Program and GnuCOBOL Subprogram Example Source: https://devdocs.io/gnu_cobol/index This snippet shows a C program calling a GnuCOBOL subprogram. The C program must initialize the GnuCOBOL runtime using `cob_init`. Arguments can be passed by value or reference. ```c #include /* COB RUN-TIME */ #include int main (int argc, char **argv) { int returnCode; char arg1[7] = "Arg1"; char arg2[7] = "Arg2"; unsigned long arg3 = 123456789; printf("Starting mainc...\n"); cob_init (argc, argv); /* COB RUN-TIME */ returnCode = subcob(arg1,arg2,&arg3); printf("Back\n"); printf("Arg1=%s\n",arg1); printf("Arg2=%s\n",arg2); printf("Arg3=%d\n",arg3); printf("Returned value=%d\n",returnCode); return returnCode; } ``` ```cobol IDENTIFICATION DIVISION. PROGRAM-ID. subcob. DATA DIVISION. LINKAGE SECTION. 01 Arg1 PIC X(7). 01 Arg2 PIC X(7). 01 Arg3 USAGE BINARY-LONG. PROCEDURE DIVISION USING BY VALUE Arg1, BY REFERENCE Arg2, BY REFERENCE Arg3. 000-Main. DISPLAY 'Starting cobsub.cbl' DISPLAY 'Arg1=' Arg1 DISPLAY 'Arg2=' Arg2 DISPLAY 'Arg3=' Arg3 MOVE 'X' TO Arg1 (1:1) MOVE 'Y' TO Arg2 (1:1) MOVE 987654321 TO Arg3 MOVE 2 TO RETURN-CODE GOBACK . ``` -------------------------------- ### PROHIBITED Rounding Examples Source: https://devdocs.io/gnu_cobol/index Illustrates the PROHIBITED mode where no rounding occurs. If the value cannot be represented exactly, an exception is raised. ```COBOL -3.510 => Undefined | +3.510 => Undefined -3.500 => Undefined | +3.500 => Undefined -3.499... => Undefined | +3.499... => Undefined -2.500 => Undefined | +2.500 => Undefined -2.499... => Undefined | +2.499... => Undefined ``` -------------------------------- ### cobcrun Help Output Source: https://devdocs.io/gnu_cobol/index Displays the help information for the cobcrun utility, outlining its usage, options, and parameters for executing GnuCOBOL modules. ```text COBOL driver program for GnuCOBOL modules Usage: cobcrun [options] PROGRAM [parameter ...] or: cobcrun options Options: -h, -help display this help and exit -V, -version display cobcrun and runtime version and exit -i, -info display runtime information (build/environment) -c , -config= set runtime configuration from -r, -runtime-conf display current runtime configuration (value and origin for all settings) -M , -module= set entry point module name and/or load path where -M module prepends any directory to the dynamic link loader library search path and any basename to the module preload list (COB_LIBRARY_PATH and/or COB_PRELOAD) ``` -------------------------------- ### Enable Procedure Tracing Source: https://devdocs.io/gnu_cobol/index Use READY TRACE to turn on procedure tracing. Tracing code must be generated using the -ftrace switch. ```cobol READY TRACE ``` -------------------------------- ### CBL_READ_FILE Built-In Subroutine Source: https://devdocs.io/gnu_cobol/index Reads a specified number of bytes from a byte-stream file into a buffer, starting at a given offset. ```APIDOC ## CBL_READ_FILE Built-In Subroutine ### Description This routine reads `nbytes` of data starting at byte number `offset` from the byte-stream file defined by `handle` into `buffer`. ### Syntax ``` CALL "CBL_READ_FILE" USING handle, offset, nbytes, flag, buffer ``` ### Parameters - **handle**: `PIC X(4) USAGE COMP-X`. Must be populated by a prior call to `CBL_OPEN_FILE`. - **offset**: `PIC X(8) USAGE COMP-X`. Defines the starting location in the file (byte 0 is the first byte). - **nbytes**: `PIC X(4) USAGE COMP-X`. Specifies the maximum number of bytes to read. - **flag**: Specifies behavior. Valid values: - `128`: Return the size of the file (in bytes) into the `offset` argument upon completion. Not all environments support this; a value of zero will be returned if unsupported. - `0`: Standard read operation. - **buffer**: Data item to receive the read data. ### Return Code - `0`: Read successful. - `10`: End-of-file condition occurred. - `-1`: Problem with subroutine arguments. ``` -------------------------------- ### Get Module Compilation Time Source: https://devdocs.io/gnu_cobol/index Retrieves the time the GnuCOBOL program was compiled, in hhmmss format. No arguments are needed. ```cobol MODULE-TIME ``` -------------------------------- ### Build Dynamically Loadable Module Source: https://devdocs.io/gnu_cobol/index Use the -m option to build a dynamically loadable module. This is the default behavior if -x is not specified. ```bash cobc -m mymodule.cob ``` -------------------------------- ### Get Module Formatted Date and Time Source: https://devdocs.io/gnu_cobol/index Retrieves the fully-formatted date and time when the program was compiled. No arguments are needed. ```cobol MODULE-FORMATTED-DATE ``` -------------------------------- ### Compile and Run with -O2 Optimization Source: https://devdocs.io/gnu_cobol/index This snippet demonstrates compiling a GnuCOBOL program with the -O2 optimization flag and then running it. It shows the execution times for different USAGE clauses. ```bash cobc -x demomath.cbl -O2;demomath USAGE DISPLAY: 1.68 SECONDS USAGE COMP: 0.60 SECONDS USAGE BINARY: 0.00 SECONDS ``` -------------------------------- ### Get Current Date and Time Source: https://devdocs.io/gnu_cobol/index Returns the current date and time as a 21-character structure. This function takes no arguments. ```COBOL CURRENT-DATE ``` ```COBOL 01 CURRENT-DATE-AND-TIME. 05 CDT-Year PIC 9(4). 05 CDT-Month PIC 9(2). *> 01-12 05 CDT-Day PIC 9(2). *> 01-31 05 CDT-Hour PIC 9(2). *> 00-23 05 CDT-Minutes PIC 9(2). *> 00-59 05 CDT-Seconds PIC 9(2). *> 00-59 05 CDT-Hundredths-Of-Secs PIC 9(2). *> 00-99 05 CDT-GMT-Diff-Hours PIC S9(2) SIGN LEADING SEPARATE. 05 CDT-GMT-Diff-Minutes PIC 9(2). *> 00 or 30 ``` -------------------------------- ### Create and Open a File for Output Source: https://devdocs.io/gnu_cobol/index Use CBL_CREATE_FILE to create a new file and open it for output as a byte-stream file. A file-handle is returned for subsequent write or close operations. ```cobol CALL "CBL_CREATE_FILE" USING file-path, 2, 0, 0, file-handle ~~~~ ~~~~~ ``` -------------------------------- ### TOWARD-LESSER Rounding Examples Source: https://devdocs.io/gnu_cobol/index Demonstrates rounding for TOWARD-LESSER mode. Rounding is toward the nearest value with a smaller algebraic value. ```COBOL -3.510 => -4 | +3.510 => +3 -3.500 => -4 | +3.500 => +3 -3.499... => -4 | +3.499... => +3 -2.500 => -3 | +2.500 => +2 -2.499... => -3 | +2.499... => +2 ``` -------------------------------- ### Configure IBM Compatibility Options Source: https://devdocs.io/gnu_cobol/index Use -fibmcomp to set binary size to 2-4-8 and synchronized clause to 'ok'. Use -fno-ibmcomp to set binary size to 1-8 and synchronized clause to 'ignore'. ```cobol -fibmcomp sets -fbinary-size=2-4-8 -fsynchronized-clause=ok ``` ```cobol -fno-ibmcomp sets -fbinary-size=1--8 -fsynchronized-clause=ignore ``` -------------------------------- ### GnuCOBOL $ Directives - Active Source: https://devdocs.io/gnu_cobol/index These directives are active and function similarly to those starting with '>>'. It is recommended to use standard directives for better portability. ```cobol $DEFINE $DISPLAY ON|OFF $IF $ELIF $ELSE $ELSE-IF $END $SET ``` -------------------------------- ### Sample Program Using All MODULE- Functions Source: https://devdocs.io/gnu_cobol/index A sample GnuCOBOL program demonstrating the usage of various MODULE- intrinsic functions. This program displays information about the module's caller ID, date, formatted date, ID, path, source, and time. ```cobol IDENTIFICATION DIVISION. PROGRAM-ID. DEMOMODULE. ENVIRONMENT DIVISION. CONFIGURATION SECTION. REPOSITORY. FUNCTION ALL INTRINSIC. PROCEDURE DIVISION. 000-Main. DISPLAY "MODULE-CALLER-ID = [" MODULE-CALLER-ID ‘]’ DISPLAY "MODULE-DATE = [" MODULE-DATE ‘]’ DISPLAY "MODULE-FORMATTED-DATE = [" MODULE-FORMATTED-DATE ‘]’ DISPLAY "MODULE-ID = [" MODULE-ID ‘]’ DISPLAY "MODULE-PATH = [" MODULE-PATH ‘]’ DISPLAY "MODULE-SOURCE = [" MODULE-SOURCE ‘]’ DISPLAY "MODULE-TIME = [" MODULE-TIME ‘]’ STOP RUN . ``` -------------------------------- ### Generate Wide Program Listing Source: https://devdocs.io/gnu_cobol/index Use the -T option to generate a wide program listing and place it into the specified file. This provides a detailed listing of the source code. ```bash cobc -T listing.lst myprogram.cob ```