CONSTANTS IN C

Dev Frank
3 min readDec 23, 2023

--

@enehfranklyn8

Constants are fixed values that cannot be changed throughout the program or during execution of a program, the value are to be fixed

#define PI 3.14    

This above is a symbolic constant.

TYPES OF CONSTANT

There a two types of constant

  1. Numeric Constant
  • Integer constant
  • Floating / Real constant

2. Character Constant

  • Single Character Constant
  • String Character Constant

INTEGER CONSTANT

These constants are integers, they have decimal values. Examples are 10, 5, 1, 0, etc.

But

05 != Decimal Consant
= Octal Constant

Integers are grouped into three

  • Decimal Constants → 0–9 (base 10)
  • Octal Constant → 0–7 (base 8)
  • Hexadecimal Constant → 0–15 (base 16)

NB: 05 is an octal decimal because octal value starts with 0 always. While in decimal 0 is always at the right hand side.

Constants are also called LITERALS. Generally by default integer constants are DECIMALS.

Hexadecimal

These are sequence of numbers preceded by 0x or 0X (i.e zero and capital X or zero and small x).Examples are 0x14, 0x2C, 0x3F etc.

In hexadecimal we use numbers from 0–9 and characters from A — F .

0,1,2,3,4,5,6,7,8,9,A,B,D,E,F

APPLICATION OF INTEGER CONSTANTS

1234. , , is integer constant, remember integer constants are sequence of digits.

56,100 is not an integer constants, because special characters are not allowed between constants.

56,100 is an INVALID CONSTANT 
-134 is a VALID CONSTANT
$35 is a INVALID CONSTANT
37 OO0 is an INVALID CONSTANT
(Whitespaces are specual characters)

REAL CONSTANT

Examples are 12.36, 3.12. They are types of values (constants) that have FRACTIONAL PARTS.

Take for instance

1   2   .   3   6

In the values above 1 & 2 are INTEGER PART, then we have the DECIMAL POINT , 3 & 6 are FRACTIONAL PART.

-56.02 is also a valid real constant.
12.56.03 is an invalid real constant,because two decimal point.

SINGLE CHARACTER CONSTANT

These are single character constants enclosed in a single quote mark. Examples ‘a’, ‘b’, ‘z’, ‘5’, ‘,’ ‘e’ etc.

NB: (5 != ‘5’) these are not the same, one is an integer constant and the other is a single character constant.

String Constant

These are sequence of characters enclosed in a double quote mark. Examples are “Frank’’, " abc’’.

The letters maybe numbers, special characters, whitespace etc .

Examples are “Smith$’’, “Jane Sam” etc.

“ a " != ‘ a ’ 

The former is a String Constant, while the latter is a Single Character Constant.

“ 1 " != ‘ 1 ’ 

The first is a String Constant and it’s not equivalent to it’s ASCII value, while the last one is a Single Character Constant and is equivalent to it’s ASCII value.

 “ Tense "

When a compiler reads this string constant “ Tense ’’, the compiler will store the address of the first character which is “ F ’’ and it will append a null character constant “ \0 ’’ the end of the string constant. WHY?

The compiler has to mark that “ \0 ’’ is the end of the specified string

(“ Tense\0’’). Now the length of the string (“ Tense\0 ’’) is now 6 not 5 anymore because, compiler has added null character just to mark that this is the end of the string.

--

--

Dev Frank

Passionate tech enthusiast, diving deep into the world of software engineering. Thrilled to share insights with the world. A Software engineering student.