标签: getchar

使用getchar()和putchar()的非常简单的C问题

您好我正在教自己C并通过K&R书,我遇到了一些麻烦(我正在运行OS X).这是第1.5.1节"文件复制",它应该将一个字符作为输入,然后输出该字符.这是代码:

#include <stdio.h>

/* --  Copy input to output -- */ 
int main(void)
{
int c;

c = getchar();

while ( c != EOF ) {
    putchar(c);
    c = getchar;
}


}
Run Code Online (Sandbox Code Playgroud)

所以,我认为我的问题不在于代码本身,而在于编译和运行.首先,在编译时我得到以下错误

/Volumes/Goliath/Dropbox/C programs/prog1_5_1.c: In function ‘main’:
/Volumes/Goliath/Dropbox/C programs/prog1_5_1.c:12: warning: assignment makes integer from pointer without a        cast
/Volumes/Goliath/Dropbox/C programs/prog1_5_1.c:16: warning: control reaches end of non-void function
Run Code Online (Sandbox Code Playgroud)

然后当我运行输出文件(在终端中)它有一个小空间,然后当我输入一个字母时,说我输入

一个

然后我点击Return

我得到一个新的一行.如果我然后点击一个新密钥,屏幕就会开始变得疯狂,并在整个地方出现问号.

我不确定我是否有多大意义,但我发现这是一个奇怪的问题.非常感谢你提前

c putchar getchar

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

getchar()不能正常工作?

我用C++编写了这段代码,我常常使用getchar()控制台,但我没有看到使用该函数的任何影响,这里是代码:

#include<iostream>
#include<stdio.h>//to pause console screen

using namespace std;
//function prototypes
int  getSmallest(int*);
int getOccurrence(int, int*);

int main(){

    int a[7], counter=0, smallest;
    cout<<"Please enter 7 integers"<<endl;
    while(counter!=7){
        cin>>a[counter];
        counter++;
    }
    smallest=getSmallest(a);
    cout<<"The smallest number is "<<smallest<<"; it apears "<<getOccurrence(smallest,a)<<" times"<<endl;
    getchar();//to pause console screen
    return 0;
}

int  getSmallest(int*x){
int count=0, smallest=*x;
//loop till last item in array
while(count!=7){

    if(*x<smallest)
        smallest=*x;
    count++;
    x++;
}
return smallest;
}


