473,809 Members | 2,575 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
Richard<rg****@ gmail.comwrites :
Peter Nilsson <ai***@acay.com .auwrites:
>Richard<rgr... @gmail.comwrote :
>>vipps...@gmai l.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.
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.

--
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 #21
Richard wrote:
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?
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
Sep 25 '08 #22
Richard<rg****@ gmail.comwrites :
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.

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.

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

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

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

Sep 26 '08 #29

"Richard" <rg****@gmail.c omwrote in message
news:gb******** **@registered.m otzarella.org.. .
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.
--
Bartc

Sep 26 '08 #30

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
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
10640
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...
1
10387
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
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.