Constants.........in C Language

Constants

  • Constant की value fixed होती है |
  • Constant किसी भी data type का हो सकता है |
  • Constant को Literals भी कहते है |
  • Constant Pointer भी होता है |


Syntax :
const data_type variable_name = value (optional) ;


Types of Constant :

  • Integer Constant
  • Decimal Constant
  • Octal Constant
  • Hexadecimal Constant
  • Floating-point / Real Constant
  • Character Constant
  • String Constant
  • Preprocessor


With Examples

Constant Types
with Examples
Integer
eg. 2, 10, -2
Decimal (Integer)
eg. 2, 10, -2
Octal (Integer)
eg. 02, 010
Hexadecimal (Integer)
eg. 0x12, 0x1f
Floating-point/Real
eg. -2.4, 4.8
Character
eg. 'i', 'j'
String
eg. "Hello", "Hi"
Preprocessor
eg. #define a 5


Integer Constant Types:

  1. Decimal Integer Constant
  2. Octal Integer Constant
  3. Hexadecimal Integer Constant
Integer Constants
  • Integer Constant normal Variable की तरह काम करता है |
  • Integer Constant positive (+) या negative (-) हो सकता है |
  • Integer Constant की Range -32768 से 32767 तक होती है |

for eg.
Source Code :
#include <stdio.h>
 
int main(){
 
const int num = 5; // integer Constant
printf("integer constant value : %d", num);
 
return 0;
 
}
Output
integer constant value : 5


Character Constants
  • Character Constant normal Variable की तरह काम करता है |
  • Character Constant सिर्फ single character लेता है | |
  • Escape Sequences के साथ भी character constant इस्तेमाल किया जाता है |

Escape Sequences
Explaination
\'
Single Quotation Mark
\"
Double Quotation Mark
\\
Backslash
\?
Question Mark
\a
Audible Bell
\b
Backspace
\f
Form Feed
\n
New line
\r
Carriage Return
\h
Horizontal Tab
\v
Vertical Tab

for eg.
Source Code :
#include <stdio.h>
 
int main(){
const char ch = 'H'; // character constant
const char escape[]= "Hello\tWorld"; // Escape sequence - Horizontal Tab and string constant
 
printf("Character constant : %c\n", ch);
printf("String constant : %s\n", escape);
 
return 0;
}
Output
Character constant : H
String constant : Hello      World

Floating-point Constant
  • Floating-point Constant normal float variable की तरह ही काम करता है |
  • Floating-point Constant में Decimal point (.) होता है |
  • अगर Floatint-point Constant की value integer type की हो तो वो Decimal point (.) लेता है |

for eg.
Source Code :
#include <stdio.h>
 
int main(){
const float num1 = 5; // floating-point constant with integer value
const float num2 = 3.525984; // Floating-point constant
 
printf("Floating-point constant : %f\n", num1);
printf("Floating-point constant : %f\n", num2);
 
return 0;
}
Output
Floating-point constant : 5.000000
Floating-point constant : 3.525984


String Constant
  • String Constant की value Double Quotation Mark (" ") के अंदर लिखी जाती है |
  • String Constant Single और Multiple characters लेता है |
  • String Constant Escape Sequences के साथ भी इस्तेमाल करते है |

for eg.
Source Code :
#include <stdio.h>
 
int main(){
const char str1 []= "H"; // String Constant with single character
const char str2 []= "Hello World"; // Normal String constant
const char str3 [] = "Hello\nWorld"; // String Constant with Escape Sequence
 
printf("String Constant with single character : %s\n", str1);
printf("Normal String constant : %s\n", str2);
printf("String Constant with Escape Sequence : %s\n", str3);
 
return 0;
}
Output
String Constant with single character : H
Normal String constant : Hello World
String Constant with Escape Sequence : Hello
World


Preprocessor Constant
  • Preprocessor के साथ भी Constant value रख सकते है |

for eg.
Source Code :
#include <stdio.h>
#define a 5 // Constant value with preprocessor
#define b 10 // Constant value with preprocessor
 
int main(){
 
printf("Value of a  : %d\n", a);
printf("Value of b  : %d", b);
 
return 0;
}
Output
Value of a  : 5
Value of b  : 10

Comments

Popular Posts