Skip to main content
Engineering LibreTexts

5.1: Literals

  • Page ID
    54255
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The simplest expression is a direct value, referred to as a literal. Since literals are actual values, not variables, they cannot be changed. There are various types of literal constants described in the following sections that correspond to the Fortran data types.

    Integer Literals

    The following are some examples of integer constants:

    1
    0
    -100
    32767
    +42
    

    An integer must be a whole number (with no fractional component).

    Real Literals

    The following are some examples of real constants:

    1.0
    -0.25
    3.14159
    

    The real number should include the decimal point (i.e., the “.”). A real number includes the fractional part, even if the fraction is 0. Fortran will accept a number with the “.” and no further digits. For example, 5. is the same as 5.0. All examples in this text will include the “.0” to ensure clarity.

    E-Notation

    For larger real numbers, e-notation may be useful. The e-notation means that you should multiply the constant by 10 raised to the power following the "E". This is sometimes referred to as scientific notation.

    The following are some real constants using e-notation:

    2.75E5
    3.3333E-1
    

    Hence, 2.75E5 is \(2.75 \times 10^5\) or 275,000 and 3.333E-1 is \(3.333 \times 10^{−1}\) or 0.3333 or approximately one third.

    Complex Literals

    A complex constant is designated by a pair of constants (integer or real), separated by a comma and enclosed in parentheses. Examples are:

    (3.2, -4.1)
    (1.0, 9.9E-1)
    

    The first number denotes the real part and the second the imaginary part. Although a complex number always consists of two elements, it is considered a single value.

    Character Literals

    A character constant is either a single character or a set of characters, called a string. A character is a single character enclosed in quotes. A string consists of an arbitrary sequence of characters also enclosed in quotes. Some examples include:

    "X"
    "Hello World"
    "Goodbye cruel world!"
    "Have a nice day"
    

    Character and string constants (enclosed with quotes) are case sensitive. So, character “X” (upper- case) is not the same as “x” (lower-case).

    A problem arises if you want to have a quote in the string itself. A double quote will be interpreted as a single within a string. The two quotes must be together (no spaces between). For example, the string:

    "He said ""wow"" when he heard"
    

    Would be displayed as

    "He said "wow" when he heard"
    

    The double-quote is sometimes referred to as an escape character. Strings and characters must be associated with the character data type.

    Logical Constants

    The fifth type is the logical constant. These can only have one of two values:

    .true.
    .false.
    

    The dots enclosing the true and false are required.


    This page titled 5.1: Literals is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?