开发者

int c = getchar()?

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.

#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 = le开发者_如何学编程n;
            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] = '\0';

    return i;
}

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

    i = 0;
    while((to[i] = from[i]) != '\0')
        ++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?

Thanks in advance


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

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

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.

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


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 0x41 returned (upgraded to an int and assuming you're on an ASCII system of course).

The reason it returns an int rather than a char is because it needs to be able to store any character plus the EOF indicator where the input stream is closed.

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.

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.


The C char type 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.

The reason why the return type is int is because the special value EOF is 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).


Now let's play a game of logic.

Char is also a type of integer which has a smaller range than int, more specifically 8 bits, that is, 1 byte. As we all know, integer types consists of signed ( default ) and unsigned. As for char, the range of signed is -127 ~ 128 and the range of unsigned is 0 ~ 255. Now we know the type and "capability" of signed and unsigned char.

We human understand characters while the computer recogonize only binary sequence. Thus all kinds of programming language must provode a model to deal with the cevertion from characters to binary sequence. ASCII code is the standard for the mapping which applied in C and many other programming languages. It takes 0 - 255 to code basic characters like 0-9, a-z and A-Z, as well as usual special ones.

You may wonder that unsigned char is the exact choice. However, the progamming should know when to stop. The simplest way is to meet a special value, a negative one is a good choice since bigger positive values might be used for other languages. Finally, C choosed -1, which is more commonly called EOF.

Now we've got the point. Signed char will not suffice to code ASCII characters while unsigned leaves no room for the termination value. We require a larger range to balace this, that is, the int type. Savy?

Thanks for the answer of @cdhowie, it acually kindled me.


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/.


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

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

c = c-48.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