Constants

A symbolic constant is a constant that is represented by a name (symbol) in your program. Like a literal constant, a symbolic constant can’t change. Whenever you need the constant’s value in your program, you use its name as you would use a variable name. The actual value of the symbolic constant needs to be entered only once, when it is first defined.

  • C has two methods for defining a symbolic constant: the #define directive and the const keyword

#define CONSTNAME
Ex: #define PI 3.14159
Defining Constants with the const Keyword
The second way to define a symbolic constant is with the const keyword. const is a modifier that can be applied to any variable declaration. A variable declared to be const can’t be modified during program execution–only initialized at the time of declaration. Here are some examples:
1.const int COUNT = 100;
2.const float PI = 3.14159;
3.const long DEPT= 12000000,
4.float TAX_RATE = 0.21;
Difference between #define and const
1. When you use #define, all the constants get replaced with their actual values before compilation by the preprocessor) and const is used by compiler.
2. #define is always global, const can be local (global also)
3. #define doesn’t have type checking where type checking is part of const
Literal Constants:
Numeric constants
1. Integer constants:

  • Decimal: 524, +45, -369, 8000, ,9876L, 100L
  • Octal: 071, 032, (078 is illegal because 8 is not octal)
  • Hexadecimal: 0x39FEE, 0x53aDbe (0 to 9 and A to E)

2. Real constants: +456.25, -500.2523,30.0, 3.42e-4, 85.,-1.2E-1
Character Constants
1. Single Character constants: ‘A’, ‘f’, ‘4’, ‘=’
2. String constants:”Hello”,”1987″
Backslash Character Constants or escape sequences: ‘\a’,’\b’,’\n’,’\r’,’\t’,’\”,’,’\\’,’\?’

NOTE:
All ‘C’ statements must end with semicolon.

  • ‘C’ is case-sensitive. This is upper case and lower case characters are different. Generally the statements are typed in lower case.
  • A ‘C’ statement can be written in one line or it can split into multiple lines.
  • Braces must always match upon pairs, i.e., every opening brace { must have a matching closing brace }.
  • No restriction in using blank lines, which separate different words or different parts of program. Blank spaces improve the readability of the statements. But blank spaces are not allowed within a variable, constant or keyword.
  • Comments cannot be nested. For example, /* A simple ‘C’ program/* it prints a message */*/ is not valid.
  • A comment can be split into more than one line.
  • Finally, the global variables can be declared outside of all functions. Generally, a variable, which is used in more than one function, can be identified as global variable

null

Mr. Sandeep Soni

Founder, Trainer & CEO, Deccansoft Software Services.

Sandeep has 21 yrs of experience working in various Microsoft Technologies/Platforms incl. VB6.0, ASP, VC++, VB.NET, C#. He is involved in managing and architecting projects at Deccansoft. He will be your liaison to Deccansoft, for any kind of communication and project updates. He knows what works and what doesn’t, and what practices are most suitable for design and programming with the ultimate goal of producing a quality system.