[comp.std.c] implicitly continued string constants

morgan@ogicse.cse.ogi.edu (Clark O. Morgan) (05/15/91)

I noticed the other day that gcc (version 1.39) does not error
implicitly continued string constants (e.g., string constants that
contain actual newlines).  This surprised me, because I personally
consider such strings to be programming typos, especially given the
alternatives available for continuing long strings (i.e., concatenation
or continuation with an explicit \n).  I have been told that gcc is
ANSI C compatible.

So here's the question, is the following program legal ANSI C?

Script started on Wed May 15 00:43:44 1991
$ cat -n t.c
     1	extern int printf(char *, ...);
     2	
     3	int
     4	main()
     5	{
     6	   printf("junk);
     7	                 string\n");
     8	   return (0);
     9	}
$ gcc -Wall t.c                    <--- No complaints
$ a.out
junk);
                 string
$ exit

script done on Wed May 15 00:44:09 1991


Thanks in advance.

henry@zoo.toronto.edu (Henry Spencer) (05/15/91)

In article <21455@ogicse.ogi.edu> morgan@ogicse.cse.ogi.edu (Clark O. Morgan) writes:
>I noticed the other day that gcc (version 1.39) does not error
>implicitly continued string constants (e.g., string constants that
>contain actual newlines)...
>
>So here's the question, is the following program legal ANSI C?

No.  Newline is specifically excluded from the list of characters that
can occur within an ANSI C string literal.

You err in assuming that gcc is an ANSI C compiler, however.  It will try
to pretend to be one if you give the right magic combination of options,
but by default it compiles something which is neither old C nor ANSI C.
-- 
And the bean-counter replied,           | Henry Spencer @ U of Toronto Zoology
"beans are more important".             |  henry@zoo.toronto.edu  utzoo!henry

rfg@NCD.COM (Ron Guilmette) (05/19/91)

In article <1991May15.155113.10624@zoo.toronto.edu> henry@zoo.toronto.edu (Henry Spencer) writes:
+In article <21455@ogicse.ogi.edu> morgan@ogicse.cse.ogi.edu (Clark O. Morgan) writes:
+>I noticed the other day that gcc (version 1.39) does not error
+>implicitly continued string constants (e.g., string constants that
+>contain actual newlines)...
+>
+>So here's the question, is the following program legal ANSI C?
+
+No.  Newline is specifically excluded from the list of characters that
+can occur within an ANSI C string literal.
+
+You err in assuming that gcc is an ANSI C compiler, however.  It will try
+to pretend to be one if you give the right magic combination of options,
+but by default it compiles something which is neither old C nor ANSI C.

There is nothing "magic" about it.

Using the -pedantic and -ansi options causes GCC to complain about all
violations of ANSI C (including the one being discussed here).

If you are a pedant, you can install your copy of gcc so that these
options are always on by default.
-- 

// Ron ("Loose Cannon") Guilmette
// Internet: rfg@ncd.com      uucp: ...uunet!lupine!rfg
// New motto:  If it ain't broke, try using a bigger hammer.