Difference between various functions

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

    Difference between various functions

    Could somebody explain in simple forms, what is/are the difference(s) between

    - scanf and sscanf and ssscanf
    - printf and sprintf
    - open and fopen

    Thanks in advance,
    SDZ
  • Joona I Palaste

    #2
    Re: Difference between various functions

    SDZ <sainyuja@hotma il.com> scribbled the following:[color=blue]
    > Could somebody explain in simple forms, what is/are the difference(s) between[/color]
    [color=blue]
    > - scanf and sscanf and ssscanf[/color]

    scanf reads from stdin, sscanf from a string. ssscanf doesn't exist.
    [color=blue]
    > - printf and sprintf[/color]

    printf prints into stdout, sprintf into a string.
    [color=blue]
    > - open and fopen[/color]

    open doesn't exist. fopen opens a file.

    --
    /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "It was, er, quite bookish."
    - Horace Boothroyd

    Comment

    • Paulo PEREIRA

      #3
      Re: Difference between various functions

      On 2004-06-14, Joona I Palaste <palaste@cc.hel sinki.fi> wrote:[color=blue]
      > SDZ <sainyuja@hotma il.com> scribbled the following:[color=green]
      >> Could somebody explain in simple forms, what is/are the difference(s) between[/color]
      >[color=green]
      >> - scanf and sscanf and ssscanf[/color]
      >
      > scanf reads from stdin, sscanf from a string. ssscanf doesn't exist.
      >[color=green]
      >> - printf and sprintf[/color]
      >
      > printf prints into stdout, sprintf into a string.
      >[color=green]
      >> - open and fopen[/color]
      >
      > open doesn't exist. fopen opens a file.
      >[/color]

      open is an UNIX system call for opening files. Not related to ISO C.

      --
      "Computers are useless. They can only give you answers"
      - Pablo Picasso

      [paulo-pereira dot perso at wanadoo dot fr]

      Comment

      • Default User

        #4
        Re: Difference between various functions

        SDZ wrote:[color=blue]
        >
        > Could somebody explain in simple forms, what is/are the difference(s) between
        >
        > - scanf and sscanf and ssscanf
        > - printf and sprintf
        > - open and fopen[/color]


        What does your C textbook say? Learning C from random questions on
        usenet is a bad idea and no substitute for proper study. You are wasting
        your time and ours.




        Brian Rodenborn

        Comment

        • Nils O. Selåsdal

          #5
          Re: Difference between various functions

          On Mon, 14 Jun 2004 04:47:13 -0700, SDZ wrote:
          [color=blue]
          > Could somebody explain in simple forms, what is/are the difference(s) between
          >
          > - scanf and sscanf and ssscanf
          > - printf and sprintf
          > - open and fopen
          >
          > Thanks in advance,
          > SDZ[/color]
          int scanf(const char *format, ...);
          int sscanf(const char *str, const char *format, ...);
          (never heard of ssscanf)
          int printf(const char *format, ...);
          int sprintf(char *str, const char *format, ...);

          int open(const char *pathname, int flags);
          int open(const char *pathname, int flags, mode_t mode);
          FILE *fopen(const char *path, const char *mode);

          You can easily see some docs for these in section 2 and 3
          at e.g. http://netbsd.gw.com/cgi-bin/man-cgi

          Comment

          • Joona I Palaste

            #6
            Re: Difference between various functions

            Nils O. Selåsdal <NOS@utel.no> scribbled the following:[color=blue]
            > On Mon, 14 Jun 2004 04:47:13 -0700, SDZ wrote:[color=green]
            >> Could somebody explain in simple forms, what is/are the difference(s) between
            >> - open and fopen[/color][/color]
            [color=blue]
            > int open(const char *pathname, int flags);
            > int open(const char *pathname, int flags, mode_t mode);
            > FILE *fopen(const char *path, const char *mode);[/color]

            Wait, wait. Two distinct functions with the same name aren't possible in
            ISO-compliant C, even if they use different parameters. Does the POSIX
            standard actually require the compiler to use non-standard extensions?

            --
            /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
            \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
            "Normal is what everyone else is, and you're not."
            - Dr. Tolian Soran

            Comment

            • Dragan Cvetkovic

              #7
              Re: Difference between various functions

              Joona I Palaste <palaste@cc.hel sinki.fi> writes:
              [color=blue]
              > Nils O. Selåsdal <NOS@utel.no> scribbled the following:[color=green]
              >> On Mon, 14 Jun 2004 04:47:13 -0700, SDZ wrote:[color=darkred]
              >>> Could somebody explain in simple forms, what is/are the difference(s) between
              >>> - open and fopen[/color][/color]
              >[color=green]
              >> int open(const char *pathname, int flags);
              >> int open(const char *pathname, int flags, mode_t mode);[/color]
              >
              > Wait, wait. Two distinct functions with the same name aren't possible in
              > ISO-compliant C, even if they use different parameters. Does the POSIX
              > standard actually require the compiler to use non-standard extensions?[/color]

              Well, the actuall prototype should be

              int open(const char *, int, ...);

              or, as commonly used in documentation

              int open(const char *path, int oflag, /* mode_t mode */);

              and that C handles quite well using <stdarg.h> type of functions.

              The same way you handle fprintf(...) type of functions.

              Bye, Dragan

              --
              Dragan Cvetkovic,

              To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer

              !!! Sender/From address is bogus. Use reply-to one !!!

              Comment

              • Eric Sosman

                #8
                Re: Difference between various functions

                Joona I Palaste wrote:[color=blue]
                > Nils O. Selåsdal <NOS@utel.no> scribbled the following:
                > [color=green]
                >>On Mon, 14 Jun 2004 04:47:13 -0700, SDZ wrote:
                >>[color=darkred]
                >>>Could somebody explain in simple forms, what is/are the difference(s) between
                >>>- open and fopen[/color][/color]
                >
                > [color=green]
                >>int open(const char *pathname, int flags);
                >>int open(const char *pathname, int flags, mode_t mode);
                >>FILE *fopen(const char *path, const char *mode);[/color]
                >
                >
                > Wait, wait. Two distinct functions with the same name aren't possible in
                > ISO-compliant C, even if they use different parameters. Does the POSIX
                > standard actually require the compiler to use non-standard extensions?[/color]

                <OT> I think

                int open(const char *path, int flags, ...);

                is acceptable. </OT>

                --
                Eric.Sosman@sun .com

                Comment

                • Goran Larsson

                  #9
                  Re: Difference between various functions

                  In article <cal2es$cr9$1@o ravannahka.hels inki.fi>,
                  Joona I Palaste <palaste@cc.hel sinki.fi> wrote:
                  [color=blue]
                  > Nils O. Selåsdal <NOS@utel.no> scribbled the following:[color=green]
                  > > int open(const char *pathname, int flags);
                  > > int open(const char *pathname, int flags, mode_t mode);[/color][/color]
                  [color=blue]
                  > Wait, wait. Two distinct functions with the same name aren't possible in
                  > ISO-compliant C, even if they use different parameters.[/color]

                  It is possible if it can be implemented as a function with a variable
                  argument list, like this:

                  | #if defined(__STDC_ _)
                  |
                  | extern int fcntl(int, int, ...);
                  | extern int open(const char *, int, ...);

                  The open() function only needs to look at the third argument (mode) if
                  a certain bit (O_CREAT) is set in the second argument (flags).
                  [color=blue]
                  > Does the POSIX
                  > standard actually require the compiler to use non-standard extensions?[/color]

                  No non-standard extensions are required.

                  --
                  Göran Larsson http://www.mitt-eget.com/

                  Comment

                  • Ben Pfaff

                    #10
                    Re: Difference between various functions

                    Joona I Palaste <palaste@cc.hel sinki.fi> writes:
                    [color=blue]
                    > Nils O. Selåsdal <NOS@utel.no> scribbled the following:[color=green]
                    >> int open(const char *pathname, int flags);
                    >> int open(const char *pathname, int flags, mode_t mode);[/color]
                    >
                    > Wait, wait. Two distinct functions with the same name aren't possible in
                    > ISO-compliant C, even if they use different parameters. Does the POSIX
                    > standard actually require the compiler to use non-standard extensions?[/color]

                    No. Typically open() is actually prototyped as
                    int open(const char *, int, ...);
                    --
                    "In My Egotistical Opinion, most people's C programs should be indented six
                    feet downward and covered with dirt." -- Blair P. Houghton

                    Comment

                    • Barry Margolin

                      #11
                      Re: Difference between various functions

                      In article <cal2es$cr9$1@o ravannahka.hels inki.fi>,
                      Joona I Palaste <palaste@cc.hel sinki.fi> wrote:
                      [color=blue]
                      > Nils O. Selåsdal <NOS@utel.no> scribbled the following:[color=green]
                      > > On Mon, 14 Jun 2004 04:47:13 -0700, SDZ wrote:[color=darkred]
                      > >> Could somebody explain in simple forms, what is/are the difference(s)
                      > >> between
                      > >> - open and fopen[/color][/color]
                      >[color=green]
                      > > int open(const char *pathname, int flags);
                      > > int open(const char *pathname, int flags, mode_t mode);
                      > > FILE *fopen(const char *path, const char *mode);[/color]
                      >
                      > Wait, wait. Two distinct functions with the same name aren't possible in
                      > ISO-compliant C, even if they use different parameters. Does the POSIX
                      > standard actually require the compiler to use non-standard extensions?[/color]

                      No, they require support for varargs functions, which is standard. The
                      actual prototype is:

                      int open(const char *pathname, int flags, ...);

                      But the only valid ways to call it correspond to the two prototypes that
                      Nils showed.

                      --
                      Barry Margolin, barmar@alum.mit .edu
                      Arlington, MA
                      *** PLEASE post questions in newsgroups, not directly to me ***

                      Comment

                      • Joona I Palaste

                        #12
                        Re: Difference between various functions

                        Goran Larsson <hoh@invalid.in valid> scribbled the following
                        on comp.lang.c:[color=blue]
                        > In article <cal2es$cr9$1@o ravannahka.hels inki.fi>,
                        > Joona I Palaste <palaste@cc.hel sinki.fi> wrote:[/color]
                        [color=blue][color=green]
                        >> Nils O. Selåsdal <NOS@utel.no> scribbled the following:[color=darkred]
                        >> > int open(const char *pathname, int flags);
                        >> > int open(const char *pathname, int flags, mode_t mode);[/color][/color][/color]
                        [color=blue][color=green]
                        >> Wait, wait. Two distinct functions with the same name aren't possible in
                        >> ISO-compliant C, even if they use different parameters.[/color][/color]
                        [color=blue]
                        > It is possible if it can be implemented as a function with a variable
                        > argument list, like this:[/color]
                        [color=blue]
                        > | #if defined(__STDC_ _)
                        > |
                        > | extern int fcntl(int, int, ...);
                        > | extern int open(const char *, int, ...);[/color]
                        [color=blue]
                        > The open() function only needs to look at the third argument (mode) if
                        > a certain bit (O_CREAT) is set in the second argument (flags).[/color]
                        [color=blue][color=green]
                        >> Does the POSIX
                        >> standard actually require the compiler to use non-standard extensions?[/color][/color]
                        [color=blue]
                        > No non-standard extensions are required.[/color]

                        D'oh. I should have paid more attention to the parameter lists. Even
                        ISO standard C functions (printf, fprint, ...) work that way. Thanks to
                        all who replied.
                        One further question: Since AFAIK ISO standard C provides no way to
                        explicitly check the length of the varargs list, how does open() know
                        whether the "mode" argument was supplied or not? Or does it even need
                        to?

                        --
                        /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
                        \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
                        "I said 'play as you've never played before', not 'play as IF you've never
                        played before'!"
                        - Andy Capp

                        Comment

                        • Rich Teer

                          #13
                          Re: Difference between various functions

                          On Mon, 14 Jun 2004, Joona I Palaste wrote:
                          [color=blue]
                          > One further question: Since AFAIK ISO standard C provides no way to
                          > explicitly check the length of the varargs list, how does open() know
                          > whether the "mode" argument was supplied or not? Or does it even need
                          > to?[/color]

                          It doesn't need to know. The writers of the function quite
                          reasonably assume that if O_CREAT was specified, then the 3rd
                          argument was also passed. If it wasn't passed, whatever junk
                          is on the stack at that location will be used instead.

                          --
                          Rich Teer, SCNA, SCSA

                          President,
                          Rite Online Inc.

                          Voice: +1 (250) 979-1638
                          URL: http://www.rite-online.net

                          Comment

                          • Joona I Palaste

                            #14
                            Re: Difference between various functions

                            Rich Teer <rich.teer@ri te-group.com> scribbled the following
                            on comp.lang.c:[color=blue]
                            > On Mon, 14 Jun 2004, Joona I Palaste wrote:[color=green]
                            >> One further question: Since AFAIK ISO standard C provides no way to
                            >> explicitly check the length of the varargs list, how does open() know
                            >> whether the "mode" argument was supplied or not? Or does it even need
                            >> to?[/color][/color]
                            [color=blue]
                            > It doesn't need to know. The writers of the function quite
                            > reasonably assume that if O_CREAT was specified, then the 3rd
                            > argument was also passed. If it wasn't passed, whatever junk
                            > is on the stack at that location will be used instead.[/color]

                            All righty, thanks. I note that this behaviour is dependent on the
                            POSIX standard and the ISO C standard alone does not suffice to
                            guarantee it.

                            --
                            /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
                            \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
                            "Nothing lasts forever - so why not destroy it now?"
                            - Quake

                            Comment

                            • Goran Larsson

                              #15
                              Re: Difference between various functions

                              In article <Pine.SOL.4.58. 0406141512250.1 2483@zaphod.rit e-online.net>,
                              Rich Teer <rich.teer@ri te-group.com> wrote:
                              [color=blue]
                              > If it wasn't passed, whatever junk
                              > is on the stack at that location will be used instead.[/color]

                              | If it wasn't passed, whatever junk
                              | is in the implementation specific location[*] used to pass arguments
                              | will be used instead.
                              |
                              |[*] location includes memory, registers, disks, punched cards,
                              | mercury delay lines, ...

                              Writing about "the stack" in comp.lang.c is not recommended. :-/

                              --
                              Göran Larsson http://www.mitt-eget.com/

                              Comment

                              Working...