What exactly are C++ definitions, declarations and assignments?

前端 未结 8 1250
长情又很酷
长情又很酷 2020-11-30 08:52

I tend to use the words define, declare and assign interchangeably but this seems to cause offense to some people. Is this justified? Should I only use the word declare for

8条回答
  •  独厮守ぢ
    2020-11-30 09:54

    Define and declare are similar but assign is very different.

    Here I am declaring (or defining) a variable:

    int x;
    

    Here I am assigning a value to that variable:

    x = 0;
    

    Here I am doing both in one statement:

    int x = 0;
    

    Note

    Not all languages support declaration and assignment in one statement:

    T-SQL

    declare x int;
    set x = 0;
    

    Some languages require that you assign a value to a variable upon declaration. This requirement allows the compiler or interpreter of the language to infer a type for the variable:

    Python

    x = 0
    

提交回复
热议问题