Why is that?

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

    Why is that?

    I know what is causing the problem, but I couldn't
    find out why.

    #define newline "\n"
    #define question_mark "\?"
    #define single_quotas "\'"
    #define double_quotas "\""
    #define form_feed "\f"
    #define horizontal_tab "\t"
    #define vertical_tab "\v"
    #define backslash "\\"
    #include <stdio.h>

    main(){
    int var;
    printf("Type a number:");
    scanf("%d",&var );
    printf("The number is ");
    printf("%d",var );
    printf(newline) ;

    char *var2;
    printf("Type a string:");
    int bytes_read=0;
    int nbytes = 100;

    var2 = (char *) malloc (nbytes + 1);
    bytes_read = getline (&var2, &nbytes, stdin);
    if(bytes_read == -1)
    {puts ("ERROR!: invalid input");}

    printf("The string is ");
    printf("%s",var 2);
    printf(newline) ;
    }


  • Michael Mair

    #2
    Re: Why is that?

    Hi Profetas,

    [color=blue]
    > I know what is causing the problem, but I couldn't
    > find out why.[/color]

    Very enlightening. What do you think your problem is?
    Your code does not even compile...
    [color=blue]
    > #define newline "\n"
    > #define question_mark "\?"
    > #define single_quotas "\'"
    > #define double_quotas "\""
    > #define form_feed "\f"
    > #define horizontal_tab "\t"
    > #define vertical_tab "\v"
    > #define backslash "\\"[/color]

    Won't comment on the sense or nonsense of these macros.
    Apart from the newline, you do not need that stuff. Please
    keep your examples minimal.
    [color=blue]
    > #include <stdio.h>[/color]

    Where is <stdlib.h> needed for malloc()?
    And where is the header for the non-standard getline()
    function? (see below, too)

    [color=blue]
    > main(){[/color]
    int main ()
    [color=blue]
    > int var;
    > printf("Type a number:");
    > scanf("%d",&var );
    > printf("The number is ");
    > printf("%d",var );
    > printf(newline) ;
    >
    > char *var2;
    > printf("Type a string:");
    > int bytes_read=0;
    > int nbytes = 100;
    >
    > var2 = (char *) malloc (nbytes + 1);[/color]
    You have been around here long enough: Do not cast the
    result of malloc(). Btw: If you want to apply a cast,
    then cast nbytes -- malloc takes a size_t argument.[color=blue]
    > bytes_read = getline (&var2, &nbytes, stdin);[/color]
    from 'man getline':
    #define _GNU_SOURCE
    #include <stdio.h>

    ssize_t getline(char **lineptr, size_t *n, FILE *stream);
    I do not know the return type ssize_t but the rest of the manpage
    seems to tell me that it is an improved version of fgets() such
    as most of us have in their personal library...
    I would just make var2=NULL -- as getline is taking care of
    allocation, this is alright for a short program.
    [color=blue]
    > if(bytes_read == -1)[/color]
    bytes_read is not of type ssize_t.[color=blue]
    > {puts ("ERROR!: invalid input");}[/color]
    It might be another problem[color=blue]
    >
    > printf("The string is ");
    > printf("%s",var 2);
    > printf(newline) ;
    > }[/color]
    free() var2. Apart from that: Why use printf() for giving
    a '\n'.

    Please produce code that compiles for
    gcc -ansi -pedantic
    and then come back.


    --Michael

    Comment

    • Michael Mair

      #3
      Re: Why is that?

      Before I forget it:

      #define _GNU_SOURCE
      #include <stdio.h>
      #include <stdlib.h>

      #define newline "\n"

      int main (void)
      {
      int var;
      char *var2 = NULL;
      ssize_t bytes_read = 0;
      size_t nbytes = 0;


      printf("Type a number: "); (void) fflush(stdout);
      bytes_read = getline (&var2, &nbytes, stdin);
      if(bytes_read == -1)
      {
      fprintf(stderr, "ERROR in line %d in return value from"
      " getline()" newline, __LINE__);
      (void) fflush(stderr);
      exit(EXIT_FAILU RE);
      }
      if (sscanf(var2, "%d", &var) != 1)
      {
      fprintf(stderr, "ERROR in line %d: scanf() could not squeeze"
      " a number out of your input" newline, __LINE__);
      (void) fflush(stderr);
      exit(EXIT_FAILU RE);
      }
      printf("The number is %d" newline, var);

      printf("Type a string: "); (void) fflush(stdout);
      bytes_read = getline (&var2, &nbytes, stdin);
      if(bytes_read == -1)
      {
      fprintf(stderr, "ERROR in line %d in return value from"
      " getline()" newline, __LINE__);
      (void) fflush(stderr);
      exit(EXIT_FAILU RE);
      }

      printf("The string is %s" newline, var2); (void) fflush(stdout);

      free(var2);
      exit(EXIT_SUCCE SS);
      }


      does the job for those of us with a new enough gcc compiler.

      Your problem, of course, was the part I replaced by the
      getline()/sscanf() pair.
      Everyone else: Think fgets()/sscanf() and allocated buffer
      var2.
      As to the why: scanf() and family just do not empty the
      input buffer as you seem to expect but get only what
      they want. getline() found the leftover '\n' and that was
      it. So, if you want to do it with scanf(), you will have
      to empty the input buffer line with s.th. like

      while ( (c=getchar()) != EOF )
      if (c=='\n')
      break;

      (not tested)

      Cheers
      Michael

      Comment

      • Default User

        #4
        Re: Why is that?

        Profetas wrote:[color=blue]
        >
        > I know what is causing the problem, but I couldn't
        > find out why.[/color]


        Which problem is that?



        Brian Rodenborn

        Comment

        • Profetas

          #5
          Re: Why is that?

          >>[color=blue][color=green]
          >> I know what is causing the problem, but I couldn't
          >> find out why.[/color][/color]

          [color=blue]
          >Which problem is that?[/color]

          after the scanf is edxecuted it won't execute the getline

          bash-2.05b$ ./a.out
          Type a number:12
          The number is 12
          Type a string:The string is


          Comment

          • Profetas

            #6
            Re: Why is that?

            >Your problem, of course, was the part I replaced by the[color=blue]
            >getline()/sscanf() pair.
            >Everyone else: Think fgets()/sscanf() and allocated buffer
            >var2.
            >As to the why: scanf() and family just do not empty the
            >input buffer as you seem to expect but get only what
            >they want. getline() found the leftover '\n' and that was
            >it. So, if you want to do it with scanf(), you will have
            >to empty the input buffer line with s.th. like[/color]

            That is what I was looking for.

            Thanks Michael Mair

            Comment

            • Default User

              #7
              Re: Why is that?

              Profetas wrote:[color=blue]
              >[color=green][color=darkred]
              > >>
              > >> I know what is causing the problem, but I couldn't
              > >> find out why.[/color][/color]
              >[color=green]
              > >Which problem is that?[/color]
              >
              > after the scanf is edxecuted it won't execute the getline
              >
              > bash-2.05b$ ./a.out
              > Type a number:12
              > The number is 12
              > Type a string:The string is[/color]






              Brian Rodenborn

              Comment

              • Jack Klein

                #8
                Re: Why is that?

                On Wed, 18 Aug 2004 11:01:45 -0400, "Profetas" <xuxu_18@yahoo. com>
                wrote in comp.lang.c:
                [color=blue]
                > I know what is causing the problem, but I couldn't
                > find out why.[/color]

                There is no version of standard C where this is a legal program.
                [color=blue]
                > main(){[/color]

                The implicit int return type of main() is illegal from C99 onward.
                [color=blue]
                > int var;
                > printf("Type a number:");[/color]

                [snip]
                [color=blue]
                > char *var2;[/color]

                Defining a variable after executable statements in a block is only
                legal from C99 onward.

                And as for these:
                [color=blue]
                > #define newline "\n"
                > #define question_mark "\?"
                > #define single_quotas "\'"
                > #define double_quotas "\""
                > #define form_feed "\f"
                > #define horizontal_tab "\t"
                > #define vertical_tab "\v"
                > #define backslash "\\"[/color]

                The standard doesn't allow it, but my opinion is that any program
                written by anyone who writes macros this dumb should automatically
                fail.

                --
                Jack Klein
                Home: http://JK-Technology.Com
                FAQs for
                comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
                comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                alt.comp.lang.l earn.c-c++

                Comment

                • Keith Thompson

                  #9
                  Re: Why is that?

                  Jack Klein <jackklein@spam cop.net> writes:[color=blue]
                  > On Wed, 18 Aug 2004 11:01:45 -0400, "Profetas" <xuxu_18@yahoo. com>
                  > wrote in comp.lang.c:[/color]
                  [...][color=blue]
                  > And as for these:
                  >[color=green]
                  > > #define newline "\n"[/color][/color]
                  [snip][color=blue]
                  > The standard doesn't allow it, but my opinion is that any program
                  > written by anyone who writes macros this dumb should automatically
                  > fail.[/color]

                  I think you meant "The standard *does* allow it...".

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

                  • CBFalconer

                    #10
                    Re: Why is that?

                    Keith Thompson wrote:[color=blue]
                    > Jack Klein <jackklein@spam cop.net> writes:[color=green]
                    >> "Profetas" <xuxu_18@yahoo. com> wrote in comp.lang.c:[/color]
                    > [...][color=green]
                    >> And as for these:
                    >>[color=darkred]
                    >>> #define newline "\n"[/color][/color]
                    > [snip][color=green]
                    >> The standard doesn't allow it, but my opinion is that any program
                    >> written by anyone who writes macros this dumb should automatically
                    >> fail.[/color]
                    >
                    > I think you meant "The standard *does* allow it...".[/color]

                    I think he means the standard doesn't allow automatic failing of
                    extremely dumb programs.

                    --
                    Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                    Available for consulting/temporary embedded and systems.
                    <http://cbfalconer.home .att.net> USE worldnet address!


                    Comment

                    • Keith Thompson

                      #11
                      Re: Why is that?

                      CBFalconer <cbfalconer@yah oo.com> writes:[color=blue]
                      > Keith Thompson wrote:[color=green]
                      > > Jack Klein <jackklein@spam cop.net> writes:[color=darkred]
                      > >> "Profetas" <xuxu_18@yahoo. com> wrote in comp.lang.c:[/color]
                      > > [...][color=darkred]
                      > >> And as for these:
                      > >>
                      > >>> #define newline "\n"[/color]
                      > > [snip][color=darkred]
                      > >> The standard doesn't allow it, but my opinion is that any program
                      > >> written by anyone who writes macros this dumb should automatically
                      > >> fail.[/color]
                      > >
                      > > I think you meant "The standard *does* allow it...".[/color]
                      >
                      > I think he means the standard doesn't allow automatic failing of
                      > extremely dumb programs.[/color]

                      I think you're right. And so was he. And so am I (now).

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

                      • Profetas

                        #12
                        Re: Why is that?

                        >The standard doesn't allow it, but my opinion is that any program[color=blue]
                        >written by anyone who writes macros this dumb should automatically[/color]
                        fail.

                        How do you know that it is dumb, if you don't even know the
                        rest of my program, and you don't know why it has been used
                        and its purpose, and how it has been written

                        don't judge what you don't know.


                        Comment

                        • Dan Pop

                          #13
                          Re: Why is that?

                          In <255f536db865d6 cb78f93f19c6fc0 5d2@localhost.t alkaboutprogram ming.com> "Profetas" <xuxu_18@yahoo. com> writes:
                          [color=blue][color=green]
                          >>The standard doesn't allow it, but my opinion is that any program
                          >>written by anyone who writes macros this dumb should automatically[/color]
                          >fail.
                          >
                          >How do you know that it is dumb, if you don't even know the
                          >rest of my program, and you don't know why it has been used
                          >and its purpose, and how it has been written[/color]

                          It is obvious, to anyone with a clue, that those macro definitions can
                          serve no good purpose in *any* C program.
                          [color=blue]
                          >don't judge what you don't know.[/color]

                          The judgment was entirely based on known things. Feel free to prove it
                          wrong by showing an example of good usage for those macros.

                          Here's an example of sensible macro:

                          #define SEPARATOR ','

                          and one of idiotic macro:

                          #define COMMA ','

                          The need to change the definition of SEPARATOR later in program's life is
                          conceivable, but there is no such need for a macro named COMMA.

                          Dan
                          --
                          Dan Pop
                          DESY Zeuthen, RZ group
                          Email: Dan.Pop@ifh.de

                          Comment

                          • Thomas Matthews

                            #14
                            Re: Why is that?

                            Profetas wrote:
                            [color=blue]
                            > I know what is causing the problem, but I couldn't
                            > find out why.
                            >
                            > #define newline "\n"
                            > #define question_mark "\?"
                            > #define single_quotas "\'"
                            > #define double_quotas "\""
                            > #define form_feed "\f"
                            > #define horizontal_tab "\t"
                            > #define vertical_tab "\v"
                            > #define backslash "\\"[/color]

                            One of the main problems of the above macros is
                            that you are using strings instead of characters.

                            If I have:
                            const char statement[] = "Hello\t\"World ?\"";
                            And I want to search for a "question_mark" ,
                            I would naturally want to use:
                            char * posn;
                            posn = strchr(statemen t, question_mark);
                            Which will not work because strchr() requires
                            a single char as the second parameter. Also,
                            this one will be hard to find:
                            posn = strchr(question _mark, statement);
                            In the above line, the first parameter has the
                            correct type, but wrong logic.

                            Also, please check your spelling. Note that
                            in English, "quota" and "quote" are two different
                            words. A quota is a limit. One could have a
                            single quota. There could also be double quotas,
                            which means having two limits; which is far from
                            the symbol (").

                            At some point, macros lead more to obfuscating
                            a program than to readablility.
                            If you want to keep down this path, may I also
                            suggest these macros:
                            #define begin {
                            #define end }
                            #ifndef and
                            #define and &&
                            #endif
                            #ifndef or
                            #define or ||
                            #endif
                            #ifndef not
                            #define not !
                            #endif
                            #define ever 1
                            #define forever 1

                            while (forever)
                            begin
                            if (a and b)
                            begin
                            continue;
                            end
                            else
                            begin
                            break;
                            end
                            end


                            --
                            Thomas Matthews

                            C++ newsgroup welcome message:

                            C++ Faq: http://www.parashift.com/c++-faq-lite
                            C Faq: http://www.eskimo.com/~scs/c-faq/top.html
                            alt.comp.lang.l earn.c-c++ faq:

                            Other sites:
                            http://www.josuttis.com -- C++ STL Library book

                            Comment

                            • Christopher Benson-Manica

                              #15
                              Re: Why is that?

                              Profetas <xuxu_18@yahoo. com> spoke thus:
                              [color=blue]
                              > How do you know that it is dumb, if you don't even know the
                              > rest of my program, and you don't know why it has been used
                              > and its purpose, and how it has been written[/color]

                              Hang around here long enough and you'll realize that when certain
                              people (the eminent Mr. Klein being among them) call something you've
                              done "dumb", they're probably right. Lord knows it's happened to me
                              enough to know :)

                              --
                              Christopher Benson-Manica | I *should* know what I'm talking about - if I
                              ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                              Comment

                              Working...