Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The GNU C Programming Tutorial - const

Node:const, Next:, Previous:Constants, Up:Constants



const

Sometime a variable must be assigned a value once and once only; for example, it might be in read-only memory. The reserved word const is, like static and volatile, a data type qualifier that can be applied to many different data types. It declares a variable to be a constant, whose value cannot be reassigned. A const must be assigned a value when it is declared.

const double avogadro = 6.02e23;
const int moon_landing = 1969;

You can also declare constant arrays:

const int my_array[] =
{0, 1, 2, 3, 4, 5, 6, 7, 8};

Any attempt to assign a new value to a const variable will result in a compile-time error such as the following:

const.c: In function `main':
const.c:11: warning: assignment of read-only variable `avogadro'

 
 
  Published under free license. Design by Interspire