[Info-vax] C limitations, was: Re: VMS process communication

Arne Vajhøj arne at vajhoej.dk
Wed Apr 12 10:44:19 EDT 2023


On 4/12/2023 10:41 AM, Arne Vajhøj wrote:
> On 4/7/2023 4:58 PM, Johnny Billquist wrote:
>> On 2023-04-03 14:31, bill wrote:
>>> On 4/2/2023 8:50 PM, Johnny Billquist wrote:
>>>> On 2023-03-29 19:51, bill wrote:
>>>>> Weird?  What about under Primos where chars always have the high order
>>>>> bit turned on?  It was called "mark memory parity" but it never 
>>>>> made any
>>>>> sense to me when I had to work with it.  :-)
>>>>
>>>> I think/suspect that this goes outside the scope of C...
>>>
>>> Of course it does.  But it affects C a lot more than other languages
>>> if for no other reason than programming style.  How many Unix C programs
>>> have you seen where parsing is done by looking at the value of a letter
>>> as opposed to just comparing chars?  Even with Primix porting a Unix
>>> program to a Pr1me was a serious task and often not even possible.
>>
>> I don't understand what you mean. Are you just talking about some 
>> people assuming you have ASCII, and looking at the decimal values, 
>> instead of the character representation. Basically writing 65 instead 
>> of 'A'?
>> If so, that is certainly something I've seen in lots of languages, and 
>> are in fact much more an issue in most other languages that I've used, 
>> since they do not consider a thing like 'A' to be a numeric value. C 
>> doing that really helps in this context.
>>
>> I'd say it's more common to see C code using the characters instead of 
>> their numeric representation just because it is so easy to do in C.
> 
> I believe there are basically two groups of languages:
> 
> A) those where one can write both c=='A' and c==65
> B) those where one can write c=='A' but c==65 gives a compile error and 
> has to be written as ord(c)==65
> 
> I find it more likely that group A will use 65 than group B.

Examples of code:

$ type isint.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static int isint(const char *s)
{
     int res, i;
     if((s == NULL) || (strlen(s) == 0))
     {
         res = FALSE;
     }
     else
     {
         res = TRUE;
         for(i = 0; i < strlen(s); i++)
         {
             if((s[i] < '0') || ('9' < s[i]))
             {
                 if((i != 0) || (s[i] != '-'))
                 {
                     res = FALSE;
                 }
             }
         }
     }
     return res;
}

static int isintx(const char *s)
{
     int res, i;
     if((s == NULL) || (strlen(s) == 0))
     {
         res = FALSE;
     }
     else
     {
         res = TRUE;
         for(i = 0; i < strlen(s); i++)
         {
             if((s[i] < 48) || (57 < s[i]))
             {
                 if((i != 0) || (s[i] != 45))
                 {
                     res = FALSE;
                 }
             }
         }
     }
     return res;
}

static void test(const char *s)
{
     printf("\"%s\" : %d\n", s, isint(s));
     printf("\"%s\" : %d\n", s, isintx(s));
}

int main(int argc, char *argv[])
{
     test("123");
     test("-123");
     test("ABC");
     test("123X");
     test("");
     return 0;
}
$ cc isint
$ link isint
$ run isint
"123" : 1
"123" : 1
"-123" : 1
"-123" : 1
"ABC" : 0
"ABC" : 0
"123X" : 0
"123X" : 0
"" : 0
"" : 0
$ type isint.pas
program main(input,output);

type
    pstr = varying [255] of char;

function isint(s : pstr) : boolean;

var
    i : integer;
    res : boolean;

begin
    if length(s) = 0 then begin
       res := false;
    end else begin
       res := true;
       for i:= 1 to length(s) do begin
          if (s[i] < '0') or ('9' < s[i]) then begin
             if (i <> 1) or (s[i] <> '-') then begin
                res := false;
             end;
          end;
       end;
    end;
    isint := res;
end;

function isintx(s : pstr) : boolean;

var
    i : integer;
    res : boolean;

begin
    if length(s) = 0 then begin
       res := false;
    end else begin
       res := true;
       for i:= 1 to length(s) do begin
          if (ord(s[i]) < 48) or (57 < ord(s[i])) then begin
             if (i <> 1) or (ord(s[i]) <> 45) then begin
                res := false;
             end;
          end;
       end;
    end;
    isintx := res;
end;

procedure test(s : pstr);

begin
    writeln('"', s, '" : ', isint(s):1);
    writeln('"', s, '" : ', isintx(s):1);
end;

begin
    test('123');
    test('-123');
    test('ABC');
    test('123X');
    test('');
end.
$ pas isint
$ link isint
$ run isint
"123" : T
"123" : T
"-123" : T
"-123" : T
"ABC" : F
"ABC" : F
"123X" : F
"123X" : F
"" : F
"" : F
$ type isint.for
       program main
       call test('123')
       call test('-123')
       call test('ABC')
       call test('123X')
       call test('')
       end
c
       subroutine test(s)
       character*(*) s
       logical*4 isint
       logical*4 isintx
       write(6,'(1x,1h",a,1h",3h : ,l1)') s,isint(s)
       write(6,'(1x,1h",a,1h",3h : ,l1)') s,isintx(s)
       return
       end
