当前位置: 技术问答>linux和unix
小小程序的小小问题 !
来源: 互联网 发布时间:2015-07-05
本文导语: 程序如下: #include #include #include #include #ifdef MAXHOSTNAMELEN #define MAXHOSTNAMELEN 256 #endif main() { char hostname[MAXHOSTNAMELEN]; long hostid; size_t size; size = MAXHOSTNAMELEN; if(gethostname(hostname,size)!=0) { printf("Co...
程序如下:
#include
#include
#include
#include
#ifdef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 256
#endif
main()
{
char hostname[MAXHOSTNAMELEN];
long hostid;
size_t size;
size = MAXHOSTNAMELEN;
if(gethostname(hostname,size)!=0) {
printf("Could not get host namen");
exit(1);
}
hostid=gethostid();
printf("hostname = %s hostid = %d n",hostname,hostid);
}
这个程序功能是获取主机信息,调用函数gethostname gethostid的
函数原型:
#include
int gethostname(char * name,size_t size);
int sethostname(const char * name , size_t length );
我在GCC下调式出现这个提示
[root@localhost root]# ./gethostnameid.c
./gethostnameid.c:
line 15: syntax error near unexpected token `hostname,size'
./gethostnameid.c: line 15: ` if(gethostname(hostname,size)!=0) {'
[root@localhost root]#
他说我语法错误? 为什么不能执行呢? !
请教大家!
#include
#include
#include
#include
#ifdef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 256
#endif
main()
{
char hostname[MAXHOSTNAMELEN];
long hostid;
size_t size;
size = MAXHOSTNAMELEN;
if(gethostname(hostname,size)!=0) {
printf("Could not get host namen");
exit(1);
}
hostid=gethostid();
printf("hostname = %s hostid = %d n",hostname,hostid);
}
这个程序功能是获取主机信息,调用函数gethostname gethostid的
函数原型:
#include
int gethostname(char * name,size_t size);
int sethostname(const char * name , size_t length );
我在GCC下调式出现这个提示
[root@localhost root]# ./gethostnameid.c
./gethostnameid.c:
line 15: syntax error near unexpected token `hostname,size'
./gethostnameid.c: line 15: ` if(gethostname(hostname,size)!=0) {'
[root@localhost root]#
他说我语法错误? 为什么不能执行呢? !
请教大家!
|
编译问题
[root@localhost root]# gcc gethostnameid.c -o gethostnameid
[root@localhost root]# ./gethostnameid
hostname = localhost.localdomain
hostid = 8323328
[root@localhost root]#
|
Funny isn't it!!!!!
[root@localhost root]# ./gethostnameid.c ////what do you want to do ??
////and funny that it can be run(with x
//// attibute
you need to gcc gethostnameid.c first
[root@localhost root]# ./gethostnameid.c ////what do you want to do ??
////and funny that it can be run(with x
//// attibute
you need to gcc gethostnameid.c first
|
我用cc编译没有报错,代码如下:
void test()/**/
{
char hostname[MAXHOSTNAMELEN];
size_t size;
long hostid;
size=MAXHOSTNAMELEN;
if(gethostname(hostname,size)!=0)
{
printf("Could not get host namen");
exit(1);
}
hostid=gethostid();
printf("hostname = %s hostid = %d n",hostname,hostid);
}
void test()/**/
{
char hostname[MAXHOSTNAMELEN];
size_t size;
long hostid;
size=MAXHOSTNAMELEN;
if(gethostname(hostname,size)!=0)
{
printf("Could not get host namen");
exit(1);
}
hostid=gethostid();
printf("hostname = %s hostid = %d n",hostname,hostid);
}
|
应该是ifndef max