Pascal - C (2)

J

jacob navia

Barry said:
Ruud said:
Hallo allemaal,


During the conversion of my program from Pascal to C, I was more or
less able to find the C equivalent of most Pascal functions so far.
Only four gave me some real trouble. I solved them but it could be I
overlooked something.

1) In Pascal you can declare functions inside a function. AFAIK this
is not possible with C. Or am I wrong?

2) In Pascal there exists the "in" function. Example:

if (c in ['A'..'F', '0'..'9']) then { c is hexadecimal }

This can be translated like:

if ( ((c >= 'A') && (c <= 'Z'))
|| ((c >= '0') && (c <= '9'))) .... // c is hexadecimal

I just wonder if there is a more simpler solution.
That one is simple enough

Except for the fact that it doesn't work on an EBCDIC system.

yes. A better solution is ishexdigit()...
 
G

George

ROTFL! If you are doing a shedload of string manipulation, C is a far
better choice than Perl. That is, if you want it to finish some time.

I remember what Leroy said about pascal. Now that Leroy is a big and rich
star, his criticism remains.

I think the dealbreaker is whether one plays fast and loose with data.

That's been popular. There has, however, been the anti-phenomenon of data
mining, which plays fast and loose with data until the *right one* comes
up.
--
George

Freedom itself was attacked this morning by a faceless coward, and freedom
will be defended.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
F

Flash Gordon

jacob navia wrote, On 02/11/08 09:56:
Barry said:
Ruud wrote:
Hallo allemaal,


During the conversion of my program from Pascal to C, I was more or
less able to find the C equivalent of most Pascal functions so far.
Only four gave me some real trouble. I solved them but it could be I
overlooked something.

1) In Pascal you can declare functions inside a function. AFAIK this
is not possible with C. Or am I wrong?

2) In Pascal there exists the "in" function. Example:

if (c in ['A'..'F', '0'..'9']) then { c is hexadecimal }

This can be translated like:

if ( ((c >= 'A') && (c <= 'Z'))
|| ((c >= '0') && (c <= '9'))) .... // c is hexadecimal

I just wonder if there is a more simpler solution.

That one is simple enough

Except for the fact that it doesn't work on an EBCDIC system.

yes. A better solution is ishexdigit()...

You means isxdigit(). This, in my opinion, is simpler than the Pascal
solution.

The OP should note that whilst '0' to '9' are guaranteed to be in
sequence (allowing "c - '0'" to work as expected) there is no such
guarantee with letters in C. Since I'm guessing there will be a
conversion from character to number for a fully portable solution strchr
could well be useful.
static const char *xdigits = "0123456789ABCDEF";
char *pos = strchr("0123456789ABCDEF",toupper((unsigned char)c));
if (pos) { /* character found so was a hex digit */
digit = pos - xdigits;
}
else { /* not a hex digit */
}

In my opinion, for characters strchr is very close to "in" in Pascal.
 
R

Ruud

Hallo allemaal,


Many thanks for the massive response!


jacob said:
If you do not know enough C please do not use this group.

Then please be a good man and tell me what level I should have before
I can attend this group?

Ruud, go ahead and ask questions.

Thank you very much for your support, Chuck!

A good book, such as K&R II, would be helpful.

The book isn't the problem, see next.

Why not at least tell him about:

if(isupper(c) || isdigit(c))

and

if(strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", c) != NULL)

The problem is not knowing all those available functions. And knowing
another language is a disavantage as well: instead of reading the book
line by line, one tends to look just at "how is this done in C".
And even reading the book line by line isn't a guarantee for success:
I justed searched the book "C in 21 days" for 'isdigit' and only found
one source file using this function. OTOH, this source file also
mentioned the function 'isspace' and this function does exactly what
one of my own made function does; detecting white space.


Not mentioned at all "C in 21 days" :( Because 'printf' was well
explained, I never used the help function of Borland C to give it a
better look. I wish I had because I just did: I learned nothing new
about 'printf' but the page also mentioned 'sprintf' and many more
other functions.

strncat(Str1, &Str3[5], 1);

Here I have no excuse, it is mentioned very clearly in the book.


--
___
/ __|__
/ / |_/ Groetjes, Ruud Baltissen
\ \__|_\
\___| http://Ruud.C64.org
 
J

jacob navia

Ruud said:
Hallo allemaal,


Many thanks for the massive response!




Then please be a good man and tell me what level I should have before
I can attend this group?

As you can see from my quotes, I do not told YOU that but to Ian
Collins. Please try to understand how quoting works.
 
H

Harald van Dijk

Ruud said:
2) In Pascal there exists the "in" function. Example:

if (c in ['A'..'F', '0'..'9']) then { c is hexadecimal }

This can be translated like:

if ( ((c >= 'A') && (c <= 'Z'))
|| ((c >= '0') && (c <= '9'))) .... // c is hexadecimal

I just wonder if there is a more simpler solution.

That one is simple enough

Except for the fact that it doesn't work on an EBCDIC system.

It will (after fixing 'Z' so that it reads 'F') work on ASCII end EBCDIC
systems. In theory, there could be other systems where it will fail. I
doubt there are any such systems in practise, though.
 
E

Eric Sosman

Ruud said:
Hallo allemaal,


During the conversion of my program from Pascal to C, I was more or
less able to find the C equivalent of most Pascal functions so far.
Only four gave me some real trouble. I solved them but it could be I
overlooked something.

1) In Pascal you can declare functions inside a function. AFAIK this
is not possible with C. Or am I wrong?

