如何使用C 中的printf打印数字列表

how to print a list of numbers using printf in C++?

本文关键字:打印 数字 列表 printf 中的 何使用      更新时间:2023-10-16

我是新学习C ,我有基本问题和基本问题:(

我想打印下一个条件下的下一条数字列表:

int list=0;
while (list<100){
    list=list+r;
}

我想使用printf而不是cout(因为我仍然不知道为什么用代表不起作用)。

任何人都可以帮助我给我类似的printf命令

cout<<list<<"t";

非常感谢!

这是一个小样本程序,以10的增量计数100。

我同时使用std::coutprintf在每个增量中显示list的值。

添加的评论希望有助于帮助您学习

#include <iostream>
#include <cstdio>
int main()
{
    int r = 10;
    int list=0;
    while (list < 100)
    {
        list += r;                 // this is the same as saying list = list + r, but is more succinct
        std::cout << list << "t"; // cout is in the std namespace, so you have to prefix with std::
        printf("%dn", list);      // the printf format specified for int is "%d"
    }
}

输出:

10    10
20    20
30    30
40    40
50    50
60    60
70    70
80    80
90    90
100   100

请注意,我没有在顶部使用using namespace std;cout导入全局名称空间。恕我直言,这是不好的做法,所以我通常更喜欢std::cout等。

printf是C函数,而不是C ,如果您正在学习C ,则应该尝试用std::cout解决问题,这是通常的打印方法。

无论如何, printf非常易于使用,它需要第一个参数(c字符串,因此,最后一个字符的字符串作为''字符)和与您在格式中一样多的参数您的字符串(格式指定器是% char,然后是另一个char,该功能是变量将是什么时间)

示例:

int intvat;
char charvar;
float floatvar;
char* stringvar; // to print it the last char of stringvar must be 
printf("this is an int: %d", intvar);
printf("this is a char: %c", charvar);
printf("this is a string: %s", stringvar);
printf("this is a float and an int: %f, %d", floatvar, intvar);

有关printf的更多信息,您可以在此处参考参考页:http://www.cplusplus.com/reference/cstdio/cstdio/printf/