Type Here to Get Search Results !

# Constants in C

In this post I'm gonna discuss what constants are actually in C. You can access my C programming full course in order to learn programming skills quickly.

When we talk about Constants in C, there are lots of questions arise,  which I will try to answer in this post.

So lets start discussing all the questions.

## What are Constants in C?

C Constants are the most fundamental and essential part of the C programming language.

Constants in C are values or variables which can't be changed in the program, also called literals.

I mean contants are like variables, which value never changes during execution once defined.

For example: 40, 54, 'd', 2.4, "c programming tutorial" etc. are constants.

## Types of Constant in C

C Constant can be classified as:

• Integer constant
• Real constant
• Character constant
• String constant

### 1. Integer Constant in C Language:

Integer Constants in C are refers to the sequence of digits. Range of integer constants is -32,768 to 32,767.

There are three types of integer constants in the C language:

#### Decimal constant (base 10)

Decimal Digits: 0 1 2 3 4 5 6 7 8 9
Example: 0. -8, 24 etc.

#### Octal constant (base 8)

Octal Digits: 0 1 2 3 4 5 6 7
Example: 421, 027, 233 etc.

Hexadecimal Digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F
Example: 0x3f, 0x5a, 0x541 etc.

### 2. Real Constant in C Language

Real constant in C refers to the numbers containing fractional parts like 45.25, are called real or floating points constant. The range of Real Constants is -3.4*1038 to 3.4*1038.

There are two form of writing Real constants:

Decimal Form: -2.0, 0.0000234

Exponential Form: -0.22E-5

### 3. Character Constant in C Language

Character Constant in C refers to the single character enclosed within a pair of single quote (' ') like 'A', 'd', '54', '8'.

The range of Character Constants is -128 to 127.

### 4. String Constant in C Language

String constants in c refers to the sequence of characters enclosed in double quotes, and they may include letters, digits, special characters, and blank spaces.

Examples:

“Hello”, “1987”, “?….!”, “x”, ” “, “” (null string constant)

## Two ways to define constant in C

Now, we see,  How to use Constants in a C Program?

There are two ways to define constant in C programming

• const keyword
• #define preprocessor

### 1. C const keyword

In order to define constant in C programming, the const keyword is used.

Syntax:

const type constant_name;

Lets understand declaration of constant using const keyword in C with example:

#include<stdio.h>

int main(){

const float PI=3.14;

printf("The value of PI is: %f",PI);

return 0

}

Output:

The value of PI is: 3.140000

Now, If we try to change the the value of PI constant, it will throw the compile time error. How? see:

#include<stdio.h>

int main(){

const float PI=3.14;

PI=4.5;

printf("The value of PI is: %f",PI);

return 0

}

Output:

Compile Time Error: Cannot modify a const object

### 2. C #define preprocessor

The another approach of defining constant in C is #define preprocessor.

Syntax:

#define token value

Let's see an example of #define to define a constant in C:

#include <stdio.h>

#define PI 3.14

main() {

printf("%f",PI);

}

Output:

3.140000

## Conclusion of Constants in C

So we have learnt What are Constants in C, Types of Constants, and 2 ways to define constants in C.

I hope you have got clear about Constants in C.

Thanks for visit!

< Previous Post
Next Post >
Operators in C