The eclipse console has weird behaviour when used for input with C programs.
I
teach C to first year undergraduates and I want them to learn their way
through eclipse. But the small silly programs that ask you to input
characters have weird behaviour, if you use the eclipse console. It
seems like it groups all input and output commands and executes them
together...for example...
the following program:
#include <stdio.h>
int main() {
int n = 0;
printf("Gimme a number: ");
scanf ("%d", &n);
printf("/nThe number you entered was %d/n", n);
return 0;
}
has the following output:
4
Gimme a number: The number you entered was 4
Pretty normal output on any console you'll find, not just with eclipse's one
instead of
Gimme a number: 4
The number you entered was 4
To obtain this output, you have to flush stdout before scanf'ing the
number. The output is flushed either implicitely when a newline
character is echoed on the console (printf("/n")) or explicitely with
fflush(stdout);
to get the output you wanted use this program :
#include <stdio.h>
int main() {
int n = 0;
printf("Gimme a number: ");
fflush(stdout);
scanf ("%d", &n);
printf("/nThe number you entered was %d/n", n);
return 0;
}
分享到:
相关推荐
函数`printf()`和`scanf()`是标准库中的函数,分别用于输出和输入数据。例如,下面的程序展示了如何接收用户输入的两个整数并计算它们的和: ```c #include int main() { int a, b, sum; printf("The program ...
根据给定的信息,我们将深入解析计算机二级考试中的C语言编程部分,主要聚焦于上机操作题目,特别是关于数列求和、最大公约数与最小公倍数、排序算法、数组处理、数字运算等方面的知识点。 ### 一、数列求和 #### ...
整数划分问题是指将一个正整数\( n \)表示为若干个正整数之和的方式的计数问题,其中这些正整数需要满足一定的顺序关系。具体地,对于正整数\( n \),我们寻找所有可能的表示形式\( n = n_1 + n_2 + \ldots + n_k \)...
- **Eclipse CDT**:基于Eclipse平台的C/C++开发工具,适合于那些习惯使用Eclipse环境的开发者。 选择合适的开发环境可以极大地提高编程效率。 ##### 1.2 第一个程序 编写第一个程序通常是从输出“Hello World”...
3. **选择组件**:在安装过程中,请确保选择了基本的 C 编译器和开发工具,例如 `mingw32-base` 和 `mingw32-gcc-g++`。 4. **配置环境变量**:安装完成后,在“控制面板”->“系统和安全”->“系统”->“高级系统...
实际上,结构化编程通过使用顺序、选择和循环三种基本结构能够解决复杂的问题,而不只是简单问题。 **知识点6:** C语言程序的基本单位 - **题目描述:** C语言程序的基本单位是什么? - **答案解析:** C语言程序的...