Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; SEOFriendlyImages has a deprecated constructor in /home/tutorig3/public_html/wp-content/plugins/seo-image/seo-friendly-images.class.php on line 2

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/tutorig3/public_html/wp-content/themes/mts_interactive/options/php-po/php-po.php on line 185
Different C Constants With Example - Tutorialwing

# Different C Constants With Example

In this post, we will learn about constants in c. We will go through Numeric Constants (Such as Integer constants, Real Constants) , Character Constants (Such as Single Character Constant, String Constant, BackSlash Character Constant) etc. At last, we will see how to use c constants in program using const keyword and #define preprocessor.

## C Constants

Constants in C refers to fixed values that don’t change during the execution of a program.

Below image depicts different constants in c programming language – Let’s go through each constant one by one.

## 1. Numeric Constant

It contains constant of numeric values. For example, values like 1, 2, 1.3 1.4 etc.

It is further divided into –
(1) Integer Constant.
(2) Real Constant.

### 1.1 Integer Constant

• An Integer constant refers to a sequence of digits.
• Integer Constants are  whole numbers which have no decimal point(.).
• There are three types of Integer constants:
• Decimal
• Octal Integer Constants in C

### 1.2 Real Constant

• It is used to represent quantities that vary continuously like distances, heights, temperatures and  so on.
• These quantities are represented  by numbers containing fractional parts like 14.284. Such numbers are called real constants.
• Examples: 0.042 , -0.452, 785.244, +452.00

## 2. Character Constant

Now, we will see different constants in character in C programming language. – Single Character, String Constants etc.

### 2.1 Single Character Constants

• It contains single character enclosed within a pair of single quote marks.
• Examples –
```'A',
'a',
'7',   // Here, '7' is not same as number 7. It is String Character Constant
' '
```

### 2.2 String Constants:

• Sequence of characters enclosed in double quotes.
• Character may be letters, numbers, special characters and blank spaces.
• Example-
```"Hello",
"12345",
"Great Work",
"Wow",
"2+4",
"A"
```

### 2.3 Backslash Character Constants

• C supports some special backslash character constants that are used in output functions.
• For Example, the symbol ‘\n’ stands for newline character.
• It is known as Escape Sequences.
Constant Meaning
‘\b’ backspace
‘\f’ form feed
‘\n’ new line
‘\r’ carriage return
‘\t’ horizontal tab
‘\v’ vertical tab
single quote
double quote
? question mark
\ backslash
‘\0’ null

## 3. How to use Constants in C Program ?

You can use c constants as below –

1. Using ‘const’ Keyword.
2. Using ‘#define’ Preprocessor Directive.

### 3.1 Using const Keyword –

You can define c constants using const keyword as below –

Syntax –

```const data_type variable_name = value;
```

Some Valid and Invalid Examples –

Valid Invalid
const int i = 10; const int i;
i=10;
const float j = 10.234; float int j;
##### Illustration of C Constant using ‘const’ keyword
```#include <stdio.h>
int main()
{
const int age =35; /*int constant*/
const float percentage = 83.14; /*Real constant*/
const char letter = 'A'; /*char constant*/
const char sequence = "HELLO"; /*string constant*/
printf("Define\tC\tConstants\tusing\tconst\tkeyword\n") ; /* '\t' and '\n' are backslash constants */
printf(" Age :%d \n", age);
printf("Percentage : %f \n",percentage);
printf("Letter : %c \n", letter );
printf("Sequence : %s \n", sequence);
return 0;
}
```
###### Output:
```Define  C   Constants   using   const    keyword

Age : 35

Percentage : 83.14

Letter : A

Sequence : HELLO
```

### 3.2 Using ‘#define’ Preprocessor Directive

This directive used to declare an alias name for existing variable or any value.

Syntax of using ‘#define’ preprocessor –

```#define identifier_name value
```
###### Illustration of C Constant using ‘#define’ Preprocessor Directive
```#include <stdio.h>
#define age 10
#define percentage 90.14
#define letter 'A'

int main()
{
printf("Integer Constant: %d\n",age);
printf("Floating point Constant: %f\n",percentage);
printf("Character Constant: %c\n",letter);

return 0;
}
```
###### Output –
```Integer Constant: 10

Floating point Constant: 90.14

Character Constant: A

```

We have seen different constants in c, how to use them in c program, different examples on each type of c constant.