Sabitler kodun herhangibir yerinde değiştirilemeyecek değişkenlerin tanımlaması için bir yol sağlar. Sabitler değişken deklarasyonunun önüne “const” anahtar kelimesi eklenerek tanımlanabilir. Eğer ki anahtar kelime “volatile” (her yerden erişilebilen), sabitten sonra yerleştirilirse, bu dış rutinlerin değişkenleri dışarıdan değiştirmesine müsaade etmesini sağlayacaktır( örneğin donanımsal müdahale ile ). Bu da ayrıca compiler’ı (derleyiciyi), değişkenin değerini register içerisinde optimize etmek yerine, her seferinde geri çağırmak için zorlayacaktır.
Sabit numaralar aşağıda ki yol ile tanımlanabilir.
Hexadecimal sabit:
0x
hexadecimal digits…
hexadecimal digits herhangi bir digit veya herhangi bir harfA
danF'ye
veyaa'dan
f
.Decimal sabit:
İlk numaranın sıfır olmadığı herhangi bir sayı.
Octal sabit:
İlk numarası sıfır olan herhangi bir sayı..
Floating constant:
Opsiyonel olarak e veya E tarafından takip edilen “exponential” kesirli bir sayı.
Sayı U veya u tarafından eklenebilir :
Buda sayının unsigned long integer (işaretsiz uzun integer) olmasını sağlar.
L
veyal
:Eğer ki sayı floating point sayısı ise, sayı long doubledır, ya da sayı unsigned long integer (işaretsiz uzun integer).
F
veyaf
:Sayının floating- sayı numarası olmasına sebep olur.
Örnekler:
const float PI=3.141;
Değişken PI’nin 3.141 olmasını sağlar. Sonradan yazılan PI değeri geçersiz oolacaktır.
const int joe=0xFFFF;
joe’nin değerinin 65535 decimal’ı oluşturmasına sebep olur.
const float penny=7.4e5;
penny’nin değerinin 740000.000000. olmasını sağlar.
1.2.6 Constants
Constants provide a way to define a variable which cannot be modified by any other part in the code. Constants can be defined by placing the keyword const
in front of any variable declaration. If the keyword volatile
is placed after const
, then this allows external routines to modify the variable (such as hardware devices). This also forces the compiler to retrieve the value of the variable each time it is referenced rather than possibly optimizing it in a register.
Constant numbers can be defined in the following way:
Hexadecimal constant:
0x
hexadecimal digits…
Where hexadecimal digits is any digit or any letterA
throughF
ora
throughf
.Decimal constant:
Any number where the first number is not zero.
Octal constant:
Any number where the first number must be zero.
Floating constant:
A fractional number, optionally followed by either
e
orE
then the exponent.The number may be suffixed by:
U
oru
:Causes the number to be an unsigned long integer.
L
orl
:If the number is a floating-point number, then it is a long double, otherwise it is an unsigned long integer.
F
orf
:Causes the number to be a floating-point number.
Examples:
const float PI=3.141;
Causes the variable PI to be created with value 3.141. Any subsequent attempts to write to PI are not allowed.
const int joe=0xFFFF;
Causes joe to be created with the value of 65535 decimal.
const float penny=7.4e5;
Causes penny to be created with the value of 740000.000000.