简体   繁体   English

我如何将输出打印为以下程序的5?

[英]how do i print output as 5 of the following program?

# include <stdio.h>

int x = 5;
int main(void)
{

        int x = 7;
        printf("output = %d\n", x);

}

The above program shows output as 7 . 上面的程序将输出显示为7 How to print 5 in c ? 如何在c中打印5

thanx... 谢谢...

So you're asking how to access a global that's shadowed by a local? 因此,您在问如何访问被本地遮挡的全局? You can't in that scope, but something like this should work 您不能在那个范围内,但是这样的事情应该起作用

# include <stdio.h>

int x = 5;
int get_global_x()
{
  return x;
}

int main(void)
{

        int x = 7;
        printf("output = %d\n", get_global_x());

}

Don't redeclare the x variable in the main function... 不要在主函数中重新声明x变量...

In fact, i'm sensing (perhaprs wrongly) that there's another question behind your code, but i'll keep my reaction to it short: if you want to mix global variables and local variables, try to have a convention to distinguish them; 实际上,我感觉到(可能是错误的)您的代码背后还有另一个问题,但是我会简短地回答一下:如果您要混合使用全局变量和局部变量,请尝试建立一个区分它们的约定; for example globals in ALL_CAPS to shout out its scope :) 例如ALL_CAPS中的globals喊出它的作用域:)


EDIT: By the way, you should be getting at least a warning from your compiler for redefining a different scope/same name variable. 编辑:顺便说一句,您应该至少从编译器获得警告,用于重新定义不同的作用域/相同的名称变量。 it's really not recommended doing that. 确实不建议这样做。 Try to always aim for minimum warnings... 尝试始终以最低警告为目标...

You need to give your two variables meaningful names. 您需要给两个变量赋予有意义的名称。 I'm sure you aren't using x in your real code so do the two vars actually have the same meaning and therefore the same name? 我确定您没有在实际代码中使用x,所以这两个变量实际上具有相同的含义,因此名称相同吗? If so you could at least do (assuming your ints are counts, but works for all other purposes): 如果是这样,您至少可以这样做(假设您的int是计数,但可以用于所有其他目的):

int global_countOfThings = 5;
int main(void)
{
    int countOfThings  = 7;
    printf("output = %d\n", global_countOfThings );    
}

But hopefully you'll be able to do something like: 但希望您能够执行以下操作:

int countOfDucks = 5;
int main(void)
{
    int countOfGeese  = 7;
    printf("output = %d\n", countOfDucks );    
}

And of course if you can some how change your code to not use globals you'll be better off in the long run. 当然,如果您可以通过一些方法将代码更改为不使用全局变量,从长远来看会更好。

You are declaring the same variable in global and local scope. 您正在全局和局部范围内声明相同的变量。 If the same variable is declared in global and local scope, the variable will be treated as local variable. 如果在全局和局部范围内声明了相同的变量,则该变量将被视为局部变量。 That's the reason you are getting the answer as 7. 这就是您得到答案为7的原因。

删除x = 7行,在打印值之前移动print语句,或将其他变量设置为7(即,写y = 7而不是x = 7)。

u have already declared x to behave as if it is a global variable.to print 5 u can write the pritf statement as: printf("%d\\n",x-2); 您已经声明x的行为就像是全局变量。要打印5,您可以将pritf语句编写为:printf(“%d \\ n”,x-2); The above would print 5. 以上将打印5。

If you want it 5, why you assign 7? 如果要5,为什么要分配7? :-) Or you want to access global variable with the same name as local one? :-)还是要访问与本地变量同名的全局变量? Then you might use namespaces... Not sure about C :-) 然后,您可能会使用命名空间...不确定C :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM