473,468 Members | 4,655 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

about string and character

what is the difference between a single character and a string
consisting only one character
Nov 24 '07
66 3071
On Nov 24, 2:58 pm, santosh <santosh....@gmail.comwrote:
In article
<bffb8ed3-e648-424a-a49f-f68b7b1a9...@s19g2000prg.googlegroups.com>,
dat...@gmail.com <dat...@gmail.comwrote 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?
Nice.
Another interesting way of answering :):)

Karthik Balaguru
Nov 29 '07 #51
Barry Schwarz wrote:
>>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[i] 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".

--
Far-Fetched Hedgehog
The "good old days" used to be much better.

Nov 29 '07 #52
In article <tt******************************@comcast.com>
Joe Wright <jo********@comcast.netwrote:
>char str[] = "Hello";
The sizeof str is 6 while strlen(str) is 5.
Indeed.
>While the nul is of type char it's not really a character, is it?
It is definitely a "char". It is a valid character, although it
is not what some people call a character. (Some people call *me*
a character. :-) ) As usual, this all comes down to the general
problem of making sure that everyone involved in some discussion
intents the same semantics when they use the same words. (I
occasionally see people say things like "bah, mere semantics", yet
"semantics" are essential to correct understanding.)

As another data point, note that strchr() and strrchr() will
find the terminating '\0'. For instance:

char s[] = "Hello";
char *p = strchr(s", '\0');

will leave p non-NULL, pointing to the '\0' stored in s[5]. So we
can say that for some Standard C functions dealing with strings,
the terminating '\0' *is* "part of the string"; for others (e.g.,
strlen()), it is excluded.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Dec 2 '07 #53
Chris wrote:
) will leave p non-NULL, pointing to the '\0' stored in s[5]. So we
) can say that for some Standard C functions dealing with strings,
) the terminating '\0' *is* "part of the string"; for others (e.g.,
) strlen()), it is excluded.

Doesn't strlen return the index of the first '\0' in a string ? :-)
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Dec 2 '07 #54
Willem said:
Chris wrote:
) will leave p non-NULL, pointing to the '\0' stored in s[5]. So we
) can say that for some Standard C functions dealing with strings,
) the terminating '\0' *is* "part of the string"; for others (e.g.,
) strlen()), it is excluded.

Doesn't strlen return the index of the first '\0' in a string ? :-)
And the last. :-)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Dec 2 '07 #55
"Richard Heathfield" <rj*@see.sig.invalida écrit dans le message de news:
_9******************************@bt.com...
Willem said:
>Chris wrote:
) will leave p non-NULL, pointing to the '\0' stored in s[5]. So we
) can say that for some Standard C functions dealing with strings,
) the terminating '\0' *is* "part of the string"; for others (e.g.,
) strlen()), it is excluded.

Doesn't strlen return the index of the first '\0' in a string ? :-)

And the last. :-)
That's debatable:

strlen("abc\0def") -3

Are 'd', 'e', 'f', and the final '\0' not part of the object "abc\0def" ?
Is "abc\0def" not a string ?

--
Chqrlie.
Dec 3 '07 #56
In article <47***********************@news.free.fr"Charlie Gordon" <ne**@chqrlie.orgwrites:
"Richard Heathfield" <rj*@see.sig.invalida écrit dans le message de news:
_9******************************@bt.com...
Willem said:
Chris wrote:
) will leave p non-NULL, pointing to the '\0' stored in s[5]. So we
) can say that for some Standard C functions dealing with strings,
) the terminating '\0' *is* "part of the string"; for others (e.g.,
) strlen()), it is excluded.

Doesn't strlen return the index of the first '\0' in a string ? :-)
And the last. :-)

That's debatable:

strlen("abc\0def") -3

Are 'd', 'e', 'f', and the final '\0' not part of the object "abc\0def" ?
Is "abc\0def" not a string ?
They are a single object. But only the firt part is a string.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Dec 3 '07 #57
Dik T. Winter wrote:
In article <47***********************@news.free.fr"Charlie Gordon"
<ne**@chqrlie.orgwrites:
"Richard Heathfield" <rj*@see.sig.invalida écrit dans le message
de news: _9******************************@bt.com...
Willem said:

Chris wrote:
) will leave p non-NULL, pointing to the '\0' stored in s[5].
So we ) can say that for some Standard C functions dealing with
strings, ) the terminating '\0' *is* "part of the string"; for
others (e.g., ) strlen()), it is excluded.
>
Doesn't strlen return the index of the first '\0' in a string ?
:-)

