about string and character

Y

ymuntyan

what is the difference between a single character and a string
consisting only one character

39 posts so far (as displayed here), and no answer. Philosophy is fun!

Yevgen
 
Y

ymuntyan

Huh? There *have* been answers, just not ones handed out on a plate.

Well, for instance yours wasn't an answer to the original question.
But I
could have missed one, I guess.

char a = 0;

The byte occupied by this object constitutes a one-character string,
you know.
Looking forward for amusing explanations of how a sequence of one
element is
different from one element ;)

Best regards,
Yevgen
 
S

santosh

Well, for instance yours wasn't an answer to the original question.
But I could have missed one, I guess.

At a glance you seem to have missed at least four perfectly good
answers. I agree that mine wasn't one of them.

As for the sub-thread following the post by "lovecreatesbeauty", the OP
can easily ignore that if he is not interested in legalistic minutiae.

<snip>
 
C

Chris Dollin

Well, for instance yours wasn't an answer to the original question.
But I
could have missed one, I guess.

char a = 0;

The byte occupied by this object constitutes a one-character string,
you know.

It does not. (Not in C, it doesn't.)
Looking forward for amusing explanations of how a sequence of one
element is different from one element ;)

Shame, really; at the moment (and earlier) I can only do unamusing
true ones.
 
Y

ymuntyan

It does not. (Not in C, it doesn't.)

Yes it does. It constitutes a (one-element) contiguous sequence
of characters terminated by and including the first null character.

Regards,
Yevgen
 
P

Philip Potter

Yes it does. It constitutes a (one-element) contiguous sequence
of characters terminated by and including the first null character.

A sequence of characters has type char[]. Your variable 'a' has type
char. Therefore, a is not a sequence of one character. If you had
instead said:

char a[1] = {0};

then your statement would have been correct.

Other properties of a sequence which your variable 'a' lacks:

* Sequences can be subscripted: a
* Sequences decay to a pointer when, among other things, used as a
function parameter - f(a) should pass a 'char *' to f()
 
P

Philip Potter

I actually did.


This I can't do unfortunately. Could you quote the answer?

No. This group helps those who help themselves. I feel that Chris
Dollin's, CBFalconer's, Joe Wright's, and Barry Schwarz's responses
provide all the necessary information to answer the OP.
 
J

J. J. Farrell

39 posts so far (as displayed here), and no answer. Philosophy is fun!

I'd get a better news server. There were three or four thorough answers
posted three or four days ago, along with others which would help the OP
work it out for himself.
 
J

J. J. Farrell

Yes, a string of length zero.
A sequence of characters has type char[].

Chapter and Verse, please. As far as I can see, the Standard uses
"sequence" in a normal English sense, with the extension of allowing a
sequence to be empty. I can't see any requirement that a sequence of
characters have type char[].
Your variable 'a' has type
char. Therefore, a is not a sequence of one character. If you had
instead said:

char a[1] = {0};

then your statement would have been correct.

Other properties of a sequence which your variable 'a' lacks:

* Sequences can be subscripted: a


Arrays can be subscripted. There is no 'sequence' type in C.
* Sequences decay to a pointer when, among other things, used as a
function parameter - f(a) should pass a 'char *' to f()

Again, true of arrays but not of sequences.
 
Y

ymuntyan

No. This group helps those who help themselves. I feel that Chris
Dollin's, CBFalconer's, Joe Wright's, and Barry Schwarz's responses
provide all the necessary information to answer the OP.

You mean responses talking about strings of
length 1? Those are not strings "consisting
only one character". Or perhaps stuff about
whether nul character is a character?

Anyway, there wasn't an answer, do I understand
you correctly? Because I agree there was more
than enough information. I just hoped someone
could formulate the difference between a nul
character and the string it makes. I seriously
tried and didn't succeed, went into metaphysics.
Oh well.

Best regards,
Yevgen
 
B

Barry Schwarz

pete said:
That's just completely wrong.

No ...
The standard says that the null byte is part of the string.

... yes. (Did you miss my earlier post?)
The number of participants in this thread
who know what a string is, is dissappointingly small.

It's not to do with what a string "is". It's to do with the meaning
of the term "character in a string". The blindingly obvious [1]

But it has everything to do with what a string is. The orignal poster
asked "what is the difference between a single character and
a string consisting only one character". Since the standard says the
terminating null is part of the string, can you write any string other
than "" consisting of a single character?

meaning of "the string S contains the character C" and the meaning
implied by the Standard are not the same; this is an interesting
fact, but it doesn't make the obvious meaning "completely wrong".

[1] To me; viz, C is in S if C == S for some i in 0 .. strlen(S) - 1.


Since by definition the standard cannot be wrong and since your
meaning precludes the '\0' from being part of the string as the
standard says it is, what part of completely wrong doesn't apply?


Remove del for email
 
K

karthikbalaguru

what is the difference between a single character and a string
consisting only one character

Try this program:

#include <stdio.h>

int main(void) {
char c0 = '0';
char c1[] = "0";

printf("Size of c0 is %u\nSize of c1 is %u\n", sizeof c0, sizeof c1);
return 0;

}

What is the output and why are the sizes for 'c0' and 'c1' different?
What does your C textbook say?

And try this one too:

#include <stdio.h>

int main(void) {
printf("Size of 'a' is %u\nSize of \"a\" is %u\n",
sizeof 'a', sizeof "a");
return 0;

}

What is the output and why are they different for 'a' and "a"?
What does you textbook say about this?

Nice.
Another interesting way of answering :):)

