A very **very** basic question

Discussion in 'C Programming' started by mdh, Sep 25, 2008.

  1. mdh

    mdh Guest

    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.
    mdh, Sep 25, 2008
    #1
    1. Advertising

  2. mdh

    Guest

    On Sep 25, 11:41 pm, mdh <> wrote:
    > 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.h> is 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.
    , Sep 25, 2008
    #2
    1. Advertising

  3. mdh

    Guest

    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.
    , Sep 25, 2008
    #3
  4. mdh

    Richard Guest

    writes:

    > On Sep 25, 11:41 pm, mdh <> wrote:
    >> 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.h> is not needed for this program.


    Petty and pedantic.

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


    Ext variable? I guess we could help or be obstructive. I will try the
    former since you have predictably done the latter.

    Ext is short for external I would think. And external variables are
    known in C.

    And yes this variable is indeed now available as an external variable to
    be referenced by external modules which might do "external int i;" in
    their headers in order to use your newly declared i.

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


    Apparently there are no such things as global variables in C. Heathfield
    told us. But yes, I agree with you here since its obvious what is meant.

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

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

    >
    >> 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?
    Richard, Sep 25, 2008
    #4
  5. In article <>,
    mdh <> wrote:

    >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.
    Richard Tobin, Sep 25, 2008
    #5
  6. mdh

    Guest

    Richard wrote:
    > writes:
    >
    > > On Sep 25, 11:41 pm, 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. 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 "initializing" 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.
    , Sep 25, 2008
    #6
  7. mdh <> writes:
    > 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_Keith) <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"
    Keith Thompson, Sep 25, 2008
    #7
  8. mdh

    Richard Guest

    writes:

    > Richard wrote:
    >> writes:
    >>
    >> > On Sep 25, 11:41 pm, 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. 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.
    Richard, Sep 25, 2008
    #8
  9. mdh

    mdh Guest

    On Sep 25, 2:03 pm, 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.
    mdh, Sep 25, 2008
    #9
  10. mdh

    mdh Guest

    On Sep 25, 2:22 pm, (Richard Tobin) wrote:
    > In article <..com>,
    >
    > mdh  <> wrote:
    > >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
    mdh, Sep 25, 2008
    #10
  11. mdh

    mdh Guest

    On Sep 25, 2:01 pm, Richard Heathfield <> wrote:
    > 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.
    mdh, Sep 25, 2008
    #11
  12. Richard<> writes:
    > writes:
    >> On Sep 25, 11:41 pm, 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. 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_Keith) <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"
    Keith Thompson, Sep 25, 2008
    #12
  13. mdh

    Richard Guest

    mdh <> writes:

    > On Sep 25, 2:01 pm, Richard Heathfield <> wrote:
    >> 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.
    Richard, Sep 25, 2008
    #13
  14. In article <gbh2kf$l4e$>,
    Richard <> wrote:

    >>> > 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
    implementational 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.
    Richard Tobin, Sep 25, 2008
    #14
  15. Richard<> wrote:
    > 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.h> code?

    --
    Peter
    Peter Nilsson, Sep 25, 2008
    #15
  16. mdh

    Richard Guest

    Peter Nilsson <> writes:

    > Richard<> wrote:
    >> 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.


    Wrong.

    Whichever way you look at it i is set to the value 0. In programming
    this is known as assignment. And I dont see any reason to confuse a
    noob by saying any different.

    Of course I am trying to use natural English and not be too clever for
    my own good.

    >
    >> I can also be done as
    >>
    >> int i =0; /* this is not in a function */

    >
    > /* Nor is it an assignment. */


    It is not in a function.

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


    And I wont be if it turns me into a robot totally unable to use common
    sense when describing something so simple as this.

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


    It is if you dont try to be overly clever and confuse people.

    >
    >> So "int i=3*4;" is not code?

    >
    > Is #include <stdio.h> code?


    Why do you ask?

    One more:

    So "int i=3*4;" is not code?
    Richard, Sep 25, 2008
    #16
  17. mdh

    Guest

    Richard wrote:
    > writes:
    >
    > > Richard wrote:
    > >> writes:
    > >>
    > >> > On Sep 25, 11:41 pm, 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. 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*


    The part you snipped gave the reason why pointing out that difference
    was not petty, but was in fact directly relevant to why yours was not
    an appropriate answer to the OP's question. If he hadn't known that
    "int i=0;" was a way to do this, why would he have included "int i=0;"
    in his question? He was clearly asking a question that he did not
    consider "int i=0;" to be an answer to.

    Assuming that he was paying more attention to the "petty" difference
    between initialization and assignment than you were makes his question
    reasonable. Assuming that he wasn't making that distinction makes his
    question idiotic. From what I've seen, mdh has a lot to learn, but
    he's no idiot. He's actually understanding and using some of the
    advice he's getting; a welcome change from several of our other recent
    visitors.
    , Sep 26, 2008
    #17
  18. Richard Heathfield wrote:
    > mdh said:

    [...]
    >> i=0;

    >
    > That's an assignment statement, which counts as code. You can't have code
    > outside a function.


    Wouldn't it be more accurate to say "you can't have *statements* outside
    a function?" To me, "code" could be anything.


    August
    August Karlstrom, Sep 26, 2008
    #18
  19. mdh

    Richard Guest

    writes:

    > Richard wrote:
    >> writes:
    >>
    >> > Richard wrote:
    >> >> writes:
    >> >>
    >> >> > On Sep 25, 11:41 pm, 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. 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*

    >
    > The part you snipped gave the reason why pointing out that difference
    > was not petty, but was in fact directly relevant to why yours was not
    > an appropriate answer to the OP's question. If he hadn't known that


    It was plenty appropriate.

    > "int i=0;" was a way to do this, why would he have included "int i=0;"
    > in his question? He was clearly asking a question that he did not
    > consider "int i=0;" to be an answer to.


    You have lost me. The question was very straightforward and very easily
    answered. And is a very common question from C beginners.

    >
    > Assuming that he was paying more attention to the "petty" difference
    > between initialization and assignment than you were makes his question


    It is a petty difference when teaching someone new.

    int i=0;

    You really want to tell me that in plain english 0 is not assigned to
    the global variable i at program initialisation?

    Really?

    You can wallow in your standard all you like, but this is starting to
    get ridiculous and certain posters here are making C almost
    impossible to understand to anyone without a degree in the standard vocab.

    > reasonable. Assuming that he wasn't making that distinction makes his
    > question idiotic. From what I've seen, mdh has a lot to learn, but
    > he's no idiot. He's actually understanding and using some of the
    > advice he's getting; a welcome change from several of our other recent
    > visitors.


    I have no idea what that last little soap box was about.

    The question was easily answered. Outside of a declaration you can not
    assign a value to a global variable unless you are in a function.

    You can fanny around and flap all you like about the use of the word
    assign here but its done me and thousands of others fine with zero
    negative impact or sudden desire to reassign outside of the declaration
    or a function.

    And if you try to tell me that

    int i=3*4;

    is NOT C code then please don your kevlar helmet ....
    Richard, Sep 26, 2008
    #19
  20. mdh

    Richard Guest

    August Karlstrom <> writes:

    > Richard Heathfield wrote:
    >> mdh said:

    > [...]
    >>> i=0;

    >>
    >> That's an assignment statement, which counts as code. You can't have
    >> code outside a function.

    >
    > Wouldn't it be more accurate to say "you can't have *statements*
    > outside a function?" To me, "code" could be anything.
    >
    >
    > August


    and "int i=0;" is not a statement now?
    Richard, Sep 26, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    456
    Raymond Hettinger
    Jul 27, 2003
  2. aghazalp

    very very basic question

    aghazalp, Apr 2, 2006, in forum: Python
    Replies:
    6
    Views:
    337
    aghazalp
    Apr 2, 2006
  3. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,597
    Merrill & Michele
    Oct 19, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    506
    Dave Thompson
    Feb 14, 2005
  5. Sunjay Varma
    Replies:
    4
    Views:
    333
    John Roth
    Mar 10, 2011
Loading...

Share This Page