#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20

    The C programming language


    I am reading chapter number for at page number 67. I was unable to understand the meaning of this line.
    The standard clarifies the rules on the scope of names: in particular , it requires that there be only definition of each external object Initialization is more general : automatic arrays and structures may now be initialized.
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Page 67 of what? Give the edition number, chapter number and section number. Page 67 of my K&R 2nd ed. does not have that text (and is certainly not that badly punctuated).

    Beside that state exactly what it is you do not understand.

    In general it means that any symbol (function or variable name) with external scope (i.e. not static) may defined exactly once. That is perhaps a fairly obvious statement.


    The second part about initialisation refers to the fact that prior to ANSI standardisation, automatic arrays (non static arrays declared within a function), and structures could not be directly initialised on declaration.

    You have to realise that the 2nd edition was first published in 1989 when a lot had changed in the language since the first edition in 1978. The comments are largely outdated since you are unlikely to see much pre ANSI code.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20
    Originally Posted by clifford
    Page 67 of what? Give the edition number, chapter number and section number. Page 67 of my K&R 2nd ed. does not have that text (and is certainly not that badly punctuated).
    I apologized for my mistake. I am reading rather solving "The C programming language" written by B.K and D.R Second edition(ANSI C). Chapter number is "4" and there is no section number because it is in introductory section of chapter 4. Paragraph number is 4 from top of the page.Hope this will help you to hunt the line.

    Comments on this post

    • clifford agrees : My error I think, I scanned the page and could not see it, and could not imagine why the statement was related to the chapter.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by K&R,2nd Ed
    The standard clarifies the rules on the scope of names; in particular, it requires that there be only definition of each external object.
    It means what it says. An extern is not a definition.

    When your project has more than one source file (we have projects with more than 100 source files), then you use the header files to inform the source files what functions exist in the other source files as well as what global data objects exist. You do the latter by using extern with a declaration of that global data object. All that does is to inform that compiler that that data object exists somewhere and here are its properties (eg, name, datatype). The compiler uses that information to generate object code and marks that data object as needing to have its location resolved by the linker, since the linker will know where that data object has been defined. So in one of the source files, that data object must be defined and it can only be defined once! If you define the same data object in two source files, then the linker will complain with a "duplicate definition" error and it will not build your executable.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20

    Thumbs up


    Originally Posted by dwise1_aol
    It means what it says. An extern is not a definition.

    When your project has more than one source file (we have projects with more than 100 source files), then you use the header files to inform the source files what functions exist in the other source files as well as what global data objects exist. You do the latter by using extern with a declaration of that global data object. All that does is to inform that compiler that that data object exists somewhere and here are its properties (eg, name, datatype). The compiler uses that information to generate object code and marks that data object as needing to have its location resolved by the linker, since the linker will know where that data object has been defined. So in one of the source files, that data object must be defined and it can only be defined once! If you define the same data object in two source files, then the linker will complain with a "duplicate definition" error and it will not build your executable.
    Very nice explanation sir.Thank you.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    You may want to read through the appendices in the book to get an overall perspective. There are a lot of details packed tightly into the body of the book, such that one may find it difficult to see the context for some of the statements (which appears to have been the problem here), so the overview offered by the appendices may help to provide that context.

    For example, in A10.2 starting on page 226 they discuss external declarations. They tie it in with linkage and scope, which is discussed in A11. Unless you restrict a file-scope variable (AKA "global") to internal linkage, meaning that its scope is restricted to that one source file, which is a translation unit, then it has external linkage, which means that it can be accessed from anywhere in the program and therefore that only one instance of that variable can be allowed to exist, else the linker wouldn't know which one to link your reference to.

    As clifford described the situation, the first edition of The C Programming Language described the C language at that time and, since Dennis Ritchie created C and since this book was the closest thing there was to a C manual, it became the de facto manual. As a result, that version of C is referred to as "K&R", after Kernighan and Ritchie. Then in 1989 an ANSI standard for C was published which incorporated many changes, all of them, in my humble opinion, for the better. That prompted the second edition of the book, which you're working from. Starting on page 259, Appendix C provides a summary of those changes. You should read it only to satisfy your own curiosity. It was meant for those programmers who were already familiar with K&R C so that they could learn what had changed in ANSI C. Since you are starting out learning ANSI C with no knowledge of K&R C, it should only be of academic interest to you. However, in the book's chapters the authors will make reference to those changes for the benefit of experience K&R C programmers, such as the statement that had confused you.

    Now, you will still find many examples of K&R source code posted on the Internet, recognizable mainly from the function headers. When you try to compile such source code, you will get errors or warnings about using obsolete function headers, so when that happens to you then bring it here and we can advize you how to change it. I remember one option setting in Turbo C was to allow it to recognize K&R syntax, but I don't think there's any compiler that will still do that.
    Last edited by dwise1_aol; May 23rd, 2013 at 02:07 PM.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20
    I am not new to C programming. I am coding since past two years but, the problem is the book from which i have learned is very theory oriented(Now i feel). When I started
    The C Programming Language (ANCI C)
    I felt that the importance of real programming. I can feel that previously i was doing seance less programming but after completing this book i will be able to code in sensible way. The exerciser of
    Bit-wise operator
    in the Second edition of The C programming Language(Exercise number 2-6,2-8) is bit difficult .It doesn't mean that I urge you to provide me source code of it, rather I will solve it on my own.Even I think the code given for
    unsigned getbits(unsigned x,int p,int n) on Page number 49 of the same book
    is some how hard to understand. I know theory of bits and their operations but unable to get the desired bit of any value.
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    My book is at work. It is now 2350 here (you had posted that at 2335). So in another 9 hours or so (don't you just love the effects of time zones in our truly global communications?).
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20
    Closing this thread now. Thank you all for making my confusion clear. :tntworth:

IMN logo majestic logo threadwatch logo seochat tools logo