C语言 %d 是什么意思?为什么是 d 而不是另一个字母?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14003951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:50:06  来源:igfitidea点击:

What does %d mean? Why d and not another letter?

cprintfc-strings

提问by Abdul Montaqim

I'm just starting out learning C and wanted to ask some very elementary questions. I don't have any programming background at all. I'm just at the very start of a tutorial (http://www.cprogramming.com/tutorial/c-tutorial.html) and there's some things it doesn't explain. Like, for example, in this following introductory simple program…

我刚开始学习 C,想问一些非常基本的问题。我根本没有任何编程背景。我刚开始学习教程(http://www.cprogramming.com/tutorial/c-tutorial.html),有些东西没有解释。例如,在下面的介绍性简单程序中……

Question: why does he use "%d" – why d and not another letter? I understand the % is the modulo operand, meaning remainder, but why d? I understand that stdio.h imports a library/glossary of terms to use, such as printf and so on… is %d the same as that? :

问题:为什么他使用“%d”——为什么是 d 而不是另一个字母?我知道 % 是模操作数,意思是余数,但为什么是 d?我知道 stdio.h 导入了要使用的术语库/词汇表,例如 printf 等…… %d 与此相同吗?:

#include <stdio.h>

int main()
{
    int this_is_a_number;

    printf( "Please enter a number: " );
    scanf( "%d", &this_is_a_number );
    printf( "You entered %d", this_is_a_number );
    getchar();
    return 0;
}

回答by David Schwartz

Actually %is not the modulo operand or modulo operator here because it's included inside a string. It's just a character chosen to separate literal text from parameters. And dis used because it performs input and output in decimal (base 10).

实际上%这里不是模操作数或模运算符,因为它包含在字符串中。它只是一个选择来将文字文本与参数分开的字符。和d被使用,因为它在执行输入和输出decimal(基体10)。

回答by Vincent L.

In this case is not a modulo operator but a sign meaning you want to display the sub-sequent information in a certain format. The letter is called a specifier.

在这种情况下不是模运算符而是一个符号,表示您希望以某种格式显示后续信息。该字母称为说明符

%dmeans you're going to display it as a decimal integer. %fwould display it as a float number etc.

%d意味着您要将其显示为十进制整数。%f将其显示为浮点数等。

More in the man: http://linux.die.net/man/3/printf

更多的http: //linux.die.net/man/3/printf