Skip to main content
Engineering LibreTexts

11.1: Character and String Constants

  • Page ID
    54304
  • \( \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 is a single character or symbol, typically enclosed in quotes. For example, letters (“A”-”Z” and “a” - “z”), punctuation (“!”, “,”, “?”, etc.) , symbols, (“@”, “#”, “>”, etc.), and digits “1”, “2” are characters.

    Some examples include:

    "X"
    "z"
    "5"
    

    Character and string constants are case sensitive. So, character “X” (upper-case) is not the same as “x” (lower-case). When a digit is enclosed in quotes, it is treated as a character and consequently arithmetic operations (addition, subtraction, etc.) are not allowed.

    A string is a series of characters. A string consists of an arbitrary sequence of characters also enclosed in quotes. Some examples include:

    "Hello World."
    "456"
    "1 2 3"
    "456?"
    "Goodbye cruel world!!"
    "Have a nice day?"
    

    Since digits enclosed in quotes are not numeric values, the strings “1 2 3” and “456?” are allowed.

    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.


    This page titled 11.1: Character and String Constants 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?