Constants in C language | what is Constant and Different types of Constants
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.
- Numeric Constants.
- Integer Constants
- Real or Floating point Constants
- Character Constants.
- String Constants.
As you can see, Numeric Constants further have two types, I.e Integer Constants and Real Constants.
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 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:
Examples of Invalid character constants :
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:
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
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.
- Start Here – Step by step tutorials to Learn C programming Online with Example Programs – SillyCodes