[英]Printing number of digits rather than actual value
Im really new to C and coding in general so sorry if this is a simple problem.我真的是 C 和编码的新手,如果这是一个简单的问题,我很抱歉。
All I'm trying to do is input some numbers, get them to be added up, and then print the sum in two different ways.我要做的就是输入一些数字,将它们相加,然后以两种不同的方式打印总和。 However for some reason the
printf("%li", z);
但是由于某种原因
printf("%li", z);
line of code is only printing the amount of digits in the number plus 1
.代码行仅打印数字中的位数加
1
。
Could someone explain to me why exactly it is doing this, please?有人可以向我解释为什么它会这样做吗? The why what's important, I'm not actually too bothered about getting it to work since I'm just playing around and practising.
为什么重要的是,我实际上并没有太在意让它工作,因为我只是在玩耍和练习。 Thanks!
谢谢!
My code is as follows:我的代码如下:
#include <cs50.h>
#include <stdio.h>
int main(void) {
long x = get_long("what is x: ");
long y = get_long("what is y: ");
long z = printf("%li\n", x + y + y);
printf("%li", z);
}
The mistake is in the line错误就在这条线上
long z = printf("%li\n", x + y + y);
The reason printf("%li", z);
原因
printf("%li", z);
prints the number of the digits in the number plus one is that printf
is a function that returns the number of characters written on the output stream (stdout -- the console -- in your case) and here printf("%li\n", x + y + y);
prints the number of the digits in the number plus one is that
printf
is a function that returns the number of characters written on the output stream (stdout -- the console -- in your case) and here printf("%li\n", x + y + y);
you're trying to write x+y+y
followed by \n
(which is an additional character).您正在尝试编写
x+y+y
后跟\n
(这是一个附加字符)。
printf
prints out a formatted string on a file called stdout
. printf
在名为stdout
的文件上打印出格式化字符串。 Read this for more details.阅读本文了解更多详情。
Please, if you are new to the C language, I advise against trying to learn more "advanced" stuff like printf
or scanf
(and *printf
or *scanf
function family) without having the proper basics (variables, pointers, dynamic memory allocation, files). Please, if you are new to the C language, I advise against trying to learn more "advanced" stuff like
printf
or scanf
(and *printf
or *scanf
function family) without having the proper basics (variables, pointers, dynamic memory allocation,文件)。 If you want to try out small programs to see if they succeed you can use a debugger ( gdb
).如果您想尝试小程序以查看它们是否成功,您可以使用调试器 (
gdb
)。 But in order to understand the use of those functions you need to know some basic things, or you will end up with magic syntax like scanf("%s %d", mystr, &myint)
and you wouldn't know why mystr
does not need the &
whereas myint
does.但是为了理解这些函数的使用,你需要了解一些基本的东西,否则你会得到像
scanf("%s %d", mystr, &myint)
这样的神奇语法,你不知道为什么mystr
不需要&
而myint
需要。 So, please, if you don't have these basics, learn them before going on with IO on files (stdout and stdin are files).因此,如果您没有这些基础知识,请在继续使用 IO 处理文件(stdout 和 stdin是文件)之前学习它们。
#include <cs50.h> #include <stdio.h> int main (void) { long x = get_long("what is x: "); long y = get_long("what is y: "); long z = x + y + y; printf("%li", z); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.