变量的声明如何表现? [英] How declaration of variables behave?

查看:114
本文介绍了变量的声明如何表现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<conio.h>

int main(){

    char i;
    int c;

    scanf("%i",&c);
    scanf("%c",&i);// catch the new line or character introduced before x number
    printf("%i",i);// value of that character

    getch();
    return(0);
}

的程序的行为以同样的方式与下一个变量声明代替上述变量声明:

The program will behave in the same way with the next variable declarations instead of the above variable declaration:

这样的:

int c;
int *x;
int i;

或本

int *x;
int c;
int i;

和只有这样: C 变量和 X 指针之前在变量。
我知道,那最后的声明都没有感觉,在 INT I 而不是字符我,以及添加指针甚至没有必要的。
但是,这已经发生了意外和IM想知道如果它只是一个巧合。

And only this way: c variable and a x pointer before the i variable. I know that those last declarations haven't sense, the int i instead of char i, and an added pointer that isn't even needed. But this have been occurred accidentally and im wondering if it's only an a coincidence.

推荐答案

在其中声明变量应该没有什么区别可言,假设有什么不对您的code其余的顺序。声明的顺序不一定必须在所有任何与他们在内存布局方式。而且,即使它没有,你指的是按名称变量;只要你的code是正确的,到引用我引用我,编译器会产生什么code是需要正确地访问变量。

The order in which you declare your variables should make no difference at all, assuming there's nothing wrong with the rest of your code. The order of declaration needn't have anything at all to do with the way they're laid out in memory. And even if it did, you refer to variables by name; as long as your code is correct, a reference to i is a reference to i, and the compiler will generate whatever code is needed to access the variable correctly.

现在,如果你这样做:

int i;
scanf("%c", &i);

那么你做错了什么。 scanf函数%I格式要求的char * 参数,它指向值将被存储到其中的字符对象。你给它一个为int * 而非的char * 。这样一来,你的程序的行为是不确定的;语言标准只字未提它会如何表现。

then you're doing something wrong. scanf with a "%i" format requires a char* argument, which points to the char object into which the value will be stored. You're giving it an int* rather than a char*. As a result, your program's behavior is undefined; the language standard says nothing about how it will behave.

那么,为什么看起来正常工作?什么是可能发生的是, scanf函数对待 INT 对象的地址 I ,好像它是一个指向字符。它可能会指向重新presentation的第一个字节 I ;例如, I 可能是32位,并且指针将指向那些比特的第一8。 (它们可以是高阶或低阶比特,取决于系统)。

So why does it appear to work correctly? What's probably happening is that scanf treats the address of the int object i as if it were a pointer to a char. It will probably point to the first byte of the representation of i; for example, i might be 32 bits, and the pointer will point to the first 8 of those bits. (They could be the high-order or low-order bits, depending on the system.)

现在,当您打印 i的值

printf("%d\n", i);

I 是,例如,1个字节组成的任何性质你刚才读进去,3个字节的垃圾中的内容。这些垃圾3个字节可能全部是零,但它们可以是任何东西。如果垃圾字节碰巧是0,第一个字节恰好是高位字节(即,你在一个大端机),那么你很可能得到正确的输出。

the contents of i are, for example, 1 byte consisting of whatever character you just read into it, and 3 bytes of garbage. Those 3 garbage bytes may well all be zeros, but they could be anything. If the garbage bytes happen to be 0, and the first byte happens to be the high-order byte (i.e., you're on a big-endian machine), then you're likely to get the "correct" output.

但不这样做。由于该行为是不确定的,它可以工作正确多年,然后在最糟糕的时刻失败壮观。

But don't do that. Since the behavior is undefined, it can work "correctly" for years, and then fail spectacularly at the worst possible moment.

这里的教训是将c往往假设你知道你在做什么。有一个的很多的是具有不确定的行为,这意味着它们是无效的构造,但无论是编译器和运行系统必须告诉你,有一个问题。在C,比大多数其他语言,它给你作为一个程序员把事情做好。编译器(以及其他工具)会告诉你的部分的错误,但不是所有的人。

The lesson here is that C tends to assume that you know what you're doing. There are a lot of constructs that have undefined behavior, which means that they're invalid, but neither the compiler nor the runtime system is required to tell you that there's a problem. In C, more than in most other languages, it's up to you as a programmer to get things right. The compiler (and other tools) will tell you about some errors, but not all of them.

和在不确定的行为presence,在其中声明变量顺序的可以的有所作为。例如,如果你写code读取或写入过去的一个变量的末尾,它可以不管发生什么事,以存储在那里。但是,不要被诱惑洗牌周围的声明,直到程序工作。摆脱未定义行为,所以顺序的的事情。

And in the presence of undefined behavior, the order in which you declare your variables can make a difference. For example, if you write code that reads or writes past the end of a variable, it can matter what happens to be stored there. But don't be tempted to shuffle your declarations around until the program works. Get rid of the undefined behavior so the order doesn't matter.

解决办法:不要犯错误摆在首位。 (当然,这要容易得多难啊。)

The solution: Don't make mistakes in the first place. (Of course that's much easier said than done.)

和命名约定可以是有益的。如果您曾呼吁你的字符变量 C 和您的 INT 变量 I ,而不是相反,这本来就容易跟踪哪个是哪个了?

And naming conventions can be helpful. If you had called your char variable c, and your int variable i, rather than vice versa, it would have been easier to keep track of which is which.

C 是用于保存输入字符值的 INT 变量合理的名称 - 不是 scanf函数,但的getchar(),如:

But c is a reasonable name for an int variable used to hold input character values -- not for scanf, but for getchar(), as in:

int c;
while ((c = getchar()) != EOF) {
    /* ... */
}

这篇关于变量的声明如何表现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