Sign ' is the same as \' ?

Discussion in 'C Programming' started by George2, Nov 23, 2007.

  1. George2

    George2 Guest

    Hello everyone,


    I am surprised to see that the value of sign ' is the same as \'. So,
    there is no need to add sign \ before sign '? In my past knowledge of
    sign ', we always need to add sign \ before sign '. Any comments?

    Here is my simple program to test.

    Code:
    #include <string.h>
    
    int main (int argc, char** argv)
    {
    	char* p1 = "Hello \'World\'";
    	char* p2 = "Hello 'World'";
    	int result = 0;
    
    	result = strcmp(p1, p2);
    
    	return 0;
    }
    

    thanks in advance,
    George
     
    George2, Nov 23, 2007
    #1
    1. Advertising

  2. George2

    Eric Sosman Guest

    George2 wrote:
    > Hello everyone,
    >
    >
    > I am surprised to see that the value of sign ' is the same as \'. So,
    > there is no need to add sign \ before sign '? In my past knowledge of
    > sign ', we always need to add sign \ before sign '. Any comments?


    This snippet

    char ch = 'a';
    printf ("It's Jon%cs\n", ch);

    .... prints "It's Jonas". Your mission, should you choose to
    accept it, is to change the first line to make the output be
    "It's Jon's".

    --
    Eric Sosman
    lid
     
    Eric Sosman, Nov 23, 2007
    #2
    1. Advertising

  3. In article <>,
    George2 <> wrote:

    >I am surprised to see that the value of sign ' is the same as \'. So,
    >there is no need to add sign \ before sign '?


    There's no need to put a backslash before a single quote in a string,
    but in a character constant you need it:

    char *s = "'";
    char c = '\'';

    Conversely you need a backslash before a double quote in a string, but
    not in a character constant:

    char *s = "\"";
    char c = '"';

    You are allowed to use the backslashed forms even when not necessary.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Nov 23, 2007
    #3
  4. George2

    Mark Bluemel Guest

    Eric Sosman wrote:
    > George2 wrote:
    >> Hello everyone,
    >>
    >>
    >> I am surprised to see that the value of sign ' is the same as \'. So,
    >> there is no need to add sign \ before sign '? In my past knowledge of
    >> sign ', we always need to add sign \ before sign '. Any comments?

    >
    > This snippet
    >
    > char ch = 'a';
    > printf ("It's Jon%cs\n", ch);
    >
    > ... prints "It's Jonas". Your mission, should you choose to
    > accept it, is to change the first line to make the output be
    > "It's Jon's".


    How do you propose to make your posting self-destruct?
     
    Mark Bluemel, Nov 23, 2007
    #4
  5. George2

    Eric Sosman Guest

    Mark Bluemel wrote:
    > Eric Sosman wrote:
    >> George2 wrote:
    >>> Hello everyone,
    >>>
    >>>
    >>> I am surprised to see that the value of sign ' is the same as \'. So,
    >>> there is no need to add sign \ before sign '? In my past knowledge of
    >>> sign ', we always need to add sign \ before sign '. Any comments?

    >>
    >> This snippet
    >>
    >> char ch = 'a';
    >> printf ("It's Jon%cs\n", ch);
    >>
    >> ... prints "It's Jonas". Your mission, should you choose to
    >> accept it, is to change the first line to make the output be
    >> "It's Jon's".

    >
    > How do you propose to make your posting self-destruct?


    Unnecessary; I'll just deny all knowledge.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Nov 23, 2007
    #5
  6. George2

    Mike Wahler Guest

    "George2" <> wrote in message
    news:...
    > Hello everyone,
    >
    >
    > I am surprised to see that the value of sign ' is the same as \'. So,
    > there is no need to add sign \ before sign '? In my past knowledge of
    > sign ', we always need to add sign \ before sign '. Any comments?
    >
    > Here is my simple program to test.
    >
    >
    Code:
    > #include <string.h>
    >
    > int main (int argc, char** argv)
    > {
    > char* p1 = "Hello \'World\'";
    > char* p2 = "Hello 'World'";
    > int result = 0;
    >
    > result = strcmp(p1, p2);
    >
    > return 0;
    > }
    > 
    >


    Context, context, context.

    char c = '\''; /* apostrophe in character literal */
    char *s = "'"; /* apostrophe in string literal */

    char c = '"'; /* quote in character literal */
    char *s = "\""; /* quote in string literal */

    char c = '''; /* invalid */
    char *s = """; /* invalid */

    -Mike
     
    Mike Wahler, Nov 23, 2007
    #6
  7. George2

    Joe Wright Guest

    Eric Sosman wrote:
    > George2 wrote:
    >> Hello everyone,
    >>
    >>
    >> I am surprised to see that the value of sign ' is the same as \'. So,
    >> there is no need to add sign \ before sign '? In my past knowledge of
    >> sign ', we always need to add sign \ before sign '. Any comments?

    >
    > This snippet
    >
    > char ch = 'a';
    > printf ("It's Jon%cs\n", ch);
    >
    > ... prints "It's Jonas". Your mission, should you choose to
    > accept it, is to change the first line to make the output be
    > "It's Jon's".
    >

    char ch = '\'';

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Nov 23, 2007
    #7
  8. "Mike Wahler" <> a écrit dans le message de news:
    ...
    >
    > "George2" <> wrote in message
    > news:...
    >> Hello everyone,
    >>
    >>
    >> I am surprised to see that the value of sign ' is the same as \'. So,
    >> there is no need to add sign \ before sign '? In my past knowledge of
    >> sign ', we always need to add sign \ before sign '. Any comments?
    >>
    >> Here is my simple program to test.
    >>
    >>
    Code:
    >> #include <string.h>
    >>
    >> int main (int argc, char** argv)
    >> {
    >> char* p1 = "Hello \'World\'";
    >> char* p2 = "Hello 'World'";
    >> int result = 0;
    >>
    >> result = strcmp(p1, p2);
    >>
    >> return 0;
    >> }
    >> 
    >>

    >
    > Context, context, context.
    >
    > char c = '\''; /* apostrophe in character literal */
    > char *s = "'"; /* apostrophe in string literal */
    >
    > char c = '"'; /* quote in character literal */
    > char *s = "\""; /* quote in string literal */
    >
    > char c = '''; /* invalid */
    > char *s = """; /* invalid */


    Since you cannot have an empty character constant, ''' is not ambiguous, so
    it could be allowed.

    For the OP: now that you understand quoting issues better, how does C handle
    triple quoting à la python:

    char *s3 = """'""";

    And what is wrong with these:

    char c2 = '""';
    char c3 = '"""';

    --
    Chqrlie.
     
    Charlie Gordon, Nov 26, 2007
    #8
  9. George2

    James Kuyper Guest

    Charlie Gordon wrote:
    ....
    > And what is wrong with these:
    >
    > char c2 = '""';
    > char c3 = '"""';


    Nothing, as far as I can see, though the value stored in c2 and c3 is
    implementation-defined.
     
    James Kuyper, Nov 26, 2007
    #9
  10. George2

    Eric Sosman Guest

    Charlie Gordon wrote:
    > [...]
    > Since you cannot have an empty character constant, ''' is not ambiguous, so
    > it could be allowed.


    int ch = ''' + ''';

    What value do you suggest ch should have? Twice the
    value of '\'', or the implementation-defined value of a
    character literal containing seven characters, or something
    else?

    --
    Eric Sosman
    lid
     
    Eric Sosman, Nov 26, 2007
    #10
  11. "Eric Sosman" <> a écrit dans le message de
    news: ...
    > Charlie Gordon wrote:
    >> [...]
    >> Since you cannot have an empty character constant, ''' is not ambiguous,
    >> so it could be allowed.

    >
    > int ch = ''' + ''';
    >
    > What value do you suggest ch should have? Twice the
    > value of '\'', or the implementation-defined value of a
    > character literal containing seven characters, or something
    > else?


    Good point. Thus exits ''' for '\'';

    But then why disallow the empty character constant ?
    We could make the empty character constant have the value 0. That would
    improve readability of code dealing with ends of strings:

    if (str == '') {
    return i;
    }

    It would be somewhat consistent with the string literal syntax:

    char *s = "";
    assert(*s == '');

    I'll wait till April 1st, 2008 to submit this one for consideration in C20XY

    --
    Chqrlie.
     
    Charlie Gordon, Nov 26, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cowboy \(Gregory A. Beamer\)

    Single Sign on for web apps on same box

    Cowboy \(Gregory A. Beamer\), Nov 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    396
    Cowboy \(Gregory A. Beamer\)
    Nov 5, 2003
  2. David Hubbard
    Replies:
    2
    Views:
    848
    David Hubbard
    Jan 12, 2006
  3. Shawn
    Replies:
    2
    Views:
    383
    =?Utf-8?B?RWx0b24gVw==?=
    Feb 19, 2006
  4. Jimmy
    Replies:
    1
    Views:
    962
    Cowboy \(Gregory A. Beamer\)
    Nov 21, 2006
  5. Jimmy
    Replies:
    3
    Views:
    2,428
    shimmyshack
    Nov 20, 2006
Loading...

Share This Page