✏️ Explanatory Question

What are the types of constants in C?

👁 798 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

What are the types of constants in C?

In C programming, a constant is a fixed value that does not change during the execution of a program. C provides several types of constants, which can be classified according to the kind of value they represent.

The major types of constants in C are:

  • Integer constants
  • Floating-point (real) constants
  • Octal constants
  • Hexadecimal constants
  • Character constants
  • String literals
  • Escape (backslash) character sequences

1. Integer Constants

An integer constant represents a whole number without a fractional or decimal part. It may be positive, negative, or zero.

10
250
-50
0

For example, in int age = 25;, the value 25 is an integer constant.

2. Floating-Point Constants

Floating-point constants, also called real constants, represent numbers that contain a fractional part. They are normally written with a decimal point or in scientific notation.

3.14
-0.75
25.50
6.02e23

For example:

float pi = 3.14f;

Here, 3.14f is a floating-point constant.

3. Octal Constants

An octal constant is an integer constant written using the base-8 number system. Octal digits range from 0 to 7. In C, an integer literal beginning with 0 is interpreted as octal when it is not followed by x or X.

012
075
0100

For example, 012 is an octal integer constant. Its decimal equivalent is 10.

4. Hexadecimal Constants

A hexadecimal constant is an integer constant written using the base-16 number system. It uses the digits 0-9 and the letters A-F or a-f. Hexadecimal constants begin with 0x or 0X.

0x10
0xFF
0X2A

For example, 0x10 represents the decimal value 16.

5. Character Constants

A character constant represents a single character and is enclosed within single quotation marks.

'A'
'b'
'7'
'@'

For example:

char grade = 'A';

Here, 'A' is a character constant.

6. String Literals

A string literal is a sequence of characters enclosed within double quotation marks. In C, a string literal is stored as an array of characters terminated by a null character ('\0').

"Hello"
"Welcome to C"
"12345"

For example:

printf("Hello World");

Here, "Hello World" is a string literal.

7. Escape (Backslash) Character Sequences

Escape sequences are special character sequences that begin with a backslash (\). They are used to represent special characters or actions that cannot always be written directly in a string or character literal.

Escape Sequence Meaning
\n New line
\t Horizontal tab
\b Backspace
\r Carriage return
\\ Backslash character
\' Single quotation mark
\" Double quotation mark
\0 Null character

For example:

printf("Hello\nWorld");

Here, \n moves the cursor to the next line.

Summary

Type Example Description
Integer constant 100 Whole-number value
Floating-point constant 3.14 Decimal or real value
Octal constant 075 Base-8 integer value
Hexadecimal constant 0xFF Base-16 integer value
Character constant 'A' Single character
String literal "Hello" Sequence of characters
Escape sequence \n Represents a special character or action

Therefore, C constants can represent numeric values, characters, strings, and special character sequences. Understanding these different types is important because they are frequently used in expressions, assignments, input/output operations, and other C programs.