Search Placementyogi.com
 

C Constants

A constant is a value that does not change during execution of the program. A "constant" is a number, character or character string that can be used in a program as a value.

C supports several types of constants 

  • Integer constants
  • Real or Floating Point (Float) constants
  • Character constants

Integer constants

An integer constant can be decimal, hexadecimal or octal. Prefix specifies the base. 

For Example :

12       /*Decimal*/
99       /*Decimal*/
OX1A     /*Hexadecimal */	
ox1a     /*Hexadecimal */
o23      /*Octal*/
O22      /*Octal*/

An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.

12    /*int*/
12l   /*long int*/
12L   /*long int*/
12u   /*unsigned int*/
12ul  /*unsigned long int*/
12UL  /*unsigned long int*/

Rules for Constrcting integer constant

  1. Must have one  or more digit without a decimal point.
  2. Can be either positive or negative. If no sign precedes the constant, it is positive
  3. No comma or blanks allowed within an integer constant
  4. Integer constants are by default int.

Example of invalid integer constants:

1.5  /*Not Valid*/
5+   /*Not valid */
1,5  /*Not valid */

Real or Floating Point (Float) constants

A floating point constant has integer part, decimal and fractional part. A floating point constant can also be represented in exponential form.

Example:

+123.50  /* 123 is decimal part, 50 is fractional part 8*/
-924.0   
+3.42e-4 /* In fractional form : 0.000342 */ 
12.5E8

Rules for constructing floating point constants

  1. It Must have at least one digit and only one decimal point.
  2. It Can be either positive or negative. If no sign precedes the constant, it is positive.
  3. No comma or blanks allowed within a floating constant. 
  4. In Exponential form, ‘e’ can be either in lower case or upper case.
  5. Floating-point constants default to type double.
  6. By using the suffixes f or l (or F or L ), the constant can be specified as float or long double, respectively.
Example :
1.1.1      /*Not valid : two decimal points*/
1,1.1      /*Not Valid : comma not allowed */
5.4e5L     /*Not Valid : L not allowed in exponential form*/
3.41E      /*Not Valid : no exponent after E*/ 

Character constants

A 'character constant' is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas.

Example :  'A'  'g'  '1'  '='  or  '\t'

char datatype variables can be used to store character constants. The length of character constant is always 1 byte. 

Rules for constructing character constants 

  1. Single alphabet, digit or special symbol.
  2. Should be enclosed within single inverted commas.
  3. Since characters are internally treated as integers, the ASCII values of character is what is stored internally.For example ASCII for ‘A’ is  65.
  4. Range : -128 to 127
  5. Example: ‘C’,‘7’, Escape sequence like ‘\n’

 

Banner to LogicGuns.com

Hide Page Information

c constants, types of c constants

c constants, types of c constants, c constants tutorial, primary constant, secondary constant