1

Is there a difference between defining a variable in C and assigning a value to a variable in C?

I know that declaring a variable simply means telling the name and its type like int a.

On the other hand defining a variable means telling the compiler its value.

So in my understanding there are the following associations between these concepts:

  1. Declaration - Name + Type (of an identifier)
  2. Declaration - Value (of an identifier)
Iharob Al Asimi
  • 51,091
  • 5
  • 53
  • 91
  • A declaration is merely telling the compiler that a variable with a given name has a given type, you can define a variable when declaring it or you can initialize it later. Although I am not sure of the use of the term "*definition*" in relation to variables according to the c standard, I am sure that the term "*initialization*" is used to denote something like `int a = 1`. – Iharob Al Asimi Oct 03 '17 at 13:47
  • 1
    *On the other hand defining a variable means telling the compiler its value.* - No. – Eugene Sh. Oct 03 '17 at 13:48
  • 1
    `int a;` is also a definition. Roughly speaking, storage will be allocated for `a`. `extern int a;` would be a declaration. – chris Oct 03 '17 at 13:48
  • A declaration provides basic attributes of a symbol: its type and its name. A definition provides all of the details of that symbol--if it's a function, what it does; if it's a class, what fields and methods it has; if it's a variable, where that variable is stored. – H.S. Oct 03 '17 at 13:53
  • 1
    The duplicate covers most of the question, but doesn't address _assignment_. For reference, the optional initializer is only valid with definitions (you can't write `extern int a = 42;`), but it doesn't change the type of the statement. – Useless Oct 03 '17 at 13:58
  • 2
    Sign. As usual, the question got closed as a duplicate of a related question which doesn't really answer this one, and before I had a chance to post an answer to this one. I can think of two big differences between definition+initializer, versus assignment: (1) Can initialize `const`-qualified array, but can't assign to it; (2) pointer initializations look difference wrt the `*`: `char *str = "hello";`, but `str = "hello". – Steve Summit Oct 03 '17 at 14:02
  • @SteveSummit Thanks for the information. I believe that it's a little bit sad how eager are people here to close questions as soon as possible as if closing a question is some kind of badge of honor. –  Oct 03 '17 at 14:08
  • @SteveSummit Detail: `int a;` as an object with file scope, is a _tentative definition_. §6.9.2. Storage may not be allocated, depends on the rest of the file's code. Unfortunately not covered in the dupe. – chux - Reinstate Monica Oct 03 '17 at 14:32
  • @SteveSummit Concerning [before I had a chance to post an answer](https://stackoverflow.com/questions/46546058/regarding-a-variable-in-c-is-an-assignation-always-equivalent-to-a-definition-o#comment80047027_46546058), vote to [re-open](https://stackoverflow.com/help/privileges/close-questions) - as you have that privilege. – chux - Reinstate Monica Oct 03 '17 at 14:37

0 Answers0