You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: "Describes type qualifiers for the C language used in the Microsoft Visual C compiler"
4
+
ms.date: "11/6/2020"
4
5
helpviewer_keywords: ["volatile keyword [C], type qualifier", "type qualifiers", "volatile keyword [C]", "qualifiers for types", "const keyword [C]", "memory, access using volatile", "volatile keyword [C], type specifier"]
5
6
ms.assetid: bb4c6744-1dd7-40a8-b4eb-f5585be30908
6
7
---
7
8
# Type Qualifiers
8
9
9
10
Type qualifiers give one of two properties to an identifier. The **`const`** type qualifier declares an object to be nonmodifiable. The **`volatile`** type qualifier declares an item whose value can legitimately be changed by something beyond the control of the program in which it appears, such as a concurrently executing thread.
10
11
11
-
The two type qualifiers, **`const`**and **`volatile`**, can appear only once in a declaration. Type qualifiers can appear with any type specifier; however, they cannot appear after the first comma in a multiple item declaration. For example, the following declarations are legal:
12
+
The type qualifiers, **`const`**, **`restrict`**, and **`volatile`**, can appear only once in a declaration. Type qualifiers can appear with any type specifier; however, they can't appear after the first comma in a multiple item declaration. For example, the following declarations are legal:
12
13
13
-
```
14
+
```c
14
15
typedefvolatileint VI;
15
16
constint ci;
16
17
```
17
18
18
-
These declarations are not legal:
19
+
These declarations aren't legal:
19
20
20
-
```
21
+
```c
21
22
typedefint *i, volatile *vi;
22
23
float f, const cf;
23
24
```
@@ -26,31 +27,69 @@ Type qualifiers are relevant only when accessing identifiers as l-values in expr
26
27
27
28
## Syntax
28
29
29
-
*type-qualifier*:
30
-
**constvolatile**
30
+
*`type-qualifier`*:\
31
+
 **`const`**\
32
+
 **`restrict`**\
33
+
 **`volatile`**
