Thread: difference between declaration and definition of a variable?

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    40

    difference between declaration and definition of a variable?

    Hi everybody

    I am declaring and initializing variable n in my code. I do not understand what meaning of define the variable in c

    Code:
    #include<stdio.h>void main()
    {
        int n  //declar variable n 
        int n = 10; // //declar and initialize variable n with 10 value 
    }
    Any one can tell me what is difference between declaration and definition of a variable?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Definitions take up real physical space in the memory of your program.

    A declaration is just a 'promise' you make to the compiler that something will exist at some point in the future.
    A good example being function prototypes in header files - they're all declarations.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    40
    Quote Originally Posted by Salem View Post
    Definitions take up real physical space in the memory of your program.
    .
    I did not understand your answer It would be even better if you gave an example as I gave

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    A variable definition is usually also a declaration, unless an extern declaration has already been seen by the compiler.
    Code:
    #include <stdio.h>
    
    extern int z;   // Declaration only, not a definition
                    // This variable may be defined in another file
                    // Usually this would be in a header file
                    // z cannot be used in the code until it has been defined
    int main(void)
    {
       int x;       // Variable declaration and definition
       int y = 10;  // Same as x but with initialization
    
       return 0;
    }

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Have you tried your favourite search engine? "c definition versus declaration" produces over 130,000,000 results, surely at least one of those links would answer your question.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    I once reviewed a book for potential use in a course on C. The book was not just badly written, some of the text and code were plain WRONG! Please recycle the book you currently have.

    Please see my list of three books above. Any of them are a good replacement. Available as either a paper book, or an e-book.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-25-2015, 09:06 AM
  2. Declaration and definition of a variable
    By raghu_1991 in forum C Programming
    Replies: 2
    Last Post: 05-11-2013, 02:21 AM
  3. Replies: 3
    Last Post: 07-11-2012, 12:27 PM
  4. Difference between definition and declaration
    By Allen in forum C++ Programming
    Replies: 4
    Last Post: 02-02-2012, 03:53 AM
  5. Difference between Declaration , initialisation and definition.
    By nkrao123@gmail. in forum C Programming
    Replies: 12
    Last Post: 09-11-2011, 10:07 AM

Tags for this Thread