Skip to main content
Engineering LibreTexts

11.2: Character Variable Declaration

  • Page ID
    54305
  • \( \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}}\)

    A character variable is a variable that can contain a set of 1 or more characters. Character variables must have a defined length. All declarations are placed in the beginning of the program (after the program statement). Declaring character variables formally defines the type and sets aside memory.

    This is performed with a type declaration statement in the form of:

    <type> :: <list of variable names>
    

    For character variables, the type is “character”. For example, to define a character variable to hold the day of week (i.e., “Wednesday”), the following declaration,

    character(len=9) :: dayofweek
    

    Would define the variable, dayofweek, with a maximum length of 9 possible characters.

    Additional examples include:

    character(len=3) :: symbol1, symbol2
    character :: symbol3
    character(1) :: symbol4, symbol5
    character(30) :: symbol6, symbol7
    

    The declarations can be entered in any order, however they must be at the beginning of the program.

    The “len=” is optional and can be omitted. When the length is omitted entirely, the default length is set to 1. This, “character”, “character(len=1), and “character(1)” are all the same.

    When multiple variables are included in a single declaration, they must all be the same length. If different lengths are required, separate declaration lines are required.


    This page titled 11.2: Character Variable Declaration 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.