And the last. :-)
>
That's debatable:
>
strlen("abc\0def") -3
>
Are 'd', 'e', 'f', and the final '\0' not part of the object
"abc\0def" ? Is "abc\0def" not a string ?

They are a single object. But only the firt part is a string.
Then the name "character string literal" would be misleading.

Dec 3 '07 #58
"Charlie Gordon" <ne**@chqrlie.orgwrites:
"Richard Heathfield" <rj*@see.sig.invalida écrit dans le message de news:
_9******************************@bt.com...
>Willem said:
>>Chris wrote:
) will leave p non-NULL, pointing to the '\0' stored in s[5]. So we
) can say that for some Standard C functions dealing with strings,
) the terminating '\0' *is* "part of the string"; for others (e.g.,
) strlen()), it is excluded.

Doesn't strlen return the index of the first '\0' in a string ? :-)

And the last. :-)

That's debatable:

strlen("abc\0def") -3

Are 'd', 'e', 'f', and the final '\0' not part of the object "abc\0def" ?
They're part of the object; they're not part of the string.
Is "abc\0def" not a string ?
No, it isn't. { 'a', 'b', 'c', '\0' } is a string, defined by the
standard as "a contiguous sequence of characters terminated by and
including the first null character".

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 3 '07 #59
santosh <sa*********@gmail.comwrites:
Dik T. Winter wrote:
>In article <47***********************@news.free.fr"Charlie Gordon"
<ne**@chqrlie.orgwrites:
[...]
> strlen("abc\0def") -3

Are 'd', 'e', 'f', and the final '\0' not part of the object
"abc\0def" ? Is "abc\0def" not a string ?

They are a single object. But only the firt part is a string.

Then the name "character string literal" would be misleading.
This is acknowledged in a footnote in C99 6.4.5:

A character string literal need not be a string (see 7.1.1),
because a null character may be embedded in it by a \0 escape
sequence.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 3 '07 #60
Chris Torek wrote:
In article <tt******************************@comcast.com>
Joe Wright <jo********@comcast.netwrote:
>char str[] = "Hello";
The sizeof str is 6 while strlen(str) is 5.

Indeed.
>While the nul is of type char it's not really a character, is it?

It is definitely a "char". It is a valid character, although it
is not what some people call a character. (Some people call *me*
a character. :-) ) As usual, this all comes down to the general
problem of making sure that everyone involved in some discussion
intents the same semantics when they use the same words. (I
occasionally see people say things like "bah, mere semantics", yet
"semantics" are essential to correct understanding.)

As another data point, note that strchr() and strrchr() will
find the terminating '\0'. For instance:

