Sunday 21 January 2018

Constants and Literals


Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals.

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

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
1
2
3
4
5
6
7
8
#include<stdio.h>
int main() {   
    char c = 'E';
    printf("Character Constant : %c", c);
    return(0);
}

Output
Character Constant : E

Backslash Character 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 SequenceDescription
\”Double quote(")
\'Single quote(')
\\Backslash Character(\)
\bBackspace
\fForm feed
\nNew line
\rCarriage return
\tHorizontal tab
\vVertical tab
\aAlert or bell
\?Question mark
\0Null character
C Program to show use of Escape Sequence
1
2
3
4
5
6
7
#include<stdio.h>
int main() {   
    printf("Tech\nCrash\nCourse");
    return(0);
}

Output
Tech
Crash
Course

Integer Constants

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.

PrefixDescriptionBaseExample
0x or 0XHexadecimal Number160x5C, 0x22
0Octal Number8012C, 0243
NothingDecimal Number1025, 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

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

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 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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
1
2
3
4
5
6
7
8
9
10
11
12
#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

No comments:

Post a Comment