473,412 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Re: C and NULL character

"mkeles84" <mk******@hotmail.comwrites:
I have a problem about NULL's.
for example, a variable is name :
--------------
char name[6]="MEHMET"; /* 6 characters */
if (strcmp(name,"MEHMET") == 0){
printf("true");
}else{
printf("false");
}
I think result must bt "true" but I saw "false" on screen.
how can I compare it. I don't want to use NULL characker after variable.
comp.std.c deals with the C standard -- the document, how it's
developed, and so forth. comp.lang.c deals with the C language, and
that's where your question belongs. I've cross-posted there and set
followups.

NULL (all-caps) is a macro that expands to a null pointer constant.
What you're asking about is the null character, '\0', sometimes
refered to as NUL (one L).

Your declaration
char name[6]="MEHMET";
doesn't create a string. It creates an array of 6 characters, *not*
terminated by a null character. By passing a pointer to this array to
strcmp(), you invoke undefined behavior; literally anything could
happen. What's most likely to happen is that strcmp will scan the
first 6 characters of the arrays pointed to by its two arguments;
finding them all to be equal, it will continue to the 7th character.
If the byte in memory following your variable ``name'' happens to be
'\0', strcmp() will return 0. If it happens not to be '\0', strcmp()
will return a non-zero value. If it's outside the range of memory
that your program is allowed to access, it could crash your program.
Message posted using http://www.talkaboutcomputing.com/group/comp.std.c/
More information at http://www.talkaboutcomputing.com/faq.html
You'll do better posting through a real Usenet server. From what I've
seen, even Google Groups is likely to be better than
talkaboutcomputing.com.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 21 '08 #1
7 3554
On Mon, 21 Jul 2008 01:25:56 -0700, Keith Thompson
<ks***@mib.orgwrote:
>"mkeles84" <mk******@hotmail.comwrites:
[snip]
>
Your declaration
char name[6]="MEHMET";
doesn't create a string. It creates an array of 6 characters, *not*
terminated by a null character.
[snip]

This is one of those irritating idiosyncracies of C. The rule is
that excess initializers are discarded. GCC, and I suppose most
compilers, will issue a warning if there are excess initializers
except in the one special case where the excess initializer is
the trailing \0. (There may be some combination of GCC flags
that will produce a warning; that is not to the point.)
Richard Harter, cr*@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
Jul 21 '08 #2
cr*@tiac.net (Richard Harter) writes:
On Mon, 21 Jul 2008 01:25:56 -0700, Keith Thompson
<ks***@mib.orgwrote:
>>"mkeles84" <mk******@hotmail.comwrites:
[snip]
>>
Your declaration
char name[6]="MEHMET";
doesn't create a string. It creates an array of 6 characters, *not*
terminated by a null character.
[snip]

This is one of those irritating idiosyncracies of C. The rule is
that excess initializers are discarded. GCC, and I suppose most
compilers, will issue a warning if there are excess initializers
except in the one special case where the excess initializer is
the trailing \0. (There may be some combination of GCC flags
that will produce a warning; that is not to the point.)
No, there's no such rule. In fact, C99 6.7.8p2 says:

No initializer shall attempt to provide a value for an object not
contained within the entity being initialized.

Excess initializers aren't discarded; they're a constraint violation.

There's only one special case for string literals, in C99 6.7.8p14:

An array of character type may be initialized by a character
string literal, optionally enclosed in braces. Successive
characters of the character string literal (including the
terminating null character if there is room or if the array is of
unknown size) initialize the elements of the array.

It's the "if there's room" clause that allows the null character to be
ignored, but only if the declared size of the array is exactly the
same as the length of the string excluding the trailing '\0'.

IMHO a better way to handle this would have been to drop the "if
there's room" wording, and perhaps add a special flavor of string
literal that doesn't include the trailing '\0', so that this:
char name[6] = "MEHMET";
would be a constraint violation (<OT>as it is in C++</OT>), and this:
char name[] = N"MEHMET";
would be equivalent to:
char name[6] = { 'M', 'E', 'H', 'M', 'E', 'T' };

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 21 '08 #3
Keith Thompson wrote:
There's only one special case for string literals, in C99 6.7.8p14:

An array of character type may be initialized by a character
string literal, optionally enclosed in braces. Successive
characters of the character string literal (including the
terminating null character if there is room or if the array is of
unknown size) initialize the elements of the array.

It's the "if there's room" clause that allows the null character to be
ignored, but only if the declared size of the array is exactly the
same as the length of the string excluding the trailing '\0'.

