JavaClassCode.com

facebook twitter googleplus

C Tutorial

C Introduction C Installation C Keywords and Identifier C Variables C Constants C Data Types C Input/Output C Operators C Introduction Examples

C Control Flow

C if...else C for loop C while loop C break and continue C switch case C goto Control Flow Examples

C Functions

C Function C User-define Function C Function Types C Recursion C Storage Class C Function Examples

C Pointers

C Pointers C Pointers & Arrays C Pointers and Functions C Memory Allocation Array and Pointer Examples

C Strings

C Strings C String Functions C String Examples

Structure and Union

C Structure C Struct and Pointers C Struct and Function C Unions C Struct Examples

C Programming Files

C Files Input/Output C Files Examples

Additional Topics

C Enumeration C Preprocessors C Standard Library C Programming Examples

C Constants


C Constants are similar to variables, but their value is fixed in the entire program. The value of constants never changed during program execution. It is also called literals. It is advisable to define constants in upper-case names. Constants are declared using the const keyword and #define preprocessor.

Syntax of C Constant

or


constants decleration

For example

or

Example of C Constant

Output

Area of circle of raduis: 5 is 78.5 sq. units

If you try to change the value of const (constant), it will generate a compile time error. For example:

Output

Compile Time Error: Cannot modify a const object

Types of C Constants

The C Constants are broadly categorized into two major parts, which are further sub categorized:

types of c constants
  1. Primary Constants
    • Integer Constant
    • Real Constant
    • Character Constant
  2. Secondary Constants
    • Array
    • Pointer
    • Structure
    • Union
    • Enum. etc.

Integer Constant

Integer constant refers to the sequence of digits. It is further divided into three types as Decimal Integer, Octal Integer, and Hexadecimal Integer.

Rules to declare Integer Constants

  1. It must contain at least one digit.
  2. It can be a positive or negative value.
  3. It should not contain a decimal point.
  4. No blanks space or commas are allowed within an integer constant.
  5. The range of integer constant lies between -32768 to 32767.

For example: 426, +743, -876

Real Constant

Real constant are also called Floating Point constant. It contains a fractional number like 44.25. This constant could be written into fractional form or exponential form.

Rules to declare Real Constants

  1. It must contain at least one digit.
  2. It must contain a decimal point.
  3. It can be a positive or negative value.
  4. No blanks space or commas are allowed within an real constant.

For example: 353.25, 320.0, -40.25, -312.423

Character Constant

Character constant is a single alphabet, a single digit, or a single special symbol declared within single inverted commas. Its maximum length is 1 character.

For example: 'a', 'A', '4', '='

Escape Sequence "Backslash" character constant

Escape Sequence character constant is declared using a backslash in front of value. The Escape Sequence is their specific meaning known to the compiler.

For example: '\t' is used for a tab, '\n' is used for a new line.

ConstantsMeaning
\abeep sound
\bbackspace
\fform feed
\nnew line
\rcarriage return
\thorizontal tab
\vvertical tab
\'single quote
\"double quote
\\backslash
\nnnoctal number
\xhhhexadecimal number
\?question mark
\0null

Example of Escape Sequence

Output

This
is
an
example
of
Escape Sequence

In the above program an escape sequence "\n" brings the cursor to new line so that string after it prints in new line.