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

IBMi (AS400) fans only ‘ Memories (IBM Coding Forms)

IBMi (AS400) fans only ' SQLCODE values: Dear readers, unleash your suggestions!