如何使用C 中的printf打印数字列表
how to print a list of numbers using printf in C++?
我是新学习C ,我有基本问题和基本问题:(
我想打印下一个条件下的下一条数字列表:
int list=0;
while (list<100){
list=list+r;
}
我想使用printf而不是cout(因为我仍然不知道为什么用代表不起作用)。
任何人都可以帮助我给我类似的printf命令
cout<<list<<"t";
非常感谢!
这是一个小样本程序,以10的增量计数100。
我同时使用std::cout
和printf
在每个增量中显示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/
相关文章:
- 打印数字图案
- 错误含义;以二进制形式打印数字
- 列中打印数字时如何避免打印垃圾?
- C++ 在地板函数之后以十六进制打印数字
- C/C++ - 用于按顺序打印数字的 sem_t 类型的单个信号量
- 仅打印数字的小数位
- 在 c++ 列中打印数字
- 为什么 al_draw_textf() 打印数字而不是存储在字符串变量中的字母
- 如何使用递归按从 1 到 n 的升序打印数字,其中 n 在该函数的参数中给出
- 该程序如何反向打印数字的数字?
- C 以升级循环时按升序打印数字
- 如何使用C 中的printf打印数字列表
- 为什么使用算术而不是bitest以二进制形式打印数字更快
- 为什么不按升序打印数字
- 为什么这段代码不能正常打印数字?
- 尝试优化和理解打印数字除数的递归函数的运行时
- 我希望程序打印数字 0 到 99,但是当我运行代码时,我看到 100 行 99
- C++程序仅在打印数字时才有效
- 只有在没有重复数字的情况下才打印数字
- 不能算出这个按数字顺序打印数字的c++小程序的最后一个