3

So, I can initialize a string in Fortran with both

CHARACTER(LEN=4)::string
string = "jklm"

and

CHARACTER(LEN=4)::string
string = 'jklm'

What's the difference?

  • 1
    As a minor (perhaps trivial) point, there's no _initialization_ here, just _assignment_. The important concept is in delimiting a character literal constant. – francescalus Jan 09 '17 at 20:22
  • @francescalus thanks for clarifying. But I don't quite get your second sentence, even though I tried to translate. Do you mean to say that ... I only assign string to an already existing constant of format string ("jklm")? – Kama Kashkanov Jan 10 '17 at 14:44
  • `'jklm'` is what is known as a (character) _literal constant_. It's a thing which just has, and always has, a value made up of those four letters. Such a constant can be used in initialization, assignment, or in many other ways, and the interpretation of quote/apostrophe doesn't change that. So, the delimiter (`"` or `'`) which tells the compiler "what follows here forms a string" and "what follows here is back to outside the string" isn't specific to initialization but to saying "constant". [Hopefully that hasn't made things less clear...] – francescalus Jan 10 '17 at 17:23

2 Answers2

3

At a glance of the fortan docs, it seems the same for the most part. Though if you plan to use a single quote within a string, use a double quote to enclose it.

// correct
"John's Stuff"

// wrong
'John's Stuff'

Alternatively:

// correct
'She said "Hey!"'
// wrong
"She said "Hey!""

http://www.fortran90.org/src/rosetta.html#strings-and-formatting

Edit: btw, same goes for most programming languages.

Brian
  • 1,873
  • 16
  • 25
2

Functionally, they are the same. There is no difference at all although "" looks a little bit more readable.

Neville
  • 484
  • 4
  • 10