Tag: return value

在C中返回本地静态

在C语言中, static变量的范围通过文件。 在以下代码中,函数返回静态变量。 int fun(){ static int i = 10; return i; } int main() { printf(“%d\n”, fun()); return 0; } 并打印输出10。 那么,在C未定义的行为中返回本地静态还是定义明确?

检查返回值fread和fwrite

#include #include int main(int argc, char *argv[]){ if(argc != 3){ printf(“Usage: ./copy filename newfile\n”); exit(1); } int bytes; long file_size, file_copied_size; FILE *file_to_copy, *new_file; if((file_to_copy = fopen(argv[1], “rb”)) == NULL){ printf(“File cannot be opened – read\n”); exit(1); } if((new_file = fopen(argv[2], “wb”)) == NULL){ printf(“File cannot be opened – write\n”); exit(1); } fseek(file_to_copy, 0, SEEK_END); […]

为什么要回到负面的错误? (例如,返回-EIO)

另一个简单例子: if (wpa_s->mlme.ssid_len == 0) return -EINVAL; 为什么一元减? 对于在成功时返回> 0且在失败时返回<(=)0的函数(通常)是否这样做了,还是有其他原因?

打印返回结构的成员

我在打印从函数返回的结构的成员时遇到问题: #include struct hex_string { char a[9]; }; struct hex_string to_hex_string_(unsigned x) { static const char hex_digits[] = “0123456789ABCDEF”; struct hex_string result; char * p = result.a; int i; for (i = 28; i >= 0; i -= 4) { *p++ = hex_digits[(x >> i) & 15]; } *p = 0; printf(“%s\n”, result.a); /* works […]