C define constants – C Programming Constants and Literals

  • In this tutorial we will learn about Constants in C, different types of constants, declaration of constants and #define preprocessor directives in C programming.

Constants in C refer to fixed values that program cannot change during the time of execution. Constants are also known as literals.

A constant can be of any data type like character constant, integer constant, string constant etc.

For Example :     ‘A’, 1234, 123.5, “TechCrashCourse”
Constants in C are like normal variables, the only difference is, their values cannot be changed by the program once they are defined.

Character Constants

C define constants: Characters constants are enclosed between a pair or single quote. We can store a character constant in a variable of char data type. The ASCII value of character constants are stored internally. “%c” format specifier is used to print character constants.

For Example:
‘a’, ‘A’, ‘1’, ‘#’ are character constants

C Program to print character constant

#include<stdio.h>
 
int main() {    
    char c = 'E'; 
    printf("Character Constant : %c", c);
 
    return(0);
}

Output

Character Constant : E

Backslash Character Constants

C define constants: There are some characters which are impossible to enter into a string from keyboard like backspace, vertical tab etc. Because of this reason, C includes a set of backslash character which have special meaning in C language.
The backslash character (‘\’) changes the interpretation of the characters by compiler.

For Example:
If we include ‘\n'(newline character) in a string like “TechCrash\nCourse” then compiler will print the characters after \n in next line.

Here is the list of escape sequence codes

Escape Sequence Description
\” Double quote(“)
\’ Single quote(‘)
\\ Backslash Character(\)
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\a Alert or bell
\? Question mark
\0 Null character

C Program to show use of Escape Sequence

#include<stdio.h>
 
int main() {    
    printf("Tech\nCrash\nCourse");
 
    return(0);
}

Output

Tech
Crash
Course

Integer Constants

Define constant in c: Integer constants are whole numbers without any fractional part or exponential part. It can either be positive or negative, If no sign precedes it is assumed to be positive by default.
The range of integer constant depends on operating system. In a 16-bit system range of integer literal is -32768 to 32767.

For Example:
3, -3, 67L, 0x2A are Integer Constants

An integer constant can be a decimal, octal, or hexadecimal. A prefix specifies the base of number system.

Prefix Description Base Example
0x or 0X Hexadecimal Number 16 0x5C, 0x22
0 Octal Number 8 012C, 0243
Nothing Decimal Number 10 25, 100

You can specify the type of integer constant by using a suffix character.

  • F : Floating-point number
  • L : Long
  • U : Unsigned

Examples of Integer Constants

  • Unsigned Integer Constant : 100U, 565U
  • Long Constant : 67L, -2398876L
  • Unsigned Long Constant : 55UL, 77652UL
  • Decimal Constants : 85, -54
  • Octal Constants : 0213, 045
  • Hexadecimal Constants : 0x4b, 0x2A

Floating Point Constants

C define constant: A floating point constant has a integer part, a decimal point, a fractional part and may contain an exponential part. Floating point literal may be positive or negative but must have a decimal point. You can also represent floating point literals in scientific notation.
For Example: 1234.5432, -100.001, 2.37E-3

String Constants

How to define constant in c: A string constant is a set of characters enclosed between a pair of double quotes. A string literal is actually stored as a character array. A string literal may contain any number of characters including alphanumeric characters, escape characters, graphical characters etc.
For Example

  • “” : Null String.
  • “A” : String literal having single characters.
  • “ABc12.iyu” : String literal with multiple characters.
  • “ABd jjuh\n” : String with spaces and escape characters.

C Program to Print Numeric C Constants

C Programming Constants and Literals

#include <stdio.h>
 
int main(){
     
    printf("Printing Integer Constants\n");
    printf("%d %u \n\n", 543, 7653U);
     
    printf("Printing Long Constants\n");
    printf("%ld\n\n", 554365L);
     
    printf("Printing Radical Constants\n");
    printf ("%d %x %o\n\n", 2015, 0x7df, 02015);
     
    return 0;
}

Output

Printing Integer Constants
543 7653

Printing Long Constants
554365

Printing Radical Constants
2015 7df 2015

Declaration of Constants in C

We can define constants in C in two ways.

  • Using const keyword in variable declaration.
  • Using #define preprocessor directives.

We can use const keyword as a prefix of data type to declare a Constant.

const data_type variable_name = Constant;

For Example
const float PI = 3.141;
Above statement declares a constant PI with initial value 3.141. After declaration, any code cannot modify value of PI.

C program to show the use of const keyword to define a constant

Declaration of Constants in C

#include<stdio.h>
 
int main() {    
    float radius;
    const float PI = 3.141;
     
    printf("Enter Radius of Circle\n");
    scanf("%f", &radius);
    printf("Area of Circle : %f", PI*radius*radius);
     
    return(0);
}

Output

Enter Radius of Circle
3.0
Circumference of Circle : 18.846000

#include Preprocessor Directive for defining C Constant

We can use #define preprocessor directive to define a constant as per following syntax.

#define Constant_Identifier Value
For Example:
#define PI 3.141

C program to shows the use of #define Preprocessor Directive to define a constant

C program to shows the use of define Preprocessor Directive to define a constant

#include<stdio.h>
#define PI 3.141
 
int main() {    
    float radius;
     
    printf("Enter Radius of Circle\n");
    scanf("%f", &radius);
    printf("Circumference of Circle : %f", 2*PI*radius);
     
    return(0);
}

Output

Enter Radius of Circle
3.0
Circumference of Circle : 18.846000