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
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
5
ms.assetid: bb4c6744-1dd7-40a8-b4eb-f5585be30908
6
6
---
7
7
# Type Qualifiers
8
8
9
9
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
10
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:
11
+
The type qualifiers, **`const`**, **restrict**, 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
12
13
-
```
13
+
```c
14
14
typedefvolatileint VI;
15
15
constint ci;
16
16
```
17
17
18
18
These declarations are not legal:
19
19
20
-
```
20
+
```c
21
21
typedefint *i, volatile *vi;
22
22
float f, const cf;
23
23
```
@@ -26,31 +26,67 @@ Type qualifiers are relevant only when accessing identifiers as l-values in expr
The following are legal **`const`** and **`volatile`** declarations:
33
34
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 */
35
+
```c
36
+
intconst *p_ci; // Pointer to constant int
37
+
intconst (*p_ci); /* Pointer to constant int */
38
+
int *const cp_i; /* Constant pointer to int */
39
+
int (*const cp_i); /* Constant pointer to int */
40
+
int volatile vint; /* Volatile integer */
40
41
```
41
42
42
43
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
44
-
This list describes how to use **`const`** and **`volatile`**.
45
-
46
45
- 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
46
48
47
- 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.
49
48
50
49
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`**.
51
50
52
51
- 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.
52
+
53
+
## `restrict`
54
+
55
+
The **`restrict`** type qualifier, introduced in C99, is used with pointer declarations. It qualifies the pointer, not the object being pointed to. It is a hint to the compiler that for the lifetime of the pointer, only the pointer itself or a value derived from it (such as pointer + 1) will be used to access the object it points at. This means that any loads and stores through the restricted pointer are the only loads and stores to that address for the lifetime of the pointer. This helps the compiler optimize the performance of code that uses the pointer.
56
+
57
+
If you try to alias the pointer, you will get warning: 4245 For example:
0 commit comments