C TUTORIALS

LEARN C PROGRAMMING

A character can be any alphabet, digit or a special symbol, which is used to represent any kind of information. When the alphabets, digits and special symbols are properly combined, they form either constant, variables or keywords. A constant is a value that doesn’t change, where is a variable is a value that may change. A keyword is a word that carries special meaning and performs fixed functions. We have covered this in detail in our previous articles.

In this post, we will learn about the rules for constructing C Constants, in C programming Language.

Types of C Constants

C constants can be divided into two major types:

  • Primary constants
  • secondary constant

These constants are further categorized as:

PRIMARY CONSTANTS

  1. Integer Constant
  2. Real Constant
  3. Character Constant

SECONDARY CONSTANTS

  1. Array
  2. Pointer
  3. Structure
  4. Union
  5. Enum etc

Here we will discuss primary constants only and secondary constants later. For constructing these different types of constants, certain rules have been defined. These rules are as follows:

Rules for constructing integer constants

  • An integer constant must have one or more digit(s).
  • Integer constant must not have a decimal point.
  • It can be either positive or negative.
  • If no sign proceeds an integer constant, it is assumed to be positive.
  • There should not be comma (,)or blank( ) in the name of a integer constant.
  • The allowable range for integer constant -2147 483648 to + 2147483647. This range can varies from compiler to compilers.

Rules for constructing real constants

Real constants are also called floating point constants. The real constants could be written in two forms – fractional form or exponential form.

Following rules must be observed while constructing real constants expressed in the fractional form:

  • A real constant must have one or more digit(s).
  • It must contain a decimal point. By default it is positive.
  • It can also be either positive or negative.
  • For real constants also the default sign is positive.
  • There should not be comma(,) or blank( ) in the name of a real constant.

The exponential form is usually used with too small values or too large values of the constant.  However, we can use the exponential form for other real constants also in any way.

In exponential form, the real constant is divided into two parts. These are: The part appearing before ‘e’ is known as mantissa, whereas the part following ‘e’ is known as exponent. Thus 0.000342 can be written in exponential form as 3.42e-4.

Following rules must be kept into consideration while constructing real constants expressed in the exponential form:

  • The Mantissa part and the exponential part of the exponential constant are separated by a letter e or E.
  • The Mantissa part may have either a positive or a negative sign. The default sign of mantissa part is also positive.
  • The exponent must have one or more digit(s).The exponent part has the positive or the negative sign accordingly.
  • Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.

Rules for constructing character constants

  • A character constant can be a single alphabet, a single digit or a single special symbol represented within single inverted commas.
  • For example, ‘Z’ is a valid character constant whereas `Z’ is not.

%d bloggers like this: