Constants in ‘C’ | Readstall

 Constants in ‘C’

13-12-18 Gudditi Naganjaneyulu 0 comment

 Constants

  • C Constants are also like normal variables. But, only difference is, their values can not be modified by the program once they are defined.
  • Constants refer to fixed values. They are also called as literals
  • Constants may be belonging to any of the data type.
  • Syntax:

Constant data_type variable_name; (or) constant data_type *variable_name;

TYPES OF C CONSTANT:

a. Integer constants

b. Real or Floating point constants

c. Octal & Hexadecimal constants

       d. Character constants                                                                                                                                                                           

          e. String constants

f. Backslash character constants

Constant type data type (Example)
Integer constants int (53, 762, -478 etc )
unsigned int (5000u, 1000U etc)
long int, long long int
(483,647 2,147,483,680)
Real or Floating point constants float (10.456789)
doule (600.123456789)
Octal constant int (Example: 013 /*starts with 0 */)
Hexadecimal constant int (Example: 0x90 /*starts with 0x*/)
character constants char (Example: ‘A’, ‘B’, ‘C’)
string constants char (Example: “ABCD”, “Hai”)

RULES FOR CONSTRUCTING C CONSTANT:

INTEGER CONSTANTS IN C:

  • An integer constant must have at least one digit.
  • It must not have a decimal point.
  • It can either be positive or negative.
  • No commas or blanks are allowed within an integer constant.
  • If no sign precedes an integer constant, it is assumed to be positive.
  • The allowable range for integer constants is -32768 to 32767.

REAL CONSTANTS IN C:

  • A real constant must have at least one digit
  • It must have a decimal point
  • It could be either positive or negative
  •      If no sign precedes an integer constant, it is assumed to be positive.
  • No commas or blanks are allowed within a real constant.

CHARACTER AND STRING CONSTANTS IN C:

  • A character constant is a single alphabet, a single digit or a single special symbol enclosed within single quotes.
  • The maximum length of a character constant is 1 character.
  • String constants are  enclosed within double quotes.

BACKSLASH CHARACTER CONSTANTS IN C:

  • There are some characters which have special meaning in C language.
  • They should be preceded by backslash symbol to make use of special function of them.
  • Given below is the list of special characters and their purpose.
Backslash_character Meaning
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\” Double quote
\’ Single quote
\\ Backslash
\v Vertical tab
\a Alert or bell
\? Question mark


Leave a reply