regarding string length

Helo,
I have character array of sixe 128

char filename[128]

now I have one problem that when I enter filename as nothing I got value
as " " ",`\0` " .

when I find this string length ( " ",`\0`) as 1(one).
actually I want to make this length as zero.

so what should I do

I m doing programming in c using linux.

I have one character array filename of 126 size.

now during developping my module I have situation where
I got filename = " ",`\0`

Now I want to check for this condtion

I have try following option but it didnot work

(1)
int len;
if (filename == " ",`\0` )
{
len =0;
}

(2) int len;

if ( strcmp(filename, " " ",`\0`") )
{
len = 0;
}

both method are not working

can you tell me how do I check filename = " ",`\0' condition

Regards,
Amit

You generally want,

if (filename[0] == '\0') {
}

But it's clear you are confused about C strings in general. This tutorial seems appropriate enough: C strings

helo I try if(filename[0] == '\0') {
}

but it was not working

actuall i got filename= " ",`\0`

so how do I do

I also try f (filename[0] == " ",'\0') {
}

but that also not working

Regards,
Amit

Oh, you mean there's a SPACE as the entire filename? Okay, then you can do:

/* version 1 */
if (filename[0] == ' ' && filename[1] == '\0') { ... do stuff here } 

/* version 2 */
if (strcmp(filename," ")==0) { ... do stuff here ... }

filename[0] is the first character of the string. If the string is empty, the first character will be the terminator null byte.

C string-handling functions generally cannot read past a null byte. Your notation " ", `\0` is extremely non-standard, and seems to correspond to a string consisting of a space, followed by a null byte (which is usually not explicitly marked or required; it's the end of string, plain and simple). That string obviously has a length of one.

The C equality operator == does not compare strings in any meaningful way; you need to use a function such as strcmp. (I too thought this was shocking when I was learning C.) You can compare individual single characters (denoted by single quotes) one by one, which is what strcmp basically does in a loop over the strings it compares. Double quotes are used around strings (character arrays).

The C string tutorial which was already pointed out to you once probably explains all of this a lot better. Please read it.

Actually, his syntax with the comma serves to nullify the expression. Remember that a comma has lower precedence than just about anything else in C, and it creates an independent expression. C will use only the right-most part as the value for the entire expression. So

if (0==0, 0) { printf("false\n"); } 
if (1==0, 1) { printf("true\n"); } 

parses and will always print "true".

Helo thx for replying me.
I completely agree with you but problem is that
one of my function in file is appening " " for some other functionality
so that If enter filename as blank it will give me filename
as filename = " ",`\0`

now tell me how I wan to check

if(filename = " ",`\0`)
then len = 0

how do I implement this

Regards,
Amit

It's still not clear what the problem is.

Is the file name the empty string, or a string consisting of one space, or a string containing double quote, space, double quote, comma, backtick, backslash, zero, backtick?

All of these can be detected by strcmp

if ((stcmp(filename,""))==0) {
  printf ("filename empty\n");
} else if ((strcmp(filename," "))==0) {
  printf ("filename is a single space\n");
} else if ((stcmp(filename,"\" \",`\\0`"))==0) {
  printf ("filename is literally \" \",`\\0`\n");
}

(Or is there even another space before the first double quote?)

It's just a wild guess, but strcmp() returns 0 when there's a match and Amit might think the usage is as he originally posted.

I think a sample of the filename would clear the air.