Constants in C Programming
Constants are fixed values that cannot be altered by the program during its execution. Constants are also known as literals. Constants are like 1,4023,123 etc. They may be of data types like integer, float, character, string constants and enumeration constants etc.
There may be some situations in programming that the value of certain variables to remain constant during execution of a program. For example , if you want to use the radius of the circle in a program without having change in its initialized value, then you may make this value as constant. So that you can make use the memory efficiently which results in the increased speed of program execution.
Integer constants
- Integer constant like 4312 is an int.
- Long constants is written with a terminal L, as in 123456789L.
- Unsigned constants are written with a terminal u (or) U. Eg:4234u
- Unsigned long constants are written with UL (or) UL as suffix. Eg:800 Ul.
- A leading 0 (zero) on an integer constant means octal (o……7). Eg:037
- A leading ox on an integer constant means hexadecimal. Eg:OXIF
Floating constants
- Floating constants will be like 123.5, 154.2 etc., i.e, they contains a decimal form (or) exponent (or) both.
- Decimal form may contain integer part (or) decimal part (or) both.
- Suffix f (or) F indicates a float constant.
- Suffix l (or) L indicates a long double constants.
Character constants (backslash character constant):
Characters are like a
, b
, d
etc.
Character constant can be written as one character within single quote such as '4'
. A character constants can be a single character (or) an escape sequence. Value of a character constant is the numeric value of character in machine’s character set i.e. its ASCII values.
printf() and scanf() statements follow the combination of characters called escape sequences. They start with '\'
.
List of escape sequences are given below.
Escape sequence | Symbol | Meaning |
\a | Alert | Sounds a beep |
\b | Backspace | Backs up one character |
\f | Form feed | Starts a new screen of page |
\n | New line | Moves to beginning of next line |
\r | Carriage return | Moves to beginning of current line |
\t | Horizontal tab | Moves to next tab position |
\v | Vertical tab | Moves down a fixed amount |
\’ | Single quotation | Prints a single quotation |
\” | Double quotation | Prints a double quotation |
\\ | Back slash | Prints back slash |
\? | Question mark | Prints question mark |
Example
C Program
#include<stdio.h>
int main() {
printf("hello \t users \n of \a tutorialkart ");
return 0;
}
Output
hello users
of tutorialkart
String Constants
String constant is a sequence of zero (or) more characters within double quotes, for example "I am good"
.
String constant is an array of character. The internal representation of a string has a null character '\0'
at the end. We can also split a long line into multiple lines using string constants and separate them using white spaces.
Examples
A constant like ‘x’ is different from “X”. ‘x’ is a character constant and “x” is a string constant.
Defining constants
There are two ways to define C constants. They are :
- Using constant key word.
- Using #define preprocessor.
Constant keyword
Initialization of a variable with “ constant” keyword as prefix, can be treated as constant.
Syntax
constant data_type variable = value ;
Example
In the following code snippet, we have defined two constants. The first one is radius
of type int and with value 5. The second is pi
of type float and with value 3.143.
constant int radius= 5 ;
constant float pi =3.143 ;
#define Preprocessor
Instead of using constant keyword,we can define a constant using #define preprocessor.we can define a macro C using #define preprocessor directive.
A macro is a fragment of code that is given a name .By convention,identifier is in upper case.
Syntax
#define identifier value
Example #define PI 3.143 .
Example
In the following example, we will define a constant NUM
using #define preprocessor.
C Program
#include <stdio.h>
#define NUM 3.14
int main() {
const int length= 100; //int constant
const char letter = 'A'; //char constant
const char str[10] = "ABC"; //string constant
const char esc = '\?'; //special char constant
printf("value of length :%d \n", length );
printf("value of num : %f \n", NUM);
printf("value of letter : %c \n", letter );
printf("value of str : %s \n", str);
printf("value of esc: %c \n", esc);
return 0;
}
Output
value of length :100
value of num : 3.140000
value of letter : A
value of str : ABC
value of esc: ?
Conclusion
In this C Tutorial – C Constants, we have dealt with different types of constants in C programming with syntax and examples.