0% found this document useful (0 votes)
28 views

C Constant

C constants are values that cannot be modified once defined. They include integer, floating point, octal, hexadecimal, character, and string constants. Constants are defined using the const keyword or #define preprocessor directive. Constants can be of various data types and have specific rules for construction depending on type, such as integers not allowing decimals. Examples demonstrate defining and using constants of different types in C programs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

C Constant

C constants are values that cannot be modified once defined. They include integer, floating point, octal, hexadecimal, character, and string constants. Constants are defined using the const keyword or #define preprocessor directive. Constants can be of various data types and have specific rules for construction depending on type, such as integers not allowing decimals. Examples demonstrate defining and using constants of different types in C programs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

C CONSTANT

C Constants are also like normal variables. But, only difference is, their values can not
be modified by the program once they are defined.
Constants refer to fixed values. They are also called as literals
Constants may be belonging to any of the data type.
Syntax: const data_type variable_name; (or) const data_type *variable_name;

TYPES OF C CONSTANT:
1. Integer constants
2. Real or Floating point constants
3. Octal & Hexadecimal constants
4. Character constants
5. String constants
6. Backslash character constants

Constant type data type (Example)

int (53, 762, -478 etc )


long int, long long int
Integer constants (483,647 2,147,483,680)

float (10.456789)
Real or Floating point constants doule (600.123456789)

Octal constant int (Example: 013 /*starts with 0 */)

Hexadecimal constant int (Example: 0x90 /*starts with 0x*/)

character constants char (Example: ‘A’, ‘B’, ‘C’)

string constants char (Example: “ABCD”, “Hai”)


RULES FOR CONSTRUCTING C CONSTANT:
1. INTEGER CONSTANTS IN C:
• An integer constant must have at least one digit.
• It must not have a decimal point.
• It can either be positive or negative.
• No commas or blanks are allowed within an integer constant.
• If no sign precedes an integer constant, it is assumed to be positive.
• The allowable range for integer constants is -32768 to 32767.

2. REAL CONSTANTS IN C:
• A real constant must have at least one digit
• It must have a decimal point
• It could be either positive or negative
• If no sign precedes an integer constant, it is assumed to be positive.
• No commas or blanks are allowed within a real constant.

3. CHARACTER AND STRING CONSTANTS IN C:


• A character constant is a single alphabet, a single digit or a single special symbol
enclosed within single quotes.
• The maximum length of a character constant is 1 character.
• String constants are enclosed within double quotes.
4. BACKSLASH CHARACTER CONSTANTS IN C:
• There are some characters which have special meaning in C language.
• They should be preceded by backslash symbol to make use of special function of
them.
• Given below is the list of special characters and their purpose.

Backslash_character Meaning

\b Backspace

\f Form feed

\n New line

\r Carriage return

\t Horizontal tab

\” Double quote

\’ Single quote

\\ Backslash

\v Vertical tab

\a Alert or bell

\? Question mark

\N Octal constant (N is an octal constant)

\XN Hexadecimal constant (N – hex.dcml cnst)


HOW TO USE CONSTANTS IN A C PROGRAM?
We can define constants in a C program in the following ways.

1. By “const” keyword
2. By “#define” preprocessor directive
Please note that when you try to change constant values after defining in C program, it
will through error.

1. EXAMPLE PROGRAM USING CONST KEYWORD IN C:

1 #include <stdio.h>
2 void main()
3 {
4 const int height = 100; /*int constant*/
5 const float number = 3.14; /*Real constant*/
6 const char letter = 'A'; /*char constant*/
7 const char letter_sequence[10] = "ABC"; /*string constant*/
8 const char backslash_char = '\?'; /*special char cnst*/
9 printf("value of height :%d \n", height );
10 printf("value of number : %f \n", number );
11 printf("value of letter : %c \n", letter );
12 printf("value of letter_sequence : %s \n", letter_sequence);
13 printf("value of backslash_char : %c \n", backslash_char);
14 }

OUTPUT:

value of height : 100


value of number : 3.140000
value of letter : A
value of letter_sequence : ABC
value of backslash_char : ?
2. EXAMPLE PROGRAM USING #DEFINE PREPROCESSOR DIRECTIVE IN C:

1 #include <stdio.h>
2 #define height 100
3 #define number 3.14
4 #define letter 'A'
5 #define letter_sequence "ABC"
6 #define backslash_char '\?'
7 void main()
8 {
9 printf("value of height : %d \n", height );
10 printf("value of number : %f \n", number );
11 printf("value of letter : %c \n", letter );
12 printf("value of letter_sequence : %s \n",letter_sequence);
13 printf("value of backslash_char : %c \n",backslash_char);
14 }

OUTPUT:

value of height : 100


value of number : 3.140000
value of letter : A
value of letter_sequence : ABC
value of backslash_char : ?

You might also like