Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > about string and character

Reply
Thread Tools

about string and character

 
 
dattts@gmail.com
Guest
Posts: n/a
 
      11-24-2007
what is the difference between a single character and a string
consisting only one character
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      11-24-2007
In article
<(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote on Saturday 24 Nov 2007 2:57
pm:

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

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      11-24-2007
In article <fi8ska$rev$(E-Mail Removed)>, santosh
<(E-Mail Removed)> wrote on Saturday 24 Nov 2007 3:28 pm:

> In article
> <(E-Mail Removed)>,
> (E-Mail Removed) <(E-Mail Removed)> wrote on Saturday 24 Nov 2007 2:57
> pm:
>
>> 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.

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      11-24-2007
(E-Mail Removed) wrote:

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

--
Perhaps Caffeine Now? Hedgehog
"A facility for quotation covers the absence of original thought." /Gaudy Night/

 
Reply With Quote
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      11-24-2007
On Nov 24, 5:58 pm, santosh <(E-Mail Removed)> wrote:
> In article
> <(E-Mail Removed)>,
> (E-Mail Removed) <(E-Mail Removed)> wrote on Saturday 24 Nov 2007 2:57
> pm:
>
> > 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 "".
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      11-24-2007
In article <(E-Mail Removed)>,
(E-Mail Removed) <(E-Mail Removed)> wrote:
>On Nov 24, 5:58 pm, santosh <(E-Mail Removed)> wrote:
>> In article
>> <(E-Mail Removed)>,
>> (E-Mail Removed) <(E-Mail Removed)> wrote on Saturday 24 Nov 2007 2:57
>> pm:
>>
>> > 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.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      11-24-2007
"(E-Mail Removed)" wrote:
>
> 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 */

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      11-24-2007
(E-Mail Removed) wrote:

> This string "0" consists of two characters.


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

--
Misplaced Hedgehog
Otherface: Jena RDF/Owl toolkit http://jena.sourceforge.net/

 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      11-24-2007
(E-Mail Removed) wrote:
> 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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      11-24-2007
On Sat, 24 Nov 2007 17:31:43 +0000, Chris Dollin wrote:
> (E-Mail Removed) wrote:
>
>> This string "0" consists of two characters.

>
> 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".
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert string with control character in caret notation to realcontrol character string. Bart Vandewoestyne C Programming 8 09-25-2012 12:41 PM
FAQ 4.31 How can I split a [character] delimited string except when inside [character]? PerlFAQ Server Perl Misc 0 01-25-2011 05:00 AM
How can I replace all occurrences of a character with another character in std string? herman C++ 5 08-30-2007 09:05 AM
8 bit character string to 16 bit character string Brand Bogard C Programming 8 05-28-2006 05:05 PM
getting the character code of a character in a string Velvet ASP .Net 9 01-19-2006 09:27 PM



Advertisments