about string and character

D

dattts

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

santosh

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?
 
S

santosh

santosh said:
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?

Note that this second example could confuse you if you happen to compile
on systems where the type int is two bytes.

It's still worthwhile to consult your prescribed textbook and do a bit
of thinking on your own rather than ask for instant answers in
newsgroups.
 
C

Chris Dollin

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

One's a single character, and the other is a sequence containing
a single character, implemented as a pointer to a sequence of
characters the second of which is a terminating nul character and
the first is the character in the string.

It's like the difference between an apple and a paper bag containing
an apple: the bag is not an apple, even though you can get an apple
out of it, or replace the apple with an orange. (Since the string
bag can contain only one fruit character, you can't mix apples and
oranges [1].)

[1] In this bag. In bigger bags you can.
 
L

lovecreatesbea...

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";

This string "0" consists of two characters. The only string that
consists of a single char is "".
 
K

Kenny McCormack

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";

This string "0" consists of two characters. The only string that
consists of a single char is "".

strlen() and common sense say otherwise. You are confusing how big
something is with how much space it takes to store it. They are rarely
the same thing, and the later is usually greater than the former.
 
C

CBFalconer

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

The character contains exactly one char. The string contains two
chars, the char followed by '\0'. It the char is 'c', then you can
define the two cases with:

char c = 'c'; /* single char */
char *s = "c"; /* a string, non modifiable */
char m[] = "c"; /* a string, modifiable */
 
J

Joe Wright

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

You are posting too early in your knowledge quest. Read your book.

char ch = 'A';
char *ar = "A";

ch is the name of an object of type char which holds the value 'A'.

ar is the name of an object of type char* which holds the address of an
array of two char objects, 'A' and '\0'.

These two are exquisitely different things. Both are explained in your
book. Please read your C book.
 


$)CHarald van D)&k

The nul terminator isn't one of the characters "in" a string.

From a common sense perspective:
char a[] = "0";
and
char a[] = { '0', '\0' };
do exactly the same thing, and common sense says that '\0' is "in" a in
the second case, so it must also be "in" a in the first.

From a standards perspective:
A string is defined as "a contiguous sequence of characters terminated by
and including the first null character".
 
B

Barry Schwarz

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

Since a string must be terminated with a '\0', the only possible
1-character string is "". If instead you meant a string whose length
is 1, then such a string obviously contains two characters.

A single character can contain any one of at least 256 possible
values.


Remove del for email
 
C

Chris Dollin

Urm -- what happened there?
But it IS one of the characters in the char array holding the
string.

Oh, yes.
Elsethread $)CHarald van D )& k <[email protected]> (and
what happened /there/? Is my newsgarbler doing something
odd to names & content?) remarks

| From a standards perspective:
| A string is defined as "a contiguous sequence of characters
| terminated by and including the first null character".

which would make my position unsupported by the Standard's
terminology.
 
S

santosh

Chris said:
Urm -- what happened there?


Oh, yes.
Elsethread $)CHarald van D )& k <[email protected]> (and
what happened /there/? Is my newsgarbler doing something
odd to names & content?) remarks

No. His name appears mangled on my newsserver/newsreader too. I suspect
that he is using an encoding not supported by some software in the
Usenet hierarchy.

It was fine when Harald started posting to this group, but for a few
months now, his name has not been displayed properly.
| From a standards perspective:
| A string is defined as "a contiguous sequence of characters
| terminated by and including the first null character".

which would make my position unsupported by the Standard's
terminology.

Yes. So my first post contains errors. Hopefully the OP would have read
far enough into the thread to note the corrections.
 


$)CHarald van D)&k

No. His name appears mangled on my newsserver/newsreader too. I suspect
that he is using an encoding not supported by some software in the
Usenet hierarchy.

It was fine when Harald started posting to this group, but for a few
months now, his name has not been displayed properly.

Apparently KNode encoded my name in utf-8, but Pan encodes it in iso-2022-
kr. I'll check if there's an option to change that.
 
R

Richard Heathfield

Harald van D?k said:
Does it display better like this?

Yes. But you're still going to have problems with the i-j ligature (or
diphthong, or whatever the proper name is).
 
H

Harald van Dijk

Apparently KNode encoded my name in utf-8, but Pan encodes it in
iso-2022-kr. I'll check if there's an option to change that.

Does it display better like this?
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top