Pascal - C (2)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CBFalconer

    #16
    Re: Pascal - C (2)

    jacob navia wrote:
    Ian Collins wrote:
    >Ruud wrote:
    >>
    .... snip ...
    >>
    >>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.
    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.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home .att.net>
    Try the download section.

    Comment

    • CBFalconer

      #17
      Re: Pascal - C (2)

      jacob navia wrote:
      Antoninus Twink wrote:
      >
      .... snip ...
      >
      >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.
      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.

      --
      [mail]: Chuck F (cbfalconer at maineline dot net)
      [page]: <http://cbfalconer.home .att.net>
      Try the download section.

      Comment

      • Richard Heathfield

        #18
        Re: Pascal - C (2)

        Ian Collins 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?
        >>
        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("ABCD EFGHIJKLMNOPQRS TUVWXYZ01234567 89", c) != NULL)

        neither of which is a precise match, but each of which might be used in
        similar situations.

        One alternative is to use a regular expression library if
        you have a lot of these.
        That seems like overkill to me.
        >
        >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.
        Um, of course there is.

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

        Note the extra flexibility that C gives you.
        >
        >4) In Pascal I can "add" just one character of another string:
        >>
        > Str1 = Str2 + Str3[5];
        >>
        >Unfortunatel y 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 - sprintf can do this easily enough.

        sprintf(Str1, "%s%c", Str2, Str3[5]);
        >>
        Don't use C.
        Um, he asked for a better way to do this in C. Not using C doesn't count.
        C does not have string objects,
        True, but it has a string format which works just fine.
        of arrays of char and library functions to manipulate then.
        I'm not sure what you mean here (unless "of" is a typo for "just" or "only"
        or "merely", but yes, C has arrays of char and library functions to
        manipulate them. Good!
        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.
        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.

        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -http://www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999

        Comment

        • Ian Collins

          #19
          Re: Pascal - C (2)

          Richard Heathfield wrote:
          >
          Why not at least tell him about:
          >
          if(isupper(c) || isdigit(c))
          >
          and
          >
          if(strchr("ABCD EFGHIJKLMNOPQRS TUVWXYZ01234567 89", 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.

          --
          Ian Collins

          Comment

          • Keith Thompson

            #20
            Re: Pascal - C (2)

            Richard Heathfield <rjh@see.sig.in validwrites:
            Ian Collins 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?
            >>>
            >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("ABCD EFGHIJKLMNOPQRS TUVWXYZ01234567 89", 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.

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

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            Nokia
            "We must do something. This is something. Therefore, we must do this."
            -- Antony Jay and Jonathan Lynn, "Yes Minister"

            Comment

            • George

              #21
              Re: Pascal - C (2)

              On Sun, 02 Nov 2008 06:54:51 +0000, Richard Heathfield wrote:
              >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.
              >
              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/

              Comment

              • Antoninus Twink

                #22
                Re: Pascal - C (2)

                On 1 Nov 2008 at 23:07, jacob navia wrote:
                Antoninus Twink wrote:
                >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!
                Yes, it's simply amazing what nonsense they talk.

                Comment

                • Barry Schwarz

                  #23
                  Re: Pascal - C (2)

                  On Sat, 01 Nov 2008 23:26:37 +0100, jacob navia <jacob@nospam.c om>
                  wrote:
                  >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.


                  --
                  Remove del for email

                  Comment

                  • jacob navia

                    #24
                    Re: Pascal - C (2)

                    Barry Schwarz wrote:
                    On Sat, 01 Nov 2008 23:26:37 +0100, jacob navia <jacob@nospam.c om>
                    wrote:
                    >
                    >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()...


                    --
                    jacob navia
                    jacob at jacob point remcomp point fr
                    logiciels/informatique

                    Comment

                    • Flash Gordon

                      #25
                      Re: Pascal - C (2)

                      jacob navia wrote, On 02/11/08 09:56:
                      Barry Schwarz wrote:
                      >On Sat, 01 Nov 2008 23:26:37 +0100, jacob navia <jacob@nospam.c om>
                      >wrote:
                      >>
                      >>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 = "0123456789ABCD EF";
                      char *pos = strchr("0123456 789ABCDEF",toup per((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.
                      --
                      Flash Gordon
                      If spamming me sent it to smap@spam.cause way.com
                      If emailing me use my reply-to address
                      See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

                      Comment

                      • Ruud

                        #26
                        Re: Pascal - C (2)

                        Hallo allemaal,


                        Many thanks for the massive response!


                        jacob navia wrote:
                        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?


                        Chuck wrote:
                        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.


                        Richard wrote:
                        Why not at least tell him about:
                        >
                        if(isupper(c) || isdigit(c))
                        >
                        and
                        >
                        if(strchr("ABCD EFGHIJKLMNOPQRS TUVWXYZ01234567 89", 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.

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


                        Trent wrote:
                        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

                        Comment

                        • jacob navia

                          #27
                          Re: Pascal - C (2)

                          Ruud wrote:
                          Hallo allemaal,
                          >
                          >
                          Many thanks for the massive response!
                          >
                          >
                          jacob navia wrote:
                          >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?
                          >
                          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 navia
                          jacob at jacob point remcomp point fr
                          logiciels/informatique

                          Comment

                          • Harald van =?UTF-8?b?RMSzaw==?=

                            #28
                            Re: Pascal - C (2)

                            On Sun, 02 Nov 2008 01:28:00 -0700, Barry Schwarz wrote:
                            On Sat, 01 Nov 2008 23:26:37 +0100, jacob navia <jacob@nospam.c om>
                            wrote:
                            >>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.

                            Comment

                            • Eric Sosman

                              #29
                              Re: Pascal - C (2)

                              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?
                              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.hheade r 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]);

                              --
                              Eric Sosman
                              esosman@ieee-dot-org.invalid

                              Comment

                              • Flash Gordon

                                #30
                                Re: Pascal - C (2)

                                Ruud wrote, On 02/11/08 12:11:
                                Hallo allemaal,
                                <snip>
                                >A good book, such as K&R II, would be helpful.
                                >
                                The book isn't the problem, see next.
                                Ah, but it could be! You would be well advised to get K&R2.


                                <snip>
                                I justed searched the book "C in 21 days" for 'isdigit' and only found
                                <snip>
                                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.
                                --
                                Flash Gordon
                                If spamming me sent it to smap@spam.cause way.com
                                If emailing me use my reply-to address
                                See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

                                Comment

                                Working...