473,799 Members | 3,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A very **very** basic question

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.


Sep 25 '08 #1
56 2667
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.
Sep 25 '08 #2
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 '08 #3
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
Sep 25 '08 #4
In article <fe************ *************** *******@v13g200 0pro.googlegrou ps.com>,
mdh <md**@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.
Sep 25 '08 #5
Richard wrote:
vi******@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.
Sep 25 '08 #6
mdh <md**@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) ks***@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"
Sep 25 '08 #7
mdh
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. netwrote:
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
Sep 25 '08 #8
mdh
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.
Sep 25 '08 #9
Richard<rg****@ gmail.comwrites :
vi******@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) ks***@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"
Sep 25 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3285
by: Tom | last post by:
Hey all, I've been planning to get myself started with DocBook for quite some time now, so when I unexpectedly encountered a task for which DocBook might actually be very useful, I thought I'd no longer wait. Some Googling pointed me to several beginner tutorials, and I chose to get myself going with the guide at http://rzserv2.fhnon.de/%7Elg002556/docbuch/
30
2733
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
5
5715
by: Lee David | last post by:
I went to the sun site and downloaded what I hope is the development part of java. I downloaded JDK5 with Netbeans. I installed it and now have a folder in my program group "Netbeans". Is that java? Would I execute that to create a java application? TIA, Lee
10
1870
by: Jason Curl | last post by:
Greetings, I have an array of 32 values. This makes it extremely fast to access elements in this array based on an index provided by a separate enum. This array is defined of type "unsigned long int". I have a typedef for this: typedef unsigned long int Uint32; typedef float Float32; Uint32 myArray;
6
1761
by: msnews.microsoft.com | last post by:
Hello All, I am very new to ASP.NET and I have a basic question. Can somebody please explain? I have an .aspx Web Page with a textbox control. When the Page initially loads I am calling a Javascript function to write a text information in the text box.
8
1645
by: pamelafluente | last post by:
Hi, I would like to get some advice. I know enough vb.net on win apps, but I have never worked on web applications and asp.net. I would appreciate if you could indicate the most basic tutorials where to start my journey with ASp net.
6
1247
by: aghazalp | last post by:
hi guys, this would be the most basic question ever...I am not a programmer but I am trying to learn programming in python...I was reading John Zelle's text book and instructed me to make .py file and save it on the desk top...then it said close the python GUI and double click on the icon of the I just made and that should run the program...well, the good news is that it does but when I input a number for calculation and press the enter...
17
2419
by: blueapricot416 | last post by:
This is a very basic question -- but I can't find the answer after looking for 20 minutes. If you code something like: function set_It() { setTimeout('Request_Complete("apple", -72)',5000) } and call it 50 times, will it cause problems because you are not
7
1291
by: Bruno43 | last post by:
Hi I am trying to learn Visual Basic and I am getting everything for the most part, mainly because of my ability to read code like a book, but my question is what is the best way to Navigate through a Visual Basic program. For example lets say I have a big Visual Basic Form1.vb and on Form1.vb there are buttons and I click on this button, now the only thing I want to change would be a "content" area, I want the navigation to be the same. ...
0
10485
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10252
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10231
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9073
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.