Inside a C function you can *declare* another function
but you cannot *define* one.
2) In Pascal there exists the "in" function. Example:

if (c in ['A'..'F', '0'..'9']) then { c is hexadecimal }

This can be translated like:

if ( ((c >= 'A') && (c <= 'Z'))
|| ((c >= '0') && (c <= '9'))) .... // c is hexadecimal

I just wonder if there is a more simpler solution.

There are no built-in "set membership" facilities in C.
It's assumed that different kinds of sets call for different
kinds of representations -- bit mask, array of elements, array
of ranges, whatever -- and that you will code according to the
representation chosen rather than to the set abstraction.

For classifying characters, though, the Standard library
offers some functions you may find useful. In particular, the
<ctype.h> header declares two functions you can combine to make
the test shown in your example:

#include <ctype.h>
...
int ch = (unsigned char)(...);
if (isxdigit(ch) && !islower(ch)) ...
3) In Pascal I can "add" lines:

Line1 = 'File size:' + sSize + ' bytes.';

My solution:

strcpy(Line1, "File size:");
strcat(Line1, sSize);
strcat(Line1, " bytes.);

Again, I just wonder if there is a more simpler solution.

That works, assuming sSize is a string and Line1 is a
char[] array of sufficient size. An alternative that can be
convenient (especially if some of the data are not already
in string form) is to use sprintf().
4) In Pascal I can "add" just one character of another string:

Str1 = Str2 + Str3[5];

Unfortunately strcat(Str1, Str3[5]); doesn't work, I get an error
message. My solution:

Str4[0] = Str3[5];
Str4[1] = 0;
strcpy(Str1, Str2};
strcat(Str1, Str4};

It works but in this case I'm certainly not happy with the solution.
Is there a better way?

"Better" is a slippery word. There are certainly "other"
ways, such as

strcpy(Str1, Str2);
Str1[ strlen(Str2) ] = Str3[5];
Str1[ strlen(Str2) ] = '\0';

or

sprintf (Str1, "%s%c", Str2, Str3[5]);
 
F

Flash Gordon

Ruud wrote, On 02/11/08 12:11:
Hallo allemaal,


The book isn't the problem, see next.

Ah, but it could be! You would be well advised to get K&R2.


I justed searched the book "C in 21 days" for 'isdigit' and only found

Not mentioned at all "C in 21 days" :( Because 'printf' was well

<snip>

"C in 21 days" is *a* book, but what makes you think it is a *good*
book? K&R2 is an excellent (but not perfect) book and there are a few
others that generally get recommended here, but the book you have is not
one of them.
 
B

Barry Schwarz

Ruud wrote:
2) In Pascal there exists the "in" function. Example:

if (c in ['A'..'F', '0'..'9']) then { c is hexadecimal }

This can be translated like:

if ( ((c >= 'A') && (c <= 'Z'))
|| ((c >= '0') && (c <= '9'))) .... // c is hexadecimal

I just wonder if there is a more simpler solution.

That one is simple enough

Except for the fact that it doesn't work on an EBCDIC system.

It will (after fixing 'Z' so that it reads 'F') work on ASCII end EBCDIC
systems. In theory, there could be other systems where it will fail. I
doubt there are any such systems in practise, though.

On EBCDIC systems, a-f are also valid hex digits. The same is true on
Solaris systems. Even in standard C, the conversion specification "x"
produces a-f while"X" produces A-F. Surely each of the characters
produced either way is a valid hex character.
 
K

Keith Thompson

jacob navia said:
As you can see from my quotes, I do not told YOU that but to Ian
Collins. Please try to understand how quoting works.

