标签: getchar

为什么这个C程序在输出中打印奇怪的字符?

我有以下程序:

#include <stdio.h>

int main()
{
        int ch;
        while( ch = getchar() != '\n') {
                printf("Read %c\n",ch);
        }  
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

无论我输入什么,我得到:

Read  
Run Code Online (Sandbox Code Playgroud)

为什么会发生这种情况,我看到的奇怪的是什么?

Stackoverflow不会打印奇怪的字符.你可以在这里看到:http://ideone.com/EfZHr

c control-characters operator-precedence getchar

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

Theory Behind getchar() and putchar() Functions

I'm working through "The C Programming Language" by K&R and example 1.5 has stumped me:

#include <stdio.h>

/* copy input to output; 1st version */
int main(int argc, char *argv[])
{
    int c;

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

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

I understand that 'getchar()' takes a character for 'putchar()' to display. However, when I run the program in terminal, why is it that I can pass an entire line of characters for 'putchar()' to display?

c putchar getchar

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

scanf("%c",&x)和x = getchar()之间有区别吗?

我写的是尽可能多地了解我试图计算的以下程序,因为我认为它有一些可能适用于日常生活.

该程序就像这样(它是一个密码):

#include <stdio.h>

void cifrario(int k) {
    char b, d;
    scanf("%c", &d);
    d = getchar();
    putchar(d);
    scanf("%c", &b);
    b = getchar();
    while (b != d) {
        if (('A' <= b && b <= 'Z') || ('a' <= b && b <= 'z')) {
            b = b + k;
            printf("%c", b);
            scanf("%c", &b);
            b = getchar();
        } else 
            printf("%c", b);
    }
}

int main() {
    int h;
    scanf("%d", &h);
    cifrario(h);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我试图改变输入中给定的某个常数的字母表.

我不太了解并且我不太了解的事情,这就是为什么程序不能在没有scanfor的情况下运行getchar,并且两者在相同的值上都是必需的,让我们在这里说:

scanf("%c", …
Run Code Online (Sandbox Code Playgroud)

c function scanf getchar

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

k&R,getchar如何阅读EOF

从k&r读书时,我遇到了以下例子

#include<stdio.h>
int main()
{
int c;
while((c=getchar())!=EOF)
{
    putchar(c);
}
printf("hello");
}
Run Code Online (Sandbox Code Playgroud)

怀疑1:当我输入字符ctrl + z(我的系统上的EOF)时.o/p是你好
但当我输入字符串如abcdef ^ Zghijk
o/p是abcdef - >(包括箭头)并等待用户输入i/p而不是终止循环并打印你好..

c getchar

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

什么是标准输入缓冲区?

#include <stdio.h>

int main(void)
{
    int c;
    c = getchar();
    putchar(c);
    c = getchar();
    putchar(c);
    c = getchar();
    putchar(c);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想了解为什么被调用三次的函数与仅输入一次的行一起工作。有人解释说,在这种情况下我们使用标准输入缓冲区,那是一块内存。我想读一些相关内容。你能给我一些资源吗?

c stdin putchar getchar

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

无法弄清楚如何使用getchar(); 在C.

#include <stdio.h>
int main(void)

{
    char F,C;

    printf("Do you have a Fever? y/n\n");
    F = getchar();

    printf("Do you have a runny nose or cough? y/n\n");
    C = getchar();


    printf("Here are the results you input:\n");
    printf("Do you have a fever?");
    putchar(F);

    printf("\nDo you have a runny nose or cough?");
    putchar(C);

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

代码从第一个输出结果getchar();然后退出而不等待更多输入.这是为什么?

c getchar

5
推荐指数
2
解决办法
4万
查看次数

从标准输入 C 读取所有数据

我编写了这个小函数来读取stdin.

我需要知道这个函数是否与 POSIX 兼容(通过这个,我的意思是它可以在 Unix 和类 Unix 系统下工作)至少它在 Windows 上工作......

char* getLine()
{
    int i = 0, c;
    char* ptrBuff = NULL;

    while ((c = getchar()) != '\n' && c != EOF)
    {
        if ((ptrBuff = (char*)realloc(ptrBuff, sizeof (char)+i)) != NULL)
            ptrBuff[i++] = c;
        else
        {
            free(ptrBuff);
            return NULL;
        }
    }

    if (ptrBuff != NULL)
        ptrBuff[i] = '\0';

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

该函数从 stdin 读取所有数据,直到 get '\n'orEOF并返回一个指向新位置的指针,其中包含所有字符。我不知道这是否是最好的或更安全的方法,也不知道这是否适用于 Unix 和类 Unix 系统……所以,我需要一点帮助。我该如何改进该功能?或者有没有更好的方法来获取所有数据stdin而不在缓冲区上留下垃圾?我知道这fgets()是一个选项,但是,如果用户输入比预期的大,它可能会留下垃圾……另外,我想获取用户编写的所有字符。

编辑: …

c unix stdin posix getchar

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

在 while 循环中等待 C 中的 Enter 键?

我正在编写一个 C 程序,我需要等待用户按任意键才能继续。当我使用getchar();它时,它等待Enter按键被按下。但是当我在while循环中使用它时,它不起作用。如何让我的代码等待按下任意键才能继续循环?

这是我的代码示例。我正在使用 GNU/Linux。

#include <stdio.h>
#include<stdlib.h>
int main()
{
    int choice;
    while(1) {
        printf("1.Create Train\n");
        printf("2.Display Train\n");
        printf("3.Insert Bogie into Train\n");
        printf("4.Remove Bogie from Train\n");
        printf("5.Search Bogie into Train\n");
        printf("6.Reverse the Train\n");
        printf("7.Exit");
        printf("\nEnter Your choice : ");
        fflush(stdin);
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                printf("Train Created.");
                break;
            case 2:
                printf("Train Displayed.");
                break;
            case 7:
                exit(1);
            default:
                printf("Invalid Input!!!\n");
        }

        printf("Press [Enter] key to continue.\n");
        getchar();
    }

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

c keypress while-loop getchar

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

bubble中的冒泡排序和getchar

我正在使用microsoft visual studio 2012并尝试进行冒泡排序.这是我的代码:

#include "stdafx.h"
#include "String.h"
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    int array[100], n, c, d, swap;
    printf("enter numbers of elements\n");
    scanf_s("%d",&n);
    printf("enter %d integers\n", n);
    for (c = 0; c < n; c++){
        scanf_s("%d", array);
    }
    for (c = 0; c < (n - 1); c++)
    {
        for (d = 0; d < n - c - 1; d++)
        {
            if (array[d]>array[d + 1]){
                swap = array[d];
                array[d] = array[d + 1]; …
Run Code Online (Sandbox Code Playgroud)

c++ sorting getchar

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

在 Python 中获取单个字符作为输入,无需按 Enter 键(类似于 C++ 中的 getch)

首先,我对Python完全陌生,刚刚开始学习它。我知道很多关于 C++ 的东西,但我只是尝试用 Python 实现其中一些。

我已经对其进行了大量搜索,但找不到任何符合我要求的解决方案。请看下面的代码,

import os

class _Getch:
    """Gets a single character from standard input.  Does not echo to the
screen."""
    def __init__(self):
    try:
        self.impl = _GetchWindows()
    except:
        print("Error!")
    def __call__(self): return self.impl()

class _GetchWindows:
    def __init__(self):
        import msvcrt

    def __call__(self):
        import msvcrt
        return msvcrt.getch()



def mainfun():
    check = fh = True
    while check:
        fh = True
        arr = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
        print ("Welcome to Tic Tac Toe Game!!!\n\n")
        print("Enter 1 to …
Run Code Online (Sandbox Code Playgroud)

python user-input menu getchar python-3.x

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