The greatest mistake you can make in life is to be continually fearing you will make one

Thursday 25 February 2010

C Constants

  • A constant value is the one which does not change during the execution of a program
  • Constants are classified as shown in the picture

Integer Constant:
  • An integer constant must have atleast one digit.
  • It can be either positive or negative.If no sign precedes then it is assumed to be positive(unsigned).
  • No decimal point,commas or blanks are allowed in an integer constant
  • The range of integer constant depends upon the compiler. For a 16 bit compiler like Turbo C or Turbo C++ the range is -32768 to 32767
  •  Example 100,-200,+500
  • If an integer is too big to fit in to an integer range then it will taken as a long. A long constant is written with a terminal l (or) L. Example 123456789L
  • Unsigned constants are written with a terminal u (or) U.
  • Unsigned long constants are written with a terminal ul (or) UL.
  • The value of an integer can be specified in octal or hexadecimal instead of decimal. A leading 0(zero) on an integer constant means octal. A leading 0x (or) 0X on an integer constant means hexadecimal.
  • For example decimal 32 can be written as 040 in octal and 0x20 in hexadecimal.
  • unsigned(u) and long(l) can also be applied to octal and hexadecimal constants. for example 0xFUL is an unsigned long constant with decimal value 15.
Real Constant (or) Floating Point Constant
  • A real constant must have atleast one digit.
  • It must have a decimal point. No commas or blanks are allowed.
  • It could be either positive or negative. Default sign is positive.
  • It can be written in a fractional form(123.4) or an exponential form(1e-2).In exponential form the part appearing before 'e' is called mantissa and the part following 'e' is called exponent.
  • The suffixes f (or) F indicate a float constant.l or L indicate a long double.
  • Example: +123.45,500.0,-300.25,10e-5,-200e-25 
Character Constant
  • A character constant is an integer, written as a single alphabet,a single digit or a single special symbol enclosed within single quotes.
  • Example 'A','5','z','='
  • The value of  a character constant is the numeric value of the character in the machines character set.
  • The maximum length of a character constant can be 1 character.
  • Escape sequences are character constants.They look like 2 characters but they are single character only.Example: \n->newline and \t->horizontal tab
String Constant
  • Sequence of 0 or more characters surrounded by double quotes
  • Example "I am a String",empty string " "
  • The double quotes are not part of the string but used to delimit the string
  • String constants can be concatenated at compile time. for example "hello" "world" is equivalent to "hello world". This is useful for spliting long strings across several source lines.
  • The string constant is an array of characters. The internal representation of a string has a null character '\0' at the end.
  • strlen() function returns the number of characters in a string excluding '\0'
Enumeration Constant
  • An enumeration is a list of constant integer values. Example: enum bool{yes,no};
  • The first name in an enum has value 0, the next one has 1 and so on unless explicit values are specified.
  • If not all the values are specified, unspecified values continue the value from the last specified value.
  • Enumeration provide a convenient way to associate constant values with names.
  • Enumeration is an alternative to #define. In enum the values automatically defined.and easy to define large number of constants

    No comments:

    Post a Comment