Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > A very **very** basic question

Reply
Thread Tools

A very **very** basic question

 
 
mdh
Guest
Posts: n/a
 
      09-25-2008
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.




 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      09-25-2008
On Sep 25, 11:41 pm, mdh <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      09-25-2008
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.
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      09-25-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> On Sep 25, 11:41 pm, mdh <(E-Mail Removed)> 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?

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      09-25-2008
In article <(E-Mail Removed)>,
mdh <(E-Mail Removed)> 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.
 
Reply With Quote
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      09-25-2008
Richard wrote:
> (E-Mail Removed) writes:
>
> > On Sep 25, 11:41 pm, mdh <(E-Mail Removed)> 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-25-2008
mdh <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      09-25-2008
(E-Mail Removed) writes:

> Richard wrote:
>> (E-Mail Removed) writes:
>>
>> > On Sep 25, 11:41 pm, mdh <(E-Mail Removed)> 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.
 
Reply With Quote
 
mdh
Guest
Posts: n/a
 
      09-25-2008
On Sep 25, 2:03*pm, (E-Mail Removed) 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.

 
Reply With Quote
 
mdh
Guest
Posts: n/a
 
      09-25-2008
On Sep 25, 2:22*pm, (E-Mail Removed) (Richard Tobin) wrote:
> In article <(E-Mail Removed)..com>,
>
> mdh *<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I found some very odd behaviour in Python's very basic types Sunjay Varma Python 4 03-10-2011 05:05 PM
very very basic question aghazalp Python 6 04-02-2006 09:35 PM
Very very very basic question Peter C Programming 14 02-14-2005 09:46 AM
Quick Book file access very very very slow Thomas Reed Computer Support 7 04-09-2004 08:09 PM
very Very VERY dumb Question About The new Set( ) 's Raymond Arthur St. Marie II of III Python 4 07-27-2003 12:09 AM



Advertisments