/ | ||||
(C++11) | ||||
(C++11) |
(C++11) | ||||
(C++20) | ||||
(C++20) |
(C++11) | ||||
expression |
pointer |
specifier | ||||
specifier (C++11) | ||||
specifier (C++11) |
(C++11) | ||||
(C++11) |
(C++11) | ||||
(C++11) |
Specifiers | ||||
function specifier | ||||
function specifier | ||||
(C++20) | ||||
/struct | ||||
volatile | ||||
(C++26) | ||||
(C++11) |
Declarators | ||||
Block declarations | ||||
→ (C++17) | ||||
(C++11) | ||||
declaration | ||||
directive | ||||
declaration (C++11) | ||||
declaration | ||||
(C++11) | ||||
Other declarations | ||||
(C++11) | ||||
(C++11) | ||||
Declarations are how names are introduced (or re-introduced) into the C++ program. Not all declarations actually declare anything, and each kind of entity is declared differently. Definitions are declarations that are sufficient to use the entity identified by the name.
A declaration is one of the following:
) | (since C++11) |
attr (optional) declarator | |||||||||
attr | - | (since C++11) sequence of any number of |
declarator | - | a function declarator |
(since C++11) |
(since C++20) | |
declaration | (since C++11) |
Simple declaration Specifiers Declarators Notes Example Defect reports See also |
A simple declaration is a statement that introduces, creates, and optionally initializes one or several identifiers, typically variables.
decl-specifier-seq init-declarator-list (optional) | (1) | ||||||||
attr decl-specifier-seq init-declarator-list | (2) | ||||||||
attr | - | (since C++11) sequence of any number of |
decl-specifier-seq | - | sequence of (see below) |
init-declarator-list | - | comma-separated list of with optional . init-declarator-list is optional when declaring a named class/struct/union or a named enumeration |
A structured binding declaration is also a simple declaration. (since C++17)
Declaration specifiers ( decl-specifier-seq ) is a sequence of the following whitespace-separated specifiers, in any order:
specifier is also allowed on variable declarations. | (since C++17) |
specifier, only allowed in variable definitions, function and function template declarations, and the declaration of static data members of literal type. | (since C++11) |
specifier, only allowed in function and function template declarations. specifier, only allowed in declaration of a variable with static or thread storage duration. At most one of the constexpr, consteval, and constinit specifiers is allowed to appear in a decl-specifier-seq. | (since C++20) |
(since C++11) | |
(since C++26) |
): see | (since C++17) |
can be combined with long. | (since C++11) |
Attributes may appear in decl-specifier-seq , in which case they apply to the type determined by the preceding specifiers.
Repetitions of any specifier in a decl-specifier-seq , such as const static const , or virtual inline virtual are errors , except that long is allowed to appear twice (since C++11) .
init-declarator-list is a comma-separated sequence of one or more init-declarators , which have the following syntax:
declarator initializer (optional) | (1) | ||||||||
declarator requires-clause | (2) | (since C++20) | |||||||
declarator | - | the declarator |
initializer | - | optional initializer (except where required, such as when initializing references or const objects). See for details. |
requires-clause | - | , which adds a to a |
Each init-declarator in an init-declarator sequence S D1, D2, D3 ; is processed as if it were a standalone declaration with the same specifiers: S D1 ; S D2 ; S D3 ; .
Each declarator introduces exactly one object, reference, function, or (for typedef declarations) type alias, whose type is provided by decl-specifier-seq and optionally modified by operators such as & (reference to) or [ ] (array of) or ( ) (function returning) in the declarator. These operators can be applied recursively, as shown below.
A declarator is one of the following:
unqualified-id attr (optional) | (1) | ||||||||
qualified-id attr (optional) | (2) | ||||||||
identifier attr (optional) | (3) | (since C++11) | |||||||
attr (optional) cv (optional) declarator | (4) | ||||||||
nested-name-specifier attr (optional) cv (optional) declarator | (5) | ||||||||
attr (optional) declarator | (6) | ||||||||
attr (optional) declarator | (7) | (since C++11) | |||||||
noptr-declarator constexpr (optional) attr (optional) | (8) | ||||||||
noptr-declarator parameter-list cv (optional) ref (optional) except (optional) attr (optional) | (9) | ||||||||
In all cases, attr is an optional sequence of . When appearing immediately after the identifier, it applies to the object being declared. | (since C++11) |
cv is a sequence of const and volatile qualifiers, where either qualifier may appear at most once in the sequence.
When a block-declaration appears inside a block , and an identifier introduced by a declaration was previously declared in an outer block, the outer declaration is hidden for the remainder of the block.
If a declaration introduces a variable with automatic storage duration, it is initialized when its declaration statement is executed. All automatic variables declared in a block are destroyed on exit from the block (regardless how the block is exited: via exception , goto , or by reaching its end), in order opposite to their order of initialization.
Note: this example demonstrates how some complex declarations are parsed in terms of the language grammar. Other popular mnemonics are: the spiral rule , reading inside-out , and declaration mirrors use . There is also an automated parser at https://cdecl.org .
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
C++98 | the declarators of redeclarations could not be qualified | qualified declarators allowed | |
C++98 | a single standalone semicolon was not a valid declaration | it is an empty declaration, which has no effect | |
C++98 | repetition of a function specifier in a decl-specifier-seq was allowed | repetition is forbidden |
for Declarations |
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Get early access and see previews of new features.
Earn badges by improving or asking questions in Staging Ground.
Technically what are the meanings and differences of the terms declaring , instantiating , initializing and assigning an object in C#?
I think I know the meaning of assigning but I have no formal definition.
In msdn, it is said "the act of creating an object is called instantiation". But the meaning creating seems vague to me. You can write
is a then created?
Declaring - Declaring a variable means to introduce a new variable to the program. You define its type and its name.
Instantiate - Instantiating a class means to create a new instance of the class. Source .
Initialize - To initialize a variable means to assign it an initial value.
Assigning - Assigning to a variable means to provide the variable with a value.
In general:
Declare means to tell the compiler that something exists, so that space may be allocated for it. This is separate from defining or initializing something in that it does not necessarily say what "value" the thing has, only that it exists. In C/C++ there is a strong distinction between declaring and defining. In C# there is much less of a distinction, though the terms can still be used similarly.
Instantiate literally means "to create an instance of". In programming, this generally means to create an instance of an object (generally on "the heap"). This is done via the new keyword in most languages. ie: new object(); . Most of the time you will also save a reference to the object. ie: object myObject = new object(); .
Initialize means to give an initial value to. In some languages, if you don't initialize a variable it will have arbitrary (dirty/garbage) data in it. In C# it is actually a compile-time error to read from an uninitialized variable.
Assigning is simply the storing of one value to a variable. x = 5 assigns the value 5 to the variable x . In some languages, assignment cannot be combined with declaration, but in C# it can be: int x = 5; .
Note that the statement object myObject = new object(); combines all four of these.
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .
IMAGES
VIDEO
COMMENTS
Declaration: int a; Assignment: a = 3; Declaration and assignment in one statement: int a = 3; Declaration says, "I'm going to use a variable named "a" to store an integer value."Assignment says, "Put the value 3 into the variable a." (As @delnan points out, my last example is technically initialization, since you're specifying what value the variable starts with, rather than changing the value.
A declaration tells the compiler, or programmer that the function or variable exists. e.g. An assignment is when a variable has its value set, usually with the = operator. e.g. Actually I would consider "int var;" to be definition, "int var = 5;" is a combined def/ass.
Declaration and definition in C/C++ is not mutually exclusive concepts. Actually, definition is just a specific form of declaration. Every definition is a declaration at the same time (with few exceptions). ... If I have some types, names, and assignment all going on in the same tight space, it's a bit of information overload. Further, it means ...
Explain the variable declaration initialization and assignment in C language - The main purpose of variables is to store data in memory. Unlike constants, it will not change during the program execution. However, its value may be changed during execution.The variable declaration indicates that the operating system is going to reserve a piece of memory with that variable name.V
It depends on the language we're coding in and the thing we want to declare, define or initialize. 2. Declarations. A declaration introduces a new identifier into a program's namespace. The identifier can refer to a variable, a function, a type, a class, or any other construct the language at hand allows. For a statement to be a declaration ...
C Programming Variable Declarations and Definitions. A variable declaration is when you specify a type and an identifier but have not yet assigned a value to the variable. A variable definition is when you assign a value to a variable, typically with the assignment operator =. In the C programming language, variables must be declared before ...
A declaration is a C language construct that introduces one or more identifiers into the program and specifies their meaning and properties.. Declarations may appear in any scope. Each declaration ends with a semicolon (just like a statement) and consists of two (until C23) three (since C23) distinct parts:
The basic form of declaring a variable is: type identifier [= value] [, identifier [= value]]…]; OR. data_type variable_name = value; where, type = Data type of the variable. identifier = Variable name. value = Data to be stored in the variable (Optional field) Note 1: The Data type and the Value used to store in the Variable must match.
7 Assignment Expressions. As a general concept in programming, an assignment is a construct that stores a new value into a place where values can be stored—for instance, in a variable. Such places are called lvalues (see Lvalues) because they are locations that hold a value. An assignment in C is an expression because it has a value; we call it an assignment expression.
The keyword int tells C that this variable contains an integer value. (Integers are defined below.) The variable name is answer.The semicolon (;) marks the end of the statement, and the comment is used to define this variable for the programmer.(The requirement that every C variable declaration be commented is a style rule.
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. Often, the compiler only needs to have a declaration for something in order to ...
The process of specifying an initial value for an object is called initialization, and the syntax used to initialize an object is called an initializer. Informally, the initial value is often called an "initializer" as well. int width { 5 }; // define variable width and initialize with initial value 5 // variable width now has value 5.
Declarations and definitions are employed to describe more about the variables, functions, or types to the compiler or interpreter for a program. A declaration is a way of informing the program about the name and type of an entity, it will handle. A definition on the other hand initiates storage or defines where the implementation of the entity ...
1.Declaration is just naming the variable. Definition is declarartion without intialisation. initialisation is declaration with definition at thesame time. 2.Variables may have garbage values. Variables may or may not have garbage values. Variables do not have garbage values. 3 Declaration can be done any number of times.
Show 3 more. A C++ program consists of various entities such as variables, functions, types, and namespaces. Each of these entities must be declared before they can be used. A declaration specifies a unique name for the entity, along with information about its type and other characteristics. In C++ the point at which a name is declared is the ...
A simple declaration is a statement that introduces, creates, and optionally initializes one or several identifiers, typically variables. decl-specifier-seqinit-declarator-list (optional); (1) attrdecl-specifier-seqinit-declarator-list; (2) attr. -. (since C++11) sequence of any number of attributes.
1. 4. Declaration is for the compiler to accept a name(to tell the compiler that the name is legal, the name is introduced with intention not a typo). Definition is where a name and its content is associated. The definition is used by the linker to link a name reference to the content of the name. - Gab是好人.
assignment: throwing away the old value of a variable and replacing it with a new one. initialization: it's a special kind of assignment: the first.Before initialization objects have null value and primitive types have default values such as 0 or false.Can be done in conjunction with declaration. declaration: a declaration states the type of a variable, along with its name.
Declaring - Declaring a variable means to introduce a new variable to the program. You define its type and its name. int a; //a is declared. Instantiate - Instantiating a class means to create a new instance of the class. Source. MyObject x = new MyObject(); //we are making a new instance of the class MyObject.