COBOL call printf system library function

Inside cobol libc function such as printf can be called directly


Print an integer

To print an integer in %d, or %x

           CALL "printf" USING "CONST INT=[%d, 0x%04x]%c"&x"00"
                         BY VALUE 1234
                         BY VALUE 1234
                         BY VALUE 10.


Print a string

To print a string

           CALL "printf" USING "CONST STR=[%s]%c"&x"00"
                         BY REFERENCE "ABCD"
                         BY VALUE 10.



Detail Sample

      $set sourceformat(free)
       IDENTIFICATION DIVISION.
       PROGRAM-ID.    SHENHUI.
       
       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       SOURCE-COMPUTER. RM-COBOL.
       OBJECT-COMPUTER. RM-COBOL.
       
       DATA DIVISION.
       FILE SECTION.
       WORKING-STORAGE SECTION.
       01 VAR-INT PIC S9(4) COMP.
       01 VAR-STR PIC X(4).

       PROCEDURE DIVISION.
       
       MAIN-LOGIC SECTION.
       BEGIN.
           
           MOVE 1234 TO VAR-INT.
           MOVE "ABCD" TO VAR-STR.

*     * PRINT INT CONST
           CALL "printf" USING "CONST INT=[%d, 0x%04x]%c"&x"00"
                         BY VALUE 1234
                         BY VALUE 1234
                         BY VALUE 10.
                           
*     * PRINT INT VAR 
           CALL "printf" USING "VAR   INT=[%d, 0x%04x]%c"&x"00"
                         BY VALUE VAR-INT
                         BY VALUE VAR-INT
                         BY VALUE 10.
*     * PRINT STR CONST
           CALL "printf" USING "CONST STR=[%s]%c"&x"00"
                         BY REFERENCE "ABCD"
                         BY VALUE 10.
            
*     * PRINT STR VAR 
           CALL "printf" USING "VAR   STR=[%s]%c"&x"00"
                         BY REFERENCE VAR-STR
                         BY VALUE 10.

           STOP RUN.
       MAIN-LOGIC-EXIT.
           EXIT.



Execution Result

CONST INT=[1234, 0x04d2]
VAR   INT=[1234, 0x04d2]
CONST STR=[ABCD]
VAR   STR=[ABCD]



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值