Definitions: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
(new page) |
(→C/C++: clarify) |
||
Line 24: | Line 24: | ||
! Initialization |
! Initialization |
||
|colspan="2"|<source lang="C">int x = 1;</source> |
|colspan="2"|<source lang="C">int x = 1;</source> |
||
! style="text-align:center;" | |
|||
|- |
|- |
||
! Assignment |
! Assignment |
||
! style="text-align:center;" | |
! style="text-align:center;" | |
||
| <source lang="C">x = 1;</source> |
| <source lang="C">x = 1;</source> |
||
! style="text-align:center;" | |
|||
|} |
|} |
||
Note that in this table, "global" and "local" are used both for the type of variable and for the type of scope where the statement appears (if these are different, no code appears in the given cell). For example, <source lang="C">x = 1;</source> may be used to assign to a global variable, but such a statement may not appear in the global scope. Likewise, you may declare a global variable inside a function (local scope). |
|||
== See also == |
== See also == |
Revision as of 07:23, 14 February 2020
Definitions and declarations look much the same in C; it can also be difficult to tell the difference between initialization and assignment because they use the same symbol. Here I am just trying to visually lay out the various types of statements in order to show clearly what their differences and relationships are.
C/C++
Variable | Function | |||
---|---|---|---|---|
Type | Global | Local | (Global) | |
Declaration | extern int x; |
extern int fn(); | ||
Definition | int x; |
int fn() { ... } | ||
Initialization | int x = 1; |
|||
Assignment | x = 1; |
Note that in this table, "global" and "local" are used both for the type of variable and for the type of scope where the statement appears (if these are different, no code appears in the given cell). For example,
x = 1;
may be used to assign to a global variable, but such a statement may not appear in the global scope. Likewise, you may declare a global variable inside a function (local scope).