IMHO a better way to handle this would have been to drop the "if
there's room" wording, and perhaps add a special flavor of string
literal that doesn't include the trailing '\0', so that this:
char name[6] = "MEHMET";
would be a constraint violation (<OT>as it is in C++</OT>), and this:
char name[] = N"MEHMET";
would be equivalent to:
char name[6] = { 'M', 'E', 'H', 'M', 'E', 'T' };
I see no reason to provide a special syntax. If people want to use the
syntax for a string, they should be required to provide enough space to
store an entire string (including the terminator). If they want a mere
character array (without the terminator), let them use the syntax for a
normal array initializer.

IMHO, this is a flaw in the standard, but as we all know, ANSI was
chartered primarily to document what existed already, not create a
perfect language.

S
Jul 21 '08 #4
Stephen Sprunk <st*****@sprunk.orgwrites:
Keith Thompson wrote:
>There's only one special case for string literals, in C99 6.7.8p14:
An array of character type may be initialized by a character
string literal, optionally enclosed in braces. Successive
characters of the character string literal (including the
terminating null character if there is room or if the array is of
unknown size) initialize the elements of the array.
It's the "if there's room" clause that allows the null character to
be
ignored, but only if the declared size of the array is exactly the
same as the length of the string excluding the trailing '\0'.
IMHO a better way to handle this would have been to drop the "if
there's room" wording, and perhaps add a special flavor of string
literal that doesn't include the trailing '\0', so that this:
char name[6] = "MEHMET";
would be a constraint violation (<OT>as it is in C++</OT>), and this:
char name[] = N"MEHMET";
would be equivalent to:
char name[6] = { 'M', 'E', 'H', 'M', 'E', 'T' };

I see no reason to provide a special syntax. If people want to use
the syntax for a string, they should be required to provide enough
space to store an entire string (including the terminator). If they
want a mere character array (without the terminator), let them use the
syntax for a normal array initializer.
It would be useful for the rare cases where you actually want a
sequence of characters that's not terminated by '\0'. In particular,
it might make it marginally easier to work with alternative
representations for strings (here I'm using "strings" in a generic
sense, not the C sense).

Using a normal array initializer is tedious; a new string literal
syntax would make the code more readable, and the leading 'N' (or
whatever) would stand out enough to make the meaning obvious.

But I'm not at all convinced that the need is great enough to justify
the change to the language (thus the word "perhaps" above).
IMHO, this is a flaw in the standard, but as we all know, ANSI was
chartered primarily to document what existed already, not create a
perfect language.
Agreed.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 21 '08 #5
On Mon, 21 Jul 2008 09:30:11 -0700, Keith Thompson
<ks***@mib.orgwrote:
>cr*@tiac.net (Richard Harter) writes:
>On Mon, 21 Jul 2008 01:25:56 -0700, Keith Thompson
<ks***@mib.orgwrote:
>>>"mkeles84" <mk******@hotmail.comwrites:
[snip]
>>>
Your declaration
char name[6]="MEHMET";
doesn't create a string. It creates an array of 6 characters, *not*
terminated by a null character.
[snip]

This is one of those irritating idiosyncracies of C. The rule is
that excess initializers are discarded. GCC, and I suppose most
compilers, will issue a warning if there are excess initializers
except in the one special case where the excess initializer is
the trailing \0. (There may be some combination of GCC flags
that will produce a warning; that is not to the point.)

No, there's no such rule. In fact, C99 6.7.8p2 says:

No initializer shall attempt to provide a value for an object not
contained within the entity being initialized.

Excess initializers aren't discarded; they're a constraint violation.

There's only one special case for string literals, in C99 6.7.8p14:

An array of character type may be initialized by a character
string literal, optionally enclosed in braces. Successive
characters of the character string literal (including the
terminating null character if there is room or if the array is of
unknown size) initialize the elements of the array.

My bad, I misread the section in question.

G99? Isn't that a fantasy?
>It's the "if there's room" clause that allows the null character to be
ignored, but only if the declared size of the array is exactly the
same as the length of the string excluding the trailing '\0'.

IMHO a better way to handle this would have been to drop the "if
there's room" wording, and perhaps add a special flavor of string
literal that doesn't include the trailing '\0', so that this:
char name[6] = "MEHMET";
would be a constraint violation (<OT>as it is in C++</OT>), and this:
char name[] = N"MEHMET";
would be equivalent to:
char name[6] = { 'M', 'E', 'H', 'M', 'E', 'T' };
The cure is worse than the disease.
Richard Harter, cr*@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
Jul 22 '08 #6
Stephen Sprunk wrote:
Keith Thompson wrote:
...IMHO a better way to handle this would have been to drop
the "if there's room" wording, and perhaps add a special
flavor of string literal that doesn't include the trailing '\0',
so that this:
char name[6] = "MEHMET";
would be a constraint violation (<OT>as it is in C++</OT>), and this:
char name[] = N"MEHMET";
would be equivalent to:
char name[6] = { 'M', 'E', 'H', 'M', 'E', 'T' };

