Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

sorry, 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.



No, 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.


Isn't the first row both the declaration and definition is this case?

(I assume f() is a call.)


C (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.


For 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.


Okay, 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.


Yes, or the declaration of an extern variable.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: