what is difference between sizeof and strlen

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

    #16
    Re: what is difference between sizeof and strlen

    RAJU wrote on 05/08/05 :[color=blue]
    > First of all, sizeof() and strlen() are differnt. The sizeof() is an[/color]

    The is no sizeof() operator. It's sizeof. The () are around the types
    (like a typecast)

    int a;

    size_t n = sizeof a;

    n = sizeof (int);

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

    "Mal nommer les choses c'est ajouter du malheur au
    monde." -- Albert Camus.


    Comment

    • Emmanuel Delahaye

      #17
      Re: what is difference between sizeof and strlen

      (supersedes <mn.328e7d58e36 89128.15512@YOU RBRAnoos.fr>)

      RAJU wrote on 05/08/05 :[color=blue]
      > First of all, sizeof() and strlen() are differnt. The sizeof() is an[/color]

      There is no sizeof() operator. It's sizeof. The () are around the types
      (like a typecast)

      int a;

      size_t n = sizeof a;

      n = sizeof (int);


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

      "It's specified. But anyone who writes code like that should be
      transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC


      Comment

      • Emmanuel Delahaye

        #18
        Re: what is difference between sizeof and strlen

        rahul8143@gmail .com wrote on 05/08/05 :[color=blue][color=green][color=darkred]
        >>> void main()[/color][/color][/color]
        <...>
        [color=blue]
        > THANKS everybody i understood difference between sizeof operator and
        > strlen function.[/color]

        .... but you still don't understand the declaration of main()...

        The following are conforming to the C-language standard definition:

        int main ();
        int main (void);
        int main (int, char **);

        others are not standard and invokes an undefined behaviour.

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

        "Mal nommer les choses c'est ajouter du malheur au
        monde." -- Albert Camus.


        Comment

        • Emmanuel Delahaye

          #19
          Re: what is difference between sizeof and strlen

          (supersedes <mn.32927d58198 cd354.15512@YOU RBRAnoos.fr>)

          rahul8143@gmail .com wrote on 05/08/05 :[color=blue][color=green][color=darkred]
          >>> void main()[/color][/color][/color]
          <...>
          [color=blue]
          > THANKS everybody i understood difference between sizeof operator and
          > strlen function.[/color]

          .... but you still don't understand the declaration of main()...

          The following are conforming to the C-language standard definition:

          int main ();
          int main (void);
          int main (int, char **);

          others are not standard and invoke an undefined behaviour.


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

          "C is a sharp tool"


          Comment

          • Kenny McCormack

            #20
            Re: what is difference between sizeof and strlen

            In article <mn.32937d585f5 f7210.15512@YOU RBRAnoos.fr>,
            Emmanuel Delahaye <emdel@YOURBRAn oos.fr> wrote:[color=blue]
            >(supersedes <mn.32927d58198 cd354.15512@YOU RBRAnoos.fr>)
            >
            >rahul8143@gmai l.com wrote on 05/08/05 :[color=green][color=darkred]
            >>>> void main()[/color][/color]
            ><...>
            >[color=green]
            >> THANKS everybody i understood difference between sizeof operator and
            >> strlen function.[/color]
            >
            >... but you still don't understand the declaration of main()...
            >
            >The following are conforming to the C-language standard definition:
            >
            >int main ();
            >int main (void);
            >int main (int, char **);
            >
            >others are not standard and invoke an undefined behaviour.[/color]

            Wouldn't it be more accurate to say it invokes implementation defined
            behaviour?

            (Yes, I know the distinction may be moot in the context of this NG - the
            equivalent of discussing the difference between a class 2 mortal sin and
            a class 3 mortal sin in other religious contexts)

            Anyway, I still think declaring it as: double *main(double,st ruct stat)
            would be better for the newbies.

            Comment

            • pete

              #21
              Re: what is difference between sizeof and strlen

              Kenny McCormack wrote:[color=blue]
              >
              > In article <mn.32937d585f5 f7210.15512@YOU RBRAnoos.fr>,
              > Emmanuel Delahaye <emdel@YOURBRAn oos.fr> wrote:[color=green]
              > >(supersedes <mn.32927d58198 cd354.15512@YOU RBRAnoos.fr>)
              > >
              > >rahul8143@gmai l.com wrote on 05/08/05 :[color=darkred]
              > >>>> void main()[/color]
              > ><...>
              > >[color=darkred]
              > >> THANKS everybody i understood
              > >> difference between sizeof operator and
              > >> strlen function.[/color]
              > >
              > >... but you still don't understand the declaration of main()...
              > >
              > >The following are conforming to the C-language standard definition:
              > >
              > >int main ();
              > >int main (void);
              > >int main (int, char **);
              > >
              > >others are not standard and invoke an undefined behaviour.[/color]
              >
              > Wouldn't it be more accurate to say it invokes implementation defined
              > behaviour?[/color]

              No.
              Every implementation need not document how void main is handled.

              An example of implementation-defined behavior
              is the propagation of the high-order bit when a signed
              integer is shifted right.
              Every implementation must document how that is handled.

              --
              pete

              Comment

              • Keith Thompson

                #22
                Re: what is difference between sizeof and strlen

                gazelle@yin.int eraccess.com (Kenny McCormack) writes:[color=blue]
                > In article <mn.32937d585f5 f7210.15512@YOU RBRAnoos.fr>,
                > Emmanuel Delahaye <emdel@YOURBRAn oos.fr> wrote:[/color]
                [...][color=blue][color=green]
                >>The following are conforming to the C-language standard definition:
                >>
                >>int main ();
                >>int main (void);
                >>int main (int, char **);
                >>
                >>others are not standard and invoke an undefined behaviour.[/color]
                >
                > Wouldn't it be more accurate to say it invokes implementation defined
                > behaviour?[/color]

                Not really. The behavior of void main(), for example, is
                implementation defined only if the implementation documents it. If a
                non-standard declaration of main() is not specifically supported by
                the implementation, the behavior is undefined (nasal demons and all
                that).

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

                • svenpapst25@yahoo.de

                  #23
                  Re: what is difference between sizeof and strlen


                  Comment

                  • Anton Petrusevich

                    #24
                    Re: what is difference between sizeof and strlen

                    Martin Ambuhl wrote:
                    [color=blue][color=green]
                    >> void main()[/color]
                    > ^^^^
                    > Why do so many people post without
                    > c) learning the first thing about C from even the most basic textbook?[/color]

                    What if I do not care about the return value of the program? Or better, what
                    if I _do_ care but use exit(0)?
                    --
                    Anton Petrusevich

                    Comment

                    • Richard Bos

                      #25
                      Re: what is difference between sizeof and strlen

                      Anton Petrusevich <casus@att-ltd.biz> wrote:
                      [color=blue]
                      > Martin Ambuhl wrote:
                      >[color=green][color=darkred]
                      > >> void main()[/color]
                      > > ^^^^
                      > > Why do so many people post without
                      > > c) learning the first thing about C from even the most basic textbook?[/color]
                      >
                      > What if I do not care about the return value of the program? Or better, what
                      > if I _do_ care but use exit(0)?[/color]

                      Then you still haven't read the bleedin' FAQ.

                      Richard

                      Comment

                      • Martin Ambuhl

                        #26
                        Re: what is difference between sizeof and strlen

                        Anton Petrusevich wrote:[color=blue]
                        > Martin Ambuhl wrote:
                        >
                        >[color=green][color=darkred]
                        >>>void main()[/color]
                        >>
                        >> ^^^^
                        >>Why do so many people post without
                        >>c) learning the first thing about C from even the most basic textbook?[/color]
                        >
                        >
                        > What if I do not care about the return value of the program? Or better, what
                        > if I _do_ care but use exit(0)?[/color]

                        The only thing that you might care about that is relevant is whether you
                        care that you have a program which is legal C. Your lack of concern
                        about the return value is immaterial.

                        Comment

                        • Flash Gordon

                          #27
                          Re: what is difference between sizeof and strlen

                          Anton Petrusevich wrote:[color=blue]
                          > Martin Ambuhl wrote:
                          >[color=green][color=darkred]
                          >>>void main()[/color]
                          >>
                          >> ^^^^
                          >>Why do so many people post without
                          >>c) learning the first thing about C from even the most basic textbook?[/color]
                          >
                          > What if I do not care about the return value of the program? Or better, what
                          > if I _do_ care but use exit(0)?[/color]

                          Then you should still declare main as returning an int as that is the
                          only return type a hosted implementation is required to accept and
                          handle properly. Is it really so much work that declaring main properly
                          that you have to do something that might not work in some places instead?
                          --
                          Flash Gordon
                          Living in interesting times.
                          Although my email address says spam, it is real and I read it.

                          Comment

                          • Anton Petrusevich

                            #28
                            Re: what is difference between sizeof and strlen

                            Richard Bos wrote:
                            [color=blue][color=green]
                            >> What if I do not care about the return value of the program? Or better,
                            >> what if I _do_ care but use exit(0)?[/color]
                            > Then you still haven't read the bleedin' FAQ.[/color]

                            Now I did. I am using C since 1989, and I have never had "a different
                            function call/return sequence, incompatible with what the caller (in main's
                            case, the C run-time startup code) expects". I am writing "int main()" of
                            cource, but I would really appreciate if someone could point me out the
                            system/compilator with such a strange behaviour.
                            --
                            Anton Petrusevich

                            Comment

                            • Lawrence Kirby

                              #29
                              Re: what is difference between sizeof and strlen

                              On Mon, 08 Aug 2005 08:36:15 +0200, Anton Petrusevich wrote:
                              [color=blue]
                              > Martin Ambuhl wrote:
                              >[color=green][color=darkred]
                              >>> void main()[/color]
                              >> ^^^^
                              >> Why do so many people post without
                              >> c) learning the first thing about C from even the most basic textbook?[/color]
                              >
                              > What if I do not care about the return value of the program? Or better, what
                              > if I _do_ care but use exit(0)?[/color]

                              By writing void main() your program has undefined behaviour even if it
                              never returns or exits at all. There is no guarantee that will even be
                              called successfully at program startup - you havn't provided a function
                              with a type that is compatible with what the implementation expects to
                              call.

                              Lawrence

                              Comment

                              • Keith Thompson

                                #30
                                Re: what is difference between sizeof and strlen

                                Anton Petrusevich <casus@att-ltd.biz> writes:[color=blue]
                                > Richard Bos wrote:
                                >[color=green][color=darkred]
                                >>> What if I do not care about the return value of the program? Or better,
                                >>> what if I _do_ care but use exit(0)?[/color]
                                >> Then you still haven't read the bleedin' FAQ.[/color]
                                >
                                > Now I did. I am using C since 1989, and I have never had "a different
                                > function call/return sequence, incompatible with what the caller (in main's
                                > case, the C run-time startup code) expects". I am writing "int main()" of
                                > cource, but I would really appreciate if someone could point me out the
                                > system/compilator with such a strange behaviour.[/color]

                                Undefined behavior does not require any implementation to behave in
                                any particular manner. Even if every implementation in existence
                                handles void main() the way you expect it to, it's still undefined
                                behavior.

                                With void main() there's always the possibility that your program will
                                break under some future implementation, or even a current one. With
                                int main(void), you don't have to worry about that.

                                Even if the next platform you port your code to supports void main(),
                                you can't be sure that it does unless it's documented. When your
                                program misbehaves mysteriously, using int main(void) means there's
                                one less thing to worry about when you're tracking down the problem.

                                Some compilers issue a warning for void main().

                                With void main(), if you post your code here it's *guaranteed* that
                                people will jump down your throat about 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

                                Working...