IBMi (AS400) fans only - How to call a C runtime function from RPG program.



#IBMiSample
Each IBMi user knows they run the most efficient system. All others just have too much money.


This example comes from https://www.ibm.com/support/pages/coding-rpg-iv-beginners-tutorial
This example calls the C runtime printf() function to print a message to the standard output instead of to the external message queue.




       // This sample comes from
       // https://www.ibm.com/support/pages/coding-rpg-iv-beginners-tutorial
       //
       // This example calls the C runtime printf() function to print a message
       // to the standard output instead of to the external message queue.


       /if defined(*CRTBNDRPG)
         ctl-opt dftactgrp(*no)
                 actgrp(*new);
       /endif
       ctl-opt option(*srcstmt);

       dcl-s num int(10) inz(25);

       print ('This message is much longer than the 52 '
            + 'characters that DSPLY allows. '
            + 'The value of variable "num" is ' + %char(num));
       return;

       dcl-proc print;
          dcl-pi  *n;
             msg varchar(5000) const;
          end-pi;
          dcl-pr printf extproc(*dclcase);
             template pointer value options(*string);
             dummy int(10) value options(*nopass);
          end-pr;
          dcl-c NEWLINE x'15';

          printf(msg + NEWLINE);
       end-proc print;


Compile this program (CRTBNDRPG) and run it:

CALL MyProgram

I appreciate all the comments made on this blog.

Comments

  1. Just FYI, X'25' is the newline character since about 2004 when it was "corrected" from the X'15'. The X'15' still kind of works, mostly.

    ReplyDelete

Post a Comment

Popular posts from this blog

(IBM i fans only) Efficient WRKSPLF with WSF - How to Search string into spooled files, Sort, and Generate PDFs on IBMi

(IBM i fans only) Detecting and Handling Non-Printable Characters in DB2 SQL Using LOCATE() and REPLACE() Functions

(IBM i fans only) How to Sniff User Access