A very **very** basic question

M

mdh

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

vippstar

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 said:
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.
 
J

jameskuyper

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

Richard

Fine, but <stdio.h> is not needed for this program.

Petty and pedantic.
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.

Wrong. I can also be done as

int i =0; /* this is not in a function */
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.
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?
 
R

Richard Tobin

mdh said:
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
 
J

jameskuyper

Richard said:
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?

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

Keith Thompson

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

Richard

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

mdh

mdh wrote:

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

mdh

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
 
M

mdh

mdh said:


That's a declaration, and it's fine. It's also a tentative definition,
which is also fine.


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


Thank you Richard.
 
K

Keith Thompson

Richard said:
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.)
(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
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.
 
R

Richard Tobin

[/QUOTE]
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
 
P

Peter Nilsson

Richard said:

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?

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

Richard

Peter Nilsson said:
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.
/* 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.
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.
Is #include <stdio.h> code?

Why do you ask?

One more:

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

jameskuyper

Richard said:
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.
 
A

August Karlstrom

Richard said:
mdh said: [...]

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
 
R

Richard

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

Richard

August Karlstrom said:
Richard said:
mdh said: [...]

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top