Pascal - C (2)

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

    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


  • Ian Collins

    #2
    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?
    >
    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.

    --
    Ian Collins

    Comment

    • jacob navia

      #3
      Re: Pascal - C (2)

      Ian Collins wrote:
      Ruud wrote:
      >Hallo allemaal,
      >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.
      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];
      >>
      >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?
      >>
      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



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

      Comment

      • jacob navia

        #4
        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?
        >
        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]);

        Many thanks for any comment!
        >
        >
        --
        ___
        / __|__
        / / |_/ Groetjes, Ruud Baltissen
        \ \__|_\
        \___| http://Ruud.C64.org
        >
        >

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

        Comment

        • Antoninus Twink

          #5
          Re: Pascal - C (2)

          On 1 Nov 2008 at 22:23, jacob navia wrote:
          Ian Collins wrote:
          >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);
          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.

          Comment

          • jacob navia

            #6
            Re: Pascal - C (2)

            Antoninus Twink wrote:
            On 1 Nov 2008 at 22:23, jacob navia wrote:
            >Ian Collins wrote:
            >>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);
            >
            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!


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

            Comment

            • Ian Collins

              #7
              Re: Pascal - C (2)

              jacob navia wrote:
              Ian Collins wrote:
              >>>
              >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.
              >
              Why do you have to insult everyone?
              >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
              >
              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.

              --
              Ian Collins

              Comment

              • jacob navia

                #8
                Re: Pascal - C (2)

                Ian Collins wrote:
                jacob navia wrote:
                >Ian Collins wrote:
                >>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.
                >>
                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.
                >>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
                >>
                So what part of my statement do you disagree with?
                "Don't use C"



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

                Comment

                • Ian Collins

                  #9
                  Re: Pascal - C (2)

                  jacob navia wrote:
                  Ian Collins wrote:
                  >jacob navia wrote:
                  >>Ian Collins 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.
                  >>>>
                  >>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?
                  >
                  "Don't use C"
                  >
                  But why would you use C for string manipulation when there better
                  alternatives?

                  --
                  Ian Collins

                  Comment

                  • Nate Eldredge

                    #10
                    Re: Pascal - C (2)

                    Ruud <Ruud.Baltissen @apg.nlwrites:
                    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.hfor 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.

                    Comment

                    • jacob navia

                      #11
                      Re: Pascal - C (2)

                      Ian Collins wrote:
                      jacob navia wrote:
                      >Ian Collins wrote:
                      >>jacob navia wrote:
                      >>>Ian Collins 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
                      >>>>processin g
                      >>>>and might be a better option.
                      >>>>>
                      >>>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?
                      >"Don't use C"
                      >>
                      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


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

                      Comment

                      • S M Ryan

                        #12
                        Re: Pascal - C (2)

                        In article <ba5a4e47-f677-4e1c-b936-2621811c5534@f3 7g2000pri.googl egroups.com>,
                        Ruud <Ruud.Baltissen @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?
                        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.his 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'm not even supposed to be here today.

                        I ASSURE YOU WE'RE OPEN!

                        Comment

                        • Ian Collins

                          #13
                          Re: Pascal - C (2)

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

                          --
                          Ian Collins

                          Comment

                          • jacob navia

                            #14
                            Re: Pascal - C (2)

                            Ian Collins wrote:
                            jacob navia wrote:
                            >Ian Collins wrote:
                            >>But why would you use C for string manipulation when there better
                            >>alternative s?
                            >>>
                            >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.
                            >
                            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.
                            >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.
                            >
                            Maybe. But pcre is highly portable and can be used anywhere a
                            C compiler exists.


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

                            Comment

                            • Trent Josephsen

                              #15
                              Re: Pascal - C (2)

                              Ruud wrote:
                              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};
                              You can use strncat() to concatenate any number of characters from one
                              string to another. For this case you would use

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

                              Observe that Str3[5] is a char, not a pointer to char, so you have to
                              take the address of it with & to pass it to strncat. This is the source
                              of the error you mentioned.

                              Comment

                              Working...