C语言 int c = getchar()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7119470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:26:02  来源:igfitidea点击:

int c = getchar()?

cgetchar

提问by Rhexis

ok so im reading this book: The C Programming Language - By Kernighan and Ritchie (second Edition) and one of the examples im having trouble understanding how things are working.

好的,我正在阅读这本书:The C Programming Language - By Kernighan and Ritchie(第二版)以及我无法理解事物如何工作的示例之一。

#include <stdio.h>

#define MAXLINE 1000

int getline(char line[], int maxline);
void copy(char to[], char from[]);

int main(int argc, char *argv[])
{
    int len;

    int max;
    char line[MAXLINE];
    char longest[MAXLINE];

    max = 0;
    while((len = getline(line, MAXLINE)) > 1)
    {
        if(len > max)
        {
            max = len;
            copy(longest, line);
        }
    }
    if(max > 0)
        printf("%s", longest);

    getchar();
    getchar();
    return 0;   
}

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] = '##代码##';

    return i;
}

void copy(char to[], char from[])
{
    int i;

    i = 0;
    while((to[i] = from[i]) != '##代码##')
        ++i;
}

the line : for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)where it says c = getchar(), how can an integer = characters input from the command line? Integers yes but how are the characters i type being stored?

该行:for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)它说 c = getchar(),如何从命令行输入整数 = 字符?整数是的,但我输入的字符是如何存储的?

Thanks in advance

提前致谢

回答by Steve Jessop

Unlike some other languages you may have used, chars in C areintegers. charis just another integer type, usually 8 bits and smaller than int, but still an integer type.

与您可能使用过的其他一些语言不同,C中的字符整数。char只是另一种整数类型,通常为 8 位且小于int,但仍然是整数类型。

So, you don't need ord()and chr()functions that exist in other languages you may have used. In C you can convert between charand other integer types using a cast, or just by assigning.

所以,你不需要ord()chr()你可能已经使用中存在的其他语言功能。在 C 中,您可以使用强制转换char或仅通过赋值在和其他整数类型之间进行转换。

Unless EOF occurs, getchar()is defined to return "an unsigned char converted to an int" (same as fgetc), so if it helps you can imagine that it reads some char, c, then returns (int)(unsigned char)c.

除非发生 EOF,否则getchar()被定义为返回“转换为 int 的无符号字符”(与 fgetc 相同),因此如果它有帮助,您可以想象它读取一些字符c,然后返回(int)(unsigned char)c

You can convert this back to an unsigned charjust by a cast or assignment, and if you're willing to take a slight loss of theoretical portability, you can convert it to a charwith a cast or by assigning it to a char.

您可以通过强制转换unsigned char或赋值将其转换回a ,如果您愿意稍微损失理论上的可移植性,则可以将其转换为char带有强制转换的a 或将其分配给 a char

回答by paxdiablo

The getchar()function returns an integer which is the representation of the character entered. If you enter the character A, you will get 'A'or 0x41returned (upgraded to an intand assuming you're on an ASCII system of course).

getchar()函数返回一个整数,它是输入字符的表示形式。如果您输入字符A,您将得到'A'0x41返回(当然,升级为 anint并假设您使用的是 ASCII 系统)。

The reason it returns an intrather than a charis because it needs to be able to store any character plusthe EOF indicator where the input stream is closed.

它返回 anint而不是 a的原因char是因为它需要能够存储任何字符以及输入流关闭的 EOF 指示符。

And, for what it's worth, that's not really a good book for beginners to start with. It's from the days where efficiency mattered more than readability and maintainability.

而且,就其价值而言,这对于初学者来说并不是一本好书。从那时起,效率比可读性和可维护性更重要。

While it shows how clever the likes of K&R were, you should probably be looking at something more ... newbie-friendly.

虽然它显示了 K&R 之类的人是多么聪明,但你可能应该看看更多......新手友好的东西。

In any case, the last edition of it covered C89 and quite a lot has changed since then. We've been through C99 and now have C11 and the book hasn't been updated to reflect either of them, so it's horribly out of date.

无论如何,它的上一版涵盖了 C89,并且从那时起发生了很多变化。我们已经经历了 C99,现在有了 C11,而且这本书还没有更新以反映它们中的任何一个,所以它已经过时了。

回答by cdhowie

The C chartype is 8 bits, which means it can store the range of integers from (depending on if it is signed or not and the C standard does not dictate which it is if you do not specify it) either -128 to 127 or 0 to 255 (255 distinct values; this is the range of ASCII). getchar()returns int, which will be at least 16 bits (usually 32 bits on modern machines). This means that it can store the range of char, as well as more values.

Cchar类型是 8 位,这意味着它可以存储整数范围(取决于它是否有符号,如果不指定,C 标准不会规定它是哪个)-128 到 127 或 0到 255(255 个不同的值;这是 ASCII 的范围)。 getchar()返回int,至少为 16 位(在现代机器上通常为 32 位)。这意味着它可以存储 的范围char以及更多的值。

The reason why the return type is intis because the special value EOFis returned when the end of the input stream is reached. If the return type were char, then there would be no way to signal that the end of the stream was encountered (unless it took a pointer to a variable where this condition was recorded).

之所以返回类型是int因为EOF到达输入流的末尾时返回特殊值。如果返回类型是char,则将无法发出信号表示遇到了流的结尾(除非它使用指向记录此条件的变量的指针)。

回答by Matt

Every character (including numbers) entered on the command line is read as a character and every character has an integer value based on its ASCII code http://www.asciitable.com/.

在命令行上输入的每个字符(包括数字)都被读取为一个字符,并且每个字符都有一个基于其 ASCII 码http://www.asciitable.com/的整数值。

回答by Yogeesh Seralathan

Answer for your Question is answered. But just add 1 more thing.

您的问题的答案已得到解答。但只需再添加 1 样东西。

As you are declaring variable cas int. It is pretty clear that you are taking values from 0 to 9having ascii value of 48-57. So you can just add 1 more line to the code-

当您将变量声明c为 int 时。很明显,您正在从0 to 9.ascii 值中获取值48-57。因此,您只需在代码中再添加 1 行即可-

c = c-48.

c = c-48.