Constants in C Language A symbolic constant is a constant 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 constants In C language 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 the 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 In C language:
Numeric constants In C Language
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 type in lower case.
- A ‘C’ statement can be write 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 allow within a variable, constant or keyword.
- Comments cannot be nest. 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 declare outside of all functions. Generally, a variable, which is use in more than one function, can be identified as global variable
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 involve 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.