#include<stdio.h> int func(int *a,int *b,int num){ int temp; if(num==1){ if(*a>*b){ temp=*a; } else temp=*b; } else if(num==2){ int temp; if(*a<*b){ temp=*a; } else temp=*b; } else if(num==3) temp=*a+*b; } int main(){ int a,b,num; int *p,*q; p=&a; q=&b; scanf("%d %d",&a,&b); scanf("%d",num); int result=func(p,q,num); printf("%d",result); return 0; }
时间: 2024-04-08 19:35:25 浏览: 89
这段代码存在一些问题。首先,在`scanf("%d", num);`这行代码中,应该是`scanf("%d", &num);`,因为`num`是一个整型变量的地址。其次,在函数`func`中,当`num`的值为3时,没有返回值。你可以将其改为如下代码:
```c
int func(int *a, int *b, int num) {
int temp;
if (num == 1) {
if (*a > *b) {
temp = *a;
} else {
temp = *b;
}
} else if (num == 2) {
if (*a < *b) {
temp = *a;
} else {
temp = *b;
}
} else if (num == 3) {
temp = *a + *b;
}
return temp;
}
```
此外,你还需要在主函数中将`scanf("%d", num);`改为`scanf("%d", &num);`。这样就能正确地接收输入,并且将函数的返回值打印出来。
相关问题
使用C语言设计编写程序日志,实现时间戳、 日志级别、日志内容、 日志输出、 日志轮转功能,,日志文件创建生成在temp文件目录下,其中日志轮转功能具体指的是指生成的所有日志文件超出temp目录规定容量后,删除最早生成的一个日志文件,只给出完整具体的log.c,log.h和测试代码main
log.h:
```c
#ifndef _LOG_H
#define _LOG_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define DEFAULT_LOG_SIZE 1024 * 1024 * 10 // 默认日志文件大小为 10 MB
#define DEFAULT_LOG_NUM 10 // 默认日志文件数量为 10 个
#define LOG_LEVEL_DEBUG 0
#define LOG_LEVEL_INFO 1
#define LOG_LEVEL_WARN 2
#define LOG_DEBUG(fmt, args...) log_write(LOG_LEVEL_DEBUG, __FILE__, __FUNCTION__, __LINE__, fmt, ##args)
#define LOG_INFO(fmt, args...) log_write(LOG_LEVEL_INFO, __FILE__, __FUNCTION__, __LINE__, fmt, ##args)
#define LOG_WARN(fmt, args...) log_write(LOG_LEVEL_WARN, __FILE__, __FUNCTION__, __LINE__, fmt, ##args)
int log_init(const char* log_dir, const char* log_filename, int log_size, int log_num);
int log_write(int level, const char* file, const char* func, int line, const char* fmt, ...);
void log_destroy();
#endif
```
log.c:
```c
#include "log.h"
static char g_log_dir[256] = "./"; // 日志目录
static char g_log_filename[256] = "log"; // 日志文件名
static int g_log_size = DEFAULT_LOG_SIZE; // 日志文件大小限制
static int g_log_num = DEFAULT_LOG_NUM; // 日志文件数量限制
static int g_log_fd = -1; // 当前日志文件描述符
static int g_log_size_cur = 0; // 当前日志文件大小
static int g_log_num_cur = 0; // 当前日志文件序号
static void log_rotate();
int log_init(const char* log_dir, const char* log_filename, int log_size, int log_num) {
if (log_dir != NULL && strlen(log_dir) > 0) {
strcpy(g_log_dir, log_dir);
}
if (log_filename != NULL && strlen(log_filename) > 0) {
strcpy(g_log_filename, log_filename);
}
if (log_size > 0) {
g_log_size = log_size;
}
if (log_num > 0) {
g_log_num = log_num;
}
char log_path[512] = {0};
sprintf(log_path, "%s%s.log.%d", g_log_dir, g_log_filename, g_log_num_cur);
g_log_fd = open(log_path, O_CREAT | O_WRONLY | O_APPEND, 0644);
if (g_log_fd == -1) {
printf("log_init: open log file failed\n");
return -1;
}
g_log_size_cur = lseek(g_log_fd, 0, SEEK_END);
if (g_log_size_cur == -1) {
printf("log_init: lseek log file failed\n");
return -1;
}
return 0;
}
int log_write(int level, const char* file, const char* func, int line, const char* fmt, ...) {
if (g_log_fd == -1) {
printf("log_write: log is not initialized\n");
return -1;
}
char log_buf[2048] = {0};
time_t now = time(NULL);
struct tm* tm_now = localtime(&now);
sprintf(log_buf, "[%04d-%02d-%02d %02d:%02d:%02d] ",
tm_now->tm_year + 1900, tm_now->tm_mon + 1, tm_now->tm_mday,
tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
switch (level) {
case LOG_LEVEL_DEBUG:
strcat(log_buf, "[DEBUG] ");
break;
case LOG_LEVEL_INFO:
strcat(log_buf, "[INFO] ");
break;
case LOG_LEVEL_WARN:
strcat(log_buf, "[WARN] ");
break;
default:
strcat(log_buf, "[UNKNOWN] ");
break;
}
sprintf(log_buf + strlen(log_buf), "[%s:%d:%s] ", file, line, func);
va_list ap;
va_start(ap, fmt);
vsprintf(log_buf + strlen(log_buf), fmt, ap);
va_end(ap);
strcat(log_buf, "\n");
int len = strlen(log_buf);
if (g_log_size_cur + len > g_log_size) {
log_rotate();
char log_path[512] = {0};
sprintf(log_path, "%s%s.log.%d", g_log_dir, g_log_filename, g_log_num_cur);
g_log_fd = open(log_path, O_CREAT | O_WRONLY | O_APPEND, 0644);
if (g_log_fd == -1) {
printf("log_write: open log file failed\n");
return -1;
}
g_log_size_cur = lseek(g_log_fd, 0, SEEK_END);
if (g_log_size_cur == -1) {
printf("log_write: lseek log file failed\n");
return -1;
}
}
int ret = write(g_log_fd, log_buf, len);
if (ret == -1) {
printf("log_write: write log failed\n");
return -1;
}
g_log_size_cur += len;
return 0;
}
void log_destroy() {
if (g_log_fd != -1) {
close(g_log_fd);
g_log_fd = -1;
}
}
static void log_rotate() {
if (g_log_fd != -1) {
close(g_log_fd);
g_log_fd = -1;
}
char log_path[512] = {0};
sprintf(log_path, "%s%s.log.%d", g_log_dir, g_log_filename, g_log_num_cur);
unlink(log_path);
g_log_num_cur = (g_log_num_cur + 1) % g_log_num;
}
```
main.c:
```c
#include "log.h"
int main(int argc, char** argv) {
log_init("./temp/", "test", 1024 * 1024 * 5, 3); // 初始化日志,日志文件大小为 5 MB,日志文件数量为 3 个
LOG_DEBUG("test debug log");
LOG_INFO("test info log");
LOG_WARN("test warn log");
log_destroy(); // 销毁日志
return 0;
}
```
在 Linux 环境下编译运行即可。
阅读全文