Since C-style strings are always terminated with the null character (\0), you can check whether the string is empty by writing
do {
...
} while (url[0] != '\0');
Alternatively, you could use the strcmp function, which is overkill but might be easier to read:
do {
...
} while (strcmp(url, ""));
Note that strcmp returns a nonzero value if the strings are different and 0 if they're the same, so this loop continues to loop until the string is empty.
Hope this helps!
Answer from templatetypedef on Stack OverflowSince C-style strings are always terminated with the null character (\0), you can check whether the string is empty by writing
do {
...
} while (url[0] != '\0');
Alternatively, you could use the strcmp function, which is overkill but might be easier to read:
do {
...
} while (strcmp(url, ""));
Note that strcmp returns a nonzero value if the strings are different and 0 if they're the same, so this loop continues to loop until the string is empty.
Hope this helps!
If you want to check if a string is empty:
if (str[0] == '\0')
{
// your code here
}
Videos
In addition to Will Dean's version, the following are common for whole buffer initialization:
Copychar s[10] = {'\0'};
or
Copychar s[10];
memset(s, '\0', sizeof(s));
or
Copychar s[10];
strncpy(s, "", sizeof(s));
You want to set the first character of the string to zero, like this:
Copychar myString[10];
myString[0] = '\0';
(Or myString[0] = 0;)
Or, actually, on initialisation, you can do:
Copychar myString[10] = "";
But that's not a general way to set a string to zero length once it's been defined.
The easy way to do it would be like this:
if (msg[0])
printf("message: %s", msg);
If, at some later date, msg is a pointer, you would first want to assure it's not a NULL pointer.
if (msg && msg[0])
printf("message: %s", msg);
A test program to demonstrate:
#include <stdio.h>
#include <stdlib.h>
char *msg;
void test() {
if (msg && msg[0])
printf("string is %s\n", msg);
}
int main()
{
msg = NULL;
test();
msg = calloc(10, 1);
test();
msg[0] = 'm';
msg[1] = 'e';
test();
free(msg);
}
On my machine the output is:
string is me
Strings in C are represented as character arrays, terminated by a character whose value is zero (often written as '\0'). The length of a string is the number of characters that come before the zero.
So, a string is empty if the first character in the array is zero, since then by definition no characters came before the zero.
This can be checked like so:
if(msg[0] != '\0')
{
/* string isn't empty! */
}
This is very explicit code, the shorter if(msg[0]) or if(*msg) has the exact same semantics but can be slightly harder to read. It's mostly a matter of personal style.
Note though that in your case, when you use scanf() to read into msg, you should check the return value from scanf() before checking the contents of msg. It's perfectly possible for scanf() to fail, and then you can't rely on msg being set to an empty string.
For some reason, the name variable becomes empty even though I gave it an input.
Code:
#include <stdio.h>
int main(){
char name[16];
short unsigned int age;
printf("What is your name? ");
scanf("%s", &name);
printf("How old are you? ");
scanf("%u", &age);
printf("Hello, %s.\n", name);
printf("You are %u years old\n", age);
return 0;
}Terminal:
What is your name? Momus How old are you? 99 Hello, . You are 99 years old
I seems that the value for name was changed in the part somewhere in the part that prints "How old are you? " and the scanf() for the value of age because it works when I do this.
Code:
#include <stdio.h>
int main(){
char name[25];
short unsigned int age;
printf("What is your name? ");
scanf("%s", &name);
printf("Hello, %s.\n", name);
printf("How old are you? ");
scanf("%u", &age);
printf("You are %u years old\n", age);
return 0;
}Terminal:
What is your name? Momus Hello, Momus. How old are you? 99 You are 99 years old
Does anyone know what happened? How do I make it so that the first one will show the input? Thanks!
It depends on what you mean by "empty". If you just want a zero-length string, then your example will work.
This will also work:
buffer[0] = '\0';
If you want to zero the entire contents of the string, you can do it this way:
memset(buffer,0,strlen(buffer));
but this will only work for zeroing up to the first NULL character.
If the string is a static array, you can use:
memset(buffer,0,sizeof(buffer));
Two other ways are strcpy(str, ""); and string[0] = 0
To really delete the Variable contents (in case you have dirty code which is not working properly with the snippets above :P ) use a loop like in the example below.
#include <string.h>
...
int i=0;
for(i=0;i<strlen(string);i++)
{
string[i] = 0;
}
In case you want to clear a dynamic allocated array of chars from the beginning, you may either use a combination of malloc() and memset() or - and this is way faster - calloc() which does the same thing as malloc but initializing the whole array with Null.
At last i want you to have your runtime in mind. All the way more, if you're handling huge arrays (6 digits and above) you should try to set the first value to Null instead of running memset() through the whole String.
It may look dirtier at first, but is way faster. You just need to pay more attention on your code ;)
I hope this was useful for anybody ;)