I have a void function which take the string from a global variable and prints them.
我有一个void函数,它从全局变量中取出字符串并打印出来。
The problem is that the void function prints a message/does stuff even if the string is empy. so if the global variable is empt it just prints "message is:" i only want to do something if the string has characters.
问题是,void函数打印消息/做事情,即使字符串是空的。因此,如果全局变量是空的,它只打印“message is:”如果字符串有字符,我只想做一些事情。
what i want to do is to check if the string is not empty, then print a message.
我要做的是检查字符串是否为空,然后打印一条消息。
// global variable
char msg[30];
scanf("%s", &msg);
void timer(void) {
//i want to only print if there is a message
printf("message: %s", msg);
}
thanks for any help
感谢任何帮助
5 个解决方案
#1
13
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.
如果在以后的某个时候,msg是一个指针,那么您首先要确保它不是一个空指针。
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
字符串是我
#2
7
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.
C中的字符串表示为字符数组,终止符的值为0(通常写成'\0')。字符串的长度是在0之前的字符数。
So, a string is empty if the first character in the array is zero, since then by definition no characters came before the zero.
因此,如果数组中的第一个字符是0,那么字符串就是空的,因为根据定义,在0之前没有任何字符。
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.
这是非常明确的代码,较短的if(msg[0])或if(*msg)具有完全相同的语义,但读起来可能会稍微困难一些。这主要是个人风格的问题。
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.
请注意,在您的例子中,当您使用scanf()读取msg时,您应该在检查msg的内容之前检查scanf()的返回值。scanf()完全有可能失败,然后您不能指望msg被设置为空字符串。
#3
3
This should work for you:
这应该对你有用:
The Message will only be printed if it's longer then 0.
消息只有在比0长时才会被打印出来。
#include <stdio.h>
#include <string.h>
void timer() {
char msg[30];
scanf(" %s", msg);
if(strlen(msg) != 0)
printf("message: %s", msg);
}
int main() {
timer();
return 0;
}
#4
0
Here are all the ways I found (and can think of) to check if a string is empty. Sorted by efficiency.
下面是我找到(并能想到的)检查字符串是否为空的所有方法。按效率。
msg[0] == '\0'
- 味精[0]= = ' \ 0 '
strlen(msg) == 0
- strlen(味精)= = 0
!strcmp(msg, "")
- ! strcmp(味精”、“)
And this, which should only happen if the user somehow avoided input (e.g. with Ctrl+Z) or there was an error
只有当用户以某种方式避免输入(例如使用Ctrl+Z)或出现错误时才会发生这种情况
scanf("%s", &msg) <= 0
- scanf(“% s”,味精)< = 0
#5
-1
Ok,you can try this:
好吧,你可以试试这个:
char msg[30];
scanf("%s",&msg);
void timer(void)
{
//i want to only print if there is a message
if(!msg.length == 0 )
printf("message: %s", msg);
}
`
”
#1
13
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.
如果在以后的某个时候,msg是一个指针,那么您首先要确保它不是一个空指针。
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
字符串是我
#2
7
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.
C中的字符串表示为字符数组,终止符的值为0(通常写成'\0')。字符串的长度是在0之前的字符数。
So, a string is empty if the first character in the array is zero, since then by definition no characters came before the zero.
因此,如果数组中的第一个字符是0,那么字符串就是空的,因为根据定义,在0之前没有任何字符。
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.
这是非常明确的代码,较短的if(msg[0])或if(*msg)具有完全相同的语义,但读起来可能会稍微困难一些。这主要是个人风格的问题。
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.
请注意,在您的例子中,当您使用scanf()读取msg时,您应该在检查msg的内容之前检查scanf()的返回值。scanf()完全有可能失败,然后您不能指望msg被设置为空字符串。
#3
3
This should work for you:
这应该对你有用:
The Message will only be printed if it's longer then 0.
消息只有在比0长时才会被打印出来。
#include <stdio.h>
#include <string.h>
void timer() {
char msg[30];
scanf(" %s", msg);
if(strlen(msg) != 0)
printf("message: %s", msg);
}
int main() {
timer();
return 0;
}
#4
0
Here are all the ways I found (and can think of) to check if a string is empty. Sorted by efficiency.
下面是我找到(并能想到的)检查字符串是否为空的所有方法。按效率。
msg[0] == '\0'
- 味精[0]= = ' \ 0 '
strlen(msg) == 0
- strlen(味精)= = 0
!strcmp(msg, "")
- ! strcmp(味精”、“)
And this, which should only happen if the user somehow avoided input (e.g. with Ctrl+Z) or there was an error
只有当用户以某种方式避免输入(例如使用Ctrl+Z)或出现错误时才会发生这种情况
scanf("%s", &msg) <= 0
- scanf(“% s”,味精)< = 0
#5
-1
Ok,you can try this:
好吧,你可以试试这个:
char msg[30];
scanf("%s",&msg);
void timer(void)
{
//i want to only print if there is a message
if(!msg.length == 0 )
printf("message: %s", msg);
}
`
”