如何在C中用printf显示int [英] How to display int with printf in C

查看:77
本文介绍了如何在C中用printf显示int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用 printf() 函数在 C 中显示 int 时遇到了一些奇怪的问题.这是我正在使用的代码

I am having some weird trouble when I am trying to display an int in C with the printf() function. This is the code I am using

            printf("To compute pi enter n: ");
            int n_term = 1;

            printf("n: %d \n", &n_term); 
            scanf("%d", &n_term);
            printf("n: %d", &n_term);

我意识到有一个问题,因为当我尝试简单地将 n_term 打印回给用户时,甚至在他们为 n_term 输入任何值之前,它就会出现这个巨大的数字,2686776.任何关于这里问题的见解不胜感激.如果有什么不同,我正在使用 gcc 编译器.

I realized there was an issue because when I try to simply print n_term back to the user, even before they input any value for n_term, it comes out with this giant number, 2686776. Any insight as to what the issue here would be greatly appreciated. I am using gcc compiler if that makes any difference.

推荐答案

去掉 printf 调用上的与号 (&).您正在打印作为整数转换的变量 n_term 的地址.

Get rid of the ampersand (&) on the printf call. You are printing the address of the variable n_term donwcasted as an integer.

这篇关于如何在C中用printf显示int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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