473,782 Members | 2,485 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pascal - C (2)

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
Nov 1 '08
54 3181
In article <72************ *************** *******@r15g200 0prh.googlegrou ps.com>,
ro***********@y ahoo.com <ro***********@ yahoo.comwrote:
>On Nov 2, 1:17 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
>dj3va...@csclu b.uwaterloo.ca. invalid wrote:
> 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.

Actually you need to save the result of the strlen() in the second
line, since the assignment in the second line will eliminate the NUL
in Str1.
Look more closely.
He's giving strlen one string, and modifying a different one.
dave
(made the same mistake on the first reading)

--
Dave Vandervies dj3vande at eskimo dot com
You really don't want to see Dann in full flow (unless somebody else
is the victim, of course).
--Richard Heathfield in comp.lang.c
Nov 3 '08 #51
ro***********@y ahoo.com wrote:
On Nov 2, 1:17 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
>dj3va...@csclu b.uwaterloo.ca. invalid wrote:
>>> 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.


Actually you need to save the result of the strlen() in the second
line, since the assignment in the second line will eliminate the NUL
in Str1.
Having made two mistakes already in this thread, it is with
some trepidation that I maintain you're wrong here. Look again
at the function argument ...

--
Er*********@sun .com
Nov 3 '08 #52
On Nov 3, 4:44*pm, Eric Sosman <Eric.Sos...@su n.comwrote:
robertwess...@y ahoo.com wrote:
On Nov 2, 1:17 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
dj3va...@csclub .uwaterloo.ca.i nvalid wrote:
* * * *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.
Actually you need to save the result of the strlen() in the second
line, since the assignment in the second line will eliminate the NUL
in Str1.

* * *Having made two mistakes already in this thread, it is with
some trepidation that I maintain you're wrong here. *Look again
at the function argument ...

Ugh. My mistake. Too much string copying going on in this thread!
Nov 4 '08 #53
On Nov 1, 1:43*pm, Ruud <Ruud.Baltis... @apg.nlwrote:
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?
Correct. C does not support the concept of locally scoped or
anonymous functions. That said, locally scoped functions are rarely a
serious issue.
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.
The above C translates to:

if (isalnum (c)) { ... }

The Pascal is doing something different, more like:

if (isdigit (c) || (isalpha(c) && c == toupper(c))) { ... }

Except of course none of this is strictly true since in C your program
may launch nuclear missiles towards Russia if the variable c is non-
ASCII.
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.
C sucks for strings. You can try something like sprintf (Line1, "File
size:" "%s" " bytes", sSize); and hope you don't buffer overflow, but
you really don't have Pascal's self managed string semantics in the
core language.

Have no fear, though. You can use string libraries such as mine
http://bstring.sf.net/ to make life a whole lot better:

bformata (Line1 = bfromcstr ("File size:"), "%s bytes", sSize);

This has almost exactly the same semantic behavior as Pascal.
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?
Well using the Better String library its just:

bconchar (Str1 = bfromcstr (bdata (Str2)), bchare (Str3, 5));

But your questions are well motivated. Bare C is just really weak for
really simple fundamental things which is trivial in most programming
languages. To learn how do this right in straight C correctly you
just have to learn all of C's weaknesses. Libraries such as mine help
tremendously just for strings, but you still kind of have to learn the
C way of doing things.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Nov 4 '08 #54
In article <35************ *************** *******@a3g2000 prm.googlegroup s.com>,
Nick Keighley <ni************ ******@hotmail. comwrote:
>I have a reflex, as soon as I see a string of the form strn* in a post
I start to post my standard "strncpy() may not do what you expect"
reply.
strncpy does exactly what I expect; it's just not usually what I want,
so I either use something else instead or use strncpy followed by some
fixup code.
I had it all composed and ready to send when I noticed he
was using strncat(). So I pressed Discard instead of Send.
But it was close :-)
Yep. strncat is the one I always have to look up; the way it
interprets its count argument is about as unlikely as it could possibly
be without looking like it was deliberately designed to be confusing.
(But writing that post did shed some light on what it might've been
designed for.)
dave

--
Dave Vandervies dj3vande at eskimo dot com
You might have the same problem that I do, though... I've got a whole
bunch of square tuits but I can't find a file to take the corners off.
--Matt Roberds in the scary devil monastery
Nov 4 '08 #55

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4393
by: Chris Gordon-Smith | last post by:
I am tying to call a Pascal function from C++, and vice versa. Does anyone know how to do this, or where detailed information on this topic can be found? For the C++ to Pascal call I have tried declaring the Pascal function as:- extern "C" void PASCALTESTFUNC1(); within my C++ code. However, the linker is giving me an unresolved
24
3481
by: Faith Dorell | last post by:
I really donīt like C.You can write better programs in BASIC than in C, if you donīt like this language. I donīt understand how C became so popular, although much better programming languages existed in the 70s or 80s or 90s. Pascal is much better.
14
23437
by: digital | last post by:
hello anyone... pls explain me , how different between function and procedure for C/C++ and Pascal. Thankx......
28
2279
by: Skybuck Flying | last post by:
Hi, I think I understand now a bit better what the difference is between a c compiler and a pascal compiler. For example: When compiling source code with a pascal compiler. The pascal compiler will simply stop when it is missing an implementation for a procedure or whatever.
17
4378
by: David Scemama | last post by:
Hi, I'm writing a program using VB.NET that needs to communicate with a DOS Pascal program than cannot be modified. The communication channel is through some file databases, and I have a huge problem writing VB Double values to the file so as the Pascal program can read them as Pascal Real values. I've managed to find the algorithm to read the Pascal Real format and convert it to a VB Double, but I cannot figure out the opposite...
6
6913
by: kkrish | last post by:
hi, I am working on an old program written in c.The program uses a function like this "unsigned long int far pascal ReadFile(char *buff,unsigned long int *size)" . Is this a PASCAL function CALL from C?Do I need to have Pascal installed in my system.This has been written in Turbo C 2.0 under
15
1743
by: jacob navia | last post by:
Programming languages come and go. Still is amazing that in this survey from http://www.devsource.com/article2/0,1895,2016936,00.asp the C language comes second, right after Java. Java # What it is: An object-oriented programming language developed by James Gosling and colleagues at Sun Microsystems in the early 1990s. # Job availabilities: 14,408
0
1583
by: dhruba.bandopadhyay | last post by:
Am using Borland C++ 4.5 for the old dos.h APIs. It appears that newer versions of compilers stop support for the oldskool DOS routines. Am trying to convert/port an oldskool Pascal program that uses registers/ interrupts into C/C++. 1. What are the inline($FA); & inline($FB); Pascal functions? What is the C/C++ equivalent? inline($CD / $1C); inline($9C);
7
5971
by: SMALLp | last post by:
Hy! I desperately need help! I need to make application that would accept Pascal code and check if it returns good results. My idea is (from a beginner point of view) to make application in python that would send code (text) to pascal compiler (Free pascal compiler that can be used from command prompt in windows) and it would return result and then application would show that result.
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.