C Programming: What are Constants in C with examples and interview questions

C Programming, Constants in C

Constants:

  • Constants are the identifiers which cannot change their value during program execution once they are defined. The constants can be used same as the variable name in the program.
  • Constants are also known as "Literals".


There are different types of constants in c programming viz. :


  1. Decimal Constant
  2. Floating Point Constant
  3. Octal Constant
  4. Hexadecimal Constant
  5. Character Constant
  6. String Constant

➣Decimal Constant :

The decimal constant take values from 0 to 9. A decimal constant can have suffix also which 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.


Examples of Decimal Constant:

45
67u
45l
321ul
87lu


➣Octal Constant :

Octal constants take values from 0 to 7. The value is preceded by  '0'.


Examples of Octal Constant:
054
076
087        /*Invalid Octal Constant*/



➣Hexadecimal Constant :

  • Hexadecimal Constant take values from 0 to 15. the values id precede by "0x or 0X".
  • In hexadecimal constants the value after 9 will be taken as characters.These are the hexadecimal values from 0 to 15:
  • 0 1 2 3 4 5 6 7 8 9 a b c d e f.

Examples of Hexadecimal Constant:

0xf4
0Xf4            
0xabd
0x43
0x67


➣Floating Point Constant :


It is also known as "Real constant" because it represents real value i.e it takes integer, decimal and exponential value also.
Floating point constants are of three types on the basis of the memory size:

  1. float: Floating point numbers are defined by using "float" keyword and takes 2 bytes in the memory.
  2. double: Double keyword is used when precision provided by the float is not sufficient and takes 4 bytes of memory. 
  3. long double: Long double keyword is used to extend further more precision compare to double. It takes 8 bytes in the memory.

Examples of Hexadecimal Constant:

4.897
3.1546e-5
6.78e55


➣Character Constant :
A Character constant takes an alphabet, a digit or a special character. It can hold a single character at a time. The value of character constant can be enclosed in single quotes ' '.

Examples of Character Constant:

'a'
'M'
'\t'

There are certain character constants which represents a special meaning when preceded by backslash. Following are the backslash character constants :

\b        backspace
\v vertical tab
\" double quotes
\r carriage return
\t horizontal tab
\f form feed
\\ backslash
\? question mark
\a alert bell
\n new line
\' single quote


➣String Constant :

String constant is same as character constant but the values can be enclosed in doubles quotes " " instead of single quotes.


Examples of String Constant:
"a"
"C program"


C Programming, Constants in C




Integer constant in C can be defined by two ways:

  • By using const keyword
  • By using #define




➣By using #define :
we learn everything about #define earlier in detail.
#define is used to define constants.


Examples of #define Constant:
#define length 100

C Programming, Constants in C




➣By using const keyword :
const keyword is used to define constants in C programming.

C Programming, Constants in C

C programming provides error if you try to change the value of const variables.

The example can be given below:

C Programming, Constants in C

C Programming, Constants in C




Previous                                                       Next


For any query related to the content mail to [email protected]

No comments:

Powered by Blogger.