A very **very** basic question

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

    A very **very** basic question

    As I begin to write more little programs without the help of the
    exercises, little things pop up that I need to understand more fully.
    Thus, below, and although this is not the exact code, the principle of
    the question is the same, ( I hope :-) )


    #include <stdio.h>
    int i = 0;
    int main () { return 0; } /* no errors or warnings*/

    but

    #include <stdio.h>
    int i ;
    i=0;
    int main () { return 0; } /* 2 warnings. */


    I think one of the regular contributors has previously alluded to this
    issue, but I wish to understand the principle more clearly.

    So, ???

    1) int i = 0 is allowed because i is declared and initialized as an
    ext variable.
    2) int i; i = 0 is not allowed because ?

    a) even though my intention is to assign '0' to i , this can only
    occur within a function?
    b) the compiler thinks I am once again declaring 'i', which has
    previously been declared, even though my **intent** is to initialize
    an external variable.

    I assume the same principles would apply if declared i as "static".

    What key principle am I missing.

    Thank you as usual.




  • vippstar@gmail.com

    #2
    Re: A very **very** basic question

    On Sep 25, 11:41 pm, mdh <m...@comcast.n etwrote:
    As I begin to write more little programs without the help of the
    exercises, little things pop up that I need to understand more fully.
    Thus, below, and although this is not the exact code, the principle of
    the question is the same, ( I hope :-) )
    >
    #include <stdio.h>
    int i = 0;
    int main () { return 0; } /* no errors or warnings*/
    Fine, but <stdio.his not needed for this program.
    >
    but
    >
    #include <stdio.h>
    int i ;
    i=0;
    int main () { return 0; } /* 2 warnings. */
    >
    I think one of the regular contributors has previously alluded to this
    issue, but I wish to understand the principle more clearly.
    >
    So, ???
    >
    1) int i = 0 is allowed because i is declared and initialized as an
    ext variable.
    Ext variable? No, whatever that means. int i = 0; is allowed because
    you are allowed to declare and initialize objects outside of any
    function. That makes the object global (it makes the identifier (the
    name) global actually).
    2) int i; i = 0 is not allowed because ?
    >
    a) even though my intention is to assign '0' to i , this can only
    occur within a function?
    yes.
    b) the compiler thinks I am once again declaring 'i', which has
    previously been declared, even though my **intent** is to initialize
    an external variable.
    The compiler can think whatever he wants when you feed him C that's
    not valid.
    I assume the same principles would apply if declared i as "static".
    >
    What key principle am I missing.
    Code can only be inside functions. It's possible to declare and
    initialize global variables.

    Comment

    • jameskuyper@verizon.net

      #3
      Re: A very **very** basic question

      mdh wrote:
      ....
      #include <stdio.h>
      int i = 0;
      int main () { return 0; } /* no errors or warnings*/
      >
      but
      >
      #include <stdio.h>
      int i ;
      i=0;
      int main () { return 0; } /* 2 warnings. */
      >
      >
      I think one of the regular contributors has previously alluded to this
      issue, but I wish to understand the principle more clearly.
      >
      So, ???
      >
      1) int i = 0 is allowed because i is declared and initialized as an
      ext variable.
      It is allowed because it qualifies as an external declaration of the
      identifier 'i'. An external declaration is an ordinary declaration or
      a function definition. At the highest level, a C translation unit
      consists of a series of external declarations.
      2) int i; i = 0 is not allowed because ?
      Because that is the combination of an external declaration and an
      expression-statement. Statements may only appear in compound-
      statements. A compound statement starts with a '{' and ends with a '}'
      and may only occur within or as the body of a function definition.

      Comment

      • Richard Heathfield

        #4
        Re: A very **very** basic question

        mdh said:
        As I begin to write more little programs without the help of the
        exercises, little things pop up that I need to understand more fully.
        Thus, below, and although this is not the exact code, the principle of
        the question is the same, ( I hope :-) )
        >
        >
        #include <stdio.h>
        int i = 0;
        int main () { return 0; } /* no errors or warnings*/
        >
        but
        >
        #include <stdio.h>
        int i ;
        That's a declaration, and it's fine. It's also a tentative definition,
        which is also fine.
        i=0;
        That's an assignment statement, which counts as code. You can't have code
        outside a function.

        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -http://www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999

        Comment

        • Richard Tobin

          #5
          Re: A very **very** basic question

          In article <fe51e5df-0862-41b6-9fda-d7cf9b482a98@v1 3g2000pro.googl egroups.com>,
          mdh <mdeh@comcast.n etwrote:
          >int i = 0;
          >int main () { return 0; } /* no errors or warnings*/
          >int i ;
          >i=0;
          >int main () { return 0; } /* 2 warnings. */
          You can declare variables outside functions, and you can initialise
          them, but you can't have ordinary statements like assignments. Even
          though "int i = 0;" looks like a declaration and an assignment, it's
          not.

          -- Richard
          --
          Please remember to mention me / in tapes you leave behind.

          Comment

          • jameskuyper@verizon.net

            #6
            Re: A very **very** basic question

            Richard wrote:
            vippstar@gmail. com writes:
            >
            On Sep 25, 11:41 pm, mdh <m...@comcast.n etwrote:
            ....
            2) int i; i = 0 is not allowed because ?
            >
            a) even though my intention is to assign '0' to i , this can only
            occur within a function?
            yes.
            >
            Wrong. I can also be done as
            >
            int i =0; /* this is not in a function */
            That's an initialization, not an assignment. This might seem like a
            petty quibble over a minor distinction. However, the OP made it quite
            clear that he already knew that "int i=0;" was permitted. In that
            context, his question makes sense only if he did actually mean to make
            precisely that distinction. Unfortunately, he muddies the waters later
            on by referring to the assignment statement as "initializi ng" i. He's
            a newbie; confusion about the jargon is to be expected. You and
            vippstar have less of an excuse.

            ....
            Code can only be inside functions. It's possible to declare and
            initialize global variables.
            >
            Code can only be inside functions?
            >
            So "int i=3*4;" is not code?
            This is what happens when people try to avoid using the jargon. The
            correct rule is that statements (not "code") can only occur within
            functions. "int i = 3*4;" is a declaration, not a statement.

            Comment

            • Keith Thompson

              #7
              Re: A very **very** basic question

              mdh <mdeh@comcast.n etwrites:
              As I begin to write more little programs without the help of the
              exercises, little things pop up that I need to understand more fully.
              Thus, below, and although this is not the exact code, the principle of
              the question is the same, ( I hope :-) )
              >
              >
              #include <stdio.h>
              int i = 0;
              int main () { return 0; } /* no errors or warnings*/
              >
              but
              >
              #include <stdio.h>
              int i ;
              i=0;
              int main () { return 0; } /* 2 warnings. */
              Others have covered most of this, but I'll jump in anyway.

              ``i=0;'' is a statement. A statement can appear only within a
              function definition. That's not just a constraint, it's a syntax
              rule, which means that violating it is likely to confuse the
              compiler's parser. Since the compiler isn't expecting to see a
              statement at that point, it's going to tell you something like "parse
              error at line blah", or, as I just saw with gcc, "warning: data
              definition has no type or storage class". It just doesn't occur to
              the compiler to even try to interpret it as a statement.

              This is a common problem with C: the grammar is, um, "brittle".
              Syntax errors very commonly result in something that looks very much
              like some *other* syntactically valid construct, especially if the
              parser is designed to deal with obsolete forms. In early (pre-ANSI)
              versions of C, this line:
              i = 0;
              outside a function was actually legal; it was equivalent to
              int i = 0;
              but with the type being implicit.

              So, rather than treating it as a statement and then complaining that a
              statement isn't allowed in that context, gcc treats it as a
              declaration and then complains that it's an invalid form of
              declaration because of the missing type.

              As for why statements outside functions aren't allowed, consider this.
              Program execution starts with a call to the function called "main".
              If your "i = 0;" were allowed as a statement, when would it be
              executed?

              If your C compiler reports a syntax error, even in a case like this
              where it doesn't call it a syntax error, it's often best to ignore the
              wording of the error message, look at the line (and possibly the
              previous line), figure out for yourself how you've violated the syntax
              rules, fix it, and recompile. (That's a bit of an overstatement; the
              error message itself *can* be meaningful.)

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

              • Richard

                #8
                Re: A very **very** basic question

                jameskuyper@ver izon.net writes:
                Richard wrote:
                >vippstar@gmail. com writes:
                >>
                On Sep 25, 11:41 pm, mdh <m...@comcast.n etwrote:
                ...
                >2) int i; i = 0 is not allowed because ?
                >>
                >a) even though my intention is to assign '0' to i , this can only
                >occur within a function?
                >
                yes.
                >>
                >Wrong. I can also be done as
                >>
                >int i =0; /* this is not in a function */
                >
                That's an initialization, not an assignment. This might seem like a
                Sigh. The initialisation assigns the value 0 to i. Why be so silly and
                petty?

                *snip*

                Why go through so many complexities to explain trivial things? Stop the
                language lawyering please. The above confuses no one.

                I know loads of C programmers who would say that and none of them
                suddenly think they can re-assign without the declaration. Not one.

                Comment

                • mdh

                  #9
                  Re: A very **very** basic question

                  On Sep 25, 2:03 pm, jameskuy...@ver izon.net wrote:
                  mdh wrote:
                  >
                  2) int i; i = 0 is not allowed because ?
                  >
                  Because that is the combination of an external declaration and an
                  expression-statement. Statements may only appear in compound-
                  statements. A compound statement starts with a '{' and ends with a '}'
                  and may only occur within or as the body of a function definition.

                  That's what I was missing. Thanks.

                  Comment

                  • mdh

                    #10
                    Re: A very **very** basic question

                    On Sep 25, 2:22 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
                    In article <fe51e5df-0862-41b6-9fda-d7cf9b482...@v1 3g2000pro.googl egroups..com>,
                    >
                    mdh  <m...@comcast.n etwrote:
                    int i = 0;
                    int main () { return 0; }  /* no errors or warnings*/
                    int i ;
                    i=0;
                    int main () { return 0; }  /* 2 warnings. */
                    >
                    You can declare variables outside functions, and you can initialise
                    them, but you can't have ordinary statements like assignments.  Even
                    though "int i = 0;" looks like a declaration and an assignment, it's
                    not.



                    thank you Richard

                    Comment

                    • mdh

                      #11
                      Re: A very **very** basic question

                      On Sep 25, 2:01 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                      mdh said:
                      >
                      >
                      That's a declaration, and it's fine. It's also a tentative definition,
                      which is also fine.
                      >
                      i=0;
                      >
                      That's an assignment statement, which counts as code. You can't have code
                      outside a function.
                      >

                      Thank you Richard.

                      Comment

                      • Keith Thompson

                        #12
                        Re: A very **very** basic question

                        Richard<rgrdev@ gmail.comwrites :
                        vippstar@gmail. com writes:
                        >On Sep 25, 11:41 pm, mdh <m...@comcast.n etwrote:
                        [...]
                        >>2) int i; i = 0 is not allowed because ?
                        >>>
                        >>a) even though my intention is to assign '0' to i , this can only
                        >>occur within a function?
                        >>
                        >yes.
                        >
                        Wrong. I can also be done as
                        >
                        int i =0; /* this is not in a function */
                        No, that's an initialization, not an assignment. They do essentially
                        the same thing, at least in this case, but they're different
                        constructs. You're blurring the distinction in a case where it's
                        extremely relevant to the point being discussed.

                        An assignment is an expression, which can be part of a statement,
                        which can only appear inside a function definition. An initialization
                        can be part of a declaration, which can appear either inside or
                        outside any function definition.

                        (And an assignment can appear as part of an initialization expression,
                        such as:
                        int i;
                        int j = (i = 0);
                        but (a) I'd consider that poor style, and (b) that particular
                        initialization can't be used outside a function definition because the
                        expression isn't constant.)
                        >>b) the compiler thinks I am once again declaring 'i', which has
                        >>previously been declared, even though my **intent** is to initialize
                        >>an external variable.
                        >>
                        >The compiler can think whatever he wants when you feed him C that's
                        >not valid.
                        >
                        (Petty, obstructive and generally negative reply again from you)
                        >
                        No the compiler does not think you are declaring i again. What you did
                        is simply not valid C with the "i=0;" outside of a declaration or a
                        function.
                        Yes, the compiler very likely *did* think he was declaring i again.
                        For example, here's the message I got from gcc:
                        warning: data definition has no type or storage class
                        or, with stricter flags:
                        error: ISO C forbids data definition with no type or storage class
                        >>I assume the same principles would apply if declared i as "static".
                        >>>
                        >>What key principle am I missing.
                        >>
                        >Code can only be inside functions. It's possible to declare and
                        >initialize global variables.
                        >
                        Code can only be inside functions?
                        >
                        So "int i=3*4;" is not code?
                        Obviously vippstar meant that statements can only be inside functions.
                        (I think you knew what he meant.)

                        Personally, I agree that "int i=3*4;" is "code", but the standard
                        doesn't define the term, and in fact the (non-normative) C99 Foreword
                        uses the word "code" to refer to statements; in the list of changes
                        from C90 to C99, it includes "mixed declarations and code". The
                        standard uses the term elsewhere in ways that seem to refer to more
                        than just statements.

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

                        • Richard

                          #13
                          Re: A very **very** basic question

                          mdh <mdeh@comcast.n etwrites:
                          On Sep 25, 2:01 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                          >mdh said:
                          >>
                          >>
                          >That's a declaration, and it's fine. It's also a tentative definition,
                          >which is also fine.
                          >>
                          i=0;
                          >>
                          >That's an assignment statement, which counts as code. You can't have code
                          >outside a function.
                          >>
                          >
                          >
                          Thank you Richard.
                          I would disagree. But I would wouldn't I?

                          int i=0;

                          is code.

                          Comment

                          • Richard Tobin

                            #14
                            Re: A very **very** basic question

                            In article <gbh2kf$l4e$1@r egistered.motza rella.org>,
                            Richard <rgrdev@gmail.c omwrote:
                            >i=0;
                            >>>
                            >>That's an assignment statement, which counts as code. You can't have code
                            >>outside a function.
                            >int i=0;
                            >
                            >is code.
                            It is in one sense of code. But the distinction reflects the
                            implementationa l difference between things that naturally happen at
                            run-time and things that can obviously be done at compile time. (Note
                            "naturally" and "obviously" - I'm not suggesting that it couldn't be
                            made to work.)

                            -- Richard
                            --
                            Please remember to mention me / in tapes you leave behind.

                            Comment

                            • Peter Nilsson

                              #15
                              Re: A very **very** basic question

                              Richard<rgr...@ gmail.comwrote:
                              vipps...@gmail. com writes:
                              mdh wrote:
                              2) int i; i = 0 is not allowed because ?
                              >
                              a) even though my intention is to assign '0' to i ,
                              this can only occur within a function?
                              yes.
                              >
                              Wrong.
                              No, it isn't.
                              I can also be done as
                              >
                              int i =0; /* this is not in a function */
                              /* Nor is it an assignment. */

                              You're welcome to point out any section of the standard
                              which says it is. Of course you've already told us that
                              you find the thought of reading it too daunting.
                              Code can only be inside functions. It's possible to
                              declare and initialize global variables.
                              >
                              Code can only be inside functions?
                              Yes, if by code he means it to mean whatever he means it
                              to mean. Like 'global', the term isn't as obvious as
                              some people think.
                              So "int i=3*4;" is not code?
                              Is #include <stdio.hcode?

                              --
                              Peter

                              Comment

                              Working...