strtoint

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

    #16
    Re: strtoint

    >>trouble me. Why not make an array of the set of characters you want[color=blue][color=green]
    >>to be in the random string and then randomly select from the set of[/color]
    >
    >Well they aren't so magic 65 is equal to A and 25 is the range
    >of the Alphabet :-) so the generated string will be between
    >(A-Z)[/color]

    Yes, they *ARE* magic.

    C does not guarantee ANY of:

    'A' == 65
    'A' + 25 == 'Z'
    The character set is ASCII.
    The alphabet contains 25 or 26 letters.
    There are no unaccented letters in the character set.
    Alphabetic characters form a contiguous block of consecutive codes.

    Gordon L. Burditt

    Comment

    • Al Bowers

      #17
      Re: strtoint



      Profetas wrote:
      [color=blue][color=green]
      >>Well that is not a bad approach. However the magic numbers 65 and 25
      >>trouble me. Why not make an array of the set of characters you want
      >>to be in the random string and then randomly select from the set of[/color]
      >
      >
      > Well they aren't so magic 65 is equal to A and 25 is the range
      > of the Alphabet :-) so the generated string will be between
      > (A-Z)
      >[/color]

      You know, I have just searched the standard, and I find no mention
      of the value of 'A' being 65. But I did find in this paragraph

      5.2.1.1
      "The values of the members of the execution character set
      are implementation-defined."

      So you may be using a implementation-defined solution.

      This news group is dedicated to the language as defined by the
      C Standard(ISO/IEC 9899:1999). Again, I point out that in this
      newsgroup, with readers that use various implementations , that
      your assertion that 'A' is value 65 is idiotic.


      --
      Al Bowers
      Tampa, Fl USA
      mailto: xabowers@myrapi dsys.com (remove the x to send email)
      Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


      Comment

      • Emmanuel Delahaye

        #18
        Re: strtoint

        Peter Nilsson wrote on 26/07/04 :
        [color=blue][color=green]
        >> Bare in mind that in C, '=' is the affectation operator,[/color]
        >
        > I think 'affectation' must be a French/English faux ami.[/color]

        Oops, I meant assignment, of course. Sorry for the Frenchism!

        --
        Emmanuel
        The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

        "C is a sharp tool"

        Comment

        • Chris Torek

          #19
          Re: strtoint

          In article
          <5d524b0f489587 974ff7e5f36c82d a65@localhost.t alkaboutprogram ming.com>
          Profetas <xuxu_18@yahoo. com> writes:[color=blue]
          >Well they aren't so magic 65 is equal to A and 25 is the range
          >of the Alphabet :-) so the generated string will be between
          >(A-Z)[/color]

          Except that 'A' is 193 (0xC1), on that IBM mainframe using EBCDIC.
          (And yes, it has a C compiler.) Not only that, while 'A'+1 is 'B',
          and 'B'+1 is 'C', there is an odd gap so that 'I'+1 is not 'J' --
          'J' is coded as 0xD1. There is another gap before the end of the
          alphabet, and '0' is coded as 0xF0.
          --
          In-Real-Life: Chris Torek, Wind River Systems
          Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
          email: forget about it http://web.torek.net/torek/index.html
          Reading email is like searching for food in the garbage, thanks to spammers.

          Comment

          • Old Wolf

            #20
            Re: strtoint

            Emmanuel Delahaye <emdel@YOURBRAn oos.fr> wrote:[color=blue]
            > Profetas wrote on 25/07/04 :[color=green]
            > > does this function exist in c? on any equivalent? that convert string to
            > > integer?
            > >
            > > how do I convert a array to a "string"
            > > example
            > > char arr['a','b','c'];[/color]
            >
            > This is not C.[/color]

            Actually it is; if the system uses ASCII then it declares an
            array of 99 chars.

            Comment

            • Peter Nilsson

              #21
              Re: strtoint

              "Gordon Burditt" <gordonb.ibq1o@ burditt.org> wrote in message
              news:ce1m6e$i76 @library2.airne ws.net...[color=blue][color=green][color=darkred]
              > >>trouble me. Why not make an array of the set of characters you want
              > >>to be in the random string and then randomly select from the set of[/color]
              > >
              > >Well they aren't so magic 65 is equal to A and 25 is the range
              > >of the Alphabet :-) so the generated string will be between
              > >(A-Z)[/color]
              >
              > Yes, they *ARE* magic.
              >
              > C does not guarantee ANY of:
              >
              > <snip>
              > The alphabet contains 25 or 26 letters.
              > There are no unaccented letters in the character set.
              > <snip>[/color]

              What do you mean by this, given 5.2.1p3...

              Both the basic source and basic execution character sets
              shall have the following members: the 26 uppercase letters
              of the Latin alphabet
              A B C D E F G H I J K L M
              N O P Q R S T U V W X Y Z
              the 26 lowercase letters of the Latin alphabet
              a b c d e f g h i j k l m
              n o p q r s t u v w x y z
              ...

              --
              Peter


              Comment

              • Emmanuel Delahaye

                #22
                Re: strtoint

                Profetas wrote on 26/07/04 :
                [color=blue]
                > My goal was to generate a random string, I knew how to generate random
                > numbers, any way here is my solution.
                >
                > for(i=0; i<16; i++)
                > {
                > var_ptr[i]=65 + rand()%25;
                > }[/color]

                Assuming ASCII, you can replace 65 by 'A' and it will work. The trap is
                that you have to add a final 0 to make the array of char, a string [1].

                A more portable way should be:

                #include <stdio.h>
                #include <stdlib.h>
                #include <time.h>

                int main (void)
                {
                char s[16 + 1];
                size_t i;

                static char a_alpha[26] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ";

                srand ((int) time (NULL));

                for (i = 0; i < sizeof s - 1; i++)
                {
                s[i] = a_alpha[rand () % sizeof a_alpha]; /* assuming A-Z */
                }

                s[i] = 0;

                printf ("s = '%s'\n", s);

                return 0;
                }

                Output:

                s = 'NYMLWOBQXZNGHQ IQ'

                s = 'LWNWXNLMZIXIBM LF'

                ---------------------
                [1] Maybe it was obvious to you, but in that case, you should have
                shown it more clearly.

                Additionally, other people, mainly newbies, are reading the newsgroup,
                and if they see a code without corrections, they tend to read it as a
                valid code. This is why my ethic leads me to avoid to leave bad or
                incomplete code without a comment.

                --
                Emmanuel
                The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

                "C is a sharp tool"

                Comment

                • Emmanuel Delahaye

                  #23
                  Re: strtoint

                  Profetas wrote on 26/07/04 :[color=blue][color=green]
                  >> Well that is not a bad approach. However the magic numbers 65 and 25
                  >> trouble me.[/color]
                  >
                  > Well they aren't so magic 65 is equal to A[/color]

                  In ASCII only.
                  [color=blue]
                  > and 25 is the range
                  > of the Alphabet :-) so the generated string will be between
                  > (A-Z)[/color]

                  Used to be 26 to me, but maybe we don't have the same alphabet (I
                  learnt recently that some letters don't exist in Italian for
                  example)...

                  --
                  Emmanuel
                  The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

                  "C is a sharp tool"

                  Comment

                  • Emmanuel Delahaye

                    #24
                    Re: strtoint

                    Old Wolf wrote on 26/07/04 :[color=blue][color=green][color=darkred]
                    >>> char arr['a','b','c'];[/color]
                    >>
                    >> This is not C.[/color]
                    >
                    > Actually it is; if the system uses ASCII then it declares an
                    > array of 99 chars.[/color]

                    Error MAIN.C 5: Array bounds missing ]

                    --
                    Emmanuel
                    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

                    "C is a sharp tool"

                    Comment

                    • Gordon Burditt

                      #25
                      Re: strtoint

                      >> C does not guarantee ANY of:[color=blue][color=green]
                      >>
                      >> <snip>
                      >> The alphabet contains 25 or 26 letters.
                      >> There are no unaccented letters in the character set.
                      >> <snip>[/color]
                      >
                      >What do you mean by this, given 5.2.1p3...
                      >
                      > Both the basic source and basic execution character sets
                      > shall have the following members: the 26 uppercase letters
                      > of the Latin alphabet
                      > A B C D E F G H I J K L M
                      > N O P Q R S T U V W X Y Z
                      > the 26 lowercase letters of the Latin alphabet
                      > a b c d e f g h i j k l m
                      > n o p q r s t u v w x y z
                      > ...[/color]

                      I said: C does not guarantee ANY of
                      .... there are no unaccented letters in the character set.

                      Since your quote from the standard demonstrates that there
                      are at least 52 unaccented letters, it proves my point.

                      Actually, I meant "C does not guarantee that there are no accented
                      letters in the character set", but with the mistake the result was
                      still true.

                      Gordon L. Burditt

                      Comment

                      • Chris Torek

                        #26
                        Re: strtoint

                        >> Profetas wrote on 25/07/04 :[color=blue][color=green][color=darkred]
                        >> > char arr['a','b','c'];[/color][/color][/color]
                        [color=blue]
                        >Emmanuel Delahaye <emdel@YOURBRAn oos.fr> wrote:[color=green]
                        >> This is not C.[/color][/color]

                        In article <843a4f78.04072 51810.1cd6534e@ posting.google. com>
                        Old Wolf <oldwolf@inspir e.net.nz> writes:[color=blue]
                        >Actually it is; if the system uses ASCII then it declares an
                        >array of 99 chars.[/color]

                        Even in C99, a constant-expression cannot contain a comma-expression:

                        [#3] Constant expressions shall not contain assignment,
                        increment, decrement, function-call, or comma operators,
                        except when they are contained within the operand of a
                        sizeof operator.83

                        I have never understood why comma-expressions are forbidden,
                        although certainly for the code in ">>>" above, a warning is
                        quite desirable even in non-Standard modes in which comma-expressions
                        *are* allowed in constants.
                        --
                        In-Real-Life: Chris Torek, Wind River Systems
                        Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                        email: forget about it http://web.torek.net/torek/index.html
                        Reading email is like searching for food in the garbage, thanks to spammers.

                        Comment

                        • Keith Thompson

                          #27
                          Re: strtoint

                          Al Bowers <xabowers@rapid sys.com> writes:
                          [...][color=blue]
                          > This news group is dedicated to the language as defined by the
                          > C Standard(ISO/IEC 9899:1999). Again, I point out that in this
                          > newsgroup, with readers that use various implementations , that
                          > your assertion that 'A' is value 65 is idiotic.[/color]

                          No, it's not idiotic. It's merely incorrect.

                          --
                          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                          We must do something. This is something. Therefore, we must do this.

                          Comment

                          • Keith Thompson

                            #28
                            Re: strtoint

                            Chris Torek <nospam@torek.n et> writes:[color=blue][color=green][color=darkred]
                            > >> Profetas wrote on 25/07/04 :
                            > >> > char arr['a','b','c'];[/color][/color]
                            >[color=green]
                            > >Emmanuel Delahaye <emdel@YOURBRAn oos.fr> wrote:[color=darkred]
                            > >> This is not C.[/color][/color]
                            >
                            > In article <843a4f78.04072 51810.1cd6534e@ posting.google. com>
                            > Old Wolf <oldwolf@inspir e.net.nz> writes:[color=green]
                            > >Actually it is; if the system uses ASCII then it declares an
                            > >array of 99 chars.[/color]
                            >
                            > Even in C99, a constant-expression cannot contain a comma-expression:
                            >
                            > [#3] Constant expressions shall not contain assignment,
                            > increment, decrement, function-call, or comma operators,
                            > except when they are contained within the operand of a
                            > sizeof operator.83[/color]

                            Right, but in C99 the expression between the square brackets doesn't
                            have to be a constant-expression, so
                            char arr['a','b','c'];
                            declares a variable length array (even though the length can be
                            determined at compilation time).
                            [color=blue]
                            > I have never understood why comma-expressions are forbidden,
                            > although certainly for the code in ">>>" above, a warning is
                            > quite desirable even in non-Standard modes in which comma-expressions
                            > *are* allowed in constants.[/color]

                            Probably because a constant-expression with a comma operator most
                            likely indicates a bug. The left operand of a comma operator is
                            evaluated only for its side effects, and a constant-expression can't
                            have side effects.

                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                            We must do something. This is something. Therefore, we must do this.

                            Comment

                            • Chris Torek

                              #29
                              Re: strtoint

                              >Chris Torek <nospam@torek.n et> writes:[color=blue][color=green]
                              >>Even in C99, a constant-expression cannot contain a
                              >>comma-expression ...[/color][/color]

                              In article <lnfz7ftfpm.fsf @nuthaus.mib.or g>
                              Keith Thompson <kst-u@mib.org> writes:[color=blue]
                              >Right, but in C99 the expression between the square brackets doesn't
                              >have to be a constant-expression, so
                              > char arr['a','b','c'];
                              >declares a variable length array (even though the length can be
                              >determined at compilation time).[/color]

                              True, but only if "arr" appears in a context in which VLAs are
                              allowed. So it is always invalid in C89, and sometimes valid
                              in C99:

                              char wrong[1,2]; /* error in both C89 and C99 */
                              void fn(void) {
                              char ok_and_VLA[1,2]; /* error in C89, otherwise VLA of size 2 */
                              ...
                              }

                              Interestingly, one can test for "VLA-ness" at runtime by applying
                              the sizeof operator, whose operand *is* evaluated if the operand
                              is a VLA. I am not quite sure whether this can be used to detect
                              whether a given C99 compiler treats (1,2) as a constant-expression
                              in the "ok_and_VLA " case above.
                              --
                              In-Real-Life: Chris Torek, Wind River Systems
                              Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                              email: forget about it http://web.torek.net/torek/index.html
                              Reading email is like searching for food in the garbage, thanks to spammers.

                              Comment

                              • Al Bowers

                                #30
                                Re: strtoint



                                Chris Torek wrote:[color=blue][color=green][color=darkred]
                                >>>Profetas wrote on 25/07/04 :
                                >>>
                                >>>>char arr['a','b','c'];[/color][/color]
                                >
                                >[color=green]
                                >>Emmanuel Delahaye <emdel@YOURBRAn oos.fr> wrote:
                                >>[color=darkred]
                                >>>This is not C.[/color][/color]
                                >
                                >
                                > In article <843a4f78.04072 51810.1cd6534e@ posting.google. com>
                                > Old Wolf <oldwolf@inspir e.net.nz> writes:
                                >[color=green]
                                >>Actually it is; if the system uses ASCII then it declares an
                                >>array of 99 chars.[/color]
                                >
                                >
                                > Even in C99, a constant-expression cannot contain a comma-expression:
                                >
                                > [#3] Constant expressions shall not contain assignment,
                                > increment, decrement, function-call, or comma operators,
                                > except when they are contained within the operand of a
                                > sizeof operator.83
                                >[/color]

                                Because of the comma operator, the expression, ('a','b','c') cannot
                                be a constant expression. However, vla's do not require a constant
                                expression and in my opinion the non-constant expression would
                                be nothing other than a poor construct of the array.

                                In defect report 261:

                                I quote:

                                "The Committee also agrees that:

                                int fla [(0+5)]; // is a normal array, not variably modified
                                int vla [(0,5)]; // is a variable length array"


                                --
                                Al Bowers
                                Tampa, Fl USA
                                mailto: xabowers@myrapi dsys.com (remove the x to send email)
                                Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


                                Comment

                                Working...