Sign ' is the same as \' ?

G

George2

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
 
E

Eric Sosman

George2 said:
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".
 
R

Richard Tobin

George2 said:
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
 
M

Mark Bluemel

Eric said:
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?
 
M

Mike Wahler

George2 said:
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
 
J

Joe Wright

Eric said:
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 = '\'';
 
C

Charlie Gordon

Mike Wahler said:
George2 said:
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 = '"""';
 
J

James Kuyper

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.
 
E

Eric Sosman

Charlie said:
[...]
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?
 
C

Charlie Gordon

Eric Sosman said:
Charlie said:
[...]
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top