C tutorials Literals in C
CONSTANTS AND LITERALS
Constants allude to fixed qualities that the program may not
adjust during its execution. These fixed qualities are likewise called
literals. Constants can be of any of the fundamental information types like a
number steady, a gliding steady, a character consistent, or a string exacting.
There are identification constants too. Constants are dealt with simply like
customary factors aside from that their qualities can't be changed after their
definition.
Integer Literals
A number of exacting can be a decimal, octal, or hexadecimal
consistent. A prefix indicates the base or radix: 0x or 0X for hexadecimal, 0
for octal, and nothing for decimal. A number exacting can likewise have an
addition that is a mix of U and L, for unsigned and long, separately. The
addition can be capitalized or lowercase and can be in any request.
Here are a few instances of number literals:
212 /*
Legal */
215u /* Legal
*/
0xFeeL /*
Legal */
078 /*
Illegal: 8 is not an octal digit */
032UU /*
Illegal: cannot repeat a suffix */
|
Following are other examples of various types of integer
literals:
85 /*
decimal */
0213 /*
octal */
0x4b /*
hexadecimal */
30 /* int */
30u /*
unsigned int */
30l /* long
*/
30ul /*
unsigned long */
|
Floating-point Literals
A drifting point strict has a number section, a decimal the point, a partial part, and a typical part. You can speak to skimming point
literals either in decimal structure or on the other hand exponential
structure.
While speaking to decimal structure, you should incorporate
the decimal point, the type, or both; and keeping in mind that speaking to
exponential structure, you should incorporate the whole number part, the
fragmentary part, or both. The marked example is presented by e or E.
Here are a few instances of drifting point literals:
3.14159 /* Legal */
314159E-5L /* Legal */
510E /* Illegal:
incomplete exponent */
210f /* Illegal: no
decimal or exponent */
.e55 /* Illegal: missing
integer or fraction */
|
Character Constants
Character literals are encased in single statements, e.g.,
'x' can be put away in a straightforward variable of a roast kind. A character
strict can be a plain character (e.g., 'x'), a departure grouping (e.g.,
'\t'), or a widespread character (e.g., '\u02C0'). There are
sure characters in C that speak to extraordinary significance when gone before by
an oblique punctuation line, for instance, newline (\n) or tab (\t).
Here, you have a rundown of such departure arrangement
codes:
Escape
Sequence
|
Meaning
|
\\ \
|
character
|
\' '
|
character
|
\"
"
|
character
|
\??
|
character
|
\a
|
Alert or bell
|
\b
|
Backspace
|
\f
|
Form feed
|
\n
|
Newline
|
\r
|
Carriage return
|
\t
|
Horizontal tab
|
\v
|
Vertical tab
|
\ooo
|
Octal number of one to
three digits
|
\xhh...
|
Hexadecimal number of one
or more digits
|
Following is the example to show a few escape sequence characters:
#include <stdio.h>
int main()
{
printf("Hello\tWorld\n\n");
return 0;
}
|
When the above code is compiled and executed, it produces
the following result:
Hello World
|
String Literals
String literals or constants are encased in twofold
statements "". A string contains characters that are like character
literals: plain characters, escape successions, and all-inclusive characters. You
can break a long queue into different lines utilizing string literals and
isolating them utilizing whitespaces.
Here are a few instances of string literals. Every one of
the three structures are indistinguishable strings.
"Hello, dear"
"Hello, \
dear"
"Hello, " "d" "ear"
|
Defining Constants
There are two simple ways in C to define constants:
Using #define preprocessor
Using const keyword
The #define Preprocessor
Given below is the form to use #define preprocessor to
define a constant:
#define identifier value
|
#include
<stdio.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main()
{
int area;
area = LENGTH * WIDTH;
printf("value of
area : %d", area);
printf("%c",
NEWLINE);
return 0;
}
|
The following example explains it in detail:
When the above code is compiled and executed, it produces
the following result:
value of area : 50
|
The constant Keyword
You can use a const prefix to declare constants with a
specific type as follows:
const type variable = value;
|
#include
<stdio.h>
int
main()
{
const int
LENGTH = 10;
const int
WIDTH = 5;
const
char NEWLINE = '\n';
int area;
area =
LENGTH * WIDTH;
printf("value
of area : %d", area);
printf("%c",
NEWLINE);
return 0;
}
|
The following example explains it in detail:
When the above code is compiled and executed, it produces
the following result:
value of area: 50
|
Note that it is a good programming practice to define
constants in CAPITALS
No comments