# Learn C Constant with Code Example Explanation

In this tutorial, we will learn the constant in C and how to define a constant in c? What is the use of the Constants in C?

Furthermore, there are four types of Constant in C, which are:

• integer constant
• floating constant
• character constant
• string constant

## Four Types of C Constant

### Integer Constant

In C program, the integer constant are consist of hexadecimal, octal and decimal constant. The prefix defines for the base or radix such as 0 for octal, 0X or 0x for hexadecimal and for the decimal is nothing.

Also read: Learn C Data Types with Best Examples

However, the integer constant also has a suffix, which is a mix of a U for UNSIGNED and L for Long. Furthermore, a suffix will also uppercase or a lowercase and it is composed of any order.

These are the example of integer constant:

### Character Constant

In a C program, the Character constants are confined in a single quotes, for example: ‘y’ can be kept in a plain variable of char type.

Furthermore, the character constant can be a simple character example “y”, an outbreak sequence (For example: ‘\t’), or a universal character for example: ‘\uC0.

In addition, Here are the positive characters in C that show a special meaning when anticipate by a backslash for e.g., the newline symbol is (\n) or the tab symbol is (\t).

Also read or visit: Learn C Basic Syntax Rules with Examples

In the example below, it shows the outbreak sequence program.

``````#include <stdio.h>

int main() {
printf("Hello\tSourcecodeheroes\n\n");

return 0;
}``````

Output:

Hello Sourcecodeheroes

### Floating Constant

In C language, the floating point constant contains an integer, fraction, decimal point and exponent. However, the floating point shows in two ways either in a decimal way or an exponential way.

Besides, for showing the decimal it must include the decimal point and the exponent or you can include both. For showing the exponential way it must include the integer and fraction or either you can include both. The exponent has a symbol ‘e’ or ‘E’.

You may read or visit: Structure of C Program with Example

In the table below is the examples of different types of integer constant

### String Constant

In C programming language, a string constant is enclosed in a double quotes symbol(“”). Besides, the string consists of character which is the same as character constant such as universal, plain and escape sequence.

To split the long line into multiple lines we will use the string constant and to split using a white spaces.

These are the examples of string constant

``````"Hi, Sourcecodeheroes"

"Hi, \

Sourcecodeheroes"

"Hi, " "S" "ourcecodeheroes"``````

## How to Define a Constant in C?

A constant is a fixed value which the program cannot change at the same time as for execution. In addition, the constant are designed and its the same with the regular variables that the value can’t alter after the answer.

Furthermore, there are two easy ways to define a constant in C

• #define preprocessor
• const keyword

### #define Preprocessor

We will used the #define preprocessor for creating the constants. It must define it at the starting of the program. Why? because we must create all the preprocessors before the global declaration.

In the example below, it’s the syntax to #define preprocessor for defining a constant.

``#define constantvariable value``

For example:

``````#include <stdio.h>

#define const_length 30
#define const_width  15
#define const_newline '\n'

int main() {
int const_area;

const_area = const_length * const_width;
printf("The value of an area is : %d", const_area);
printf("%c", const_newline);

return 0;
}``````

Output:

The value of an area is : 450

### Const Keyword

In a C program, a const keyword is used to make a constant of any specific datatype in a code. For making a constant, we will have to declare the variable with a const keyword.

Here is the syntax to follow when we are using the ‘const’ keyword:

``const type variable_name = value;``

Let’s have a look at the example below for Const Keyword.

``````#include <stdio.h>

const int const_length 30
const int const_width  15
const char const_newline '\n'

int main() {
int const_area;

const_area = const_length * const_width;
printf("The value of an area is : %d", const_area);
printf("%c", const_newline);

return 0;
}``````

Output:

The value of an area is : 450

## Conclusion

In conclusion, we already discussed the four types of constants, how to define a constant in C and also we already discussed the use of C constant.

In previous tutorial, we discussed the Variables in C and for the next tutorial, we will discuss the “C Storage Classes.