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
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!
Huh? There *have* been answers, just not ones handed out on a plate.
39 posts so far (as displayed here), and no answer.
Well, for instance yours wasn't an answer to the original question.
But I could have missed one, I guess.
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
Clean your glasses.
Or your NNTP connection.
It does not. (Not in C, it doesn't.)
santosh said:Huh? There *have* been answers, just not ones handed out on a plate.
Yes it does. It constitutes a (one-element) contiguous sequence
of characters terminated by and including the first null character.
I actually did.
This I can't do unfortunately. Could you quote the answer?
39 posts so far (as displayed here), and no answer. Philosophy is fun!
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()
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.
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]
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.
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?
[...]Chris Dollin said:It does not. (Not in C, it doesn't.)
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.
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?
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
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.