奋斗中的鱼 发表于 2023-9-28 19:01:12

语句作用问题

system(“pause “)
这行语句有什么作用?{:9_241:}

Mike_python小 发表于 2023-9-28 19:01:37

system("pause")语句的作用是在程序执行完毕后暂停命令行窗口,等待用户按下任意键继续。这在调试和开发过程中非常有用,可以防止程序运行结束后窗口立即关闭,以便查看程序输出或调试信息。

具体解释如下:
- system(...)函数是C/C++标准库中的一个函数,用于执行系统命令。
- "pause"是一个系统命令,表示暂停命令行窗口的执行,等待用户按下任意键。
- system("pause")会调用系统命令,执行暂停操作。

然而,请注意,在实际的生产环境中,不建议使用system("pause")这种方式来暂停程序。因为它依赖于特定的操作系统命令,使得代码的可移植性变差。更好的做法是使用跨平台的方式来实现类似的功能,比如使用getchar()函数来等待用户输入。

希望这个解释对你有帮助!如果还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:}

奋斗中的鱼 发表于 2023-9-28 19:03:05

还有,system()还有什么其他用途吗?
就是说,括号中还可以填什么?

人造人 发表于 2023-9-28 20:45:54

奋斗中的鱼 发表于 2023-9-28 19:03
还有,system()还有什么其他用途吗?
就是说,括号中还可以填什么?

填命令
sh-5.1$ ls
mainmain.creadread.cwritewrite.c
sh-5.1$ cat main.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    system("ls");
    system("cat main.c");
    system("readelf -l main");
    return 0;
}
sh-5.1$ gcc -m32 -g3 -Wall -o main main.c
sh-5.1$ ./main
mainmain.creadread.cwritewrite.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    system("ls");
    system("cat main.c");
    system("readelf -l main");
    return 0;
}

Elf file type is DYN (Position-Independent Executable file)
Entry point 0x1060
There are 12 program headers, starting at offset 52

Program Headers:
Type         Offset   VirtAddr   PhysAddr   FileSiz MemSizFlg Align
PHDR         0x000034 0x00000034 0x00000034 0x00180 0x00180 R   0x4
INTERP         0x0001b4 0x000001b4 0x000001b4 0x00013 0x00013 R   0x1
      
LOAD         0x000000 0x00000000 0x00000000 0x00428 0x00428 R   0x1000
LOAD         0x001000 0x00001000 0x00001000 0x00204 0x00204 R E 0x1000
LOAD         0x002000 0x00002000 0x00002000 0x000d4 0x000d4 R   0x1000
LOAD         0x002ee8 0x00003ee8 0x00003ee8 0x00128 0x0012c RW0x1000
DYNAMIC      0x002ef0 0x00003ef0 0x00003ef0 0x000f0 0x000f0 RW0x4
NOTE         0x0001c8 0x000001c8 0x000001c8 0x00078 0x00078 R   0x4
GNU_PROPERTY   0x0001ec 0x000001ec 0x000001ec 0x00034 0x00034 R   0x4
GNU_EH_FRAME   0x002028 0x00002028 0x00002028 0x00024 0x00024 R   0x4
GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW0x10
GNU_RELRO      0x002ee8 0x00003ee8 0x00003ee8 0x00118 0x00118 R   0x1

Section to Segment mapping:
Segment Sections...
   00
   01   .interp
   02   .interp .note.gnu.build-id .note.gnu.property .note.ABI-tag .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rel.dyn .rel.plt
   03   .init .plt .text .fini
   04   .rodata .eh_frame_hdr .eh_frame
   05   .init_array .fini_array .dynamic .got .got.plt .data .bss
   06   .dynamic
   07   .note.gnu.build-id .note.gnu.property .note.ABI-tag
   08   .note.gnu.property
   09   .eh_frame_hdr
   10
   11   .init_array .fini_array .dynamic .got
sh-5.1$

liuhongrun2022 发表于 2023-9-28 22:28:50

这条语句相当于你在终端中执行pause命令,里面可以填各种你系统支持的命令

奋斗中的鱼 发表于 2023-9-29 15:40:53

liuhongrun2022 发表于 2023-9-28 22:28
这条语句相当于你在终端中执行pause命令,里面可以填各种你系统支持的命令

也就是说让系统在终端自己给自己写一条指令吗?

liuhongrun2022 发表于 2023-9-29 18:16:23

奋斗中的鱼 发表于 2023-9-29 15:40
也就是说让系统在终端自己给自己写一条指令吗?

是的
页: [1]
查看完整版本: 语句作用问题