相关疑难解决方法(0)

System()调用C++及其在编程中的作用

我经常听说使用system("PAUSE")是不好的做法std::cin.get()而是使用.现在我对系统调用的理解是它们将一个字符串输入到系统命令行并与操作系统通信,因此PAUSE是一个DOS命令,用于暂停命令窗口中的输出.我认为这与Mac和使用不同关键字的unix类似,并且由于缺乏跨操作系统兼容性而不鼓励使用系统调用.(如果我错了,请纠正我)

我的问题是:什么时候使用system()调用?它们应该如何应用?什么时候不应该申请?

c++ windows operating-system system dos

7
推荐指数
1
解决办法
2万
查看次数

不包括stdlib.h不会产生任何编译错误!

希望这是一个非常简单的问题.以下是我的C pgm(test.c).

#include <stdio.h>
//#include <stdlib.h>

int main (int argc, char *argv[]) {
    int intValue = atoi("1");
    double doubleValue = atof("2");
    fprintf(stdout,"The intValue is %d and the doubleValue is %g\n", intValue, doubleValue);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

请注意,我正在使用stdlib.h中的atoi()和atof(),但我没有包含该头文件.我编译pgm(gcc test.c)并且没有编译错误!

我运行pgm(./a.out),这是输出,这是错误的.

The intValue is 1 and the doubleValue is 0
Run Code Online (Sandbox Code Playgroud)

现在我包含stdlib.h(通过删除#include之前的注释)并重新编译它并再次运行它.这次我得到了正确的输出:

The intValue is 1 and the doubleValue is 2
Run Code Online (Sandbox Code Playgroud)

为什么编译器没有抱怨不包含stdlib.h并且仍然让我使用atoi(),atof()函数?

我的gcc信息:

$ gcc --version
gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-27)
Run Code Online (Sandbox Code Playgroud)

任何想法赞赏!

c gcc std atof

4
推荐指数
1
解决办法
5378
查看次数

标签 统计

atof ×1

c ×1

c++ ×1

dos ×1

gcc ×1

operating-system ×1

std ×1

system ×1

windows ×1