Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Basics of C Programming
  5. Constants
  6. Literal Constants

Literal Constants

Literal Constants or literals denotes a fixed value, which may be an integer, floating point number, character or a string

The type of literal constants is determined by its value

  • Integer Literal Constants
  • Floating Point Literal Constants
  • Character Literal Constants
  • String Literal Constants

Integer literal constants

Integer literal Constants are integer values like -1,2,8

Rules for writing integer literal constants are as follows:

  • An integer literal constant must have at least one digit
  • No decimal point
  • It can be either positive or negative 
  • No special characters and blank spaces are allowed within integer literal 
  • If an integer literal constant starts with 0, then it is assumed to be in octal number system. Example: 023 is valid, 23 is in octal its equivalent decimal is 19 
  • If an integer literal constant starts with 0x or 0X, then it is assumed to be in hexadecimal number system. Example:0x23 or 0X23 is valid, 23 is in hexadecimal its decimal equivalent is 35
  • The size of the integer literal constant is modified by length modifier. The length modifier can be l, L, u, U. Example: 23l is a long integer
  • 23u is an unsigned integer

Floating point literal constants

Floating point literal constants are value like -23.1, 12.8, -1.8e12. Floating point literal constants can be written in fractional form or in exponential form

Rules for writing floating point literal constants in fractional form are as follows:

  • Must have at least one digit
  • Should have decimal point
  • It can be either positive or negative 
  • No special characters and blank spaces are allowed within a floating point literal constants 
  • A floating point literal constant by default is assumed to be of type double, Example: 23.45 is double 
  • The size of the floating point literal constant can be modified by using the length modifier f or F Example: 23.45 is written as 23.45f or 23.45F
  • Some valid examples: -2.5, 12.523, 2.5f, 12.5F

Rules for writing floating point literal constants in exponential form are as follows:

  • A floating point literal constant is an exponential form has two parts: the mantissa part and the exponent part. Both parts are separated by e or E
  • The mantissa can be either positive or negative. Default is positive
  • The mantissa should have at least one digit 
  • The mantissa part can have decimal point, but not mandatory 
  • The exponent part must have at least one digit. It can be either positive or negative but default is positive 
  • The exponent part cannot have decimal point No special characters and blank spaces are allowed withing mantissa and exponent part
  • Example: -2.5E12, -2.5e-12. 2e10

Character Literal Constants

A character literal constant can have one enclosed within single quotes ‘A’, ‘a’, ‘\n’

Character literal constants are classified as:

  • Printable character literal constant
  • Non Printable character literal constant

Printable character literal constant

All characters except quotation mark, backslash and new line character when enclosed within single quotes form a printable character literal constant. Example: ‘A’, ‘#’

Non Printable character literal constant

  • Non printable character literal constants are represented with the help of escape sequences
  •  An escape sequence consists of a backward slash (\) followed by a character and both enclosed within single quotes

String Literal Constants

  • A string literal constant consists of a sequence of characters enclosed within double quotes
  • Each string literal constant is implicitly terminated by a null character (‘\0’)
  • The number of bytes occupied by a string literal constant is one more than the number of characters present in the string
  • The additional byte is occupied by the terminating null character
  • Thus, the empty string ( Example: “”) occupies one byte in the memory due to the presence of the terminating null character.
  • But, the terminating null character is not counted while determining the length of a string.
  • Therefore, the length of the string “ABC” is 3 though it occupies 4 bytes in the memory

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments