View previous topic :: View next topic
|
Author |
Message |
arivazhagan_k
New User
Joined: 05 Dec 2007 Posts: 57 Location: chennai
|
|
|
|
SOURCE :
Code: |
IDENTIFICATION DIVISION.
PROGRAM-ID. PGM1.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC S9(8).
PROCEDURE DIVISION.
ACCEPT A.
DISPLAY 'A IS:' A.
STOP RUN. |
RUN JCL :
Code: |
//job11### JOB ,'SAMPLE',
//CLASS=S,MSGCLASS=1,REGION=0M,MSGLEVEL=1,NOTIFY=XXXX
//STEP10 EXEC COBXG,
// PROG=PGM1
//STEPLIB DD DSN=LOADLIB,
// DISP=(SHR,KEEP,KEEP)
//SYSPRINT DD SYSOUT=*
//SYSUDUMP DD SYSOUT=*
//SYSIN DD *
ASDFGH
/*
//SYSOUT DD SYSOUT=*
// |
OUTPUT :
I am trying to understand how it is accepting non numeric value into numeric field... can some one help me to understand...
If try to initialize a value in source itself then getting error message
Code: |
"'ASDFGH'" AND "A (NUMERIC INTEGER)" DID NOT FOLLOW THE "MOV DISCARDED. |
|
|
Back to top |
|
|
arivazhagan_k
New User
Joined: 05 Dec 2007 Posts: 57 Location: chennai
|
|
|
|
Try to initialize like below
Code: |
01 A PIC 9(8) VALUE('ASDFGH'). |
|
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8697 Location: Dubuque, Iowa, USA
|
|
|
|
Your variable is USAGE DISPLAY. Such variables allow anything to be stored in them. Where you would get a problem would be attempting to use the non-numeric value for arithmetic (S0C7 abend, anyone?) -- but merely displaying the value or moving it to another USAGE DISPLAY field is not prohibited by COBOL.
A lot of people new to COBOL seem to think that a numeric variable cannot hold anything but digits. This is a false belief. Variables of USAGE COMP (and all the other COMP types) can hold only numeric digits. But USAGE DISPLAY variables can hold anything a PIC X field can. COBOL prevents obvious discrepancies such as MOVE 'ASDFG' TO A, but ACCEPT and MOVE variable to A will both be allowed. |
|
Back to top |
|
|
arivazhagan_k
New User
Joined: 05 Dec 2007 Posts: 57 Location: chennai
|
|
|
|
Thanks for this valuable information.
Quote: |
but ACCEPT and MOVE variable to A will both be allowed. |
As per your statement the following code should not through any error but it is throwing a compilation error.
source:
Code: |
IDENTIFICATION DIVISION.
PROGRAM-ID. MANU.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(8).
01 B PIC A(8) VALUE 'ASDF'.
PROCEDURE DIVISION.
MOVE B TO A.
DISPLAY 'A IS:' A.
DISPLAY 'B IS:' B. |
compilation error:
Code: |
"B (ALPHABETIC)" AND "A (NUMERIC INTEGER)" DID NOT FOLLOW
STATEMENT WAS DISCARDED. |
This code is working fine.
Code: |
IDENTIFICATION DIVISION.
PROGRAM-ID. MANU.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(8).
01 B PIC X(8) VALUE 'ASDF'.
PROCEDURE DIVISION.
MOVE B TO A.
DISPLAY 'A IS:' A.
DISPLAY 'B IS:' B. |
output:
Code: |
----+----1----+----2----+----3-
A IS:ASDF 0
B IS:ASDF
******************************* |
But how 0 is displaying at end... |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8697 Location: Dubuque, Iowa, USA
|
|
|
|
Note in my post I said PIC X. You are using PIC A. See the difference? A means ALPHABETIC, which by its very definition cannot be moved to a numeric variable.
The zero comes about because COBOL ensures that your numeric variable is unsigned. How does it do this? By doing an OR of the last byte of the variable with X'F0', which ensures there is no sign associated with the number. Since you moved 4 bytes to an 8-byte variable, and the move was left justified, the value was actually ASDF followed by 4 spaces (or X'40'). X'40', when ORed to X'F0', becomes X'F0' -- which is a zero. Hence your display shows ASDF, three spaces, then a zero. |
|
Back to top |
|
|
arivazhagan_k
New User
Joined: 05 Dec 2007 Posts: 57 Location: chennai
|
|
|
|
Good explanation. thanks. |
|
Back to top |
|
|
|