A very **very** basic question

K

Keith Thompson

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

The C language distinguishes between "initialization" and
"assignment". I apparently have a higher opinion of mdh's
intelligence than you do; I don't think he'll have any trouble
understanding the distinction.

If you call them both "assignment", it just makes it more difficult to
explain why one is allowed outside functions and the other is not --
which is exactly what we're discussing in this thread.

We didn't just make up these terms for the fun of it, and they
certainly weren't invented with the intention of confusing people.
 
A

August Karlstrom

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

It is a declaration as far as I am concerned. If I'm not mistaken a
global declaration like

int i = 0;

does not make the compiler generate instructions for an assignment, the
compiler just makes sure the initial value of i is zero.


August
 
K

Keith Thompson

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

and "int i=0;" is not a statement now?

No, it's not a statement. It's a declaration.

Words have meanings. If you don't know what a word means, look it up.
 
A

August Karlstrom

Richard wrote:
[...]
Whichever way you look at it i is set to the value 0. In programming
this is known as assignment.

Believe it or not but some programming languages don't even have assignment.


August
 
R

Richard

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

It is a declaration as far as I am concerned. If I'm not mistaken a
global declaration like

int i = 0;

does not make the compiler generate instructions for an assignment,
the compiler just makes sure the initial value of i is zero.

The workings of the underlying compiler are immaterial.

--
 
R

Richard

August Karlstrom said:
Richard wrote:
[...]
Whichever way you look at it i is set to the value 0. In programming
this is known as assignment.

Believe it or not but some programming languages don't even have assignment.


August

That nice. And I have no reason to disbelieve you. But what that has to
do with this I dont know. Would you like me to list some 68k or Z80 op
codes? But in the context of C it takes a very special person to "not
understand" when I say:

"Whichever way you look at it i is set to the value 0. In programming
this is known as assignment."

or

"At program initialisation the global variable i is assigned the value
0"

I'm not arguing against the standard. I'm not arguing about being
correct. I am arguing for some common sense here. using this word does
not suddenly make some program C "wrong". Hell it wont even compile to
"assign to i" outside of the declaration or the function for crying out
loud.
 
A

August Karlstrom

Richard said:
August Karlstrom said:
Richard wrote: [...]
and "int i=0;" is not a statement now?
It is a declaration as far as I am concerned. If I'm not mistaken a
global declaration like

int i = 0;

does not make the compiler generate instructions for an assignment,
the compiler just makes sure the initial value of i is zero.

The workings of the underlying compiler are immaterial.

Just trying to explain the difference between a declaration and a
statement. How a (typical) compiler deals with the two is a part of the
explanation.


August
 
A

August Karlstrom

Richard said:
August Karlstrom said:
Richard wrote:
[...]
Whichever way you look at it i is set to the value 0. In programming
this is known as assignment.
Believe it or not but some programming languages don't even have assignment.


August

That nice. And I have no reason to disbelieve you. But what that has to
do with this I dont know.

There is a fundamental difference between assignment and [compile time]
initialization (as I tried to explain in a different posting in this
thread).


August
 
R

Richard

August Karlstrom said:
Richard said:
August Karlstrom said:
Richard wrote:
[...]
Whichever way you look at it i is set to the value 0. In programming
this is known as assignment.
Believe it or not but some programming languages don't even have assignment.


August

That nice. And I have no reason to disbelieve you. But what that has to
do with this I dont know.

There is a fundamental difference between assignment and [compile
time] initialization (as I tried to explain in a different posting in
this thread).


August

You snipped my "plain english" explanation for some reason.

Where I suggested that saying that "At program initialisation the global variable i is assigned the value
0" is perfectly clear.

Tell me, hand on your heart, are you really trying to tell that makes
"no sense" when explaining to a new programmer what "int i=0;" means
outside of a function?
 
K

Kenny McCormack

does not make the compiler generate instructions for an assignment,
the compiler just makes sure the initial value of i is zero.

The workings of the underlying compiler are immaterial.[/QUOTE]

See how it works? When you speak in terms of a specific implementation,
they go all theoretical - all "You can't say that - we don't talk about
implementaions here - we only talk about things that are universally
true in all possible universes" - on your ass.

When *you* speak in general terms - speaking in terms of an abstract
machine (as evidenced by your comment above - The workings of the
underlying compiler are immaterial), then they go "The compiler will
generate code that consists of the following assembler instructions,
arranged exactly like this".

You can't win.
 
R

Richard

The workings of the underlying compiler are immaterial.

See how it works? When you speak in terms of a specific implementation,
they go all theoretical - all "You can't say that - we don't talk about
implementaions here - we only talk about things that are universally
true in all possible universes" - on your ass.

When *you* speak in general terms - speaking in terms of an abstract
machine (as evidenced by your comment above - The workings of the
underlying compiler are immaterial), then they go "The compiler will
generate code that consists of the following assembler instructions,
arranged exactly like this".

You can't win.
[/QUOTE]

