PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Sign ' is the same as \' ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Sign ' is the same as \' ?

Réponse
 
LinkBack Outils de la discussion
Vieux 23/11/2007, 13h29   #1
George2
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Sign ' is the same as \' ?

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
  Réponse avec citation
Vieux 23/11/2007, 14h14   #2
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sign ' is the same as \' ?

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
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 23/11/2007, 14h14   #3
Richard Tobin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sign ' is the same as \' ?

In article <e85dd802-d28c-4c4a-887a-e525b5f19793@a39g2000pre.googlegroups.com>,
George2 <george4academic@yahoo.com> 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.
  Réponse avec citation
Vieux 23/11/2007, 14h21   #4
Mark Bluemel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sign ' is the same as \' ?

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?
  Réponse avec citation
Vieux 23/11/2007, 14h38   #5
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sign ' is the same as \' ?

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
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 23/11/2007, 16h50   #6
Mike Wahler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sign ' is the same as \' ?


"George2" <george4academic@yahoo.com> wrote in message
news:e85dd802-d28c-4c4a-887a-e525b5f19793@a39g2000pre.googlegroups.com...
> 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


  Réponse avec citation
Vieux 23/11/2007, 19h04   #7
Joe Wright
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sign ' is the same as \' ?

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 ---
  Réponse avec citation
Vieux 26/11/2007, 09h27   #8
Charlie Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sign ' is the same as \' ?

"Mike Wahler" <mkwahler@mkwahler.net> a écrit dans le message de news:
13ke17p76urbr54@corp.supernews.com...
>
> "George2" <george4academic@yahoo.com> wrote in message
> news:e85dd802-d28c-4c4a-887a-e525b5f19793@a39g2000pre.googlegroups.com...
>> 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.


  Réponse avec citation
Vieux 26/11/2007, 12h59   #9
James Kuyper
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sign ' is the same as \' ?

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.
  Réponse avec citation
Vieux 26/11/2007, 13h35   #10
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sign ' is the same as \' ?

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
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 26/11/2007, 14h48   #11
Charlie Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sign ' is the same as \' ?

"Eric Sosman" <esosman@ieee-dot-org.invalid> a écrit dans le message de
news: Acudne_NXIY_VtfanZ2dnUVZ_q6mnZ2d@comcast.com...
> 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[i] == '') {
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.


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 04h22.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,22155 seconds with 19 queries