Constants in C Language with examples explain

Constant is a any value that cannot be changed during program execution. In any Language Constants are very important, so to know Constants in C Language with examples read complete article. 

In C, any number, single character, or character string is known as a constant. A constant is an entity that doesn’t change whereas a variable is an entity that may change. For example, the number 50 represents a constant integer value. 

The character string "Programming in C is fun.\n" is an example of a constant character string. C constants can be divided into two major categories: 

  1. Primary constants − Integer, float, and character are called as Primary constants.
  2. Secondary constants − Array, structures, pointers, Enum, etc., called as secondary constants.

List of Constants in C Language

There are different types of constants in C programming.

  • Decimal Constant 10, 20, 450 etc.
  • Real or Floating-point Constant 10.3, 20.2, 450.6 etc.
  • Octal Constant 021, 033, 046 etc.
  • Hexadecimal Constant 0x2a, 0x7b, 0xaa etc.
  • Character Constant 'a', 'b', 'x' etc.
  • String Constant "c", "c program", "c in javatpoint" etc.


NUMERIC CONSTANTS in C Example

  • Example for an integer constant is 786,-127
  • Long constant is written with a terminal ‘l’or ‘L’,for example 1234567899L is a Long constant.
  • Unsigned constants are written with a terminal ‘u’ or ‘U’,and the suffix ‘ul’ and ‘UL’ indicates unsigned long. for example 123456789u is a Unsigned constant and 1234567891ul is an unsigned long constant.
  • The advantage of declaring an unsigned constant is to increase the range of storage.
  • Floating point constants contain a decimal point or an exponent or both. 
  • For Eg : 123.4,1e-2,1.4E-4,etc.The suffixes f or F indicate a float constant while the absence of for F indicate the double, l or L indicate long double.

CHARACTER CONSTANTS in C


A character constant is written as one character with in single quotes such as ‘a’. The value of a character constant is the numerical value of the character in the machines character set. certain character constants can be represented by escape sequences like ‘\n’. These sequences look like two characters but represent only one.

The following are the some of the examples of escape sequences:

  1. \a  -Alert
  2. \b Backspace 
  3. \f Form feed 
  4. \n New Line 
  5. \r Carriage return 
  6. \t Horizontal Tab 
  7. \v Vertical Tab 

STRING CONTANTS IN C LANGAUGE EXAMPLE

String constants or string literal is a sequence of zero or more characters surrounded by a double quote. 

Example , “ I am a little boy”. quotes are not a part of the string.


To distinguish between a character constant and a string that contains a single character 

ex: ‘a’ is not same as “a”. ‘a’ is an integer used to produce the numeric value of letter a in the machine character set, while “a” is an array of characters containing one character and a ‘\0’ as a string in C is an array of characters terminated by NULL.

There is one another kind of constant i.e Enumeration constant , it is a list of constant integer values.

Ex.: enum color { RED, Green, BLUE }


The first name in the enum has the value 0 and the next 1 and so on unless explicit values are specified.

If not all values specified , unspecified values continue the progression from the last
specified value. 

For example

  • Enum months { JAN=1, FEB,MAR, …, DEC 
  • Where the value of FEB is 2 and MAR is 3 and so on. 
  • Enumerations provide a convenient way to associate constant values with names

So, all above is detail explanation of Constants in C Language with examples explain.

Thanks for visiting: Check more on our website.

Comments

Popular posts from this blog

Implement Bouncing Ball Animation Using C Graphics

Walking Stickman -Using C graphics

Sine Wave - Using C Graphics