这是一个基本问题..但不得不问.对于这样的程序,如果用例是123 ^ Z,程序不会终止,即使我在末尾放置了一个EOF(Ctrl + Z).为什么会这样?只有当我在CR之后放置一个EOF它才有效.任何anwers将不胜感激.谢谢.
#include < stdio.h>
void main()
{
int i, nc;
nc = 0;
i = getchar();
while (i != EOF) {
nc = nc + 1;
i = getchar();
}
printf("Number of characters in file = %d\n", nc);
}
Run Code Online (Sandbox Code Playgroud) 我的问题只是"为什么第10行和第11行的代码无法正常运行?" 我的代码的目的是完全按照原始的K&R代码进行操作,但不计算nc(getchar()=='\n'),请你启发我吗?
略微修改的K&R代码:
/** K&R - 1.5.2 Character Counting **/
#include <stdio.h>
/* count characters in input; 1st version */
main(){
long nc;
nc = 0;
while (getchar() != EOF){
if (getchar() != '\n'){
++nc;
}
}
printf("%ld\n", nc);
}
Run Code Online (Sandbox Code Playgroud)
我使用的是64位Windows 7,CodeBlocks10.05,GNU GCC编译器.
我目前的进步和理解:
在示例运行中,我输入单词two并按Enter键,等于4个输入,然后按ctrl + Z输入一个^Z或EOF字符.程序然后打印1.我期待它打印3.我想唯一合乎逻辑的解释是它完全与我的意图完全相反(它只计算换行符?).事实证明,如果我输入单词two并按回车键,可以说4次,它会打印出来4.它似乎在计算nc输入的每个换行符,但是如果我单独输入(在这种情况下为4次)然后按EOF,它总是打印出来0.经过进一步的实验,通过一些看不见的手4可能是这个程序的神奇数字.如果我启动它并完全按下输入键(一个可被4整除的数字)然后打印出EOF 0.但是,如果我输入其他次数输入EOF什么都不做,我必须^Z一个接一个地输入两行,以正确结束while循环,然后打印1.这令人难以置信!
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a,b;
printf("enter the firstkeyword \n");
a = getchar();
printf("enter your second keyword \n");
b = getchar();
if(a>b)
{
printf("it is '%c' greater than '%c' as i expected \n",a,b);
}
else if (b>a)
{
printf("here'%c'is greater than '%c' \n",b,a);
}
else
{
printf("dont type the same character twice in next session \n");
}
return(0);
}
Run Code Online (Sandbox Code Playgroud)
在编译程序之后,o/p是:
输入第一个关键字
我输入'$'并使用ctrl + z来eof并'enter'继续该程序.但即使没有输入第二个关键字,编译器也会将输出打印为
输入第二个关键字
正如我所料,它比' - >'更大'.'
任何人都可以帮助这个计划吗?
对不起,如果有任何语法或短语错误.
我有这个简单的C程序,它从标准输入中读取字符,并显示一个带有编号行和列的表,其中包含我们编写的字符.代码是:
#include <stdio.h>
#define COLS 6
#define WIDTH 5
int main(int argc, char *argv[]){
char buffer[COLS]={0};
int c;
int cols;
for(cols=0;cols<COLS && (c=getchar())!=EOF;cols++){
buffer[cols]=c;
}
if(cols!=0){
int a;
printf("%-*s",WIDTH,"");
for(a=0;a<cols;a++){
(cols!=(a+1)) ? printf("%-*d",WIDTH,(a+1)) : printf("%-*d\n",WIDTH,(a+1));
}
printf("%-*d",WIDTH,1);
for(a=0;a<cols;a++){
(cols!=(a+1)) ? printf("%-*c",WIDTH,buffer[a]) : printf("%-*c\n",WIDTH,buffer[a]);
}
cols=0;
int rows=2;
while((c=getchar())!=EOF){
if(cols==0){
printf("%-*d%-*c",WIDTH,rows,WIDTH,c);
cols++;
}
else{
if(COLS!=(cols+1)){
printf("%-*c%",WIDTH,c);
cols++;
}
else{
printf("%-*c\n",WIDTH,c);
cols=0;
rows++;
}
}
}
printf("End-Of-File\n");
}
else{
printf("No valid characters input\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在Windows XP上用cc编译程序,如果在Windows控制台(cmd)中,我执行它:
program.exe
Run Code Online (Sandbox Code Playgroud)
然后它读取我用键盘输入的字符.它工作正常.当我决定从txt文件中读取字符时,会出现问题.所以我用记事本创建了一个txt文件(在program.exe的同一个文件夹中),我在里面写了几个句子.然后在控制台中我执行:
program.exe<file.txt …Run Code Online (Sandbox Code Playgroud) 在下面的例子中,从"C编程"一书中,当输入字符时,程序计数两次.
main(){
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
a
b
c
d
e
f
12
Run Code Online (Sandbox Code Playgroud)
怎么了?
我正在使用Ubuntu和gcc编译器.
我从学习ç C编程语言。我试图使以下代码工作:
#include <stdio.h>
int main() {
int char_count = 0;
while (getchar() != EOF)
++char_count;
printf("Number of chars are %ld ", char_count);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我构建并运行代码。然后,我输入一个随机单词并按回车键。我希望在下一行看到一个数字,但发生的情况是光标移动到下一行。当我输入一个新词时,也会发生同样的事情。我错过了什么?
编辑:我希望在程序完成获取第一个单词的字符时getchar()返回EOF。
我正在尝试使箭头键在单行(左+右)中的字符之间以及自定义外壳(学期项目)的历史命令(上+下)之间移动。
此时,当击中其中一个箭头时 ^[[A, ^[[B, ^[[C 或 ^[[D 显示出来,按下回车后,我意识到其中一个被击中了:
char a = getchar();
if (a == '\033') {
getchar();
int ch2 = getchar();
switch(ch2){
case 'A':
printf("UP\n");
break;
case 'B':
printf("DOWN\n");
break;
case 'D':
printf("LEFT\n");
break;
case 'C':
printf("RIGHT\n");
break;
default:
printf("SOME OTHER SCROLL KEY PRESSED: %d %d\n", a, ch2);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
我想得到的是,一旦我击中其中一个箭头,动作就会发生而不会显示任何内容。
i am new to StackOverflow. I hope to be able to learn a lot here. So, i'm a beginner in C. I'm just trying a few things, like using very basic functions. Here's my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{ int c;
int i,wl[20];
int count = 0;
i = 0;
printf("Insert line: ");
while(c= getchar() != '\n'&& c != EOF)
printf("integer value of the variable is %d\n", c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
This should be an easy program: …
我有下面的代码,它应该与用户交互,了解他想要做什么。
int input;
printf("Would you like to update the name of the student?\n");
printf("Enter Y(yes) or N(no)\n");
input = getchar();
if (input == 'Y' || input == 'y') {
printf("Please enter the updated name\n");
scanf("%s", tmpSt->name);
}
printf("Would you like to update the student's ID?\n");
printf("Enter Y(yes) or N(no)\n");
input = getchar();
Run Code Online (Sandbox Code Playgroud)
但执行时它不会停止获取第一个输入。
Would you like to update the name of the student?
Enter Y(yes) or N(no)
Would you like to update the student's ID?
Enter Y(yes) or N(no)
Run Code Online (Sandbox Code Playgroud)
它应该得到名称的答案,但它直接转到 …
以下是第 29 页 K&R 中的 getline 实现,
# define MAXLINE 1000
int getLine(char s[], int lim)
{
int c ,i ;
for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c!= '\n'; ++i)
s[i] = c;
if(c == '\n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我们需要在 for 循环中执行“i < lim - 1”。对于正确的索引,为什么“ i < lim ”不够?
任何帮助将非常感激...
c ×10
getchar ×10
windows ×2
arrow-keys ×1
c-strings ×1
compilation ×1
for-loop ×1
increment ×1
input ×1
loops ×1
output ×1
redirect ×1
scanf ×1
shell ×1
user-input ×1