|
|
这是一个十分基础的问题,在写代码的过程中,几乎大部分程序都要返回值,那么这俩个语句有什么区别呢? return 0;和exit(0); 谢谢了!
我的程序里出现这样的问题,下面是我的代码:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <string.h>
#define PORTNUM 13000
#define HOSTLEN 256
#define oops(msg) {perror(msg);return 0;}
int main(int ac,char *av[])
{
struct sockaddr_in saddr;
struct hostent *hp;
char hostname[HOSTLEN];
int sock_id,sock_fd;
FILE *sock_fp;
char *ctime();
time_t thetime;
sock_id = socket(PF_INET,SOCK_STREAM,0);
if(sock_id == -1)
perror("socket");
bzero((void *)&saddr,sizeof(saddr));
gethostname(hostname,HOSTLEN);
hp = gethostbyname(hostname);
bcopy((void *)hp->h_addr,(void *)&saddr.sin_addr,hp->h_length);
saddr.sin_port = htons(PORTNUM);
saddr.sin_family = AF_INET;
if(bind(sock_id,(struct sockaddr *)&saddr,sizeof(saddr)) != 0)
oops("bind");
if(listen(sock_id,1) != 0)
oops("listen");
while(1){
sock_fd = accept(sock_id,NULL,NULL);
printf("Wow!got a call\n");
if(sock_fd == -1)
oops("accept");
sock_fp = fdopen(sock_fd,"w");
if(sock_fp == NULL)
oops("fdopen");
thetime = time(NULL);
fprintf(sock_fp,"The time here is ..");
fprintf(sock_fp,"%s",ctime(&thetime));
fclose(sock_fp);
}
}
在#define oops(msg) {perror(msg);return 0;}这句中如果我用exit就报不匹配的错误。请诸位解释一下。 |
|