[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  Electronics
. -> [Folder]  Microprocessors
. . -> [Subject]  Declaration vs Definition in C
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Declaration vs Definition in C

Postings by administrators only.

[Refresh] Refresh page


Posted by Nick Gammon   Australia  (21,607 posts)  [Biography] bio   Forum Administrator
Date Sun 22 Feb 2015 03:21 AM (UTC)
Message
A short discussion about the difference in C (and C++) between declarations and definitions.



This example declares foo:


extern int foo;
extern int foo;

void setup ()
  {
  foo = 42;
  }  // end of setup

void loop () { }


Interestingly, you can declare foo multiple times (provided you declare it the same way each time).

The code above compiles but does not link, because the linker cannot find a definition of foo. That is, we promise that foo will eventually exist in some compilation unit, but it does not.


sketch_feb22a.cpp.o: In function `loop':
/home/nick/Development/arduino-1.0.6/sketch_feb22a.ino:9: undefined reference to `foo'
/home/nick/Development/arduino-1.0.6/sketch_feb22a.ino:9: undefined reference to `foo'


You can tell it is a linker error because the file name is "sketch_feb22a.cpp.o" - which ends with .o for "object" file.




If we remove the declaration the compiler complains that it isn't declared (not that it isn't defined):


void setup ()
  {
  foo = 42;
  }  // end of setup

void loop () { }


Error:


sketch_feb22a.ino: In function ‘void setup()’:
sketch_feb22a:4: error: ‘foo’ was not declared in this scope


Note this is a compiler error, not a linker error, because the file name ends in ".ino".




If we add a definition of foo, it compiles without errors:


int foo;

void setup ()
  {
  foo = 42;
  }  // end of setup

void loop () { }


This definition also provides a declaration, thus we don't get any messages about foo not being declared.




You can also declare and also define foo:


extern int foo;   // declare
int foo;          // define

void setup ()
  {
  foo = 42;
  }  // end of setup

void loop () { }






You can also do a similar thing with functions:


void bar ();

void setup ()
  {
  bar ();
  }  // end of setup

void loop () { }


In this case bar is declared but not defined.


sketch_feb22a.cpp.o: In function `setup':
/home/nick/Development/arduino-1.0.6/sketch_feb22a.ino:5: undefined reference to `bar()


The linker (not the compiler) complains that it cannot find a definition of bar().





void bar ();
void setup ()
  {
  bar ();
  }  // end of setup
void bar ()
  {
  Serial.println ("Hello, world");
  }
void loop () { }


Now bar is both declared (a forward declaration) and defined.

The definition is the part that instantiates the function or variable. And this can only be done once.




You cannot have multiple definitions of one thing:


int foo;         
int foo;

void setup ()
  {
  foo = 42;
  }  // end of setup

void loop () { }


Error:


sketch_feb22a:2: error: redefinition of ‘int foo’
sketch_feb22a:1: error: ‘int foo’ previously declared here


Interestingly, the compiler reports where it was previously declared, not where it was previously defined, although the error clearly is a duplicate definition.




If you move foo into a different compilation unit (eg. foo.cpp) then you get a different message (a linker error). This is because each compilation unit compiled OK, but the linker found multiple definitions of foo.


sketch_feb22a.cpp.o: In function `loop':
/home/nick/Development/arduino-1.0.6/sketch_feb22a.ino:9: multiple definition of `foo'
foo.cpp.o:(.bss.foo+0x0): first defined here


Now the error message refers to where it was previously defined (not previously declared).




A declaration provides enough information for the compiler to "work" with that variable or function, without necessarily knowing where it resides or what it does. For example:


extern int foo;


The compiler knows to generate code to load/store/work with an int, even though at this stage it doesn't know where foo resides.


int bar (byte a, char * b);


In this case it knows bar is a function, it returns an int, and it takes two arguments, one is a byte, and one is a char. This is enough to generate code to call bar, and handle whatever bar returns.

However at this stage it does not know exactly what bar does. That is eventually provided when bar is defined (possibly in a different compilation unit).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


2,890 views.

Postings by administrators only.

[Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]