C Programming - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C Constants

C Constants

shape Description

C Constants are same as the identifiers which are fixed values and cannot be modified once they are defined. Constants are represented by a name in the program. This name can be used same as the variable name. C Constants of type integer can be declared in the following two ways. 1) Defining C Constants with const keyword : This keyword is a modifier that can be applied to any variable declaration. A variable declared to be const cant be modified during program execution. It is only initialised only at the time of declaration.
Const int a=1; Int const a=1;
2) Defining C constants with #define directive : Directive #define is also used to declare the constants.
#define a 1 (pre-processor directive)

Differences between #define and const

  1. When #define is used, all constants gets replaced with their actual values before compilation by the pre-processor and const is used by the compiler.
  2. #define is always global, const can be local(global also).
  3. #define does not have type checking where as type checking is part of const.

shape Types

Constant Meaning
Integer Constant Constant which stores integer value
Floating Constant Constant which stores float value
Character Constant Constant which stores character value
String Constant Constant which stores string value

C-Integer constant:

shape Description

An Integer constant may be decimal (base 10), octal (base 8), or hexadecimal (base 16) number which represents an integral value.

Types of Integer Constants

Decimal integer constant:

shape Description

Decimal integer constants takes values from 0 to 9. Sign is optional.

Floating point integer constant:

shape Description

Floating point integer constant represents a real integer value i.e it takes fractional, decimal and integer values also.

shape Conceptual figure

Character constants:

shape Description

A character constant is an alphabet, a digit or a single special symbol.

shape Example

[c] #include<stdio.h> int main() { char cvar = 'A'; printf("Character is : %c", cvar); return(0); }[/c]

String Constants:

shape Description

String constant is same as character constant and can be enclosed in double quotes" ". There is no special datatype for string. For example, char area= "A"; Here "char" is only used as datatype. There is also another constant which can be used in C-language.

Backslash character constants: 

shape Backslashes

These constants should be started with backslash.
Symbol Backslash symbol
\b Backspace
\v Vertical tab
\" Double quote
\r Carriage return
\t Horizontal tab
\f Form feed
\\ Backslash
\? Question mark
\a Alert or bell
\' Single quote
\n New line

shape Example

[c] #include<stdio.h> #include<conio.h> int main() { const int length=100; /*integer constant*/ const float number=22.7; /*float constant*/ const char c=‘a’; /*char constant*/ const char str[10] = “ABC”; /*string constant*/ const char backslash_char = ‘\?’; /*special char cnst*/ printf(“value of length :%d \n”, length ); printf(“value of number : %f \n”, number ); printf(“value of c : %c \n”, c); printf(“value of string : %s \n”, str); printf(“value of backslash_char : %c \n”, backslash_char); }[/c]


shape Key Points

  • C Constants cannot be modified.
  • Integer, float, character, backslash and string are C constants types.

shape Programming Tips

Always enclose string constants in double quotes only, otherwise it is considered as character value.