Constants in C language | what is Constant and Different types of Constants

Introduction:

In our previous article, We have looked at the Operators and different types of Operators in C language. In todays article, We will learn about the Constant in C language and categories of C constants.

Constants in C Language :

Constants as the name suggests, The things which can’t be changed.

Similarly in C programming Constant is value that cannot be changed during execution of program. In other words It is Fixed value never be changed during the program execution. 


C Programming language support different types of Constants. All these Constants are broadly classified into three categories.

  1. Numeric Constants.
    1. Integer Constants
    2. Real or Floating point Constants
  2. Character Constants.
  3. String Constants.

As you can see, Numeric Constants further have two types, I.e Integer Constants and Real Constants.

Constants-in-c-language-programming-in-general

Let’s discuss about the each constant type.

Numeric Constants in C Language :

Numeric Constants are Number Constants. These Constants may have fractional part or may not have fractional part.

C Language have few rules to construct Numeric constants. Here are the rules.

  • Numeric constant should have at-least one digit.
  • No comma are Space is allowed within the Numeric Constant.
  • Numeric Constants can be positive value or Negative value. If you don’t specify any sign default sign is Positive.

The Numeric constants are further classified into two types

  • Integer Constants.
  • Real Constants or Floating point Constants

Integer Constants:

Integer constants are all about the Numbers. All Valid Integer numbers falls under this category.

Integer constants doesn’t contains Decimal Point (.)

Example of Integer Constants:

431, 85, 65, 31....etc.

Real Constants or Floating Point Constants:

Real Constants are Numeric constants with Decimal Point or Floating Point. Floating point data constants falls under this category.

Real Constants also called as Floating point Constants.

The Real constants can be written in two forms, Which are Fractional Representation and Exponential Representation.

Real Constants in Fractional Representation:

Examples : 0.7, 4.31, 98.2,...etc. ( Fractional representation ).

Real Constants in Exponential Representation:

Examples : +4.3e-5, 1.2e+5,...etc. ( Exponential Representation )

If you have very long number, It makes sense to represent it in exponential representation.

Character Constants :

A Character Constant is single character it is enclosed by the single quotes ( ‘ ‘ ).

Examples of Valid character constants:

'M'

'V'

'4'

'$'

Examples of Invalid character constants :

'Joey'

'Checo'

'Lewis'

Character constants should only have one character within the single quotes.

The single character with double quotes also not a valid character constant

Example : "M"  // Not a Valid Character Constant

The Double quotes are not allowed. If you use double quotes with single character, That will becomes the C string.

Character constant must be within single quotes.

Example:    v               // Not a Valid Character Constant         

In above example, The character v is not enclosed within the Single quotes. So it is not a valid Character constant.

Please follow above suggestions while creating the character constants.

String Constants in C Langauge :

A string character has zero or one or more characters Enclosed within the Double Quotes.(” “).

Examples of String Constants:

"Hello"

"Max"

"M1 Chip"

For each and every string in C programming, The compiler adds an additional NULL character at the end of the string. This NULL character is useful to detect the boundaries of string. For example, If you are reading the string one character by one, Then if you reach the NULL character, That means you reached the end of the string and you can stop printing the value.

There is no special Datatype for string constants C programming language. C Uses Character array to represent the String data.

Point to note here is that, Character constants are enclosed by single quotes and String constants are enclosed by Double quotes.

📢 Character constants are enclosed by single quotes and String constants are enclosed by Double quotes.

The "V" and The 'V' are completely different in C language. The first one ( "V" ) is string constant and second one ( 'V' ) is character constant.

Also The "V" contains two characters one is character V and another character is Compiler placed NULL Character while The 'V' contains only one character which is v

Conclusion:

In this article, We have discussed about the Constants in C programming language and different types of C constants like Numeric, Character and String Constants.

Related Articles:

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

11 Responses

  1. […] our previous article, We have discussed about the Constants in the C programming. In this article, We will discuss about the Identifiers in C and what are the rules for creating or […]

  2. […] unlike the Constants in C Language, The variables can change its value during the program […]

  3. […] two Constants named MAXROWS and MAXCOLUMNS and initialize them with 100. The MAXROWS and MAXCOLUMNS constants hold […]

  4. […] in the array are 100 ( change this if you need to use larger arrays). We can also use a Macro or Constant to store the 100 […]

  5. […] two integer constants named ROWS and COLUMNS, Which holds the max number of rows and columns.(Change this number if you […]

  6. […] two integer constants named ROWS and COLUMNS, Which holds the max number of rows and columns in the Matrix. (Modify this […]

  7. […] two integer constants called ROWS and COLUMNS with Value 100, The constant ROWS holds the max row size of the matrix and […]

  8. […] two integer constants named ROWS and COLUMNS, Which holds the max number of rows and columns in the Matrix. (Change these […]

  9. […] a Global constant named SIZE and initialize it with 100. This SIZE constant contains the max size of the character […]

  10. […] declaring a string and naming it as inputStr with the size of 100 characters (We are using a global constant to store the […]

  11. […] a string to store the input string(i.e inputStr). The size of the string is equal to the SIZE constant. which is equal to […]

Leave a Reply