开发者

Why can I assign/compare int and char in C

I have the code like this :

#include <stdio.h>
main()
{
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
}

The C documentation says that the getchar() returns the int value. And in the above program we have assigned c type as an int. And most importantly EOF is a integer constant defined in the header function.

Now if the code changes to something like this:

 #include <stdio.h>
    main()
    {
    char c;
    c = getchar();
    while (c != EOF) {
    putchar(c);
    c = getchar();
    }
    }

This code also works! Wait a min, as per C documentation getchar() returnsint, but see in the above code I'm storing it in char. And C compiler doesn't throw any error. And also in while loop I have compared c which is an char with EOF which is an int and compiler doesn't throw any error and my program executes!开发者_StackOverflow中文版

Why does the compiler doesn't throw any error in the above two cases?

Thanks in advance.


No. It simply means that the returned value which is an int, implicitly converts into char type. That is all.

The compiler may generate warning messages for such conversion, as sizeof(int) is greater than sizeof(char). For example, if you compile your code with -Wconversion option with GCC, it gives these warning messages:

c.c:5:7: warning: conversion to 'char' from 'int' may alter its value
c.c:8:8: warning: conversion to 'char' from 'int' may alter its value

That means, you should use int to avoid such warning messages.


I'm afraid that the term "dynamic programming language" is too vaguely defined to make a such a fine distinction in this case.

Though I'd argue that implicit converting to one numeric type to another is not a dynamic language feature, but just syntax sugar.


No. Lets look at wikipedia's definition

These behaviors could include extension of the program, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. These behaviors can be emulated in nearly any language of sufficient complexity, but dynamic languages provide direct tools to make use of them.

What you have demonstrated is that a char and int in C/C++ are pretty much the same, and C/C++ automatically casts between the two. Nothing more. There's no modification of the type system here.


Lets rewrite your code to illustrate what's going on

int main(int argc, char** argv)
{
    char c;
    c = EOF; /* supposing getchar() returns eof */
    return (c == EOF) ? 0 : 1;
}

What should the return value of this program be? EOF is not a char, but you cast it to a char. When you do a comparison, that cast happens again, and it gets squashed to the same value. Another way of rewriting this to make it clear what's going on is:

#include <stdio.h>
main()
{
    int c;
    c = getchar();
    while ((char)c != (char)EOF) {
        putchar((char)c);
        c = getchar();
    }
}

EOF is getting squashed; it doesn't matter how it's getting squashed, it could be squashed to the letter 'M', but since it gets squashed the same way every time, you still see it as EOF.


C will let you do what you want, but you have to accept the consequences. If you want to assign an int to a char then you can. Doesn't make it a dynamic language.

(As an aside, the title of this question should be something like "why does c let me assign an int to a char?" and just contain the final paragraph. But presumably that wouldn't attract enough attention. If it had attracted any upvotes then I'd edit the title, but since it isn't I'll leave it as an example of how not to ask a question.)


If you are not reading 7-bit ASCII data, it is possible that \xFF is valid data. If EOF is \xFFFFFFFF and you return the value of getchar() to a char then you can not distinguish EOF from \xFF as data.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