Karthik Balaguru
 
K

Keith Thompson

Chris Dollin said:
It does not. (Not in C, it doesn't.)
[...]

I believe it does.

Consider this:

#include <stdio.h>
#include <string.h>
int main(void)
{
char a = 0;
printf("strlen(&a) = %d\n", (int)strlen(&a));
return 0;
}

A single object of the char can be treated as an array of one char.
 
C

Chris Dollin

Keith said:
Chris Dollin said:
It does not. (Not in C, it doesn't.)
[...]

I believe it does.

Consider this:

#include <stdio.h>
#include <string.h>
int main(void)
{
char a = 0;
printf("strlen(&a) = %d\n", (int)strlen(&a));
return 0;
}

A single object of the char can be treated as an array of one char.

Duh ... this time, the `= 0;` made it along the optic nerve to the
brain.

Point to Keith. I'm fortunate that I can't boil my head, which would
otherwise be the appropriate response.
 
C

Chris Dollin

Barry said:
meaning of "the string S contains the character C" and the meaning
implied by the Standard are not the same; this is an interesting
fact, but it doesn't make the obvious meaning "completely wrong".

[1] To me; viz, C is in S if C == S for some i in 0 .. strlen(S) - 1.


Since by definition the standard cannot be wrong and since your
meaning precludes the '\0' from being part of the string as the
standard says it is, what part of completely wrong doesn't apply?


"Completely".
 
B

Barry Schwarz

Well, for instance yours wasn't an answer to the original question.
But I
could have missed one, I guess.

char a = 0;

The byte occupied by this object constitutes a one-character string,
you know.
Looking forward for amusing explanations of how a sequence of one
element is
different from one element ;)

Then why don't you just code
char a = "";
and see what your compiler has to say about it?

Probably because you think you know this and are just trolling.

Here are four bytes in memory on an 8-byte boundary: 0x20414200.
Do they represent a string or part of one; four individual char; 2
short; 1 short; 2 int; 1 int; if either type of int are the
magnitude(s) large, medium, or small; 1 long; or one float? It's the
same four bytes; how could they possibly mean different things?

Try
char x[5];
void *p1 = x; /*(1)*/
void *p2 = &x; /*(2)*/

p1 is obviously equal to p2. Since equality is transitive, how can
the right hand expression of (1) possibly be different than the right
hand expression of (2)?


Remove del for email
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top