473,725 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: C and NULL character

"mkeles84" <mk******@hotma il.comwrites:
I have a problem about NULL's.
for example, a variable is name :
--------------
char name[6]="MEHMET"; /* 6 characters */
if (strcmp(name,"M EHMET") == 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
talkaboutcomput ing.com.

--
Keith Thompson (The_Other_Keit h) 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 3592
On Mon, 21 Jul 2008 01:25:56 -0700, Keith Thompson
<ks***@mib.orgw rote:
>"mkeles84" <mk******@hotma il.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.orgw rote:
>>"mkeles84" <mk******@hotma il.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_Keit h) 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_Keit h) 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.orgw rote:
>cr*@tiac.net (Richard Harter) writes:
>On Mon, 21 Jul 2008 01:25:56 -0700, Keith Thompson
<ks***@mib.org wrote:
>>>"mkeles84" <mk******@hotma il.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_Keit h) 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
4684
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 table dummy(col1 blob); -- Next insert a null ascii character (0) insert into dummy values (char(0)); -- To verify there is actually something there type: select length(col1) from dummy;
29
3759
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 compiler dependent (and that NULL might be anything, but it is always NULL). The source snippet is below. The question is: - When I use calloc to allocate a block of memory, preinitialising it to zero, is this equivalent (and portable C) to...
15
2144
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 automatically to the end of the string. Let say char name="123456789" If entered the name in a loop;
2
9238
by: Ithaqua | last post by:
How do you create them? -- Cheers Ithaqua
12
1981
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, just like to find out the causes as much as possible so that I could impose stricter checking toward my codes. note: I could not use std::string cause it will require a total rewrite. thanks.
8
2299
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 0xb7d4c2f7 in vfprintf () from /lib/tls/i686/cmov/libc.so.6 #2 0xb7d6441b in vsprintf () from /lib/tls/i686/cmov/libc.so.6 #3 0x08049ba0 in character_data::printf (this=0x800, argument=0x0) at character.c:198
6
9419
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
1798
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 simply want user to input a string and a character and then to search if the string contains the character. well the problem is that I was unable to enter a null character in my input string while program is running. I know that a string in C is...
3
12591
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 problem to translate. How to translate string also allow null data to integer. If null data it will read as space. My Data :- GEOSEG_ID SEQNO
4
292
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 of why creating a NTCS is normally not very difficult. For instance, the second "MEHMET" in your program causes a piece of memory to be allocated for an array of 7 chars, with the contents {'M', 'E', 'H', 'M', 'E', 'T', '\0'}. "MEHMET" itself...
0
9246
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9162
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9091
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8069
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6694
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4773
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3211
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 we have to send another system
2
2619
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.