Pascal - C (2)

R

Ruud

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.

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.

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?

Many thanks for any comment!


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

Ian Collins

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?
No, C does not have nested functions, although some compilers support
them as extensions.
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.
Sorry, no. One alternative is to use a regular expression library if
you have a lot of these.
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.
Not in C.
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?
Don't use C. C does not have string objects, of arrays of char and
library functions to manipulate then.

If you are doing a lot of string manipulation, C might not be your best
choice. Scripting language like Perl are designed for string processing
and might be a better option.
 
J

jacob navia

Ian said:
Not in C.

That is nonsense

sprintf(Line1,"File size: %s bytes",sSize);

And you can save yourself transforming Size into a string with

sprintf(Line1,"File size: %d bytes",Size);
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?
Don't use C. C does not have string objects, of arrays of char and
library functions to manipulate then.

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

The above can be done with

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

If you are doing a lot of string manipulation, C might not be your best
choice. Scripting language like Perl are designed for string processing
and might be a better option.

C has problems with strings but it is usable. Other languages have other
(bigger) problems
 
J

jacob navia

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
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.

Yes:
sprintf(Line1,"File size: %s bytes",sSize);

And you can save yourself transforming Size into a string with

sprintf(Line1,"File size: %d bytes",Size);
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?

Yes.
The above can be done with

sprintf(str1,"%s%c",Str2,Str3[5]);
 
A

Antoninus Twink

That is nonsense

sprintf(Line1,"File size: %s bytes",sSize);

And you can save yourself transforming Size into a string with

sprintf(Line1,"File size: %d bytes",Size);

Yes. In fact, many implementations also provide an asprintf() function
in their standard library, which allocates memory for Line1 with malloc,
saving you the trouble of working out the size of the buffer needed and
eliminating possible overflows if you miscalculate.
 
J

jacob navia

Antoninus said:
Yes. In fact, many implementations also provide an asprintf() function
in their standard library, which allocates memory for Line1 with malloc,
saving you the trouble of working out the size of the buffer needed and
eliminating possible overflows if you miscalculate.

Yes, that would be an even better alternative.

I answered so quickly because I was astonished that somebody could answer

"Not in C"

to such elemntary question!
 
I

Ian Collins

jacob said:
If you do not know enough C please do not use this group.
Why do you have to insult everyone?
C has problems with strings but it is usable. Other languages have other
(bigger) problems
So what part of my statement do you disagree with? A pair of pliers has
problems but is usable for undoing bolts, would you use them if you had
a spanner that fitted?

Your C tunnel vision appears to have blinded you the the existence of
other languages.
 
J

jacob navia

Ian said:
Why do you have to insult everyone?

I am not insulting you. But failing to point
to sprintf as an obvious solution for that problem seems (to me)
a serious problem with the basics of the language.

I do not see why stating "you do not know enough C"
is an insult!

Besides I would have never said that if you wouldn't have
started with that arrogant

"Don't use C"

stuff.
So what part of my statement do you disagree with?

"Don't use C"
 
N

Nate Eldredge

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.

In this case, yes. There are macros in <ctype.h> for testing whether a
character is a member of various classes. This has the added advantage
of being more portable, in case you should find yourself using a
character set where 'A','B','C','D','E','F' don't occur contiguously.
So a better way to write the above might be

#include <ctype.h>
/* ... */
if (isxdigit(c)) {
/* c is hexadecimal */
}

There are others, such as isalpha(), isdigit(), isspace(), isupper(),
etc. See your compiler's documentation or the C standard for a complete
list.

As a side note, some compilers (such as gcc) support an extension that
would let you do

switch (c) {
case 'A' ... 'F':
case '0' ... '9':
/* c is hexadecimal */
}

but that suffers the same character-set dependency as what you wrote
above, and is non-standard, and isn't really much simpler to read or write.
 
J

jacob navia

Ian said:
But why would you use C for string manipulation when there better
alternatives?

For many reasons:

1) C is a simple language easily available
2) Languages like perl/python have better string manipulation
primitives but have other problems like performance,
and availability.
3) If you use the pcre library (distributed with lcc-win) you have
all the power of perl in C without the problems associated
with perl.
4) Extensions to C like those proposed by lcc-win make the
problems with raw C strings disappear.

jacob
 
S

S M Ryan

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?

Not in standard C. Some compilers have an extension to allow this.
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.

if (isxdigit(c)) ...

There are various ways to represent sets as bit arrays in C. None are officially
sanctified as a set type.
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.

The standard C library <string.h> is not really intended to deal with
dynamickally resized strings. There are numerous libraries and implementation
that do all this for you. For example, Tcl has its Tcl_DString. Other solutions
are available.
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?

As above. You can layer on top of the standard library.
 
I

Ian Collins

jacob said:
For many reasons:

1) C is a simple language easily available
2) Languages like perl/python have better string manipulation
primitives but have other problems like performance,
and availability.

I'll concede performance, although scripting language string
manipulation is well optimised. Availability is debatable, requirements
for lots of string processing tend to be on platforms where other
languages are available.
3) If you use the pcre library (distributed with lcc-win) you have
all the power of perl in C without the problems associated
with perl.
4) Extensions to C like those proposed by lcc-win make the
problems with raw C strings disappear.
lcc-win is way less portable than most, if not all, scripting languages.
 
J

jacob navia

Ian said:
I'll concede performance, although scripting language string
manipulation is well optimised. Availability is debatable, requirements
for lots of string processing tend to be on platforms where other
languages are available.

Well the language choice is maybe not only for string manipulation.
Other reasons are probably more important, we do not know what
application the OP has. Note that he is translating from Pascal to
C. Pascal is even worst for string manipulation than C since
libraries like PCRE regular expressions are not easily available,
and regular expressions at all are not easy to find in Pascal.

C + the PCRE library is a very good choice for string manipulation,
combining performance and power.
lcc-win is way less portable than most, if not all, scripting languages.

Maybe. But pcre is highly portable and can be used anywhere a
C compiler exists.
 
C

CBFalconer

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

Of course, if you are as knowledgeable as Jacob, and similarly
adept at avoiding off-topic messages (sarcasm) you can make such
foolish requests.

Ruud, go ahead and ask questions. It is one way to learn. A good
book, such as K&R II, would be helpful.
 
C

CBFalconer

jacob said:
Antoninus Twink wrote:
.... snip ...


Yes, that would be an even better alternative.

Hardly, bearing in mind the fact that there is no such function as
'asprintf()' in the C standard, and that no code to implement it
has been proposed here. Twink is a known troll, whose primary
objective appears to be disruption of the newsgroup. Navia also
tends to be off-topic, but that appears to be largely due to
ignorance of the language combined with his refusal to listen.
 
I

Ian Collins

Richard said:
Why not at least tell him about:

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

and

if(strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", c) != NULL)
I don't know, must have been a bad hair day. Too many kids making too
much noise too early after too little caffeine.
 
B

Barry Schwarz

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.
 
K

Keith Thompson

Richard Heathfield said:
Ian Collins said:
No, C does not have nested functions, although some compilers support
them as extensions.

Right so far.
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.
Sorry, no.

Why not at least tell him about:

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

and

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

Or, even better, isxdigit(). "(c >= 'A') && (c <= 'Z')" was a typo;
the original Pascal was looking for hexadecimal digits, not
alphanumerics. If you want to reject lowercase hex digits, you'll
need to write something like (isxdigit(c) && !islower(c)). If c is of
type char and there's any chance its value could be negative, you'll
need to convert it to unsigned char before passing it to one of the
is*() functions.

[...]
sprintf(Line1, "File size: %d byte%s", sSize, bytes == 1 ? "" : "s");

Note the extra flexibility that C gives you.

Yes, but you have to work a bit harder (than in, say, Perl) to manage
the memory.

[...]
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.

That hasn't been my experience, but this is off-topic.
 
A

Antoninus Twink

Yes, that would be an even better alternative.

I answered so quickly because I was astonished that somebody could answer

"Not in C"

to such elemntary question!

Yes, it's simply amazing what nonsense they talk.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top