Their desire to exaggerate their own knowledge of the standard is simply
ridiculous. Fine when it comes to real world programming errors (I
acknowledge my e-- error for example): but playing ridiculous word games
in an "English language explanation" is simply nuts.

How the hell is "int i=0;" not code?!?!?!

Why is it so "bad" to say 0 is assigned to i at program initialisation?

I dont need to back my own abilities or background when it comes to
teaching tech issues to new programmers. I do know for sure, however,
that a few of the posters here had better to hell not go anywhere near a
lecture podium or half the auditorium will be asleep within a minute. As
Ben B said in one of his posts there are times when it pays to allow a
bit of flexibility in the explanation/understanding and one can deal
with it a later date when the student is more comfortable.

The constant quest for one upsmanship here has totally polluted the
group.
 
B

Bartc

How the hell is "int i=0;" not code?!?!?!

Why is it so "bad" to say 0 is assigned to i at program initialisation?

It's not executable code (assuming i is outside a function or is static)

In fact int i=0 may not even reserve any actual space in the executable,
while int i=1 probably would.

Writing the explicit assignment i=0 however, would probably generate some
executable code.

Compilers of course can implement these as they like, but I think the
behaviour above is typical.
 
K

Keith Thompson

August Karlstrom said:
Richard said:
August Karlstrom said:
Richard wrote:
[...]
Whichever way you look at it i is set to the value 0. In programming
this is known as assignment.
Believe it or not but some programming languages don't even have
assignment.

August
That nice. And I have no reason to disbelieve you. But what that has
to
do with this I dont know.

There is a fundamental difference between assignment and [compile
time] initialization (as I tried to explain in a different posting in
this thread).

But the distinction between assignment and initialization isn't a
run-time vs. compile-time distinction.

For example:

#include <time.h>

time_t t = -1;
/* initialization, likely handled at compile time */

int main(void)
{
time_t now = time(NULL);
/* initialization, definitely handled at run time */

now = time(NULL);
/* assignment, behaves very similarly to the above
initialization. */

return 0;
}

Initialization and assignment are very similar. And occasionally
mixing up the terms doesn't even bother me that much. For example:

int x;
/* x is an uninitialized variable */
x = 42;
/* x is no longer an uninitialized variable */

or

int y = 43;
/* This initializer assigns the value 43 to y. */

*But* there is are differences between the two. For example, you can
initialize an array, but you can't assign to an array.

The most important difference between the two language constructs is
that an initializer is part of a *declaration*, and an assignment is
an *expression* which can be part of a *statement*. And that
distinction is exactly why you can have an initializer, but not an
assignment, outside any function -- which is exactly what the original
poster was asking about.

I think that by now everyone here understands the difference between
an initializer and an assignment, and the OP's question has been
answered. Can we drop this now?
 
M

mdh

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.


James...you are very kind...though at times my wife would attribute my
interest in C to nothing more than that!!! :)
 
M

mdh

The C language distinguishes between "initialization" and
"assignment".


Which was the key issue I was missing. Not sure if I missed this in
K&R ( Probably) and the importance of the location in which these are
allowed to occur, but it has come up a few times in my coding, so
wanted to clarify it.
We didn't just make up these terms for the fun of it,

....and in fact, as has been clearly imprinted on me ( as some of you
have helped me along this road) is important, which in fact, I find
adds to clarity.

Having said that, I am always grateful to any input and try and follow
closely the discussions that follow my questions.
 
C

CBFalconer

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;

This is a definition and initialization statement for a global.
int main () { return 0; } /* no errors or warnings*/

but

#include <stdio.h>
int i ;
i=0;

This is executable code.
int main () { return 0; } /* 2 warnings. */

You are not allowed to place executable code outside of a function.
 
L

lovecreatesbeauty

mdh said:

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

This isn't much presice.

Everything in a translation unit is code, right? A statement can't
appear in file scope is just because translation unit can only contain
declares or definitions as stated in sec 6.9 p1.
 
R

Richard

Bartc said:
It's not executable code (assuming i is outside a function or is
static)

Who said its executable code? It is C code. As defined by the C
language.
In fact int i=0 may not even reserve any actual space in the executable,
while int i=1 probably would.

Do explain the difference.....
Writing the explicit assignment i=0 however, would probably generate
some executable code.

Probably? And by "explicit" I assume you mean inside a function.

Or is "int i=0;" not "explicit"?
Compilers of course can implement these as they like, but I think the
behaviour above is typical.

It really is not the point when using the English language.
 
R

Richard

Yes. But we are not compilers.

And to say "0 is assigned to global i at program initialization" is not
exactly going to break the bank.

*snip*
 
R

Richard

This isn't much presice.

Everything in a translation unit is code, right? A statement can't
appear in file scope is just because translation unit can only contain
declares or definitions as stated in sec 6.9 p1.

They are being typically difficult.

My favorite so far is

is "#include <stdio.h> code ?

It is of course. Since it contains/can contain all sorts of stuff
including inline function, defines, macros etc. And if thats not "C
code" then i dont know what it.

The language lawyers in this group will be the death of it.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top