4 ms·
C is declare anywhere and has been for a looong time (C99).
by unwind 4y ago
C is declare anywhere and has been for a looong time (C99).
- zabzonk 4y agosorry, my bad, i don't do any c programming these days. but to be pedantic, i think you mean define rather than declare, though of course you can also declare things at different scopes.
- stefncb 4y agoNo, I think they do mean declare. In C89 you can define a variable anywhere, like so: int x; f(); x=3; The improvement was allowing declarations anywhere.
- rightbyte 4y agoIsn't the first row both the declaration and definition is this case? (I assume f() is a call.)
- Jtsummers 4y agoC (and C++) considers the declaration to be separate from the definition, though you can combine them. The declaration is the name + type, definition provides a value. The example code is meant to illustrate separate declaration and definition with some code (the call to `f`) between the two. If you try to use `x` before it is defined (the assignment) your compiler should give you a warning, at least. If it doesn't, find a new compiler.
- LegionMammal978 4y agoFor block-scope declarations, it's not the definition but the initialization that you're thinking of. At least in C, every block-scope variable declaration is a definition: > A definition of an identifier is a declaration for that identifier that: > — for an object, causes storage to be reserved for that object; [...] But initialization (in C) does not occur for block-scope objects without the optional initializer.
- stefncb 4y agoOkay, that's somewhat confusing. So a declaration is when the identifier enters the scope, and a definition is for a variable specifically. So a declaration that's not a definition could be a forward declaration for a function, or a typedef.
- LegionMammal978 4y agoYes, or the declaration of an extern variable.