#1
  1. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12

    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?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    Balance,

    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.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  4. #3
  5. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    ok, thanks for the reply :)

IMN logo majestic logo threadwatch logo seochat tools logo