c
       logical*4 function isint(s)
       character*(*) s
       integer*4 i
       logical*4 res
       if(len(s).eq.0) then
         res = .false.
       else
         res = .true.
         do 100 i = 1,len(s)
           if((s(i:i).lt.'0').or.('9'.lt.s(i:i))) then
             if((i.ne.1).or.(s(i:i).ne.'-')) then
               res = .false.
             end if
           end if
100     continue
       end if
       isint = res
       return
       end
c
       logical*4 function isintx(s)
       character*(*) s
       integer*4 i
       logical*4 res
       if(len(s).eq.0) then
         res = .false.
       else
         res = .true.
         do 100 i = 1,len(s)
           if((ichar(s(i:i)).lt.48).or.(57.lt.ichar(s(i:i)))) then
             if((i.ne.1).or.(ichar(s(i:i)).ne.45)) then
               res = .false.
             end if
           end if
100     continue
       end if
       isintx = res
       return
       end
$ for isint
$ link isint
$ run isint
"123" : T
"123" : T
"-123" : T
"-123" : T
"ABC" : F
"ABC" : F
"123X" : F
"123X" : F
"" : F
"" : F
$ type isint.bas
program main

external sub test(string)

call test("123")
call test("-123")
call test("ABC")
call test("123X")
call test("")

end program
!
sub test(string s)

external integer function isint(string)
external integer function isintx(string)

print using '"' + s + '"' + " : ##", isint(s)
print using '"' + s + '"' + " : ##", isintx(s)

end sub
!
function integer isint(string s)

declare integer i, res

if len(s) = 0 then
     res = 0
else
     res = -1
     for i = 1 to len(s)
         if (mid$(s, i, 1) < "1") or ("9" < mid$(s, i, 1)) then
             if (i <> 1) or (mid$(s, i, 1) <> "-") then
                 res = 0
             end if
         end if
     next i
end if
isint = res

end function
!
function integer isintx(string s)

declare integer i, res

if len(s) = 0 then
     res = 0
else
     res = -1
     for i = 1 to len(s)
         if (asc(mid$(s, i, 1)) < 48) or (57 < asc(mid$(s, i, 1))) then
             if (i <> 1) or (asc(mid$(s, i, 1)) <> 45) then
                 res = 0
             end if
         end if
     next i
end if
isintx = res

end function
$ bas isint
$ link isint
$ run isint
"123" : -1
"123" : -1
"-123" : -1
"-123" : -1
"ABC" :  0
"ABC" :  0
"123X" :  0
"123X" :  0
"" :  0
"" :  0
$ type IsInt.java
public class IsInt {
     private static boolean isInt(String s) {
         boolean res;
         if((s == null) || (s.length() == 0)) {
             res = false;
         } else {
             res = true;
             for(int i = 0; i < s.length(); i++) {
                 if((s.charAt(i) < '0') || ('9' < s.charAt(i))) {
                     if((i != 0) || (s.charAt(i) != '-')) {
                         res = false;
                     }
                 }
             }
         }
         return res;
     }
     private static boolean isIntX(String s) {
         boolean res;
         if((s == null) || (s.length() == 0)) {
             res = false;
         } else {
             res = true;
             for(int i = 0; i < s.length(); i++) {
                 if((s.charAt(i) < 48) || (57 < s.charAt(i))) {
                     if((i != 0) || (s.charAt(i) != 45)) {
                         res = false;
                     }
                 }
             }
         }
         return res;
     }
     private static void test(String s) {
         System.out.printf("\"%s\" : %b\n", s, isInt(s));
         System.out.printf("\"%s\" : %b\n", s, isIntX(s));
     }
     public static void main(String[] args) throws Exception {
         test("123");
         test("-123");
         test("ABC");
         test("123X");
         test("");
     }
}
$ javac IsInt.java
$ java "IsInt"
"123" : true
"123" : true
"-123" : true
"-123" : true
"ABC" : false
"ABC" : false
"123X" : false
"123X" : false
"" : false
"" : false
$ type isint.py
def isint(s):
     if len(s) == 0:
         res = False
     else:
         res = True
         for i in range(len(s)):
             if (s[i] < '0') or ('9' < s[i]):
                 if (i != 0) or (s[i] != '-'):
                     res = False
     return res

def isintx(s):
     if len(s) == 0:
         res = False
     else:
         res = True
         for i in range(len(s)):
             if (ord(s[i]) < 48) or (57 < ord(s[i])):
                 if (i != 0) or (ord(s[i]) != 45):
                     res = False
     return res

def test(s):
     print('"%s" : %r' % (s, isint(s)))
     print('"%s" : %r' % (s, isintx(s)))

test('123')
test('-123')
test('ABC')
test('123X')
test('')
$ python isint.py
"123" : True
"123" : True
"-123" : True
"-123" : True
"ABC" : False
"ABC" : False
"123X" : False
"123X" : False
"" : False
"" : False

Arne





More information about the Info-vax mailing list