I think he understood that. If your remark was intended only for Ian
Collins, you could have sent him e-mail. You seemed to be mandating
some minimum level of knowledge as a requirement for posting here;
surely such a rule would not apply only to Ian Collins. Ruud just
wanted to you clarify the rules.
 
K

Keith Thompson

Barry Schwarz said:
Ruud wrote:
2) In Pascal there exists the "in" function. Example:

if (c in ['A'..'F', '0'..'9']) then { c is hexadecimal }

This can be translated like:

if ( ((c >= 'A') && (c <= 'Z'))
|| ((c >= '0') && (c <= '9'))) .... // c is hexadecimal

I just wonder if there is a more simpler solution.

That one is simple enough

Except for the fact that it doesn't work on an EBCDIC system.

It will (after fixing 'Z' so that it reads 'F') work on ASCII end EBCDIC
systems. In theory, there could be other systems where it will fail. I
doubt there are any such systems in practise, though.

On EBCDIC systems, a-f are also valid hex digits. The same is true on
Solaris systems. Even in standard C, the conversion specification "x"
produces a-f while"X" produces A-F. Surely each of the characters
produced either way is a valid hex character.

Maybe the OP has some specific reason to accept only uppercase
letters. (Or maybe he just forgot about lowercase letters.)
 
D

dj3vande

[Copying a single character into a string]
"Better" is a slippery word. There are certainly "other"
ways, such as

strcpy(Str1, Str2);
Str1[ strlen(Str2) ] = Str3[5];
Str1[ strlen(Str2) ] = '\0';

That last line looks wrong to me; I think you want to add 1 to
strlen(Str2) before you use it as an index into Str1.

If I were writing that code, I would probably use something like this
instead:
strcpy(Str1,Str2);
len=strlen(Str1);
Str1[len++]=Str3[5];
Str1[len++]='\0';

This is also a case where strncat's (generally confusing)
interpretation of its count argument turns out to be useful:
strcpy(Str1,Str2);
strncat(Str1,Str3+5,1);
(But be careful using this one in code that has to be maintained.)


dave

--
Dave Vandervies dj3vande at eskimo dot com
You are determined to martyr yourself on a nonexistent altar
An altar could be arranged.
--Mark McIntyre and Richard Heathfield in comp.lang.c
 
L

Lew Pitcher

Hallo allemaal, [snip]
2) In Pascal there exists the "in" function. Example:

if (c in ['A'..'F', '0'..'9']) then { c is hexadecimal }

This can be translated like:

if ( ((c >= 'A') && (c <= 'Z'))
|| ((c >= '0') && (c <= '9'))) .... // c is hexadecimal

Actually, no it can't.

First off, your Pascal original only validates the
characters 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5',
'6', '7', '8', and '9', while your C "translation" (if it did what you
intended) would validate all the uppercase characters from 'A' to 'Z'
(exceeding your original requirement by 20 character values) along with the
digits '0' through '9'.

*But*, your C "translation" of the Pascal code is incorrect. In C, the
only "target characterset" characters required to be in ascending,
sequential order are the values from '0' to '9'. The target characterset
is /not/ obliged to support ascending sequential 'A' to 'Z', and indeed,
some target charactersets (notably all the EBCDIC variants) insert
additional characters /between/ various alphabetics. On an EBCDIC-US
machine, your code would accept as alphabetic about a dozen unprintable
characters, along with '}' and '\', between 'A' and 'Z' inclusive.

[snip]

HTH
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
E

Eric Sosman

[Copying a single character into a string]
"Better" is a slippery word. There are certainly "other"
ways, such as

strcpy(Str1, Str2);
Str1[ strlen(Str2) ] = Str3[5];
Str1[ strlen(Str2) ] = '\0';

That last line looks wrong to me; I think you want to add 1 to
strlen(Str2) before you use it as an index into Str1.

Right you are. Thanks.
If I were writing that code, I would probably use something like this
instead:
strcpy(Str1,Str2);
len=strlen(Str1);
Str1[len++]=Str3[5];
Str1[len++]='\0';

This is also a case where strncat's (generally confusing)
interpretation of its count argument turns out to be useful:
strcpy(Str1,Str2);
strncat(Str1,Str3+5,1);
(But be careful using this one in code that has to be maintained.)

... and now you need to tack on the terminating '\0'
that strncat() didn't give you. (We're both obviously
suffering from the effects of a 25-hour day, and besides:
Turnabout is fair play.)
 
C

CBFalconer

jacob said:
As you can see from my quotes, I do not told YOU that but to
Ian Collins. Please try to understand how quoting works.

