Home > Backend Development > C++ > body text

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

Barbara Streisand
Release: 2024-10-28 04:45:02
Original
897 people have browsed it

 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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!