2

I know there has been awfully lot debates over the differences and meanings of Declaration and Definition but I still can't understand where and when should we use Declaration.

Declaration

A variable declaration provides assurance to the compiler that there exists a variable with the given type and name so that the compiler can proceed for further compilation without requiring the complete detail about the variable. A variable definition has its meaning at the time of compilation only, the compiler needs actual variable definition at the time of linking the program.

Definition

A variable definition tells the compiler where and how much storage to create for the variable. A variable definition specifies a data type and contains a list of one or more variables of that type as follows −

EDIT 1: Is declaration necessary at all times, if not, when it is necessary to use? Does definition do enough job as it does in other languages like Java, JS

EDIT 2: I have already said that there have been answers to their differences, I have problems about understanding their usages.

keser
  • 2,472
  • 1
  • 12
  • 38
  • 1
    I dont worry about them in the context of variables anymore... it was a big deal in C89 and older, but basically they are the same in most places now... with the exception of something that is `extern` or has the type masked from the interface, like an opaque struct or an object handle that is passed around as `(void *)` – Grady Player Oct 09 '18 at 16:03
  • 1
    There are no debates, it is defined straight and clear. Look at the duplicate I am going to propose... – Eugene Sh. Oct 09 '18 at 16:04
  • I can go with that dup, good answers there – Grady Player Oct 09 '18 at 16:05
  • I saw that q but I still have problems about their usages – keser Oct 09 '18 at 16:06
  • 2
    Well, I am probably going to get some fire here, but I would say that you can successfully use it intuitively, without understanding it. – Eugene Sh. Oct 09 '18 at 16:08
  • 3
    A declaration says there is a "thing" somewhere. A definition **is** the thing. – Blastfurnace Oct 09 '18 at 16:10
  • I edited my question – keser Oct 09 '18 at 16:14
  • And then there is [tentative definition](https://stackoverflow.com/questions/3095861/about-tentative-definition).... – chux - Reinstate Monica Oct 09 '18 at 16:21
  • 1
    A C compiler won't look forwards to find things that haven't been mentioned yet. If I have a function foo that calls a function bar, then my options are either to 1) put (define) bar in the .c file first, so that when we get to foo the compiler will already know what bar is, or 2) declare bar first, then write function foo, then write function bar afterwards. If I just write function foo then function bar then when we get to the bar call inside foo the compiler will error out. – Rup Oct 09 '18 at 16:23
  • @Rup can you post that comment with a short code example as an answer I cant imagine it like this – keser Oct 09 '18 at 16:29
  • 1
    The question is closed now so I can't. Was roughly like program 1 and program 2 in this answer: https://stackoverflow.com/a/41463559 – Rup Oct 09 '18 at 16:31
  • 1
    "Is declaration necessary at all times, if not, when it is necessary to use?" No. needed when item used before definition. – chux - Reinstate Monica Oct 09 '18 at 16:39
  • @DavidBowling Yes, comment deleted and re commented. – chux - Reinstate Monica Oct 09 '18 at 16:40
  • Looking at the "EDIT 3" I would say we have an XY-problem here. There is a clear confusion between "definition" and "initialization". – Eugene Sh. Oct 09 '18 at 16:41
  • I think I'm going to give up.... Thank you all for your comments. – keser Oct 09 '18 at 16:43
  • @EugeneSh. Is there a documentation or a good source I can dig? – keser Oct 09 '18 at 16:45
  • 1
    @aomerk You just need to ask the right question. Looks like your real question is about the storage classes and the usage of unitialized variables. Should be covered in any decent C textbook – Eugene Sh. Oct 09 '18 at 16:50
  • @EugeneSh. Actually, I know the usage of unitialized variables and their results but I just got lost after some point in this question. Sorry. – keser Oct 09 '18 at 16:52
  • 1
    @aomerk -- "Is there a documentation or a good source I can dig?": [yes](https://port70.net/~nsz/c/c11/n1570.html). – ad absurdum Oct 09 '18 at 17:00

0 Answers0