question about string literal

This is a discussion on question about string literal within the C Programming forums, part of the General Programming Boards category; Dear friends, Could you please explain what is the difference between "string literal" and "string constant"? I searched the Web, ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    3

    question about string literal

    Dear friends,
    Could you please explain what is the difference between "string literal" and "string constant"?
    I searched the Web, didn't find consistent description. some said that "string literal" is not constant in fact, but some said that "string literal" is also called "string constant".

    Can anyone help me?

    Thank you very much!
    Last edited by pangzhang; 07-27-2010 at 06:20 AM.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Code:
    #DEFINE STRING_CONSTANT "String Literal";
    
    char * string_constant = "String Literal";
    In the lines above, a string literal is when I actually have quotes surrounding the literal text. It's when I actually specify the exact string. In my opinion, string constant could mean two things. It could mean the #define statement that I have first. This is usually how constants are done in C. It means that before your code is compiled, every occurence of STRING_CONSTANT will be swapped with the string literal. The second line just puts that string somewhere in the applications memory, and gives me a pointer to it. I can create new pointers to it, etc... but I can't edit the original string because it's hard-coded into the application. That is why this method is also called a string constant.

    C++ has a keyword for declaring things as constants ('const'), but this is how it's done in C.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    @sean, What you are saying are not *true*.

    >>C++ has a keyword for declaring things as constants ('const'), but this is how it's done in C.
    C also has...

    There's no difference here.

    Code:
    // it's #define & no semicolon!
    #define STRING_CONSTANT "String Literal"
    
    char * string_constant = "String Literal";
    #define is just a textual replacement....
    You cannot modify string literal according to standard. That's why people recommends to use.
    const char *s = "string literal"; // compiler will emit error msg if you attempt to modify s content.
    // compared to
    char *s = "string literal";
    Last edited by Bayint Naung; 07-27-2010 at 07:24 AM.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    @sean, What you are saying are not *true*.
    My apologies, can you point out my mistake?

    edit: Ah yes I see - one can actually edit the string when a literal is assigned to a pointer.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    > Could you please explain what is the difference between "string literal" and "string constant"?
    String literal is standard terminology for a sequence of zero or more characters surrounded by double quotes. String constant is not a term used in the C standard, so it could mean whatever the speaker wants. Most often when both are used, they mean essentially the same thing.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    Quote Originally Posted by Prelude View Post
    > Could you please explain what is the difference between "string literal" and "string constant"?
    String literal is standard terminology for a sequence of zero or more characters surrounded by double quotes. String constant is not a term used in the C standard, so it could mean whatever the speaker wants. Most often when both are used, they mean essentially the same thing.
    I am more clear!
    Thank you very much!

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    Quote Originally Posted by sean View Post
    Code:
    #DEFINE STRING_CONSTANT "String Literal";
    
    char * string_constant = "String Literal";
    In the lines above, a string literal is when I actually have quotes surrounding the literal text. It's when I actually specify the exact string. In my opinion, string constant could mean two things. It could mean the #define statement that I have first. This is usually how constants are done in C. It means that before your code is compiled, every occurence of STRING_CONSTANT will be swapped with the string literal. The second line just puts that string somewhere in the applications memory, and gives me a pointer to it. I can create new pointers to it, etc... but I can't edit the original string because it's hard-coded into the application. That is why this method is also called a string constant.

    C++ has a keyword for declaring things as constants ('const'), but this is how it's done in C.
    Thank you very much for detailed explanation!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  4. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM
  5. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 05:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21