17. Constants/Literals in C programming

Constants/Literals in C :

During the program execution, any unchanged value in a program is known as constant. A constant in C language refers to a fixed value that does not change during execution of a program. C supports following types of constants:
(1) Numeric Constants : C language supports two types of numeric constants. They are Integer Constants and Floating point or Real Constants. An integer constant is a signed or unsigned whole number, such that + 25, - 10, 33 etc. where as any signed or unsigned number with fractional part is known as floating point or real constants, such that 0.23, +0.23, - 0.23, 0.23 ÷ 5 (0.23 x 10 to the power 5).
(2) Character constant : C language supports two types of character constants. They are Single character constants and String constants. Any letter enclosed in single apostrophe is called single character constant. For example ‛s’ , ‛+’ , ‛$’ etc. where as any string of characters consisting of letters, digits, and symbols enclosed in double quotes is known as string constants. For example: “My name is Ram”, “sum =” etc.

How to define constants using #define preprocessor directive

#include<stdio.h>
#define val 10
#define floatVal 4.5
#define charVal 'G'
int main()
{
printf("Integer Constant : %d\n", val);
printf("Floating point Constant : %f\n", floatVal);
printf("Character Constant : %c\n", charVal);

return0;
}

Output :
Integer Constant : 10
Floating point Constant : 4.500000
Character Constant : G


How to define constants using const
#include <stdio.h>
int main()
{
const int intVal = 10; // int constant
const float floatVal = 4.14; // Real constant
const char charVal = 'A'; // char constant
const char stringVal[10] = "ABC"; // string constant

printf("Integer constant : %d \n", intVal );
printf("Floating point constant : %f \n", floatVal );
printf("Character constant : %c \n", charVal );
printf("String constant : %s \n", stringVal);

return 0;
}

Output :
Integer constant : 10
Floating point constant : 4.140000
Character constant : A
String constant : ABC

Comments

Popular posts from this blog

12. Library Functions of C Programming

13. Control Strings in C Programming

4. What is Compiler ?

7. Introduction of C programming :

18. Identifiers and Keyword in C programming

10. Data Types of C Programming

3. What is programming language ?

8. Program structure rules of C programming

9. Header Files of C Programming