这是我试图理解的代码:
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
int main(void){
unsigned long word;
ssize_t nr;
int file = open("koray.txt",O_RDONLY);
nr = read(file,&word,sizeof(unsigned long));
printf("%li\n",word);
}
koray.txt
只有 1 个字符是 k
。
当我运行程序时,我看到:
koray@koray-VirtualBox:~$ ./a.out
4195435
这个大值是多少?
最佳答案
word
变量中会有随机垃圾,因为您从未对其进行初始化。然后 read
将只能从文件中获取一个字节(nr
可能返回 1,你应该检查一下!)这节省了一个字节,但是 word
变量中仍有 3-7 个字节的未初始化垃圾被打印出来。
关于c - 这个 C 编程读物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30415461/