Home  >  Article  >  Backend Development  >  int const vs. const int: Are they Equivalent in C/C ?

int const vs. const int: Are they Equivalent in C/C ?

Barbara Streisand
Barbara StreisandOriginal
2024-10-28 04:45:02900browse

 int const vs. const int: Are they Equivalent in C/C  ?

const int vs. int const

In C and C , there are two possible ways to declare a constant integer:

  • int const x = 3;
  • const int x = 3;

Which is the correct way? Are they equivalent?

Answer:

Both int const x = 3; and const int x = 3; are valid code and they are both equivalent. They both declare a constant integer variable named x with a value of 3.

However, in the case of pointer types, int const and const int are not equivalent.

  • const int *p = &someInt; declares a pointer whose data cannot be changed through the pointer (it points to a constant integer).
  • int * const p = &someInt; declares a pointer who cannot be changed to point to something else (it points to a constant address).

The above is the detailed content of int const vs. const int: Are they Equivalent in C/C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn