Thread: Declaration and definition of a variable

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    Declaration and definition of a variable

    Hello, I am new to this forum. Kindly tolerate any mistakes.

    I read that Memory is allocated during definition of a variable and not during declaration. Declaration is something like,

    Code:
    int x;
    And definition is assigning some value to it. This is what my professor taught.

    My doubt is if memory is not allocated during declaration, then how the compiler successfully compiles and runs the following, which i had already tried.

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {   
        int c;
        int *p=&c;
        printf("%x",p);
        getch();
        return 0;
    }
    The variable c is only declared. But the program outputs a memory address. Shouldn't it show an error?

    Thanks in Advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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
    May 2013
    Posts
    2
    Thank you... Clear as a crystal.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaration vs Definition
    By grob115 in forum C++ Programming
    Replies: 1
    Last Post: 04-04-2013, 12:15 AM
  2. Variable declaration Vs definition
    By callkalpa in forum C Programming
    Replies: 11
    Last Post: 12-17-2009, 12:21 PM
  3. declaration or definition
    By BEN10 in forum C Programming
    Replies: 5
    Last Post: 07-19-2008, 07:50 PM
  4. definition/declaration/...
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2007, 02:38 PM
  5. Declaration vs definition
    By ripper079 in forum C++ Programming
    Replies: 8
    Last Post: 03-15-2002, 11:00 PM