[英]What does “%d! = %ld'n” mean in this code?
I'm still a beginner at C, so I'm finding difficulty in understanding "%d! = %ld". 我仍然是C语言的初学者,所以我很难理解“%d!=%ld”。 I know that %d and %ld are respectively used for an integer and long, so "! =" is confusing me.
我知道%d和%ld分别用于整数和long,所以“!=”使我感到困惑。
#include<stdio.h>
long factorial(int);
int main() {
int n;
long f;
printf("Enter an non-negative integer: ");
scanf("%d", &n);
if (n < 0)
printf("Negative integers are not allowed.\n");
else {
f = factorial(n);
printf("%d! = %ld\n", n, f); //what does this mean?
}
return 0; }
long factorial(int n) {
if (n == 0)
return 1;
else
return(n * factorial(n-1)); }
%d
, ie the decimal value of int n
%d
,即int n
的十进制值 ! =
! =
, ie the literal character sequence ! =
,即文字字符序列 %ld
, ie the decimal value of long f
%ld
,即long f
的十进制值 %d
and %ld
are the formatting placeholders for int
and long int
in printf
. %d
和%ld
是printf
int
和long int
的格式占位符。 The exclamation point is just the factorial symbol, as mentioned in the comment. 如注释中所述,感叹号只是阶乘符号。
printf() allows you to print a string with variables inside of it. printf()允许您打印一个带有变量的字符串。 Let's say you have a variable
i
, containing an integer, 7. 假设您有一个变量
i
,其中包含一个整数7。
printf("My variable is %d", i);
Will print 将打印
My variable is 7
to the console! 到控制台! That's because %d is how you tell printf(), "Hey, put an integer variable here!".
这是因为%d是您告诉printf()的方式,“嘿,在这里放一个整数变量!”。 The integer is then supplied as the next argument to the function.
然后将整数作为函数的下一个参数提供。 In your case, %d represents the integer
n
, and %ld represents the long integer f
. 在您的情况下,%d表示整数
n
,而%ld表示长整数f
。 Since f might be really big, we make it a long, which means more bytes are allocated to it internally on your computer. 由于f可能确实很大,因此我们将其设置得较长,这意味着您的计算机内部会为其分配更多的字节。 So for example, if we wanted to get the factorial of 5 and print it, we might do the following:
因此,例如,如果我们想获取因子5并打印出来,我们可以执行以下操作:
printf("Factorial of %d equals %ld\n", 5, factorial(5))
// this will print "Factorial of 5 is 120" then a newline
Oh, and \\n
just means print a newline afterwords! 哦,
\\n
仅表示打印换行符后缀!
printf("%d! = %ld\n", n, f); //what does this mean?
%d
- print an integer as a signed decimal number. %d
将整数打印为带符号的十进制数字。
l
- specifies that the argument is a long int
or unsigned long int
as appropriate. l
指定参数是适当的long int
或unsigned long int
。 %ld
then prints a long int
or unsigned long int
然后,
%ld
打印long int
或unsigned long int
The printed text will become something like 打印的文字将变成类似
n! = f
(factorial notation n!
) (阶乘符号
n!
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.