What is the output of this program? [code] #include<stdio.h> #include<stdlib.h> #include<fcntl.h> int main() { int

What is the output of this program?
Code:
   #include<stdio.h>
   #include<stdlib.h>
   #include<fcntl.h>
   int main()
   {
       int fd, new_fd;
       char *buff;
       buff = (char *)malloc(sizeof(char)*8);
       fd = open("san.c",O_RDONLY);
       new_fd = dup(fd);
       close(fd);
       read(new_fd,buff,8);
       printf("%s\n",buff);
   }
A. this program will not print anything
B. this program will print “#include”
C. this program will give the segmentation fault
D. this program will give the syntax error
 
Answer: b
Explanation: The “dup” system creates the a copy of the file descriptor.
Output:
[root@localhost 123hoidap]# gcc -o san san.c
[root@localhost 123hoidap]# ./san
#include
[root@localhost 123hoidap]#
 

Tìm kiếm

Back
Top