473,809 Members | 2,699 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
In article <Sb************ *******@text.ne ws.virginmedia. com>,
Bartc <bc@freeuk.comw rote:
....
>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.
But such speculation/discussion is, of course, off-topic, since we are
supposed to remain completely oblivious to such things (messy details).

Sep 26 '08 #51
On Sep 26, 8:37*pm, gaze...@shell.x mission.com (Kenny McCormack)
wrote:
In article <d8055424-e34e-4169-8429-d405fa01c...@b2 g2000prf.google groups.com>,

lovecreatesbea. ..@gmail.com <lovecreatesbea ...@gmail.comwr ote:
On Sep 26, 2:47 pm, Keith Thompson <ks...@mib.orgw rote:
Right, the standard forbids it because the standard forbids it. *But
the standard doesn't exist in a vacuum, and understanding *why* the
rules are as they are can be useful.
Yes :)

But (again), we're not allowed to think about such things ("why" questions)
here.
Why?

Sep 26 '08 #52
On 26 Sep, 10:01, Old Wolf <oldw...@inspir e.net.nzwrote:
On Sep 26, 8:37*pm, gaze...@shell.x mission.com (Kenny McCormack)
wrote:
In article <d8055424-e34e-4169-8429-d405fa01c...@b2 g2000prf.google groups.com>,
lovecreatesbea. ..@gmail.com <lovecreatesbea ...@gmail.comwr ote:
>On Sep 26, 2:47 pm, Keith Thompson <ks...@mib.orgw rote:
>Right, the standard forbids it because the standard forbids it. *But
>the standard doesn't exist in a vacuum, and understanding *why* the
>rules are as they are can be useful.
>Yes :)
But (again), we're not allowed to think about such things ("why" questions)
here.

Why?
Kenny is trolling

Sep 26 '08 #53
Keith Thompson wrote:
August Karlstrom <fu********@gma il.comwrites:
>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.
As far as I know, in C compile-time initialization can only be applied
to variables declared outside a function. If that is the case I consider
my statement above to be correct.
August
Sep 26 '08 #54
Richard<rg****@ gmail.comwrites :
mdh <md**@comcast.n etwrites:
>On Sep 25, 4:17Â*pm, Keith Thompson <ks...@mib.orgw rote:
>>>
The C language distinguishes between "initialization " and
"assignment ".

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.
I would not object to that wording but I would try to avoid in
teaching. The things that can go on the right of the = in an
initialisation are quite different to the things that can go there in
an assignment, so I would want to stress the distinction as soon a
possible since it is not a difficult point. If it were in some way
very complex, I might want to simplify and then expand later, but I
don't think this is the case here.

Initialisations give an initial value to an object through a mechanism
that is quite different to assignment despite the similarity when the
object is not an aggregate.

If you go on to learn other languages, the distinction is even greater
and that can be an important factor when teaching a first language.

--
Ben.
Sep 26 '08 #55
August Karlstrom wrote:
Keith Thompson wrote:
>August Karlstrom <fu********@gma il.comwrites:
>>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.

As far as I know, in C compile-time initialization can only be applied
to variables declared outside a function. If that is the case I consider
my statement above to be correct.
#include <stdio.h>
int main(void) {
static char hello[] = "Hello, world!";
puts (hello);
return 0;
}

It's impossible to say whether the initialization of hello[] actually
occurs at compile time, but we do know it occurs before main() begins
to execute.

--
Er*********@sun .com
Sep 26 '08 #56
Eric Sosman wrote:
August Karlstrom wrote:
>Keith Thompson wrote:
>>August Karlstrom <fu********@gma il.comwrites:
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.

As far as I know, in C compile-time initialization can only be applied
to variables declared outside a function. If that is the case I
consider my statement above to be correct.

#include <stdio.h>
int main(void) {
static char hello[] = "Hello, world!";
puts (hello);
return 0;
}

It's impossible to say whether the initialization of hello[] actually
occurs at compile time, but we do know it occurs before main() begins
to execute.
OK, you got me. I should change "compile-time initialization" to
"initializa tion of variables with static extent."
August
Sep 26 '08 #57

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...
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
9200
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...
1
7662
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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...

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.