34
+
35
+
## `const` and `volatile`
31
36
32
37
The following are legal **`const`** and **`volatile`** declarations:
33
38
34
-
```
35
-
int const *p_ci; /* Pointer to constant int */
36
-
int const (*p_ci); /* Pointer to constant int */
37
-
int *const cp_i; /* Constant pointer to int */
38
-
int (*const cp_i); /* Constant pointer to int */
39
-
int volatile vint; /* Volatile integer */
39
+
```c
40
+
intconst *p_ci; // Pointer to constant int
41
+
intconst (*p_ci); // Pointer to constant int
42
+
int *const cp_i; // Constant pointer to int
43
+
int (*const cp_i); // Constant pointer to int
44
+
int volatile vint; // Volatile integer
40
45
```
41
46
42
-
If the specification of an array type includes type qualifiers, the element is qualified, not the array type. If the specification of the function type includes qualifiers, the behavior is undefined. Neither **`volatile`** nor **`const`** affects the range of values or arithmetic properties of the object.
43
-
44
-
This list describes how to use **`const`** and **`volatile`**.
47
+
If the specification of an array type includes type qualifiers, the element is qualified, not the array type. If the specification of the function type includes qualifiers, the behavior is undefined. **`volatile`** and **`const`** don't affect the range of values or arithmetic properties of the object.
45
48
46
49
- The **`const`** keyword can be used to modify any fundamental or aggregate type, or a pointer to an object of any type, or a **`typedef`**. If an item is declared with only the **`const`** type qualifier, its type is taken to be **const int**. A **`const`** variable can be initialized or can be placed in a read-only region of storage. The **`const`** keyword is useful for declaring pointers to **`const`** since this requires the function not to change the pointer in any way.
47
50
48
-
- The compiler assumes that, at any point in the program, a **`volatile`** variable can be accessed by an unknown process that uses or modifies its value. Therefore, regardless of the optimizations specified on the command line, the code for each assignment to or reference of a **`volatile`** variable must be generated even if it appears to have no effect.
51
+
- The compiler assumes that, at any point in the program, a **`volatile`** variable can be accessed by an unknown process that uses or modifies its value. Regardless of the optimizations specified on the command line, the code for each assignment to or reference of a **`volatile`** variable must be generated even if it appears to have no effect.
52
+
53
+
If **`volatile`** is used alone, **`int`** is assumed. The **`volatile`** type specifier can be used to provide reliable access to special memory locations. Use **`volatile`** with data objects that may be accessed or altered by signal handlers, by concurrently executing programs, or by special hardware such as memory-mapped I/O control registers. You can declare a variable as **`volatile`** for its lifetime, or you can cast a single reference to be **`volatile`**.
54
+
55
+
- An item can be both **`const`** and **`volatile`**, in which case the item couldn't be legitimately modified by its own program, but could be modified by some asynchronous process.
56
+
57
+
## `restrict`
49
58
50
-
If **`volatile`**is used alone, **`int`** is assumed. The **`volatile`** type specifier can be used to provide reliable access to special memory locations. Use **`volatile`** with data objects that may be accessed or altered by signal handlers, by concurrently executing programs, or by special hardware such as memory-mapped I/O control registers. You can declare a variable as **`volatile`** for its lifetime, or you can cast a single reference to be **`volatile`**.
59
+
The **`restrict`** type qualifier, introduced in C99, can be applied to pointer declarations. It qualifies the pointer, not what it points at.
51
60
52
-
- An item can be both **`const`** and **`volatile`**, in which case the item could not be legitimately modified by its own program, but could be modified by some asynchronous process.
61
+
**`restrict`** is an optimization hint to the compiler that no other pointer in the current scope refers to the same memory location. That is, only the pointer or a value derived from it (such as pointer + 1) is used to access the object during the lifetime of the pointer. This helps the compiler produce more optimized code. C++ has an equivalent mechanism, [`__restrict`](../cpp/extension-restrict.md)
62
+
63
+
Keep in mind that **`restrict`** is a contract between you and the compiler. If you do alias a pointer marked with **`restrict`**, the result is undefined.
@@ -127,7 +128,7 @@ When an array is passed to a function, it's passed as a pointer to the first ele
127
128
The following example shows a function that accepts an array and a length. The pointer points to the original array, not a copy. Because the parameter isn't **`const`**, the function can modify the array elements.
Like the **__declspec ( [restrict](../cpp/restrict.md) )** modifier, the **`__restrict`** keyword indicates that a symbol is not aliased in the current scope. The **`__restrict`** keyword differs from the `__declspec (restrict)` modifier in the following ways:
10
+
Like the **`__declspec` ( [`restrict`](../cpp/restrict.md) )** modifier, the **`__restrict`** keyword (two leading underscores '_') indicates that a symbol isn't aliased in the current scope. The **`__restrict`** keyword differs from the `__declspec (restrict)` modifier in the following ways:
11
11
12
-
- The **`__restrict`** keyword is valid only on variables, and `__declspec (restrict)` is only valid on function declarations and definitions.
12
+
- The **`__restrict`** keyword is valid only on variables, and `__declspec (restrict)` is only valid on function declarations and definitions.
13
13
14
-
-**`__restrict`** is similar to **`restrict`** from the C99 spec, but **`__restrict`** can be used in C++ or C programs.
14
+
-**`__restrict`** is similar to [`restrict`](../c-language/type-qualifiers.md#restrict) for C starting in C99, but **`__restrict`** can be used in both C++ and C programs.
15
15
16
-
- When **`__restrict`** is used, the compiler will not propagate the no-alias property of a variable. That is, if you assign a **`__restrict`** variable to a non-**`__restrict`** variable, the compiler will still allow the non-__restrict variable to be aliased. This is different from the behavior of the **`restrict`** keyword from the C99 specification.
16
+
- When **`__restrict`** is used, the compiler won't propagate the no-alias property of a variable. That is, if you assign a **`__restrict`** variable to a non-**`__restrict`** variable, the compiler will still allow the non-__restrict variable to be aliased. This is different from the behavior of the C99 C language **`restrict`** keyword.
17
17
18
-
Generally, if you affect the behavior of an entire function, it is better to use `__declspec (restrict )` than the keyword.
18
+
Generally, if you want to affect the behavior of an entire function, use **`__declspec (restrict)`** instead of the keyword.
19
19
20
-
For compatibility with previous versions, **_restrict** is a synonym for **`__restrict`** unless compiler option [/Za \(Disable language extensions)](../build/reference/za-ze-disable-language-extensions.md) is specified.
20
+
For compatibility with previous versions, **`_restrict`** is a synonym for **`__restrict`** unless compiler option [`/Za`\(Disable language extensions)](../build/reference/za-ze-disable-language-extensions.md) is specified.
21
21
22
22
In Visual Studio 2015 and later, **`__restrict`** can be used on C++ references.
23
23
24
24
> [!NOTE]
25
-
> When used on a variable that also has the [volatile](../cpp/volatile-cpp.md) keyword, **`volatile`** will take precedence.
25
+
> When used on a variable that also has the [`volatile`](../cpp/volatile-cpp.md) keyword, **`volatile`** will take precedence.
Supports the ios class from the old iostream library.
11
+
Supports the `ios` class from the old `iostream` library.
12
12
13
13
```cpp
14
14
typedef basic_ios<char, char_traits<char>> ios;
15
15
```
16
16
17
17
### Remarks
18
18
19
-
The type is a synonym for class template [basic_ios](../standard-library/basic-ios-class.md), specialized for elements of type **`char`** with default character traits.
19
+
The type is a synonym for class template [`basic_ios`](../standard-library/basic-ios-class.md), specialized for elements of type **`char`** with default character traits.
20
20
21
-
## <aname="streamoff"></a> streamoff
21
+
## `streamoff`
22
22
23
23
Supports internal operations.
24
24
@@ -32,9 +32,9 @@ Supports internal operations.
32
32
33
33
### Remarks
34
34
35
-
The type is a signed integer that describes an object that can store a byte offset involved in various stream positioning operations. Its representation has at least 32 value bits. It is not necessarily large enough to represent an arbitrary byte position within a stream. The value `streamoff(-1)` generally indicates an erroneous offset.
35
+
The type is a signed integer. It describes an object that can store a byte offset in stream positioning operations. Its representation has at least 32 value bits. It isn't necessarily large enough to represent an arbitrary byte position within a stream. The value `streamoff(-1)` generally indicates an erroneous offset.
36
36
37
-
## <aname="streampos"></a> streampos
37
+
## `streampos`
38
38
39
39
Holds the current position of the buffer pointer or file pointer.
The type is a synonym for [fpos](../standard-library/fpos-class.md)< `mbstate_t`>.
47
+
The type is a synonym for [`fpos`](../standard-library/fpos-class.md)< `mbstate_t`>.
48
48
49
49
### Example
50
50
@@ -69,7 +69,7 @@ int main( )
69
69
7
70
70
```
71
71
72
-
## <aname="streamsize"></a> streamsize
72
+
## `streamsize`
73
73
74
74
Denotes the size of the stream.
75
75
@@ -83,11 +83,11 @@ Denotes the size of the stream.
83
83
84
84
### Remarks
85
85
86
-
The type is a signed integer that describes an object that can store a count of the number of elements involved in various stream operations. Its representation has at least 16 bits. It is not necessarily large enough to represent an arbitrary byte position within a stream.
86
+
The type is a signed integer that describes an object that can store a count of the number of elements involved in various stream operations. Its representation has at least 16 bits. It isn't necessarily large enough to represent an arbitrary byte position within a stream.
87
87
88
88
### Example
89
89
90
-
After compiling and running the following program, look at the file test.txt to see the effect of setting `streamsize`.
90
+
After compiling and running the following program, look at the file `test.txt` to see the effect of setting `streamsize`.
91
91
92
92
```cpp
93
93
// ios_streamsize.cpp
@@ -105,19 +105,19 @@ int main( )
105
105
}
106
106
```
107
107
108
-
## <a name="wios"></a> wios
108
+
## `wios`
109
109
110
-
Supports the wios class from the old iostream library.
110
+
Supports the `wios` class from the old `iostream` library.
The type is a synonym for class template [basic_ios](../standard-library/basic-ios-class.md), specialized for elements of type **`wchar_t`** with default character traits.
118
+
The type is a synonym for class template [`basic_ios`](../standard-library/basic-ios-class.md), specialized for elements of type **`wchar_t`** with default character traits.
119
119
120
-
## <aname="wstreampos"></a> wstreampos
120
+
## `wstreampos`
121
121
122
122
Holds the current position of the buffer pointer or file pointer.
0 commit comments