Constant In C

Prashant | Thu, 11 Jun, 2020 | 153

Contant in C languauge

Constants normally refers to some fixed value, whose values can not be changed, these fixed values are called literals. Constants can be pf any of the basic data types like an integer constant, a floating constant. a character constant, or a string literals.

We can define constants in two ways :- 

  1. Using #define preprocessor directives
  2. Using a const keyword

Using #define preprocessor directive: This directive is used to declare an alias name for existing variable or any value. We can use this to declare a constant as shown below:

#define identifierName value
  • identifierName: It is the name given to constant.
  • value: This refers to any value assigned to identifierName.


#define val 10  
#define floatVal 4.5  
#define charVal 'G'  
int main()  
    printf("Integer Constant: %d\n",val);  
    printf("Floating point Constant: %.1f\n",floatVal);  
    printf("Character Constant: %c\n",charVal);  
    return 0;  

Output :- 

Integer Constant: 10
Floating point Constant: 4.5
Character Constant: G

2. using a const keyword: Using const keyword to define constants is as simple as defining variables, the difference is you will have to precede the definition with a const keyword.

Example :- 

#include <stdio.h> 
int main() 
    // int constant 
    const int intVal = 10;  
    // Real constant 
    const float floatVal = 4.14; 
    // char constant  
    const char charVal = 'A';  
    // string constant 
    const char stringVal[10] = "ABC";  
    printf("Integer constant:%d \n", intVal ); 
    printf("Floating point constant: %.2f\n", floatVal ); 
    printf("Character constant: %c\n", charVal ); 
    printf("String constant: %s\n", stringVal); 
    return 0; 


Integer constant: 10 
Floating point constant: 4.14
Character constant: A 
String constant: ABC 







Related Articles :- 

1Introduction And Getting Started With C

2Basic Of C Language

3. Why We Should Use C Language

4. Applications Of C Programming

5. Basic Rules For Writting C Program

6. Keywords In C Language

7. Comments In C Programming

8. Tokens In C

9. Difference Between Int Main And Void Main

10. C Processor And Macros

11. Variables In C

12. Types Of Variables In C

13. Difference Between Local And Global Variable

14. Difference Bwtween Auto / Extern / Static Variable

15. Constant In C

16. How To Access Global Variable Using Extern Keyword In C

17. Exit And Return Staterment In C

18. Print Float Value Upto N Decimals In C Programming

19. How To Print Multiline Message Using Single Printf In C Programming ?

20. What Value Returned By Scanf Function In C Language ?

21. What Value Is Returned By Printf And Scanf In C

22. What Do 'Lvalue' And 'Rvalue' Mean In C/C++?

23. Errors In C Programming ?

Leave a comment