C语言多参数函数的疑惑
来源:百度知道 编辑:UC知道 时间:2024/06/03 12:25:33
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
void send_msg(int sock_fd,...)
{
va_list argp;
char *para=NULL;
va_start(argp,sock_fd);
while(1)
{
para = va_arg(argp,char *);
if( strcmp( para,"\0") == 0 )
break;
puts(para);
}
va_end(argp);
}
int main()
{
send_msg(0,"hello","world!","\0"); ///把字符串之间的逗号去掉,结果是一样的,这是为什么???????????
return 0;
}
弄错了,只是把"hello","world!"之间的逗号去掉不影响结果
#include <stdarg.h>
#include <string.h>
void send_msg(int sock_fd,...)
{
va_list argp;
char *para=NULL;
va_start(argp,sock_fd);
while(1)
{
para = va_arg(argp,char *);
if( strcmp( para,"\0") == 0 )
break;
puts(para);
}
va_end(argp);
}
int main()
{
send_msg(0,"hello","world!","\0"); ///把字符串之间的逗号去掉,结果是一样的,这是为什么???????????
return 0;
}
弄错了,只是把"hello","world!"之间的逗号去掉不影响结果
C里面字符串之间有自动续行功能,比如 "hello" "world" 会被自动续行为"helloworld",
如:
printf("hello" "world"); /*输出为 helloworld
这与C#等语言不一样,在那些语言中两个字符串这必须通过+来连接
因此,当你加逗号时,函数把所有的字符串一个一个地传入,,,当你不加逗号时,则字符串被自动续行,结果是传入了一个整体的字符串,最终结果都一样^^
不一样啊,去掉逗号就没有分行了啊,是helloworld