473,809 Members | 2,617 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
56 2670
ja*********@ver izon.net writes:
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
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.
Sep 25 '08 #11
mdh
On Sep 25, 2:03*pm, jameskuy...@ver izon.net 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.

Sep 25 '08 #12
mdh <md**@comcast.n etwrites:
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.
I would disagree. But I would wouldn't I?

int i=0;

is code.
Sep 25 '08 #13
In article <gb**********@r egistered.motza rella.org>,
Richard <rg****@gmail.c omwrote:
>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
implementationa l 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.
Sep 25 '08 #14
Richard<rgr...@ gmail.comwrote:
vipps...@gmail. com 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.hcode?

--
Peter
Sep 25 '08 #15
Peter Nilsson <ai***@acay.com .auwrites:
Richard<rgr...@ gmail.comwrote:
>vipps...@gmail .com 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.hcode?
Why do you ask?

One more:

So "int i=3*4;" is not code?
Sep 25 '08 #16
Richard wrote:
ja*********@ver izon.net writes:
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

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 25 '08 #17
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
Sep 25 '08 #18
ja*********@ver izon.net writes:
Richard wrote:
>ja*********@ver izon.net writes:
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

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 ....
Sep 25 '08 #19
August Karlstrom <fu********@gma il.comwrites:
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?

Sep 25 '08 #20

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
2734
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
5
5718
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
1872
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
1647
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
2422
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
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
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...
0
10120
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
6881
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
5550
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...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3015
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.