是否有可能有一个Java printf语句,其输出是语句本身?
一些片段来说明:
// attempt #1
public class Main {
public static void main(String[] args) {
System.out.printf("something");
}
}
Run Code Online (Sandbox Code Playgroud)
这打印something.
因此,尝试#1的输出并不完全是printf尝试#1中的语句.我们可以尝试这样的事情:
// attempt #2
public class Main {
public static void main(String[] args) {
System.out.printf("System.out.printf(\"something\");");
}
}
Run Code Online (Sandbox Code Playgroud)
现在输出是 System.out.printf("something");
所以现在尝试#2的输出与输出#1中的语句匹配,但是我们回到了之前的问题,因为我们需要尝试#2的输出来匹配尝试#2中的语句.
那么可以写一个printf打印自己的单行语句吗?
考虑以下代码:
typedef int64_t Blkno;
#define BLKNO_FMT "%lld"
printf(BLKNO_FMT, (Blkno)some_blkno);
Run Code Online (Sandbox Code Playgroud)
这在x86上运行良好.在x64和int64_t实际上是一个long,而不是一个long long,并且当long和long long是在x64相同的尺寸,编译器会生成一个错误:
src/cpfs/bitmap.c:14:警告:格式'%lld'需要类型'long long int',但参数6的类型为'Blkno'
printf我通过64位类型?#define像BLKNO_FMT上面那样使用?在这里.
我正在学习C,我有这个代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
double buyval, deliveredval, change;
printf("What's the buy value? ");
scanf("%lf", &buyval);
do{
printf("What's the value delivered? ");
scanf("%lf", &deliveredval);
if (deliveredval < buyval){
printf("Delivered value must be greater then buy value \n\n");
}
} while (deliveredval < buyval);
change = deliveredval - buyval;
printf("Change is %4.2lf", change);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用此代码,最后一次打印始终为0.00但是我更改了
printf("Change is %4.2lf", change);
Run Code Online (Sandbox Code Playgroud)
至
printf("Change is %4.2f", change);
Run Code Online (Sandbox Code Playgroud)
它按预期工作.这是为什么?双打未格式化为lf?
printf("%d.%d.%d", year, month, day);
Run Code Online (Sandbox Code Playgroud)
我可以这样做,但没有印刷,喜欢
char* date = "%d.%d.%d", year, month, day;
Run Code Online (Sandbox Code Playgroud)
或者其他一些简单的方法可以做到这一点?
可能重复:
警告:格式不是字符串文字,也不是格式参数
我有一个非常简单的问题:为什么当我char[] s = "hi"; printf(s)发出警告时:"警告:格式化不是字符串文字而没有格式参数",同时printf("aa")没有.
我已经读过char数组和字符串文字之间的区别(一个是const char const*另一个char*),但是来自printf()签名:
我发现它适用于任何类型.所以我的问题是为什么printf("aaa")不发出任何警告(它以某种方式检查文字是一个const,同时数组不是)?
我用一个程序写pthread.
环境:windows 7,CYGWIN_NT-6.1 i686 Cygwin,gcc(GCC)4.5.3
源代码
#include<stdio.h>
#include<pthread.h>
void *th_func(void *p)
{
int iLoop = 0;
for(iLoop = 0;iLoop<100;iLoop++)
{
printf("Thread Thread Thread Thread\n");
}
return;
}
int main()
{
int iLoop = 0;
pthread_t QueThread;
printf("Main : Start Main\n");
printf("Main : Start Create Thread\n");
pthread_create(&QueThread,NULL,th_func,NULL);
printf("Main : End Create Thread\n");
for(iLoop = 0;iLoop<100;iLoop++)
{
printf("Main Main Main Main\n");
}
pthread_join(QueThread,NULL);
printf("Main : End Main\n");
printf("---------------\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我编译源代码时,没有警告或错误,但它的输出很奇怪.
它的一部分输出
Main : Start Main
Main : Start …Run Code Online (Sandbox Code Playgroud) 我正在Matlab中编写一个模块,将我的实验的配置参数输入到'parameters.txt'文件中.
这是执行该操作的模块:
for i=1:size(ParamSheetText,1)
fprintf(fparam, ParamSheetText{i,1});
fprintf(fparam,'\n');
end
Run Code Online (Sandbox Code Playgroud)
其中一个参数是文件夹位置:"D:\ temp".fprintf正在解释\t为逃避序列.有什么办法可以抑制转义序列或修改代码,以便抑制转义序列.
谢谢
我正在尝试使用转换字符来计算到目前为止使用以下代码打印的字符数.
#include <stdio.h>
int main(void) {
int n;
printf("%s: %nFoo\n", "hello", &n);
printf("%*sBar\n", n, "");
}
Run Code Online (Sandbox Code Playgroud)
正确工作时,此代码打印Hello Foo,然后在下一行中,Bar与Foo对齐.
在我的linux机箱上使用GCC编译时,这很好用.但是,当我在Windows PC上的代码块中使用minGW编译时%n不起作用,所有打印的都是hello:Bar all在一行上.为什么这样,我能解决这个问题吗?
有人可以解释为什么这里的行为存在差异吗?
perl -E 'say sprintf("%.2f",5.555);'
5.55
perl -E 'say sprintf("%.2f",0.555);'
0.56
Run Code Online (Sandbox Code Playgroud)
sprintf()如果以下数字是5或更大,我已经阅读了这些数据.那么为什么在第二种情况下它不能达到0.56呢?
好的,这是一个黑客问题(https://www.hackerrank.com/challenges/bash-tutorials---arithmetic-operations).基本上,输入是一个算术表达式,我应该打印出格式化的答案(3位小数).我一开始就尝试过这个
read exp
echo "scale = 3; $exp" | bc -l
Run Code Online (Sandbox Code Playgroud)
它通过了几次测试,但不是第一次.
5+50*3/20 + (19*2)/7答案是,17.929但我的代码打印出来17.928.我试过这个代码
read exp
printf "%.3f\n" `echo $exp | bc -l`
Run Code Online (Sandbox Code Playgroud)
注意:回声部分应该是反引号,但我把''不要与块引号混淆.所有测试都通过了 那有什么区别?