为什么编译器还允许初始化的变量与自身? [英] Why does the compiler allow initializing a variable with itself?

查看:103
本文介绍了为什么编译器还允许初始化的变量与自身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于追查下来一个错字的错误,这是类似以下code的东西。
但不应编译器检测到这个(默认选项)?

 的#include<&stdio.h中GT;诠释的main()
{
    INT C = C;
    返回的printf(%d个\\ N,C);
}
$ GCC --version
海湾合作委员会(Ubuntu的4.4.3-4ubuntu5.1)4.4.3


解决方案

我不明白为什么它不会编译。定义发生初始化之前。当然,这个初始化是没有意义的,但是,没有理由它不会从编译器工作的站立点。

C不具有相同类型的保护,像C#更现代的语言都具有的。 C#编译器会给您使用一个未分配的变量错误。 C不关心。它不会保护你自己。

I finally trace down a typo bug, which is something similar to the following code. But shouldn't the compiler detect this (by default options)?

#include <stdio.h>

int main()
{
    int c = c;
    return printf("%d\n", c);
}


$ gcc --version        
gcc (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3

解决方案

I don't see why it wouldn't compile. Definition happens prior to initialization. Of course this initialization is pointless, however, there's no reason it wouldn't work from the compilers stand point.

C does not have the same types of protections that more modern languages like C# have. The C# compiler would give an error that you're using an unassigned variable. C doesn't care. It will not protect you from yourself.

这篇关于为什么编译器还允许初始化的变量与自身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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