Numeric Constant in C
Numeric constants are fixed values that represent numbers in the C programming language. They can be written in different formats, depending on the base and precision of the number.
The most common format for numeric constants is the decimal notation, which is used to represent whole numbers.
For example:
int num = 42;
In this case, the numeric constant 42 is assigned to the variable num.
Another format for numeric constants in C is the octal notation, which uses a base of 8. Octal numbers are preceded by a 0 (zero) in C.
For example:
int num = 0123;
In this case, the numeric constant 123 is represented in octal notation and assigned to the variable num.
A third format for numeric constants in C is the hexadecimal notation, which uses a base of 16. Hexadecimal numbers are preceded by 0x or 0X in C.
For example:
int num = 0xABCD;
The numeric constant ABCD is represented in hexadecimal notation and assigned to the variable num.
C also supports exponential notation for numeric constants, which is used to represent very large or minimal numbers. The exponential notation uses the letter E or e to indicate the exponent.
For example:
float num = 1.23e-4;
In this case, the numeric constant 1.23 times 10 to the power of negative 4 is assigned to the variable num.
It is important to note that numeric constants have a specific data type in C, depending on their format and size. For example, an integer constant without a suffix is of type int by default, while a long integer constant has the suffix L or l. Similarly, a floating-point constant without a suffix is of type double by default, while a float constant has the suffix F or f.
In C, numeric constants are typically used in assignments, arithmetic operations, comparisons, and other computations. They can also be used as arguments in function calls or as operands in conditional statements.
In summary, numeric constants in C are fixed values that represent numbers and can be written in various formats, including decimal, octal, hexadecimal, and exponential notation. They have a specific data type in C, depending on their format and size.
Types of Numeric Constants in C
- Integer constants: Integer constants are numeric constants that represent whole numbers. They can be written in decimal, octal, or hexadecimal notation. By default, an integer constant is of type int in C. Here are some examples of integer constants:
int a = 123; // decimal notation
int b = 0123; // octal notation
int c = 0x1A; // hexadecimal notation
- Floating-point constants: Floating-point constants are numeric constants that represent decimal numbers with a fractional component. They can be written in decimal or exponential notation. By default, a floating-point constant is of type double in C. Here are some examples of floating-point constants:
double d = 3.14159; // decimal notation
float e = 1.23e-4; // exponential notation
double f = 5.67E+8; // exponential notation
- Character constants: Character constants are numeric constants that represent single characters. They are enclosed in single quotes in C. By default, a character constant is of type int in C. Here are some examples of character constants:
char ch1 = 'A';
char ch2 = '5';
char ch3 = '\n'; // newline character
- String literals: String literals are sequences of characters enclosed in double quotes in C. They are treated as arrays of characters and terminated with a null character ('\0'). By default, a string literal is of type char[] in C. Here are some examples of string literals:
char str1[] = "Hello, world!";
char str2[] = "1234";
char str3[] = "a\nb\tc";
- Enumeration constants: Enumeration constants are numeric constants that represent integer values assigned to named variables. They are defined using the enum keyword in C. By default, an enumeration constant is of type int in C. Here is an example of an enumeration constant:
enum weekday { Monday, Tuesday, Wednesday, Thursday, Friday };
enum weekday today = Wednesday;
- Boolean constants: Boolean constants are numeric constants that represent either true or false values. In C, a true value is represented by any non-zero numeric constant, while a false value is represented by the numeric constant 0. By default, a Boolean constant is of type int in C.
Here are some examples of Boolean constants:
int true_val = 1;
int false_val = 0;
In summary, the different types of numeric constants in C include integer constants, floating-point constants, character constants, string literals, enumeration constants, and Boolean constants. Each type of constant has a default data type in C, depending on its format and size.