--
Message posted using http://www.talkaboutcomputing.com/group/comp.std.c/
More information at http://www.talkaboutcomputing.com/faq.html
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"
>char name[6]="MEHMET"; /* 6 characters */
>if (strcmp(name,"MEHMET") == 0){
>how can I compare it. I don't want to use NULL characker after variable.
If you don't use null-terminated strings you can't use strcmp().
You can use strncmp(), but it won't look at any characters after it
reaches the null at the end of the literal string.
-- Richard
--
Please remember to mention me / in tapes you leave behind.
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 is replaced in
your code with the address of the first character of that array. As a
result, it is automatically null-terminated, without you having to say
so.
The same thing did NOT happen with the initialization of the 'name'
array, because you explicitly declared it as having a length of 6. As
a result, the definition is equivalent to:
char name[6] = {'M', 'E', 'H', 'M', 'E', 'T'};
Note that there is no terminating null character in that array. If,
instead, you had either explicity made it big enough to hold the
terminating null character:
char name[7] = "MEHMET";
or, more simply, just left the array length unspecified:
char name[] = "MEHMET";
your definition would have been equivalent to:
char name[7] = {'M', 'E', 'H', 'M', 'E', 'T', '\0'};
Cross-posted to comp.lang.c, followups redirected there, since
comp.lang.c is more appropriate for questions about how to use C.
You mean NUL characters.
> 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.
Then use memcmp:
if (memcmp(name, "MEHMET", sizeof(name)) == 0) ...
-drt