Thread: Difference between Declaration , initialisation and definition.

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    Difference between Declaration , initialisation and definition.

    Hi,

    Can any one give small explanation about Declaration , initialization and definition in c language?

    Because i have little confusion about extern storage class even i goggled different materials.

    1. Extern calss should be use in .h files only?
    2. int i declaration should be in source file and "extern int i" sould be in .h files only?


    can any one give clear idea about usage of extern strage class?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What does your textbook say about it?

    The extern keyword (there are no classes in C) simply tells the compiler "the actual declaration is somewhere else"...

    file 1 ... int Setup;
    File2 ... extern int Setup ... refers to the Setup in file 1.

    No magic... just a promise made to the compiler.

  3. #3
    kotin
    Join Date
    Oct 2009
    Posts
    132
    HI Tater,

    I am trying to compile the below program

    Note: 1.c and 2.c are in same folder

    2.c
    Code:
    extern int i;
    printf("%d\n",i);
    1.c
    Code:
    #include<stdio.h>
    #include"2.c"
    int i;
    int main()
    {
            printf ("%d\n",i);
            return 0;
    }
    i compiling from gcc as #cc 1.c

    i seeing below error

    In file included from 1.c:2:
    2.c:2: error: syntax error before string constant
    2.c:2: error: conflicting types for 'printf'
    2.c:2: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
    2.c:2: error: conflicting types for 'printf'
    2.c:2: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
    2.c:2: warning: data definition has no type or storage class


    i would appreciate any answer

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Because your 2.c file has code outside of a function... you are directly including a C file (which is major bad juju) and you aren't using headers. Also try to pick something other than lazy arsed names for things... really single letters... not good.

    Hit those textbooks again and look up how to structure multi-file projects.

  5. #5
    kotin
    Join Date
    Oct 2009
    Posts
    132
    hi,

    "you aren't using headers"-- this mean if i include #include"1.c" in 2.c earlier program would work?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nkrao123@gmail. View Post
    Can any one give small explanation about Declaration , initialization and definition in c language?
    A declaration declares and names a variable, type, or function, eg:

    Code:
    int x;  // variable declaration
    struct whatever;  // type declaration
    int somefunc(int); // function declaration, aka prototype.
    Note that unless int x is global, that declaration leaves it uninitialized, meaning it could have any value (if it is global, it is initialized to 0). Initialization is the assignment of a specific value, eg:

    Code:
    x = 5;
    Definition applies to types and functions. Just as variables may be declared and initialized at the same time (eg, "int x = 5"), types and functions may be declared and defined at the same time:

    Code:
    struct whatever {
         int x;
    };
    
    int somefunc(int x) {
        return x*2;
    }
    Those look the same if the type or function has already been declared. You may declare a type or function multiple times (hopefully, the same way), but you may only have one definition. The reason for separate, potentially multiple declarations (which are optional) is to do with compiling multiple files into a single executable.

    Quote Originally Posted by nkrao123@gmail. View Post
    hi,

    "you aren't using headers"-- this mean if i include #include"1.c" in 2.c earlier program would work?
    Nope. You put your definitions in a .c file and declarations in a .h (header) file. Then you include the .h file.

    eg.h
    Code:
    struct whatever;
    int somefunc(int);
    eg.c
    Code:
    struct whatever {
         int x;
    };
    
    int somefunc(int x) {
        return x*2;
    }
    Last edited by MK27; 09-11-2011 at 08:15 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No it means that you should not be directly including .c files into one another... you risk creating loops the compiler will not know how to resolve.

    This is why you write header files .h to be included into your .c files... Headers only contain declarations, extern variables and constants and only as needed by the page you are including them into... The stdio.h file used in almost every console program is a good example... go ahead open it in a text editor (don't change anything!) and you will see there is no code in the file, just declarations, externs, constants and typedefs...

    I've seen your messages across several of the forums here and I do hope you don't mind a little advice...

    You need to learn C or C++ by an act of deliberate study. You are not going to absorb it by blundering around trying this, that and the other thing... You need to sit down with the textbooks and take on a deliberate course of learning before it's going to make any sense to you at all. That is: start on page 1 of the book, read the text, compile the examples, play with the code until you understand it, review as needed... then move to page 2... and so on until you finish the book.

    At that point you should be able to write small projects -- balance your chequebook, do a home inventory, simple games, etc. -- without too much trouble. After that it's a matter of practice practice practice. C is a very simple language; programming, on the other hand, is an extremely complex task.

    Seriously... I see this a lot on these forums... People think that because they read chapter 1 of C for Dummies, they're suddenly qualified to rewrite Windows7... Believe me... it just doesn't work that way.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    Initialization is the assignment of a specific value
    More accurately, this "specific value" is the initial value. MK27's example is actually of assignment, not initialization, though it may also be used to provide what is effectively an initial value (overwriting an existing "garbage" value).

    Quote Originally Posted by MK27
    Definition applies to types and functions.
    It also applies to variables. The definition of a variable is a declaration of the variable that causes storage to be reserved for it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    MK27's example is actually of assignment, not initialization, though it may also be used to provide what is effectively an initial value (overwriting an existing "garbage" value).
    Ah. I thought "initialization" could refer to the first assignment of an uninitialized variable.

    Maybe I will apply to have this approved by the C Standard of Colloquialisms (CSC), after I found the CSC committee and appoint myself chairperson.
    Last edited by MK27; 09-11-2011 at 08:53 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi,

    any one can let me know the definition file name for "printf" function? i fell it would be defined in some where in .c file, if i am not mistake.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nkrao123@gmail. View Post
    Hi,

    any one can let me know the definition file name for "printf" function? i fell it would be defined in some where in .c file, if i am not mistake.
    No it won't be in any source code you have access to... It's most likely part of the C libraries supplied with your compiler and for the most part those are supplied in pre-compiled form.

    If you want to know how to use it, just look it up in your compiler's library documentation ... that is, Read The Help File.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nkrao123@gmail. View Post
    Hi,
    any one can let me know the definition file name for "printf" function? i fell it would be defined in some where in .c file, if i am not mistake.
    It is/was, but as Tater says, you do not need this source file on your system, because that file was compiled into a shared library which your executable is linked to.

    So printf() is declared in stdio.h, and all your code needs is the correct declaration for the function. The function itself is compiled into the library, and linkage takes care of the rest.

    http://en.wikipedia.org/wiki/Linkage_%28software%29

    Looking at the source can be useful and informative, of course. If your C library is open source (such as on linux), you can get a copy of whatever .c file it's in if you want. Those will be in the source package for glibc, and via:

    GLIBC, the GNU C Library
    http://ftp.gnu.org/gnu/libc/

    If your library is not open source, you are out of luck, but a gander at glibc might satisfy your curiosity if you are interested seeing in a very widely used, standards compliant code definition of printf(). The source from ftp above is in a g'zipped tarball, so you need the *nix commands "tar" and "gzip" to open it. I'm sure there are crossplatform versions around (eg, from minGW).
    Last edited by MK27; 09-11-2011 at 09:57 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MK27 View Post
    If your library is not open source, you are out of luck, but a gander at glibc might satisfy your curiosity...
    Okay, I found it:

    Code:
    #include <libioP.h>
    #include <stdarg.h>
    #include <stdio.h>
    
    #undef printf
    
    /* Write formatted output to stdout from the format string FORMAT.  */
    /* VARARGS1 */
    int
    __printf (const char *format, ...)
    {
      va_list arg;
      int done;
    
      va_start (arg, format);
      done = vfprintf (stdout, format, arg);
      va_end (arg);
    
      return done;
    }
    
    #undef _IO_printf
    ldbl_strong_alias (__printf, printf);
    The source for vfprintf() is over 2000 lines, lol, but if what you were interested in was how printf takes it's arguments, there it is, and here is an explanation:

    stdarg.h - Wikipedia, the free encyclopedia
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. declaration and definition
    By sugarfree in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2010, 06:53 AM
  2. declaration or definition
    By BEN10 in forum C Programming
    Replies: 5
    Last Post: 07-19-2008, 07:50 PM
  3. Declaration vrs Definition
    By curlious in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2004, 07:13 PM
  4. Declaration vs definition
    By ripper079 in forum C++ Programming
    Replies: 8
    Last Post: 03-15-2002, 11:00 PM
  5. Declaration/Definition ??
    By Manish in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2002, 11:37 AM