2

I'm a perl programmer and surprised to find c language has no a convenient way to store a paragraph, like:

       my $a = <<'dd';
          hello wolrd..
           1
            2
             3
       dd 

So how do I the smiliary operation in C?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
xiaoyafeng
  • 41
  • 4

3 Answers3

5

It's done like this:

char a[] = 
    "hello world\n"
    " 1\n"
    "  2\n"
    "   3\n";
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    There are a couple of syntactical errors in that code if I'm not terribly mistaken. You need an array of char pointers (with fixed length) instead of just an array of chars and the initialization list requires brackets and commas. This should work: char a[][50] = {"Hello World\r\n", " 1\r\n", " 2\r\n", " 3\r\n"}; (usage of '\r' depends on target OS). You could also use just one string: char a[] = "Hello World\r\n 1\r\n 2\r\n 3\r\n"; – SvenS Oct 15 '12 at 08:06
  • @Sven Ummm, no. An array of strings would be a different type altogether. Also, you really need to split the string up into multiple strings that are concatenated by the compiler if you're building with MSVC or you run the risk of hitting a rather low limit on the length of individual parsed tokens. – Donal Fellows Oct 15 '12 at 08:20
  • 1
    @SvenS A string followed by another string (with only whitespace, no matter how much or of what kind, between them) are automatically concatenated to be a single string. – Some programmer dude Oct 15 '12 at 08:23
  • @DonalFellows I thought an array of strings was what Joachim had aimed for, but I guess I was wrong. That MSVC has a limit on string literal length was news to me, thanks for the info. – SvenS Oct 15 '12 at 08:30
5

You've two different answers to your question which provide many of the same functionalities.

Both

char *str="Line 1\nLine 2\nLine 3";

and

char str[]="Line 1\nLine 2\nLine 3";

Allow you to print a paragraph as such:

printf("%s",str);

However, the first declaration (char *str) creates a string in what is generally read-only memory, whereas the second allows the string to be edited during run-time. This delineation is important, but not always clear. See this question for a few more details.

The character \n is the line feed character, and you should check to ensure that it behaves the way you expect it to on your target platform. For instance, on DOS you may need to use `"\r\n", which is carriage return + line feed. Wiki has an article about this.

Another difference in these forms, as one commenter pointed out, is that *str works as a pointer whereas str[] does not. They often, but not always, have the same behavior; this question has more information regarding this.

As some commenters have pointed out, there is a limit on the length of string literals in some compilers. MSVC has a limit of 2048 characters (see here) whereas GCC has no limit, by some accounts. A length of at least 509 one-byte characters is guaranteed by C90; this was increased to 4095 in C99.

Regardless, if you want to avoid this length limit or you want to organize the text in a prettier way, you can use this format (note that newlines and quotes must be used explicitly, the compiler treats the adjacent strings as being concatenated):

char *str =
  "Line 1\n"
  "Line 2\n"
  "Line 3\n";

or this (the backslashes at the end of the line escape the newline you've inserted for the formatting, if you indent your code here, that will become part of the string):

char *str =
"Line 1 \
Line 2 \
Line 3";
Community
  • 1
  • 1
Richard
  • 56,349
  • 34
  • 180
  • 251
  • I'd also add for clarity that in the first case `str` is a pointer to `char` and in the second case it's an array of `char`. – Alexey Frunze Oct 15 '12 at 08:01
  • I think they're both pointers, though the syntax is perhaps clearer for the second case. Certainly, if you define `str[]="hi"` then both `str[0]` and `*str` point to "h". C doesn't differentiate between arrays and pointers and the arithmetic works the same for both. – Richard Oct 15 '12 at 08:03
  • 1
    They are not both pointers. Read your favorite C book or FAQ or look this up on stack overflow. They can only be both pointers if `char* str` or `char str[]` are function parameters, which they aren't here. Look up "array to pointer decay". – Alexey Frunze Oct 15 '12 at 08:05
1

you may try

char *str = "hello world\n 1\n  2\n   3\n";
Weacked
  • 954
  • 2
  • 7
  • 18
  • With some compilers (notably MSVC) you hit a limit on token if you try doing that for any real length of string (though not on the size in your example). Far better to use the fact that two adjacent string literals are concatenated by the compiler. (Yes, that's standard, and it happens after the problem part of MSVC; the compiler core is happy with huge string literals if you need them.) – Donal Fellows Oct 15 '12 at 08:23