January 1st, 2003, 05:42 PM
c - don't understand something in k&r book
following c programming by kernighan and ritchie at the moment.
something doesn't add up. contradicts:
(page 40) - talking about variable declerations - "if the variable in question is not automatic, the initialization is done once only, conceptually before the program starts executing, and the initializer must be a constant expression"
then when talking about scope rules (page 80) - "the scope of an external variable or a function lasts from the point at which it is declared to the end of the file being compiled"
this seems to be a contradiction to me. the first part i've quoted gives the impression that variables that are outside any functions (not automatic), an external variable, (regardless of their position in the code) will be available from the start of the code.
where as the other part says that it's available from the point it's initialised downwards - and that's what more of that section round page 80 gives the impresssion of.
if an external variable is initialised some way down the code, then this will not be initialised conceptually before the program starts. i'm guessing the 2nd part is correct?
January 1st, 2003, 08:22 PM
You're thinking too much about this and confusing yourself.
Your compiler knows about a variable only from the point where it first appears in the source code, which is what the p80 quote is saying. The p40 quote indicates that when the runtime is loaded into memory, part of that runtime may include the initialized value. The mechanism for runtime initialization of variables isn't something you need to worry about - that's a detail for your compiler implementer to sweat.
Really, I wouldn't worry much about it right now. The discussion of variable scope is what you'll need to understand to write your code.
Articles and commentary on web development
January 1st, 2003, 09:13 PM
ok, thanks for the reply