我没有机会在学校接受任何严肃的低级编程课程.(我知道我真的应该继续学习"幕后花絮"才能成为更好的程序员.)我很欣赏Java的便利性,包括将任何内容粘贴到System.out.print语句中的能力.但是,你有什么理由想要使用System.out.printf吗?
另外,我应该在"真实应用程序"中避免这样的打印调用吗?使用某种UI功能将消息打印到客户端的显示器可能更好,对吧?
我的表中有一个INT字段,我想选择它作为零填充字符串.因此,例如,8将出现008,23出现023,依此类推.这在MySQL查询中是否可行?
我想的是:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main(void) {
//test pointer to string
char s[50];
char *ptr=s;
printf("\nEnter string (s): ");
fgets(s, 50, stdin);
printf("S: %s\nPTR: %s\n", s, *ptr);
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
或者我应该使用*(s + i)和格式说明符%c的for循环?这是通过指针和简单的printf打印字符串的唯一可能方法吗?
更新:printf使用数组的第一个元素的地址进行操作,所以当我使用*ptr时,我实际上是使用第一个元素而不是它的地址.谢谢.
这基本上就是我想要做的
// ... some code, calculations, what have you ...
long timeToAdd = returnTimeToAddInLongFormat();
// lets output the long type now, and yes i need the width and precision.
System.out.printf("Time to add: %13.10ld", timeToAdd);
Run Code Online (Sandbox Code Playgroud)
我已经阅读了围绕这个主题的大部分谷歌搜索,并认为我理解如何在概念上做到这一点,但JRE不断抛弃我UnknownFormatConversionException并告诉我我的输入大小修改器l不起作用.
还有另一种方法可以做到这一点,还是我想念一些小事?
我有以下代码:
char *s1, *s2;
char str[10];
printf("Type a string: ");
scanf("%s", str);
s1 = &str[0];
s2 = &str[2];
printf("%s\n", s1);
printf("%s\n", s2);
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,输入输入"A 1"如下:
Type a string: A 1
Run Code Online (Sandbox Code Playgroud)
我得到了以下结果:
A
?<?
Run Code Online (Sandbox Code Playgroud)
我正在尝试将第一个字符作为字符串读取,将第三个字符作为整数读取,然后在屏幕上打印出来.第一个角色总是有效,但屏幕只会在那之后显示随机的东西....我该如何解决?
谢谢
该printf()文件说,如果有人想打印%在C,他可以使用:
printf("%%")
Run Code Online (Sandbox Code Playgroud)
为什么不是:
printf("\%")
Run Code Online (Sandbox Code Playgroud)
和其他特殊字符一样?
许多命令行工具实现了基于文本的进度条.像rpm安装一样:
安装############## [45%]
#随着百分比的增长而增长,同时保持一条线.我想要的是类似的东西:我需要一个进度指示器只占一行,也就是说,当百分比增长时,它被覆盖,而不是创建一个新行(\n).
我试过这个:
#include <stdio.h>
int main (){
int i = 0;
for (i = 0; i < 10000; i++){
printf("\rIn progress %d", i/100);
}
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)
\r可以覆盖单行.但是,\r将光标移到行的开头printf并将光标移到末尾,这会导致光标快速波动.你们可以通过一点点编译来感受它.任何人都可以提出替代方案来避免这个问题吗?
我一直收到编译警告,但我不知道如何解决它:
'%d' expects argument of type 'int', but argument 2 has type 'long unsigned int' [
Run Code Online (Sandbox Code Playgroud)
程序运行正常,但我仍然收到编译警告:
/* Sizeof.c--Program to tell byte size of the C variable */
#include <stdio.h>
int main(void) {
printf("\nA Char is %d bytes", sizeof( char ));
printf("\nAn int is %d bytes", sizeof( int ));
printf("\nA short is %d bytes", sizeof( short ));
printf("\nA long is %d bytes", sizeof( long ));
printf("\nA long long is %d bytes\n", sizeof( long long ));
printf("\nAn unsigned Char is %d …Run Code Online (Sandbox Code Playgroud) 假设我有一printf()行长字符串:
printf( "line 1\n"
"line 2\n"
"line 3\n"
"line 4\n"
"line 5\n"
"line 6\n"
"line 7\n"
"line 8\n"
"line 9\n.. etc");
Run Code Online (Sandbox Code Playgroud)
与printf()每条线路的多个线路相比,这种风格产生的成本是多少?
如果字符串太长,是否会出现堆栈溢出?
%n在这个问题上,我正在阅读C中的格式说明符.但是当我在不同的C++编译器上尝试以下程序时,它给了我不同的输出.
为什么?是什么原因?是否存在未定义或实现定义的行为?
#include<stdio.h>
int main()
{
int c = -1;
printf("geeks for %ngeeks ", &c);
printf("%d", c);
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
geeks for geeks 10
Run Code Online (Sandbox Code Playgroud)
geeks for geeks 10
Run Code Online (Sandbox Code Playgroud)
geeks -1
Run Code Online (Sandbox Code Playgroud)
Debug assertion failed ("'n' format specifier disabled",0)
Run Code Online (Sandbox Code Playgroud)