int getOccurrence(int smallest, int* address){

int count=0,occ=0;
//loop till last item …
Run Code Online (Sandbox Code Playgroud)

c++ getchar

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

getchar的意外行为

只是学习C编程,坚持我确定的Do/While循环是微不足道的.我有一段代码需要用户点击'E'退出程序:

char exitletter;

do {
printf ("Please hit E to exit the Program\n");
exitletter = getchar();
} while (exitletter !='E');
Run Code Online (Sandbox Code Playgroud)

但是,如果用户输入了错误的字符,则会打印两次"请按E退出程序".如果用户输入say abcd,则会打印消息五次.

有人可以解释一下这里发生了什么吗?

c getchar do-while

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

为什么getchar()函数不循环?

我已经阅读了很多关于getchar()它及其行为的问题,但我仍然不明白这个简单的代码..

    while (scanf("%d", &z) != 1)
  {
    while (getchar() != '\n');
    printf ("Try again: ");}
Run Code Online (Sandbox Code Playgroud)

这段代码是为了验证字符..我从这段代码推断出来的是,如果我输入的话

Stackoverflow
Run Code Online (Sandbox Code Playgroud)

然后用换行符'\n'将整行推送到缓冲区.然后getchar()从缓冲区读取每个字符并返回一个整数,清理缓冲区.在这种情况下,while循环应循环12次(数量为Stackoverflow中的字符直到它到达'\n'字符..但实际上它只循环一次,输出是

Try again:
Run Code Online (Sandbox Code Playgroud)

意味着它的循环scanf再次要求...它违背了我对循环的理解..也许我误解了循环..另外一个问题,如果getchar()返回整数然后如何将它与'\n'之类的字符进行比较?

c while-loop getchar

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

为什么我的第二个printf在预期时没有被调用?

我刚学用C编程和感到有点困惑的东西去与scanfgetchar.我知道使用类似的东西scanf("%d", &i)会读取整数输入,但在输入缓冲区中保留以下换行符(这就是为什么必须getchar() == '\n'通过getcharscanf调用之后使用之前查找来清除输入缓冲区的原因.

这是我的简单(不正确)程序,它读取整数,然后是字符输入,并将它们打印回给用户:

int main(void)
{
    int i;
    printf("Enter an integer: ");
    scanf("%d\n", &i);

    printf("Enter a char: ");
    char ch = getchar();

    printf("You entered integer: %d\nYou entered character: %c\n", i, ch);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我将换行符留在我的scanf格式字符串中(所以只有"%d"),有意义的是,只要用户输入一个整数并点击输入,该整数将被读取并存储i,程序将继续执行( "输入一个字符:"立即打印,并ch存储换行符.

但是,使用格式String "%d\n"我会遇到意外行为.当用户输入一个整数并按Enter键时,我希望打印"输入字符:".相反,你可以继续按Enter键,直到你决定输入一个不同的角色时才会发生任何事情.所以你可以输入"10",输入6次,然后键入"d"并getchar()读取"正确".整个程序输出看起来像这样

Enter an integer: 10




d
Enter a char: You entered integer: 10
You entered character: d
Run Code Online (Sandbox Code Playgroud)

为什么我的程序在输入字符之前会停止?似乎奇怪的是,我的第二个 …

c input scanf getchar

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

用作计数器的整数值的意外输出

在编写应该是一个非常简单的程序(在K&R C书中)时,我得到了一些我不理解的输出.如果我EOF马上nl_count就是0,但space_count总是32767(最大int范围的一半),并且tab_count是一个很大的值(通常非常高或非常低).

当我将所有变量初始化为0时,程序正常工作.我在这里错过了什么?

 10 int main(void)
 11 {
 12         int tab_count, space_count, nl_count;
 13 
 14         //tab_count = space_count = nl_count = 0;
 15 
 16         int c;
 17         while( (c = getchar()) != EOF ){
 18                 if( c == '\t' )
 19                         tab_count++;
 20                 if( c == ' ' )
 21                         space_count++;
 22                 if( c == '\n' )
 23                         nl_count++;
 24         }
 25 
 26         printf("\n");
 27         printf("TABS = %d\n", tab_count);
 28 …
Run Code Online (Sandbox Code Playgroud)

c initialization getchar

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

为什么getchar允许单字符输出函数显示字符串?

刚开始使用K&R并点击:

#include <stdio.h>
/* copy input to output; 1st version */
main()
{
    int c;
    c = getchar();
    while (c != EOF) {
        putchar(c);
        c = getchar();
    }
}
Run Code Online (Sandbox Code Playgroud)

它只显示我输入的内容,即使它是一串文本.

cacti
cacti
stop repeating what i'm saying
stop repeating what i'm saying
Run Code Online (Sandbox Code Playgroud)

就像那样.

我没有得到的是为什么我不能用一串文本实例化变量c并以相同的方式打印它.(为了示例而忽略while循环)与:

main()
{
    int c;

    c = "cacti";
    putchar(c);
}
Run Code Online (Sandbox Code Playgroud)

其中输出显然只是'd'

c character getchar

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

scanf和getchar处理流的方式有何不同?

我对scanf和getchar如何处理不同的流感到困惑,以下是示例代码:

while(scanf("%d", &input) != 1)
{
    while((ch = getchar()) != '\n')
    {
        putchar(ch);
    }
    printf("\nThis is wrong\n");
}

printf("That is right\n");
Run Code Online (Sandbox Code Playgroud)

这是一个简单的程序,用于测试输入是否为整数.内部while循环用于在单击Enter之前显示每个错误的输入值.当我输入一个随机字符串,如:

qwert
Run Code Online (Sandbox Code Playgroud)

putchar将打印出确切的字符串.但是,如果我换了

while(scanf("%d", &input) != 1)
Run Code Online (Sandbox Code Playgroud)

while((ch = getchar()) != '\n')
Run Code Online (Sandbox Code Playgroud)

并打印出完全相同的字符串,第一个字母"q"被删除.所以我的问题是外循环中的scanf和getchar如何处理这种情况?

c io scanf getchar

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

这个c文件处理代码有什么问题?

我正在尝试从终端写入文件中的内容.文件正在创建,但内容未写入文件.

    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    int main(int argc, char *argv[])
    {
        FILE *fp;
        fp=fopen(argv[1],"w");
        char ch;
        while((ch=getchar())!=EOF)
        {
           putc(ch,fp);
        }
        fclose(fp);
        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

c file-handling putchar getchar getc

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

在C中使用递归创建二叉树

我尝试使用递归创建二叉树,但是当我键入时ABD***CE**FG***,代码未产生任何结果。我按了空格键,但是代码仍然没有成功。代码是错误的还是我的输入错误?

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

typedef struct tree
{
    struct tree *left;
    struct tree *right;
    char val;
}treeNode;
void createTree(treeNode **node)
{
    char value=0;
    value=getchar();
    if(value=='*')
        *node=NULL;
    else
    {
        *node = (treeNode*)malloc(sizeof(treeNode));
        if(!*node)  exit(-1);
        (*node)->val=value;
        createTree(&(*node)->left);
        createTree(&(*node)->right);
    }

}
void preOrder(treeNode *node)
{
    if (node==NULL) return;
    putchar(node->val);
    preOrder(node->left);
    preOrder(node->right);
}
int main() {
    // insert code here...
    treeNode **node;
    createTree(node);
    preOrder(*node);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c recursion binary-tree getchar

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