-1

I want to have a array with multiple lines, but as my code sample shows, the C code doesn't recognize the ", also depending on the IDE the code is having a diferrent behavior, I tried change the inside " for ' but it doesn't work too

char palavra2 [] = "{Conversor de Temperatura: °C -> °F}
                        prg Exemplo_02;
                        {Declaração de variáveis}
                        var
                          int c;
                          float f;
                        {Programa principal}
                          begin
                          write("Informe a temperatura em °C: ");
                          read(c);
                          f <- 1.8 * C - 32;
                          write("O correspondente em Fahrenheit é: ", f);
                        end."
  • 1
    You can write `"Hello, " "World!"`, it will be *one* string literal and the individual parts of the string can be on separate lines. You could have easily found this out, it is well documented. – Cheatah Sep 24 '22 at 20:15
  • Funny to realise that you want the LF at the end of each of those lines, but don't recognise that the first line has far less indent whitespace (meaning zero) than the other lines you've typed... – Fe2O3 Sep 25 '22 at 03:12

1 Answers1

4

Your string cannot span multiple line like this. The two good options are:

char palavra2[] = "{Conversor de Temperatura: °C -> °F}\n\
prg Exemplo_02;\n\
  ...";

or:

char palavra2 [] =
   "{Conversor de Temperatura: °C -> °F}\n"
   "prg Exemplo_02;\n"
   "...";

You can, of course, store your string in a file and open/read it into a string.

The next c standard (c23) includes an #embed feature to include arbitrary data from an external file.

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • Too bad C and C++ standards people seem to have some kind of enmity thing going on. C++ already has raw string literals. It would be nice not to have yet another obstacle to making C code that will compile cleanly in both languages, just because each language has to do the same thing differently... /end pipe dream. Nice answer, btw. – Dúthomhas Sep 24 '22 at 20:56
  • gcc supports raw string literals as an extension per https://stackoverflow.com/questions/24850244/does-c-support-raw-string-literals – Allan Wind Sep 24 '22 at 21:05