Jacob (and others), Usenet is NOT for private communications. Any
such should be done via email, if possible. All Usenet messages
are totally public, and addressed to the general readership. They
are delivered to the readership, barring plonking etc.

You have been told this before, but you persist in the same error.

The following is primarily for Ruuds benefit, but all are invited
to examine the referances. The C99 items (n869_txt.bz2 is bzip2
compressed) describe the standard, and all functions in the
standard library. The dinkumware reference is also an excellent
introduction the the standard library.

Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://c-faq.com/> (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf> (C99)
<http://cbfalconer.home.att.net/download/n869_txt.bz2> (pre-C99)
<http://www.dinkumware.com/c99.aspx> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>
 
J

jacob navia

CBFalconer said:
Jacob (and others), Usenet is NOT for private communications.

Hey you, can't you read? My answer was public.

This is a typical facloner post.

he doesn't knopw wnything of what's going on, and goes around
by "keywords". He sees "jacobn" then, of course it must be wrong!


Any
such should be done via email, if possible. All Usenet messages
are totally public, and addressed to the general readership. They
are delivered to the readership, barring plonking etc.

You have been told this before, but you persist in the same error.

Yes falconer. You have been told this but you persist:
Read the messages and the context before spewing nonsense.


1) The OP asked a question
2) Ian collins said it wasn't possible to do that in C and that C
wasn't appropiate language for string processing.
I answered that post with a refultal and an example of how the OP
question could be answered. My answer was to Ian Collin's reply.

Look at the attributions now and follow the posts. It is not that
difficult.

Then, the OP misunderstood my quoting and thought I answered to him

Then you saw that wrong reply and without looking you take your
"professor chuck" hat and start spewing nonsense.
 
D

dj3vande

(e-mail address removed) wrote:
[Copying a single character into a string]
This is also a case where strncat's (generally confusing)
interpretation of its count argument turns out to be useful:
strcpy(Str1,Str2);
strncat(Str1,Str3+5,1);
(But be careful using this one in code that has to be maintained.)

... and now you need to tack on the terminating '\0'
that strncat() didn't give you.

It's strncpy that doesn't give a terminating '\0'; strncat copies at
most count characters from the source string to the end of the dest
string and then adds a '\0'. (I *did* say that it was confusing. I
had to check the man page to see which strncat was. I just checked
N1124, and it agrees with the man page (7.21.3.2).)


dave
 
K

Keith Thompson

Lew Pitcher said:
On November 1, 2008 17:43, in comp.lang.c, Ruud ([email protected])
wrote: [...]
2) In Pascal there exists the "in" function. Example:

if (c in ['A'..'F', '0'..'9']) then { c is hexadecimal }

This can be translated like:

if ( ((c >= 'A') && (c <= 'Z'))
|| ((c >= '0') && (c <= '9'))) .... // c is hexadecimal

Actually, no it can't.
[...]

*But*, your C "translation" of the Pascal code is incorrect. In C, the
only "target characterset" characters required to be in ascending,
sequential order are the values from '0' to '9'. The target characterset
is /not/ obliged to support ascending sequential 'A' to 'Z', and indeed,
some target charactersets (notably all the EBCDIC variants) insert
additional characters /between/ various alphabetics. On an EBCDIC-US
machine, your code would accept as alphabetic about a dozen unprintable
characters, along with '}' and '\', between 'A' and 'Z' inclusive.

You're assuming that Pascal does require the letters 'A'-'Z' to have
sequential codes. I don't know whether it does or not, but my guess
is that it doesn't, any more than C does. I'm sure there have been
Pascal implementations for EBCDIC-based systems.
 
E

Eric Sosman

(e-mail address removed) wrote:
[Copying a single character into a string]
This is also a case where strncat's (generally confusing)
interpretation of its count argument turns out to be useful:
strcpy(Str1,Str2);
strncat(Str1,Str3+5,1);
(But be careful using this one in code that has to be maintained.)
... and now you need to tack on the terminating '\0'
that strncat() didn't give you.

It's strncpy that doesn't give a terminating '\0'; strncat copies at
most count characters from the source string to the end of the dest
string and then adds a '\0'. (I *did* say that it was confusing. I
had to check the man page to see which strncat was. I just checked
N1124, and it agrees with the man page (7.21.3.2).)

(Sigh.) Not my day, is it? I must have forgotten not
to take my stupid pills ...

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Pascal -> C 16
Beginner at c 0
Help with C negative indexes 2
C++ vs Pascal 38
Getting an exception.., 10
Is there a Lazarus equivalent for C++ available? 4
Using multiple files in (Borland) C 13
strcat and three strings 1

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
ScottChare

Latest Threads

Top