Fortran Constants
Constants or more formally literal constants are the tokens
used to denote the value of a particular type. Fortran has five
types of constants: integer, real, complex, logical, and character string.
- Integer Constants: a string of digits with an optional
sign:
- Correct Examples: 0, -345, 768, +12345
- Incorrect Examples:
- 1,234 : comma is not allowed
- 12.0: no decimal point
- --4 and ++3: too many optional signs
- 5- and 7+: the optional sign must precede the string
of digits
- Real Constants: There are two representations,
decimal representation and
exponential representation.
- Decimal Representation: A decimal point
must be presented, but no commas are allowed.
A real constant can have an optional sign.
- Correct Examples:
23.45, .123, 123., -0.12, -.12
- Incorrect Examples:
- 12,345.95: no comma is allowed
- 75: real constant must have a
decimal point
- 123.5-: the optional sign must precede
the number
- $12.34: cannot use dollar sign $
- Exponential Representation: It consists of an
integer or a real number in
decimal representation
(the mantissa or fractional part), followed by the letter
E or e, followed by an integer (the
exponent).
- Correct Examples
- 12.3456E2 or 12.3456e2:
this is equal to 1234.56
- -3.14E1 or -3.14e1:
this is equal to -31.4
- -1.2E-3 or -1.2e-3:
this is equal to -0.0012
- 12E3 or 12e3:
this is equal to 12000.0
- 0E0 or 0e0:
this is equal to 0.0
- Incorrect Examples
- 12.34E1.2:
the exponential part must be an
integer constant
- 12.34-5:
there is no exponential sign
E or e
- Complex: Will not be covered in this course
- Logical: See Chapter 3
-
Character String: Character constants must be enclosed
between double quotes or apostrophes (single quotes).
The content of a string consists of all characters,
spaces included, between the single or quote quotes, while the
length of the string is the number of characters of its
content. The content of a string can be zero and in this case
it is an empty string
- Correct Examples:
- 'John' and "John":
content = John and length = 4
- ' ' and " ":
content = a single space and length = 1
- 'John Dow #2' and
"John Dow #2":
content = John Dow #2 and length = 11
- '' and "":
content = nothing and length = 0
(empty string)
- Incorrect Examples:
- 'you and me:
the closing apostrophe is missing
- Hello, world':
the opening apostrophe is missing
- 'Hi" and "Hi': the opening and
closing quotes do not match.
If single quote is used in a string, then double quotes should
be used to enclose the string:
"Lori's apple"
This string has content Lori's apple and length 12.
Alternatively, you can write the single quote twice as follows:
'Lori''s apple'
The compiler will treat a pair of single quotes in the content
of a string as one. Thus, the content of the above string is
still Lori's apple.
- Correct Examples:
- 'What''s this?':
content = What's this? and length = 11
- '''''':
content = '' and length = 2
- Incorrect Examples:
- 'Tech's seminar':
the single quote between h and s should be
written twice.