What is the Difference between Constant and Volatile in C [duplicate] - c++

This question already has answers here:
What are the differences between const and volatile pointer in C?
(4 answers)
Usage wise difference between const & volatile qualifier in C?
(6 answers)
Closed 9 years ago.
what is the difference between Constant and Volatile
and
What is difference
Volatile int i =10
And
int i =10;
please let me briefly

For a start
int i = 0;
Is not a constant, as it requires the const keyword. A constant cannot be modified and requires to be initialized (you can't put const int i; i = 10;)
Volatile [variables] can be modified externally, for example
asm volatile
Would state the the assembly code can be externally modified

Related

Is it UB to modify a const object's member via its non-const reference? [duplicate]

This question already has answers here:
C++ const object's member can be modified
(1 answer)
Why does C++ not have a const constructor?
(5 answers)
C++, Classes, Const, and strange syntax
(8 answers)
Closed 3 months ago.
class T {
public:
int v;
int &vRef;
public:
T() : v(0), vRef(v) {}
};
const T t; // note, it's a const object
t.vRef = 2;
printf("v: %d\n", t.v);
The code presented above compiles OK, and the const object's internal value did change.
Question. Is this Undefined Behavior or not?
Yes. If the object is declared as const, then modifying it (through any means, be that a non-const reference like in your example, via const_cast or something else) is UB.

Is there a difference between static const int and const int? [duplicate]

This question already has answers here:
C++ semantics of `static const` vs `const`
(2 answers)
What is the difference between static const and const?
(4 answers)
Closed 3 years ago.
Is there a difference between const int with the preceded class qualifier of const (static const int) and const int or is it the same?
If it isnĀ“t equivalent,
Why should I explicitly make a const variable also static?

is_const<const int&>::value is false -- why? [duplicate]

This question already has answers here:
Why does std::is_const<const int&>::value evaluate to false?
(2 answers)
Closed 7 years ago.
Why does this static assertion fire?
static_assert(std::is_const<const int&>::value, "Pain");
Would be awesome to get both a syntactic (why the implementation would do this) and a semantic reasoning (why they would have designed this type trait's interface to do this).
I am aware it is possible to throw in a std::remove_reference call to get the expected outcome, but I'm not sure why that's necessary.
const int& is a reference to const int. So the reference itself isn't const.
It's slightly confusing, so I'm going to present an analogy with const int*. It's the pointer to const int. But you can modify it
const int a = 5, b = 7;
const int* ptr = &a;
ptr = &b; // pointer is modified
so the pointer isn't const. (the const pointer would be int* const instead)

What is the difference between const T * and T * const? [duplicate]

This question already has answers here:
What is the difference between const int*, const int * const, and int const *?
(23 answers)
constant pointer vs pointer on a constant value [duplicate]
(11 answers)
Closed 8 years ago.
For a given type T:
What is the difference between const T * and T * const?
Also, are there other places that const can go? For instance, is T const * a thing? Can you have more than one const in an expression, such as const T * const?
const T *x means x points to an object of type T, but that object should not be modified.
On the other hand, T *const x means the pointer itself cannot be modified (but the object it points to can).
const T *const x is just a combination of the two; both the pointer and the object it points to cannot be modified.

C++ const variable in function declaration [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the difference between const int*, const int * const, and int const *?
I am trying to understand how const works in a function parameter. What is the difference between void function(int const *&i); and void function(const int *&i);? And I would appreciate if you gave an example of their implementation and of the usage of i.
They are the same, just read it from right to left:
First example: i is a reference to a pointer of a constant integer.
Second example: i is a reference to a pointer of an integer constant.
The placement of const only changes things on either side of the pointer. For example:
const int* i
means: i is a pointer to a constant integer
whereas int* const i
means: i is a constant pointer to an integer.
Those declarations are equivalent. For more info, see this page.
Best place for const correctness is the - C++ FAQ Lite
They're both the same.
It means a reference to a pointer to a const int.