I see no reason to provide a special syntax.
Neither do I.
If people want to use the syntax for a string, they should be required
to provide enough space to store an entire string (including the
terminator). If they want a mere character array (without the
terminator), let them use the syntax for a normal array initializer.
IMHO, this is a flaw in the standard, but as we all know, ANSI was
chartered primarily to document what existed already, not create
a perfect language.
Fixed width fields still exist; many databases are still filled with
CHAR(N) columns.

You may not appreciate the rule, but that's no reason to deprive those
that do, even if C++ has already done so.

--
Peter
Jul 22 '08 #7
Peter Nilsson <ai***@acay.com.auwrites:
Stephen Sprunk wrote:
>Keith Thompson wrote:
...IMHO a better way to handle this would have been to drop
the "if there's room" wording, and perhaps add a special
flavor of string literal that doesn't include the trailing '\0',
so that this:
char name[6] = "MEHMET";
would be a constraint violation (<OT>as it is in C++</OT>), and this:
char name[] = N"MEHMET";
would be equivalent to:
char name[6] = { 'M', 'E', 'H', 'M', 'E', 'T' };

I see no reason to provide a special syntax.

Neither do I.
>If people want to use the syntax for a string, they should be required
to provide enough space to store an entire string (including the
terminator). If they want a mere character array (without the
terminator), let them use the syntax for a normal array initializer.
IMHO, this is a flaw in the standard, but as we all know, ANSI was
chartered primarily to document what existed already, not create
a perfect language.

Fixed width fields still exist; many databases are still filled with
CHAR(N) columns.

You may not appreciate the rule, but that's no reason to deprive those
that do, even if C++ has already done so.
Ok, so my idea for a special literal syntax for non-null-terminated
character arrays doesn't seem to be very popular.

I'll just point out a problem with the current special-case rule. If
I want to store "hello, world" with no '\0' in an array, I have two
choices. I can either write out a verbose array initializer for
something that consists entirely of printable characters:

char message[] = { 'h', 'e', 'l', 'l', 'o', ',', ' ',
'w', 'o', 'r', 'l', 'd' };

or I can declare message with an explicit length:

char message[12] = "hello world";

The problem with the latter is that I have to *count characters*,
something that the compiler could and should have done for me.

And when I want to change the string, I'm likely to forget to update
the length. Did you notice that I deleted the comma but left the
length at 12, so message will have the trailing '\0' after all, with
no warning from the compiler?

Even Fortran replaced Hollerith constants.

There's no *need* for a new syntax, and I'm not even suggesting that
it would be worthwhile to add one, but there would be *some* benefit.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 22 '08 #8

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

Similar topics

1
by: Greg.Harabedian | last post by:
I'll start off by saying I am using MySQL v4.0 and my question is...how do I get mysqldump to dump the actual binary values store in a blob? Here is an example: -- Create a test table create...
29
by: Jason Curl | last post by:
I've been reading this newsgroup for some time and now I am thoroughly confused over what NULL means. I've read a NULL pointer is zero (or zero typecast as a void pointer), others say it's...
15
by: ehabaziz2001 | last post by:
Hi, Till now I do not understand how the null character automatically added to the end of the string and it is not an element of the string array . All books said the null character (\0) added...
2
by: Ithaqua | last post by:
How do you create them? -- Cheers Ithaqua
12
by: semut | last post by:
Given that the string is of null terminated type. What could be the possible causes (by experience) the string to have no null terminated and cause buffer overflow later. I know it is quite broad,...
8
by: A. Anderson | last post by:
Howdy everyone, I'm experiencing a problem with a program that I'm developing. Take a look at this stack report from GDB - #0 0xb7d782a3 in strlen () from /lib/tls/i686/cmov/libc.so.6 #1 ...
6
by: linq936 | last post by:
Hi, I have the following code: #include <stdio.h> int main(void){ char* str1 = "abc"; char* str2 = '\0'; if ( strstr(str1, str2) == NULL ){ printf("yes\n");
2
AmberJain
by: AmberJain | last post by:
Hello everyone, Right now I am learning C and I'm using a BORLAND TURBO C++ 3.0 compiler to compile my programs. Well I am facing a problem with one of my program which I got as my assignment. I...
3
by: amija0311 | last post by:
Hi, I am new using DB2 9.1 database by windows base. I want to query the data that contain string then translate the string into integer using DB2. The problems is If the data is null, i got the...
4
by: jameskuyper | last post by:
mkeles84 wrote: The other responses you've received have explained why 'name' needs to contain a Null-Terminated Character String (NTCS). However, it occurred to me that you might not be aware...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.