function

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

    #16
    Re: function

    santosh wrote:
    Bill Cunningham wrote:
    >Earlier in this
    >post I wanted to write zeros to a file and found out the proper use
    >was with the string terminating character '\0' which is a byte with
    >all the bits turned off.
    >
    Not necessarily. It's just a byte with value zero.
    Bill is right, you are wrong.

    5.2 Environmental considerations
    5.2.1 Character sets

    A byte with all bits set to 0, called the null character,
    shall exist in the basic execution character set;
    it is used to terminate a character string literal.

    --
    pete

    Comment

    • Keith Thompson

      #17
      Re: function

      "Bill Cunningham" <nospam@nspam.c omwrites:
      "Joachim Schmitz" <nospam.jojo@sc hmitz-digital.dewrote in message
      news:g1rq2v$nf9 $1@online.de...
      >
      [snip]
      >
      >>>Still needs some error checking (num>=0, fp != NULL, fclose and fput
      >>>worked)
      >>>
      >> I can get that part. I think.
      >Yep, left as an excercise to the reader 8-)
      >
      Now here's what I tried in error checking that gave me a warning. I
      must've messed up.
      >
      if((fputc(a,fp) )!=NULL)
      if((fclose(fp)) !=NULL)
      >
      comparing argument with a without a cast or something like that the compiler
      said. Without cast and comparing was mentioned by the compiler but it
      compiled and didn't work.
      "something like that"? Is there some reason you couldn't tell us
      *exactly* what the compiler said?

      (Incidentally, be wary of any compiler diagnostic that implies a cast
      is missing. 99% of the time a cast is exactly the wrong solution.)
      So I tried this.
      >
      if((fputc(a,fp) )==NULL)
      puts("fputc error");
      >
      When I ran the binary fputc error was mentioned 10 times. My 2nd arg was 10.
      RTFM. (Read The Fine Manual.)

      Read the documentation for fputc, either your system's online
      documentation, or your C textbook, or some version or draft of the C
      standard. What type does it return? What do different returned
      values mean? What value or values does it return to indicate that
      there was an error? Does fputc return a pointer type? Does it make
      any sense to compare its result to NULL, which is a null pointer
      constant?

      Repeat the above paragraph, replacing "fputc" with "fclose".

      (Don't bother posting the answers here; most of us already know them.)

      You're obviously guessing, yet again. Don't do that. Don't even
      think about using a function until you've read and understood the
      documentation for that function. If your compiler warns you about a
      function call, it's very likely that you're using that function
      incorrectly; consult the function's documentation *before* (or instead
      of) posting here.

      And add some whitespace to make your code more readable. You wrote:

      if((fputc(a,fp) )!=NULL)

      I'd write:

      if (fputc(a, fp) != NULL)

      Note that I've also dropped an extraneous set of parentheses.

      (I just fixed the layout, not the error; I'll leave that to you.)

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

      • Keith Thompson

        #18
        Re: function

        santosh <santosh.k83@gm ail.comwrites:
        Bill Cunningham wrote:
        >"santosh" <santosh.k83@gm ail.comwrote in message
        >news:g1s4e8$ig h$3@registered. motzarella.org. ..
        [...]
        >it didn't work but I guess I've had NULL on my mind. Earlier in this
        >post I wanted to write zeros to a file and found out the proper use
        >was with the string terminating character '\0' which is a byte with
        >all the bits turned off.
        >
        Not necessarily. It's just a byte with value zero.
        Which, for all practical purposes, *is* a byte with all the bits
        turned off.

        (If plain char is signed, and if it has more than one representation
        of zero, it's conceivable that storing '\0' in a char object might
        give it a representation other all-bits-zero -- but even in such an
        implementation, I'd be astonished if it didn't choose the
        all-bits-zero representation. )

        Incidentally, it's true that '\0' is used to terminate a string, but I
        don't think that's relevant in this case. You're writing zero bytes
        to a binary file. The data isn't being treated as a string or
        strings, so the fact that '\0' happens to be a string terminator isn't
        relevant.

        [...]

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

        • Bill Cunningham

          #19
          Re: function


          "santosh" <santosh.k83@gm ail.comwrote in message
          news:g1s7f0$1ei $1@registered.m otzarella.org.. .
          Not necessarily. It's just a byte with value zero.
          >
          I wrote this program in my djgpp compile not my gcc one and got the
          ascii value of 48.

          printf("%i\n",' 0');

          Bill


          Comment

          • Lew Pitcher

            #20
            Re: function

            In comp.lang.c, Bill Cunningham wrote:
            >
            "santosh" <santosh.k83@gm ail.comwrote in message
            news:g1s7f0$1ei $1@registered.m otzarella.org.. .
            >
            >Not necessarily. It's just a byte with value zero.
            >>
            I wrote this program in my djgpp compile not my gcc one and got the
            ascii value of 48.
            >
            printf("%i\n",' 0');
            Right. That's not a "byte with value zero". That's a character with a value
            of '0' (much like a character with a value of 'Q').

            --
            Lew Pitcher

            Master Codewright & JOAT-in-training | Registered Linux User #112576
            http://pitcher.digitalfreehold.ca/ | GPG public key available by request
            ---------- Slackware - Because I know what I'm doing. ------


            Comment

            • Keith Thompson

              #21
              Re: function

              "Bill Cunningham" <nospam@nspam.c omwrites:
              "santosh" <santosh.k83@gm ail.comwrote in message
              news:g1s7f0$1ei $1@registered.m otzarella.org.. .
              >
              >Not necessarily. It's just a byte with value zero.
              >>
              I wrote this program in my djgpp compile not my gcc one and got the
              ascii value of 48.
              >
              printf("%i\n",' 0');
              '0' and '\0' are two very different things.

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

              • CBFalconer

                #22
                Re: function

                Bill Cunningham wrote:
                >
                .... snip ...
                >
                I wrote this program in my djgpp compile not my gcc one and got
                the ascii value of 48.
                The DJGPP compiler IS gcc.

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


                ** Posted from http://www.teranews.com **

                Comment

                • Barry Schwarz

                  #23
                  Re: function

                  On Sat, 31 May 2008 16:16:25 GMT, "Bill Cunningham" <nospam@nspam.c om>
                  wrote:
                  >
                  >"Joachim Schmitz" <nospam.jojo@sc hmitz-digital.dewrote in message
                  >news:g1rq2v$nf 9$1@online.de.. .
                  >
                  >[snip]
                  >
                  >>>Still needs some error checking (num>=0, fp != NULL, fclose and fput
                  >>>worked)
                  >>>
                  >> I can get that part. I think.
                  >Yep, left as an excercise to the reader 8-)
                  >
                  Now here's what I tried in error checking that gave me a warning. I
                  >must've messed up.
                  >
                  >if((fputc(a,fp ))!=NULL)
                  >if((fclose(fp) )!=NULL)
                  What is the return type of fputc and fclose? What is the type of
                  NULL? Is there any implicit conversion between the two types? Why do
                  you think either function returns NULL on error? Do you have a
                  reference text of any kind? Have you downloaded any of the free
                  standard drafts (they all provide complete prototypes for every
                  standard function)? Somewhere along the line you determined that
                  fputc takes an int and a FILE*. How come you cannot use the same
                  source to determine its return type?
                  >
                  >comparing argument with a without a cast or something like that the compiler
                  What do you mean "something like that"? You are not a novice poster,
                  regardless of your skill at C. What is so prohibitively difficult
                  that you cannot cut and paste the text of the diagnostic into your
                  message when asking for help?
                  >said. Without cast and comparing was mentioned by the compiler but it
                  >compiled and didn't work. So I tried this.
                  If it produced a diagnostic that you don't understand, it did not
                  compile cleanly.
                  >
                  >if((fputc(a,fp ))==NULL)
                  >puts("fputc error");
                  >
                  >When I ran the binary fputc error was mentioned 10 times. My 2nd arg was 10.
                  Your 2nd arg of what? Not fputc, the 2nd arg there is fp, not 10. Not
                  puts, it only has one arg.
                  >
                  >Bill
                  >

                  Remove del for email

                  Comment

                  • Bill Cunningham

                    #24
                    Re: function


                    "CBFalconer " <cbfalconer@yah oo.comwrote in message
                    news:4841F7D1.2 A144B83@yahoo.c om...
                    The DJGPP compiler IS gcc.
                    >
                    Oh yes sorry it is. I mean my windows environment compiler not my linux
                    gcc which I use mostly.

                    Bill


                    Comment

                    • Joachim Schmitz

                      #25
                      Re: function

                      Barry Schwarz wrote:
                      On Sat, 31 May 2008 16:16:25 GMT, "Bill Cunningham" <nospam@nspam.c om>
                      wrote:
                      <snip>
                      >>
                      >if((fputc(a,fp ))==NULL)
                      >puts("fputc error");
                      >>
                      >When I ran the binary fputc error was mentioned 10 times. My 2nd arg
                      >was 10.
                      >
                      Your 2nd arg of what? Not fputc, the 2nd arg there is fp, not 10. Not
                      puts, it only has one arg.
                      2nd arg to his program, AKA argv[2], obviously (if you look upthread), the
                      number of null bytes to writen to a file (who's name is given in argv[1])

                      Bye, JOjo


                      Comment

                      • Richard

                        #26
                        Re: function

                        "Bill Cunningham" <nospam@nspam.c omwrites:
                        "santosh" <santosh.k83@gm ail.comwrote in message
                        news:g1s7f0$1ei $1@registered.m otzarella.org.. .
                        >
                        >Not necessarily. It's just a byte with value zero.
                        >>
                        I wrote this program in my djgpp compile not my gcc one and got the
                        ascii value of 48.
                        >
                        printf("%i\n",' 0');
                        >
                        Bill
                        Come on. Please stop trolling.

                        Comment

                        Working...