char s[] = "Hello";
char *p = strchr(s", '\0');

will leave p non-NULL, pointing to the '\0' stored in s[5]. So we
can say that for some Standard C functions dealing with strings,
the terminating '\0' *is* "part of the string"; for others (e.g.,
strlen()), it is excluded.
All good and true, nonetheless I contend, given ASCII, that codes from 0
through 127 are values of type char but not what I call a character. The
character values start at 32 (space) through 126. Please consider..

| 0 NUL| 1 SOH| 2 STX| 3 ETX| 4 EOT| 5 ENQ| 6 ACK| 7 BEL|
| 8 BS | 9 HT | 10 LF | 11 VT | 12 FF | 13 CR | 14 SO | 15 SI |
| 16 DLE| 17 DC1| 18 DC2| 19 DC3| 20 DC4| 21 NAK| 22 SYN| 23 ETB|
| 24 CAN| 25 EM | 26 SUB| 27 ESC| 28 FS | 29 GS | 30 RS | 31 US |
| 32 | 33 ! | 34 " | 35 # | 36 $ | 37 % | 38 & | 39 ' |
| 40 ( | 41 ) | 42 * | 43 + | 44 , | 45 - | 46 . | 47 / |
| 48 0 | 49 1 | 50 2 | 51 3 | 52 4 | 53 5 | 54 6 | 55 7 |
| 56 8 | 57 9 | 58 : | 59 ; | 60 < | 61 = | 62 | 63 ? |
| 64 @ | 65 A | 66 B | 67 C | 68 D | 69 E | 70 F | 71 G |
| 72 H | 73 I | 74 J | 75 K | 76 L | 77 M | 78 N | 79 O |
| 80 P | 81 Q | 82 R | 83 S | 84 T | 85 U | 86 V | 87 W |
| 88 X | 89 Y | 90 Z | 91 [ | 92 \ | 93 ] | 94 ^ | 95 _ |
| 96 ` | 97 a | 98 b | 99 c |100 d |101 e |102 f |103 g |
|104 h |105 i |106 j |107 k |108 l |109 m |110 n |111 o |
|112 p |113 q |114 r |115 s |116 t |117 u |118 v |119 w |
|120 x |121 y |122 z |123 { |124 | |125 } |126 ~ |127 DEL|

It's easy for me to see ACK (6) as char type but I can't see it as a
character. This argument is a mole-hill, not a mountain. :-)

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 3 '07 #61
Willem wrote:
Chris wrote:
) will leave p non-NULL, pointing to the '\0' stored in s[5]. So we
) can say that for some Standard C functions dealing with strings,
) the terminating '\0' *is* "part of the string"; for others (e.g.,
) strlen()), it is excluded.

Doesn't strlen return the index of the first '\0' in a string ? :-)
SaSW, Willem
No. 'size_t strlen(const char *s);' returns an unsigned integer count of
the number of characters before the terminating nul.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 3 '07 #62
Joe Wright said:
Willem wrote:
<snip>
>Doesn't strlen return the index of the first '\0' in a string ? :-)
No. 'size_t strlen(const char *s);' returns an unsigned integer count of
the number of characters before the terminating nul.
How does that result differ from the index of the first '\0' in a string?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Dec 3 '07 #63
Richard Heathfield wrote:
Joe Wright said:
>Willem wrote:

<snip>
>>Doesn't strlen return the index of the first '\0' in a string ? :-)
No. 'size_t strlen(const char *s);' returns an unsigned integer count of
the number of characters before the terminating nul.

How does that result differ from the index of the first '\0' in a string?
It doesn't. Apologies to all.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 4 '07 #64
Joe Wright <jo********@comcast.netwrites:
[...]
All good and true, nonetheless I contend, given ASCII, that codes from
0 through 127 are values of type char but not what I call a
character. The character values start at 32 (space) through
126. Please consider..
[ASCII chart snipped]
It's easy for me to see ACK (6) as char type but I can't see it as a
character. This argument is a mole-hill, not a mountain. :-)
It's a character as the term is defined by the C standard. See C99
3.7 and 3.7.1.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 4 '07 #65
Joe Wright wrote:
Willem wrote:
>Chris wrote:
>>will leave p non-NULL, pointing to the '\0' stored in s[5]. So
we can say that for some Standard C functions dealing with
strings, the terminating '\0' *is* "part of the string"; for
others (e.g., strlen()), it is excluded.

Doesn't strlen return the index of the first '\0' in a string ? :-)

No. 'size_t strlen(const char *s);' returns an unsigned integer
count of the number of characters before the terminating nul.
Which (index, not address) is the same thing. If the char array
doesn't hold a string it causes UB.

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

Dec 4 '07 #66
Joe Wright wrote:
[...]
All good and true, nonetheless I contend, given ASCII, that codes from 0
through 127 are values of type char but not what I call a character. The
character values start at 32 (space) through 126. Please consider..
[...]
It's easy for me to see ACK (6) as char type but I can't see it as a
character. This argument is a mole-hill, not a mountain. :-)
Poor guy. I guess one could get along without tabs,
but C without newlines must be pretty hard to manage ...

--
Er*********@sun.com
Dec 4 '07 #67

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Pavils Jurjans | last post by:
Hello, Here's an excerpt from msdn online documentation: An index is the position of a Char, not a Unicode character, in a String. An index is a zero-based, nonnegative number starting from...
3
by: John Salerno | last post by:
This is an example in the book I'm reading: string fullName = " Edward C Koop "; fullName = fullName.Trim(); string names = fullName.Split(' '); string firstName = names; //...
33
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
23
by: Steven T. Hatton | last post by:
This is one of the first obstacles I encountered when getting started with C++. I found that everybody had their own idea of what a string is. There was std::string, QString, xercesc::XMLString,...
8
by: hu | last post by:
hi, everybody! I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0. Program is simple, but mistake is confusing. First, the below code can get right outcome:"ello world, hello...
5
by: hn.ft.pris | last post by:
Hi: I'm a beginer of STL, and I'm wondering why none of below works: ######################################################################## .......... string str("string"); if ( str == "s" )...
21
by: softwindow | last post by:
#include "stdio.h" #include "malloc.h" struct student{ int age; char *nms; struct student *next; }; struct student *create(){ int ags=0,size=sizeof(struct student); char *nms=" ";
7
by: tempest | last post by:
Hi all. This is a rather long posting but I have some questions concerning the usage of character entities in XML documents and PCI security compliance. The company I work for is using a...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.